Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add handling for unset request types in endpoint #137

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ pub fn main() !void {
.path = "/test",
.get = endpoint_http_get,
.unauthorized = endpoint_http_unauthorized,
.unset = Endpoint.dummy_handler,
});

// create authenticator
Expand Down
1 change: 1 addition & 0 deletions examples/endpoint/stopendpoint.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub fn init(
.ep = zap.Endpoint.init(.{
.path = path,
.get = get,
.unset = zap.Endpoint.dummy_handler,
}),
};
}
Expand Down
1 change: 1 addition & 0 deletions examples/endpoint_auth/endpoint_auth.zig
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub fn main() !void {
.path = "/test",
.get = endpoint_http_get,
.unauthorized = endpoint_http_unauthorized,
.unset = zap.Endpoint.dummy_handler,
});

// create authenticator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ const HtmlEndpoint = struct {
.ep = zap.Endpoint.init(.{
.path = "/doesn't+matter",
.get = get,
.unset = zap.Endpoint.dummy_handler,
}),
};
}
Expand Down
22 changes: 13 additions & 9 deletions src/endpoint.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub const Settings = struct {
options: ?RequestFn = null,
/// Only applicable to Authenticating Endpoint: handler for unauthorized requests
unauthorized: ?RequestFn = null,
// callback to any unset request type
unset: ?RequestFn = null,
};

settings: Settings,
Expand All @@ -41,19 +43,20 @@ pub fn init(s: Settings) Endpoint {
return .{
.settings = .{
.path = s.path,
.get = s.get orelse &nop,
.post = s.post orelse &nop,
.put = s.put orelse &nop,
.delete = s.delete orelse &nop,
.patch = s.patch orelse &nop,
.options = s.options orelse &nop,
.unauthorized = s.unauthorized orelse &nop,
.get = s.get orelse s.unset orelse @panic("Endpoint handler `.get` is unset, and no `.unset` handler is provided."),
.post = s.post orelse s.unset orelse @panic("Endpoint handler `.post` is unset, and no `.unset` handler is provided."),
.put = s.put orelse s.unset orelse @panic("Endpoint handler `.put` is unset, and no `.unset` handler is provided."),
.delete = s.delete orelse s.unset orelse @panic("Endpoint handler `.delete` is unset, and no `.unset` handler is provided."),
.patch = s.patch orelse s.unset orelse @panic("Endpoint handler `.patch` is unset, and no `.unset` handler is provided."),
.options = s.options orelse s.unset orelse @panic("Endpoint handler `.options` is unset, and no `.unset` handler is provided."),
.unauthorized = s.unauthorized orelse s.unset orelse @panic("Endpoint handler `.unauthorized` is unset, and no `.unset` handler is provided."),
.unset = s.unset,
},
};
}

// no operation. Dummy handler function for ignoring unset request types.
fn nop(self: *Endpoint, r: Request) void {
pub fn dummy_handler(self: *Endpoint, r: Request) void {
_ = self;
_ = r;
}
Expand Down Expand Up @@ -98,6 +101,7 @@ pub fn Authenticating(comptime Authenticator: type) type {
.patch = if (e.settings.patch != null) patch else null,
.options = if (e.settings.options != null) options else null,
.unauthorized = e.settings.unauthorized,
.unset = e.settings.unset,
}),
};
}
Expand Down Expand Up @@ -224,7 +228,7 @@ pub fn Authenticating(comptime Authenticator: type) type {
return;
}
},
.AuthOK => authEp.ep.settings.put.?(authEp.ep, r),
.AuthOK => authEp.ep.settings.options.?(authEp.ep, r),
.Handled => {},
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/tests/test_auth.zig
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ test "BearerAuthSingle authenticateRequest OK" {
.path = "/test",
.get = endpoint_http_get,
.unauthorized = endpoint_http_unauthorized,
.unset = Endpoint.dummy_handler,
});

// create authenticator
Expand Down Expand Up @@ -244,6 +245,7 @@ test "BearerAuthSingle authenticateRequest test-unauthorized" {
.path = "/test",
.get = endpoint_http_get,
.unauthorized = endpoint_http_unauthorized,
.unset = Endpoint.dummy_handler,
});

const Set = std.StringHashMap(void);
Expand Down Expand Up @@ -301,6 +303,7 @@ test "BearerAuthMulti authenticateRequest OK" {
.path = "/test",
.get = endpoint_http_get,
.unauthorized = endpoint_http_unauthorized,
.unset = Endpoint.dummy_handler,
});

// create authenticator
Expand Down Expand Up @@ -352,6 +355,7 @@ test "BearerAuthMulti authenticateRequest test-unauthorized" {
.path = "/test",
.get = endpoint_http_get,
.unauthorized = endpoint_http_unauthorized,
.unset = Endpoint.dummy_handler,
});

// create authenticator
Expand Down Expand Up @@ -403,6 +407,7 @@ test "BasicAuth Token68 authenticateRequest" {
.path = "/test",
.get = endpoint_http_get,
.unauthorized = endpoint_http_unauthorized,
.unset = Endpoint.dummy_handler,
});
// create a set of Token68 entries
const Set = std.StringHashMap(void);
Expand Down Expand Up @@ -459,6 +464,7 @@ test "BasicAuth Token68 authenticateRequest test-unauthorized" {
.path = "/test",
.get = endpoint_http_get,
.unauthorized = endpoint_http_unauthorized,
.unset = Endpoint.dummy_handler,
});
// create a set of Token68 entries
const Set = std.StringHashMap(void);
Expand Down Expand Up @@ -514,6 +520,7 @@ test "BasicAuth UserPass authenticateRequest" {
.path = "/test",
.get = endpoint_http_get,
.unauthorized = endpoint_http_unauthorized,
.unset = Endpoint.dummy_handler,
});

// create a set of User -> Pass entries
Expand Down Expand Up @@ -580,6 +587,7 @@ test "BasicAuth UserPass authenticateRequest test-unauthorized" {
.path = "/test",
.get = endpoint_http_get,
.unauthorized = endpoint_http_unauthorized,
.unset = Endpoint.dummy_handler,
});

// create a set of User -> Pass entries
Expand Down
1 change: 1 addition & 0 deletions src/zap.zig
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub const Tls = @import("tls.zig");
/// .ep = zap.Endpoint.init(.{
/// .path = path,
/// .get = get,
/// .unset = Endpoint.dummy_handler,
/// }),
/// };
/// }
Expand Down