//! Issuer keys for verifying service auth JWTs and space credentials: an //! account on this server by its stored signing key, any other DID by its //! DID document from the configured PLC directory or did:web. const std = @import("std"); const clock = @import("../core/clock.zig"); const config = @import("../core/config.zig"); const store = @import("../storage/store.zig"); const zat = @import("zat"); /// the resolver for `zat.service_auth.verify` pub const KeyResolver = struct { /// refuse an issuer that is not an account here before resolving anything local_only: bool = false, pub fn resolveSigningKey(self: KeyResolver, arena: std.mem.Allocator, request: zat.service_auth.KeyRequest) ![]const u8 { if (self.local_only and try store.findAccount(arena, request.did) == null) return error.UnknownIssuer; return signingKey(arena, request.did, request.key_id); } }; /// the multibase public key `did` publishes under `kid` (`#atproto` or an /// absolute id) pub fn signingKey(allocator: std.mem.Allocator, did: []const u8, kid: []const u8) ![]const u8 { if (try store.findAccount(allocator, did) != null) { if (!std.mem.eql(u8, kid, "#atproto")) return error.MissingSigningKey; var keypair = try store.signingKeypair(did); const did_key = try keypair.did(allocator); defer allocator.free(did_key); if (!std.mem.startsWith(u8, did_key, "did:key:")) return error.InvalidSigningKey; return allocator.dupe(u8, did_key["did:key:".len..]); } const parsed = zat.Did.parse(did) orelse return error.InvalidDid; var resolver = didResolver(allocator); defer resolver.deinit(); var doc = try resolver.resolve(parsed); defer doc.deinit(); const absolute_kid = if (std.mem.startsWith(u8, kid, "#")) try std.fmt.allocPrint(allocator, "{s}{s}", .{ did, kid }) else try allocator.dupe(u8, kid); defer allocator.free(absolute_kid); for (doc.verification_methods) |method| { if (std.mem.eql(u8, method.id, kid) or std.mem.eql(u8, method.id, absolute_kid)) { if (!std.mem.eql(u8, method.controller, did)) return error.InvalidSigningKey; return allocator.dupe(u8, method.public_key_multibase); } } return error.MissingSigningKey; } pub fn didResolver(allocator: std.mem.Allocator) zat.DidResolver { var resolver = zat.DidResolver.init(store.currentIo(), allocator); resolver.plc_url = config.plcDirectory(); return resolver; } test "did resolution uses the configured PLC directory" { const previous = config.plcDirectory(); defer config.setPlcDirectory(previous); config.setPlcDirectory("http://127.0.0.1:2586/"); var resolver = didResolver(std.testing.allocator); defer resolver.deinit(); try std.testing.expectEqualStrings("http://127.0.0.1:2586", resolver.plc_url); } test "service auth from a local account verifies against its stored key" { try store.init(std.Options.debug_io, ":memory:"); defer store.close(); var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const a = arena.allocator(); _ = try store.createAccount(a, "local.test", "local@test.com", "password", "did:plc:localissuer", true); var keypair = try store.signingKeypair("did:plc:localissuer"); const method = "com.atproto.repo.uploadBlob"; const token = try zat.service_auth.create(a, std.Options.debug_io, &keypair, .{ .iss = "did:plc:localissuer", .aud = config.serverDid(), .lxm = method }); const now = clock.nowSeconds(); const options: zat.service_auth.VerifyOptions = .{ .now = now, .audiences = &.{config.serverDid()}, .lxm = method }; const verified = try zat.service_auth.verify(a, token, options, KeyResolver{ .local_only = true }); try std.testing.expectEqualStrings("did:plc:localissuer", verified.issuer_did); const stranger = try zat.Keypair.fromSecretKey(.p256, @splat(0x47)); const remote = try zat.service_auth.create(a, std.Options.debug_io, &stranger, .{ .iss = "did:plc:elsewhere", .aud = config.serverDid(), .lxm = method }); try std.testing.expectError(error.UnknownIssuer, zat.service_auth.verify(a, remote, options, KeyResolver{ .local_only = true })); const forged = try zat.service_auth.create(a, std.Options.debug_io, &stranger, .{ .iss = "did:plc:localissuer", .aud = config.serverDid(), .lxm = method }); try std.testing.expectError(error.BadJwtSignature, zat.service_auth.verify(a, forged, options, KeyResolver{})); }