| 1 | package handoff |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/sha256" |
| 6 | "encoding/base64" |
| 7 | "time" |
| 8 | |
| 9 | "amadan.net/rastrillo/rastrillo/assertion" |
| 10 | ) |
| 11 | |
| 12 | // The native slice (Home spec 2026-11-14-oficina-id-native.md §7) has no |
| 13 | // browser and so no pending row: a native client proves it requested an |
| 14 | // assertion by holding the verifier behind the challenge it gave Home, and |
| 15 | // Home, not the client, sets the nonce from that challenge. These two |
| 16 | // functions are the whole derivation. Home's assert and every instance's |
| 17 | // NativeExchange use them, so the two sides cannot drift apart. |
| 18 | // |
| 19 | // On the wire every value is unpadded base64url: |
| 20 | // |
| 21 | // verifier = 32 random bytes (43 characters) |
| 22 | // challenge = SHA-256(the verifier's 43 ASCII characters) |
| 23 | // (43 characters) |
| 24 | // nonce = SHA-256("oficina-id/native/v1" || 0x00 || challenge's 32 bytes)[:16] |
| 25 | // (22 characters) |
| 26 | // |
| 27 | // The challenge hashes the verifier string, not its decoded bytes: that is |
| 28 | // the S256 convention rastrillo/native's /native/exchange already uses, and |
| 29 | // the one the Swift client implements. The nonce is cut to 16 bytes because |
| 30 | // an assertion nonce is exactly that (rastrillo assertion: 22 characters of |
| 31 | // 16 bytes). |
| 32 | const nativeNonceContext = "oficina-id/native/v1\x00" |
| 33 | |
| 34 | // NativeChallenge is the challenge for a native verifier, or false unless |
| 35 | // the verifier is the canonical encoding of exactly 32 bytes. |
| 36 | func NativeChallenge(verifier string) (string, bool) { |
| 37 | if _, ok := decode32(verifier); !ok { |
| 38 | return "", false |
| 39 | } |
| 40 | sum := sha256.Sum256([]byte(verifier)) |
| 41 | return base64.RawURLEncoding.EncodeToString(sum[:]), true |
| 42 | } |
| 43 | |
| 44 | // NativeNonce is the nonce Home sets on a native assertion for challenge, |
| 45 | // or false unless the challenge is the canonical encoding of 32 bytes. |
| 46 | // Nothing else may produce a native nonce: a caller-chosen nonce is a |
| 47 | // browser's (/authorize), and NativeExchange accepts only this one. |
| 48 | func NativeNonce(challenge string) (string, bool) { |
| 49 | raw, ok := decode32(challenge) |
| 50 | if !ok { |
| 51 | return "", false |
| 52 | } |
| 53 | sum := sha256.Sum256(append([]byte(nativeNonceContext), raw...)) |
| 54 | return base64.RawURLEncoding.EncodeToString(sum[:16]), true |
| 55 | } |
| 56 | |
| 57 | func decode32(s string) ([]byte, bool) { |
| 58 | if len(s) != 43 { |
| 59 | return nil, false |
| 60 | } |
| 61 | raw, err := base64.RawURLEncoding.Strict().DecodeString(s) |
| 62 | return raw, err == nil && len(raw) == 32 && base64.RawURLEncoding.EncodeToString(raw) == s |
| 63 | } |
| 64 | |
| 65 | // Verify checks an assertion against this store's pinned keys, issuer and |
| 66 | // audience at now. It consumes and writes nothing: a native exchange |
| 67 | // verifies first and reserves the jti only once it has decided to mint. |
| 68 | func (s *Store) Verify(token string, now time.Time) (assertion.Claims, error) { |
| 69 | claims, err := s.verifier.Verify(token, now) |
| 70 | if err != nil { |
| 71 | return assertion.Claims{}, ErrRefused |
| 72 | } |
| 73 | return claims, nil |
| 74 | } |
| 75 | |
| 76 | // Reserve burns an assertion's (iss, jti) in its own short transaction and |
| 77 | // commits before returning, so no caller can hold the writer across it. |
| 78 | // It re-verifies the token, and refuses one whose jti is already reserved |
| 79 | // or is waiting in a browser's inbox. A reserved jti that then produces no |
| 80 | // session costs only a fresh assertion; a jti is never reserved twice, so |
| 81 | // replay stays impossible (the native slice's accepted non-atomicity). |
| 82 | func (s *Store) Reserve(ctx context.Context, token string, now time.Time) (assertion.Claims, error) { |
| 83 | zero := assertion.Claims{} |
| 84 | claims, err := s.Verify(token, now) |
| 85 | if err != nil { |
| 86 | return zero, err |
| 87 | } |
| 88 | tx, _, _, err := s.begin(ctx, now) |
| 89 | if err != nil { |
| 90 | return zero, err |
| 91 | } |
| 92 | defer tx.Rollback() |
| 93 | jti := jtiHash(claims) |
| 94 | var waiting int |
| 95 | if err = tx.QueryRowContext(ctx, `SELECT COUNT(*) FROM oficina_handoff_inbox WHERE jti_hash=?`, jti).Scan(&waiting); err != nil { |
| 96 | return zero, ErrStorage |
| 97 | } |
| 98 | if waiting != 0 { |
| 99 | return zero, ErrRefused |
| 100 | } |
| 101 | result, err := tx.ExecContext(ctx, `INSERT INTO oficina_handoff_replay(jti_hash,expires_at) VALUES(?,?) ON CONFLICT DO NOTHING`, jti, claims.ExpiresAt) |
| 102 | if err != nil { |
| 103 | return zero, ErrStorage |
| 104 | } |
| 105 | rows, err := result.RowsAffected() |
| 106 | if err != nil { |
| 107 | return zero, ErrStorage |
| 108 | } |
| 109 | if rows != 1 { |
| 110 | return zero, ErrRefused |
| 111 | } |
| 112 | if tx.Commit() != nil { |
| 113 | return zero, ErrStorage |
| 114 | } |
| 115 | return claims, nil |
| 116 | } |
| 117 | |