srctree

Gregory Mullen parent a85c7cd8 ff8fcb1b
remove Auth as a object

This make a few other changes too. e.g. it reduces the amount of state available...
src/auth.zig added: 44, removed: 45, total 0
@@ -1,10 +1,12 @@
provider: Provider,
current_user: ?User = null,
 
pub const Auth = @This();
pub const User = @import("auth/user.zig");
 
pub const Provider = @import("auth/provider.zig");
 
pub const AuthZ = @import("authorization.zig");
pub const AuthN = @import("authentication.zig");
 
pub const User = @import("auth/user.zig");
 
pub const Error = error{
UnknownUser,
Unauthenticated,
@@ -13,15 +15,15 @@ pub const Error = error{
 
/// Fails closed: the provider used may return an error which will be caught and
/// returned as false.
pub fn valid(a: Auth) bool {
return a.provider.valid() catch false;
}
//pub fn valid(a: Auth) bool {
// return a.provider.valid() catch false;
//}
 
/// Unauthenticated is the only error this is able to return as the correct
/// definition for an HTTP 401
pub fn requireValid(a: Auth) error{Unauthenticated}!void {
if (a.current_user == null or !a.valid()) return error.Unauthenticated;
}
//pub fn requireValid(a: Auth) error{Unauthenticated}!void {
// if (a.current_user == null or !a.valid()) return error.Unauthenticated;
//}
 
pub const MTLS = struct {
pub fn provider(mtls: *MTLS) Provider {
@@ -34,19 +36,16 @@ pub const MTLS = struct {
};
}
 
pub fn valid(mtls: *MTLS) bool {
_ = mtls;
pub fn valid(_: *MTLS, _: *const User) bool {
return false;
}
 
fn validPtr(ptr: *anyopaque) bool {
fn validPtr(ptr: *anyopaque, user: *const User) bool {
const self: *MTLS = @ptrCast(ptr);
return self.valid();
return self.valid(user);
}
 
pub fn lookupUser(mtls: *MTLS, user_id: []const u8) Error!User {
_ = mtls;
_ = user_id;
pub fn lookupUser(_: *MTLS, _: []const u8) Error!User {
return error.UnknownUser;
}
 
@@ -65,12 +64,19 @@ pub const InvalidAuth = struct {
pub fn provider() Provider {
return Provider{
.ctx = undefined,
.vtable = Provider.VTable.DefaultEmpty,
.vtable = .{
.valid = valid,
.lookup_user = lookupUser,
},
};
}
 
fn lookupUser(_: @This(), _: []const u8) Error!User {
return error.NotProvided;
fn valid(_: *const anyopaque, _: *const User) bool {
return false;
}
 
fn lookupUser(_: *const anyopaque, _: []const u8) Error!User {
return error.UnknownUser;
}
};
 
@@ -83,7 +89,7 @@ const TestingAuth = struct {
// Do not use
if (std.mem.eql(u8, "12345", user_id)) {
return User{
.username = "testing",
.user_ptr = undefined,
};
} else return error.UnknownUser;
}
@@ -106,7 +112,7 @@ const TestingAuth = struct {
 
test Provider {
const expected_user = Auth.User{
.username = "testing",
.user_ptr = undefined,
};
 
var t = TestingAuth{};
@@ -119,5 +125,3 @@ test Provider {
 
const std = @import("std");
const Allocator = std.mem.Allocator;
pub const AuthZ = @import("authorization.zig");
pub const AuthN = @import("authentication.zig");
 
src/auth/provider.zig added: 44, removed: 45, total 0
@@ -10,7 +10,7 @@ pub const VTable = struct {
valid: ?ValidFn,
 
pub const LookupUserFn = *const fn (*const anyopaque, []const u8) Auth.Error!Auth.User;
pub const ValidFn = *const fn (*const anyopaque) Auth.Error!bool;
pub const ValidFn = *const fn (*const anyopaque, *const User) bool;
 
pub const DefaultEmpty = .{
.lookup_user = null,
@@ -18,6 +18,12 @@ pub const VTable = struct {
};
};
 
pub fn valid(self: *const Provider, user: *const User) bool {
if (self.vtable.valid) |valid_fn| {
return valid_fn(self.ctx, user);
} else false;
}
 
/// TODO document the implications of non consttime function
pub fn lookupUser(self: *const Provider, user_id: []const u8) Auth.Error!Auth.User {
if (self.vtable.lookup_user) |lookup_fn| {
@@ -25,14 +31,5 @@ pub fn lookupUser(self: *const Provider, user_id: []const u8) Auth.Error!Auth.Us
} else return error.NotProvided;
}
 
//pub fn any(self: *const ) AnyAuth {
// return .{
// .ctx = self,
// .vtable = .{
// .valid = null,
// .lookup_user = lookupUserUntyped,
// },
// };
//}
 
const Auth = @import("../auth.zig");
const User = @import("user.zig");
 
src/auth/user.zig added: 44, removed: 45, total 0
@@ -1,5 +1,5 @@
//! This is a default User provided by Verse. This is almost certainly not what
//! you want.
pub const User = @This();
user_ptr: *anyopaque,
 
username: []const u8 = "invalid username",
pub const User = @This();
 
src/verse.zig added: 44, removed: 45, total 0
@@ -25,7 +25,7 @@ downstream: union(Downstream) {
uri: UriIter,
 
// TODO fix this unstable API
auth: Auth,
auth: Auth.Provider,
/// The RouteData API is currently unstable, use with caution
route_data: RouteData,
 
@@ -90,9 +90,7 @@ pub fn init(a: Allocator, req: *const Request) !Verse {
.http => .{ .http = req.raw.http.server.connection.stream },
},
.uri = splitScalar(u8, req.uri[1..], '/'),
.auth = Auth{
.provider = Auth.InvalidAuth.provider(),
},
.auth = Auth.InvalidAuth.provider(),
.headers = Headers.init(a),
.cookie_jar = try Cookies.Jar.init(a),
.route_data = .{ .items = std.ArrayList(RouteData.Pair).init(a) },