@@ -19,19 +19,21 @@ pub fn initType(stor: Types.Storage) !void {
mtls_fp: [40]u8 = .{0} ** 40,
not_before: i64,
not_after: i64,
username: []const u8,
username: UsernameArray,
pub fn findMTLSFingerprint(a: Allocator, fp: []const u8) !User {
pub const UsernameArray = std.BoundedArray(u8, 128);
pub fn findMTLSFingerprint(fp: []const u8) !User {
if (fp.len != 40) return error.InvalidFingerprint;
const file = try openFile(fp);
return readFile(a, file);
return readFile(file);
}
pub fn open(a: Allocator, username: []const u8) !User {
pub fn open(username: []const u8) !User {
for (username) |c| if (!std.ascii.isLower(c)) return error.InvalidUsername;
const ufile = try openFile(username);
return try readFile(a, ufile);
return try readFile(ufile);
}
pub fn commit(self: User) !User {
@@ -40,17 +42,20 @@ pub fn commit(self: User) !User {
try self.writeOut(w);
}
fn readVersioned(a: Allocator, file: std.fs.File) !User {
fn readVersioned(file: std.fs.File) !User {
var reader = file.reader();
const ver: usize = try reader.readInt(usize, endian);
switch (ver) {
0 => {
return User{
var u: User = .{
.mtls_fp = try reader.readBytesNoEof(40),
.not_before = std.math.minInt(i64),
.not_after = std.math.maxInt(i64),
.username = try reader.readUntilDelimiterAlloc(a, 0, 0xFFF),
.username = .{},
};
const slice = try reader.readUntilDelimiter(u.username.unusedCapacitySlice(), 0);
try u.username.resize(slice.len);
return u;
},
else => return error.UnsupportedVersion,
}
@@ -62,9 +67,9 @@ fn openFile(fp: []const u8) !std.fs.File {
return try datad.createFile(filename, .{ .read = true, .truncate = false });
}
fn readFile(a: Allocator, file: std.fs.File) !User {
fn readFile(file: std.fs.File) !User {
defer file.close();
return readVersioned(a, file);
return readVersioned(file);
}
pub fn raze(self: User, a: Allocator) void {