import CottonKit import XCTest /// A Cotton-account passkey's WebAuthn JSON on the iPhone: the core's request /// read into what the system sheet takes, and the sheet's answer written in /// the spelling the Worker checks (`apps/business/src/passkey.ts`). Every /// value is made up. final class AccountPasskeyJSONTests: XCTestCase { // RULE: The challenge reaches the sheet as the bytes the Worker's base64url text names, with the relying party it names. func testAnAssertionRequestReadsItsRelyingPartyAndChallengeBytes() throws { let challenge = Data([0xFB, 0xFF, 0x00, 0x10, 0x7E]) let json = #"{"challenge":"\#(AccountPasskeyJSON.encode(challenge))","rpId":"cottondb.example","timeout":300000,"userVerification":"required","allowCredentials":[]}"# let request = try XCTUnwrap(AccountPasskeyJSON.request(json)) XCTAssertEqual(request.rpId, "cottondb.example") XCTAssertEqual(request.challenge, challenge) XCTAssertNil(AccountPasskeyJSON.request(#"{"challenge":"AAAA"}"#), "no relying party, no sheet") } // RULE: A creation request carries the account's user id, its name and the passkeys it already has. func testACreationRequestReadsTheUserAndThePasskeysToExclude() throws { let user = Data("11111111-1111-4111-8111-111111111111".utf8) let held = Data([1, 2, 3, 4]) let json = """ {"rp":{"id":"cottondb.example","name":"Cotton"},"user":{"id":"\(AccountPasskeyJSON.encode(user))","name":"+15550100123","displayName":"+15550100123"}, "challenge":"\(AccountPasskeyJSON.encode(Data([9, 9])))","pubKeyCredParams":[{"type":"public-key","alg":-7}], "excludeCredentials":[{"type":"public-key","id":"\(AccountPasskeyJSON.encode(held))"}]} """ let creation = try XCTUnwrap(AccountPasskeyJSON.creation(json)) XCTAssertEqual(creation.rpId, "cottondb.example") XCTAssertEqual(creation.userId, user) XCTAssertEqual(creation.userName, "+15550100123") XCTAssertEqual(creation.challenge, Data([9, 9])) XCTAssertEqual(creation.exclude, [held]) } // RULE: The sheet's answer is the WebAuthn JSON the Worker reads: id and rawId alike, every binary field base64url without padding. func testTheSheetsAnswersAreWrittenAsTheWorkerReadsThem() throws { let id = Data([0xFF, 0xEE, 0x01]) let assertion = AccountPasskeyJSON.json(AccountPasskeyJSON.Assertion( credentialId: id, clientDataJSON: Data("{}".utf8), authenticatorData: Data([0xFB]), signature: Data([0x3F, 0xBF]), userHandle: Data("db".utf8))) let got = try XCTUnwrap(try JSONSerialization.jsonObject(with: Data(assertion.utf8)) as? [String: Any]) XCTAssertEqual(got["id"] as? String, "_-4B") XCTAssertEqual(got["rawId"] as? String, "_-4B") XCTAssertEqual(got["type"] as? String, "public-key") let response = try XCTUnwrap(got["response"] as? [String: String]) XCTAssertEqual(response["clientDataJSON"], "e30") XCTAssertEqual(response["authenticatorData"], "-w") XCTAssertEqual(response["signature"], "P78") XCTAssertEqual(response["userHandle"], "ZGI") XCTAssertFalse(assertion.contains("="), "no padding anywhere") let made = AccountPasskeyJSON.json(AccountPasskeyJSON.Registration(credentialId: id, clientDataJSON: Data("{}".utf8), attestationObject: Data([0xA1]))) let registered = try XCTUnwrap(try JSONSerialization.jsonObject(with: Data(made.utf8)) as? [String: Any]) XCTAssertEqual((registered["response"] as? [String: String])?["attestationObject"], "oQ") } }