fediglam/src/api/services/tokens.zig

41 lines
1.0 KiB
Zig
Raw Normal View History

2023-01-02 00:28:36 +00:00
const std = @import("std");
const util = @import("util");
const Uuid = util.Uuid;
const DateTime = util.DateTime;
pub const Token = struct {
account_id: Uuid,
issued_at: DateTime,
hash: []const u8,
};
pub fn create(db: anytype, account_id: Uuid, hash: []const u8, alloc: std.mem.Allocator) !void {
const now = DateTime.now();
try db.insert("token", .{
.account_id = account_id,
.hash = hash,
.issued_at = now,
}, alloc);
}
2023-01-02 07:34:30 +00:00
pub fn getByHash(db: anytype, hash: []const u8, community_id: Uuid, alloc: std.mem.Allocator) !Token {
2023-01-02 00:28:36 +00:00
return db.queryRow(
Token,
\\SELECT account_id, issued_at, hash
\\FROM token
2023-01-02 07:34:30 +00:00
\\ JOIN account
\\ JOIN actor
\\ ON token.account_id = account.id AND account.id = actor.id
\\WHERE token.hash = $1 AND actor.community_id = $2
2023-01-02 00:28:36 +00:00
\\LIMIT 1
,
2023-01-02 07:34:30 +00:00
.{ hash, community_id },
2023-01-02 00:28:36 +00:00
alloc,
) catch |err| switch (err) {
error.NoRows => error.InvalidToken,
else => error.DatabaseFailure,
};
}