oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1import CryptoKit
2import Foundation
3import Testing
4@testable import EquipoKit
5
6/// The server's bytes, from `personapi.TestNativeDeviceProofVectors`.
7struct ProofVector: Decodable {
8 let name, seed, publicKey, instance, id, challenge, nonce, message, signature: String
9}
10
11@Suite struct DeviceProofTests {
12 let vectors: [ProofVector] = try! Fixture.decode("device-proof-vectors")
13
14 @Test func messageIsTheServersBytes() {
15 for v in vectors {
16 let ours = DeviceProof.message(instance: v.instance, id: v.id, challenge: v.challenge, nonce: v.nonce)
17 #expect(String(decoding: ours, as: UTF8.self) == v.message, "vector \(v.name)")
18 }
19 }
20
21 @Test func signaturesVerifyBothWays() throws {
22 for v in vectors {
23 let key = try Curve25519.Signing.PrivateKey(rawRepresentation: Base64URL.decode(v.seed)!)
24 #expect(Base64URL.encode(key.publicKey.rawRepresentation) == v.publicKey)
25 let message = Data(v.message.utf8)
26 // The server's signature verifies here, and ours verifies with the server's key.
27 #expect(key.publicKey.isValidSignature(Base64URL.decode(v.signature)!, for: message))
28 #expect(key.publicKey.isValidSignature(try key.signature(for: message), for: message))
29 }
30 }
31
32 @Test func instanceComesFromTheChallenge() {
33 for v in vectors { #expect(DeviceProof.instance(fromChallenge: v.challenge) == v.instance) }
34 #expect(DeviceProof.instance(fromChallenge: "not base64 json") == nil)
35 }
36
37 @Test func noncesAreFreshAndLongEnough() {
38 let a = DeviceProof.nonce(), b = DeviceProof.nonce()
39 #expect(a != b)
40 #expect(Base64URL.decode(a)!.count == 32)
41 }
42
43 @Test func exchangeBodyIsSnakeCase() throws {
44 let challenge: DeviceChallenge = try Fixture.decode("device-challenge")
45 let body = try DeviceProof.body(for: challenge, key: .init(), nonce: "nonce-0123456789ab")
46 let json = try JSONSerialization.jsonObject(with: EquipoJSON.encoder.encode(body)) as! [String: String]
47 #expect(Set(json.keys) == ["id", "device_secret", "challenge", "nonce", "signature"])
48 }
49}
50