Graphley / app / Tests / OwnerMetricsKitTests / PasskeyClientTests.swift
| 1 | import Foundation |
| 2 | import Testing |
| 3 | @testable import OwnerMetricsKit |
| 4 | |
| 5 | private let session = String(repeating: "B", count: 43) |
| 6 | private let code = String(repeating: "C", count: 43) |
| 7 | private let json = "application/json; charset=utf-8" |
| 8 | |
| 9 | private func body(_ request: URLRequest) -> [String: Any] { |
| 10 | (try? JSONSerialization.jsonObject(with: request.httpBody ?? Data())) as? [String: Any] ?? [:] |
| 11 | } |
| 12 | |
| 13 | @Test func signInUsesDiscoverableOptionsAndPostsTheAssertionAsWebAuthnJSON() async throws { |
| 14 | let client = PasskeyClient { request in |
| 15 | #expect(request.httpMethod == "POST") |
| 16 | #expect(request.value(forHTTPHeaderField: "Origin") == nil) |
| 17 | #expect(request.value(forHTTPHeaderField: "Authorization") == nil) |
| 18 | switch request.url?.absoluteString { |
| 19 | case "https://projectatom.ai/api/graphley/passkeys/login-options": |
| 20 | return .init(code: 200, contentType: json, data: Data(#"{"challengeId":"c1","options":{"challenge":"AQID","rpId":"projectatom.ai","userVerification":"required"}}"#.utf8)) |
| 21 | case "https://projectatom.ai/api/graphley/passkeys/login": |
| 22 | let sent = body(request) |
| 23 | #expect(sent["challengeId"] as? String == "c1") |
| 24 | let credential = try #require(sent["response"] as? [String: Any]) |
| 25 | #expect(credential["id"] as? String == "_-8") |
| 26 | #expect(credential["type"] as? String == "public-key") |
| 27 | let response = try #require(credential["response"] as? [String: Any]) |
| 28 | #expect(response["signature"] as? String == "BAU") |
| 29 | #expect(response["userHandle"] as? String == "Z3JhcGhsZXktb3duZXI") |
| 30 | return .init(code: 200, contentType: json, data: Data(#"{"session":"\#(session)","expiresAt":"2027-01-01T00:00:00.000Z"}"#.utf8)) |
| 31 | default: |
| 32 | Issue.record("Unexpected request") |
| 33 | return .init(code: 500, contentType: nil, data: Data()) |
| 34 | } |
| 35 | } |
| 36 | let challenge = try await client.signInChallenge() |
| 37 | #expect(challenge.challenge == Data([1, 2, 3])) |
| 38 | let assertion = PasskeyAssertion(credentialID: Data([0xff, 0xef]), clientDataJSON: Data("{}".utf8), |
| 39 | authenticatorData: Data([9]), signature: Data([4, 5]), userID: Data("graphley-owner".utf8)) |
| 40 | #expect(try await client.signIn(challengeID: challenge.id, assertion: assertion) == session) |
| 41 | } |
| 42 | |
| 43 | @Test func setupLinkRegistrationSendsTheCodeAndReturnsTheFirstSession() async throws { |
| 44 | let client = PasskeyClient { request in |
| 45 | switch request.url?.lastPathComponent { |
| 46 | case "register-options": |
| 47 | #expect(body(request)["setupCode"] as? String == code) |
| 48 | #expect(request.value(forHTTPHeaderField: "Authorization") == nil) |
| 49 | return .init(code: 200, contentType: json, data: Data(#""" |
| 50 | {"challengeId":"r1","options":{"challenge":"AQID","user":{"id":"Z3JhcGhsZXktb3duZXI","name":"Graphley owner"},"excludeCredentials":[{"id":"_-8"}],"authenticatorSelection":{"residentKey":"required","userVerification":"required"}}} |
| 51 | """#.utf8)) |
| 52 | default: |
| 53 | let response = try #require((body(request)["response"] as? [String: Any])?["response"] as? [String: Any]) |
| 54 | #expect(response["attestationObject"] as? String == "AAE") |
| 55 | return .init(code: 200, contentType: json, data: Data(#"{"added":true,"session":"\#(session)"}"#.utf8)) |
| 56 | } |
| 57 | } |
| 58 | let challenge = try await client.registrationChallenge(setupCode: code, session: nil) |
| 59 | #expect(challenge.userID == Data("graphley-owner".utf8)) |
| 60 | #expect(challenge.excludedCredentialIDs == [Data([0xff, 0xef])]) |
| 61 | let registration = PasskeyRegistration(credentialID: Data([7]), clientDataJSON: Data("{}".utf8), attestationObject: Data([0, 1])) |
| 62 | #expect(try await client.register(challengeID: challenge.id, registration: registration, session: nil) == session) |
| 63 | } |
| 64 | |
| 65 | @Test func signedInRequestsCarryTheSessionAndMapServerRefusals() async throws { |
| 66 | let spent = PasskeyClient { _ in .init(code: 401, contentType: nil, data: Data()) } |
| 67 | await #expect(throws: MetricsError.setupLinkInvalid) { try await spent.registrationChallenge(setupCode: code, session: nil) } |
| 68 | await #expect(throws: MetricsError.passkeyRejected) { try await spent.signInChallenge() } |
| 69 | |
| 70 | let last = PasskeyClient { request in |
| 71 | #expect(request.value(forHTTPHeaderField: "Authorization") == "Bearer " + session) |
| 72 | return .init(code: 409, contentType: nil, data: Data()) |
| 73 | } |
| 74 | await #expect(throws: MetricsError.lastPasskey) { try await last.remove(id: "a", session: session) } |
| 75 | } |
| 76 | |
| 77 | @Test func onlyGraphleySetupLinksWithAValidCodeAreAccepted() { |
| 78 | #expect(PasskeyClient.setupCode(from: URL(string: "graphley://passkey-setup?code=\(code)")!) == code) |
| 79 | #expect(PasskeyClient.setupCode(from: URL(string: "graphley://passkey-setup?code=short")!) == nil) |
| 80 | #expect(PasskeyClient.setupCode(from: URL(string: "https://projectatom.ai/passkey-setup?code=\(code)")!) == nil) |
| 81 | #expect(PasskeyClient.setupCode(from: URL(string: "graphley://other?code=\(code)")!) == nil) |
| 82 | } |