From 6fb7dc81764d2504e2197f5871b7b9029500f9bb Mon Sep 17 00:00:00 2001 From: Rene Schallner Date: Sun, 16 Apr 2023 20:32:17 +0200 Subject: [PATCH] BasicAuth/Token68 works + tested --- src/http_auth.zig | 10 ++++------ src/test_auth.zig | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 6 deletions(-) diff --git a/src/http_auth.zig b/src/http_auth.zig index 23989f0..d6c4bce 100644 --- a/src/http_auth.zig +++ b/src/http_auth.zig @@ -61,7 +61,7 @@ const BasicAuthStrategy = enum { /// WWW-Authenticate: Basic realm="this" /// /// T : any kind of map that implements get([]const u8) -> []const u8 -pub fn BasicAuth(Lookup: type, kind: BasicAuthStrategy) type { +pub fn BasicAuth(comptime Lookup: type, comptime kind: BasicAuthStrategy) type { return struct { // kind: BasicAuthStrategy, allocator: std.mem.Allocator, @@ -100,10 +100,8 @@ pub fn BasicAuth(Lookup: type, kind: BasicAuthStrategy) type { /// Use this to just look up if the base64-encoded auth_header exists in lookup pub fn authenticateToken68(self: *Self, auth_header: []const u8) bool { - _ = auth_header; - _ = self; - // TODO - return false; + const token = auth_header[AuthScheme.Basic.str().len..]; + return self.lookup.*.contains(token); } // dispatch based on kind @@ -115,7 +113,7 @@ pub fn BasicAuth(Lookup: type, kind: BasicAuthStrategy) type { } } pub fn authenticateRequest(self: *Self, r: *const zap.SimpleRequest) bool { - if (extractAuthHeader(.Bearer, r)) |auth_header| { + if (extractAuthHeader(.Basic, r)) |auth_header| { return self.authenticate(auth_header); } return false; diff --git a/src/test_auth.zig b/src/test_auth.zig index ffedba5..d8594c2 100644 --- a/src/test_auth.zig +++ b/src/test_auth.zig @@ -187,3 +187,53 @@ test "BearerAuthSingle authenticateRequest" { try std.testing.expectEqualStrings(HTTP_RESPONSE, received_response); } + +test "BasicAuth authenticateRequest" { + const a = std.testing.allocator; + const token = "QWxhZGRpbjpvcGVuIHNlc2FtZQ=="; + + // setup listener + var listener = zap.SimpleEndpointListener.init( + a, + .{ + .port = 3000, + .on_request = null, + .log = false, + .max_clients = 10, + .max_body_size = 1 * 1024, + }, + ); + defer listener.deinit(); + + // create mini endpoint + var ep = Endpoints.SimpleEndpoint.init(.{ .path = "/test", .get = endpoint_http_get }); + // create a set of Token68 entries + const Set = std.StringHashMap(void); + var set = Set.init(a); // set + defer set.deinit(); + try set.put(token, {}); + + // create authenticator + const Authenticator = Authenticators.BasicAuth(Set, .Token68); + var authenticator = try Authenticator.init(a, &set, null); + defer authenticator.deinit(); + + // create authenticating endpoint + const BearerAuthEndpoint = Endpoints.AuthenticatingEndpoint(Authenticator); + var auth_ep = BearerAuthEndpoint.init(&ep, &authenticator); + + try listener.addEndpoint(auth_ep.getEndpoint()); + + listener.listen() catch {}; + std.debug.print("Listening on 0.0.0.0:3000\n", .{}); + std.debug.print("Please run the following:\n", .{}); + std.debug.print("./zig-out/bin/http_client http://127.0.0.1:3000/test Basic " ++ token, .{}); + + // start worker threads + zap.start(.{ + .threads = 1, + .workers = 0, + }); + + try std.testing.expectEqualStrings(HTTP_RESPONSE, received_response); +}