diff --git a/.gitignore b/.gitignore index 2fc273e..783ac19 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ zig-out .zig-cache/ +.credentials diff --git a/src/main.zig b/src/main.zig index 8304fe4..1a26221 100644 --- a/src/main.zig +++ b/src/main.zig @@ -13,14 +13,37 @@ const AuthResult = struct { user_uuid: []const u8, }; -fn readCredentials(allocator: std.mem.Allocator) !struct { username: []const u8, password: []const u8, content: []const u8 } { - const file = try std.fs.cwd().openFile("../.credentials", .{}); +const CognitoCredentials = struct { + allocator: std.mem.Allocator, + buffer: []const u8, + username: []const u8, + password: []const u8, + + pub fn deinit(self: *CognitoCredentials) void { + self.allocator.free(self.buffer); + self.buffer = undefined; + self.username = undefined; + self.password = undefined; + } +}; +fn readCredentials(allocator: std.mem.Allocator) !CognitoCredentials { + // TODO: something different + const file = try std.fs.cwd().openFile(".credentials", .{}); defer file.close(); const content = try file.readToEndAlloc(allocator, 1024); - var it = std.mem.splitScalar(u8, std.mem.trim(u8, content, &std.ascii.whitespace), '\n'); + var it = std.mem.splitScalar(u8, std.mem.trim( + u8, + content, + &std.ascii.whitespace, + ), '\n'); const username = it.next() orelse return error.InvalidCredentials; const password = it.next() orelse return error.InvalidCredentials; - return .{ .username = username, .password = password, .content = content }; + return .{ + .username = username, + .password = password, + .buffer = content, + .allocator = allocator, + }; } fn authenticate(allocator: std.mem.Allocator, username: []const u8, password: []const u8) !AuthResult { @@ -164,8 +187,8 @@ pub fn main() !void { defer _ = gpa.deinit(); const allocator = gpa.allocator(); - const creds = try readCredentials(allocator); - defer allocator.free(creds.content); + var creds = try readCredentials(allocator); + defer creds.deinit(); std.debug.print("🔐 Authenticating...\n", .{}); const auth = try authenticate(allocator, creds.username, creds.password);