Skip to content

Commit

Permalink
zig- finish handlers to complete login flow
Browse files Browse the repository at this point in the history
  • Loading branch information
nektro committed Sep 15, 2021
1 parent 9a285b6 commit ce6f520
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/handler/_.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const oauth2 = @import("oauth2");
const json = @import("json");

const mime = @import("../mime.zig");
const db = @import("../db/_db.zig");

const _internal = @import("./_internal.zig");
const _index = @import("./index.zig");
Expand Down Expand Up @@ -73,16 +74,30 @@ fn StaticPek(comptime path: string) type {
}

pub fn isLoggedIn(request: http.Request) !bool {
_ = request;
return false;
const x = _internal.JWT.veryifyRequest(request) catch |err| switch (err) {
error.NoTokenFound, error.InvalidSignature => return false,
else => return err,
};
// don't need to waste hops to the db to check if its a value user ID because
// if the signature is valid we know it came from us
_ = x;
return true;
}

pub fn saveInfo(response: *http.Response, request: http.Request, idp: oauth2.Provider, id: string, name: string, val: json.Value) !void {
_ = response;
_ = request;
_ = idp;
_ = id;
_ = name;
_ = val;

const alloc = request.arena;
const r = try db.Remote.byKey(alloc, .domain, idp.domain());
var u = try r.?.findUserBy(alloc, .snowflake, id);

if (u == null) {
// TODO insert new users
return error.TODO;
}

try response.headers.put("Set-Cookie", try std.fmt.allocPrint(alloc, "jwt={s}", .{
try _internal.JWT.encodeMessage(alloc, u.?.uuid),
}));
}
4 changes: 4 additions & 0 deletions src/handler/_internal.zig
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,8 @@ pub const JWT = struct {
const q = try request.context.url.queryParameters(request.arena);
return q.get("jwt");
}

pub fn encodeMessage(alloc: *std.mem.Allocator, msg: string) !string {
return try jwt.encodeMessage(alloc, .HS256, msg, .{ .key = jwt_secret });
}
};

0 comments on commit ce6f520

Please sign in to comment.