package handoff import ( "context" "crypto/sha256" "encoding/base64" "time" "amadan.net/rastrillo/rastrillo/assertion" ) // The native slice (Home spec 2026-11-14-oficina-id-native.md ยง7) has no // browser and so no pending row: a native client proves it requested an // assertion by holding the verifier behind the challenge it gave Home, and // Home, not the client, sets the nonce from that challenge. These two // functions are the whole derivation. Home's assert and every instance's // NativeExchange use them, so the two sides cannot drift apart. // // On the wire every value is unpadded base64url: // // verifier = 32 random bytes (43 characters) // challenge = SHA-256(the verifier's 43 ASCII characters) // (43 characters) // nonce = SHA-256("oficina-id/native/v1" || 0x00 || challenge's 32 bytes)[:16] // (22 characters) // // The challenge hashes the verifier string, not its decoded bytes: that is // the S256 convention rastrillo/native's /native/exchange already uses, and // the one the Swift client implements. The nonce is cut to 16 bytes because // an assertion nonce is exactly that (rastrillo assertion: 22 characters of // 16 bytes). const nativeNonceContext = "oficina-id/native/v1\x00" // NativeChallenge is the challenge for a native verifier, or false unless // the verifier is the canonical encoding of exactly 32 bytes. func NativeChallenge(verifier string) (string, bool) { if _, ok := decode32(verifier); !ok { return "", false } sum := sha256.Sum256([]byte(verifier)) return base64.RawURLEncoding.EncodeToString(sum[:]), true } // NativeNonce is the nonce Home sets on a native assertion for challenge, // or false unless the challenge is the canonical encoding of 32 bytes. // Nothing else may produce a native nonce: a caller-chosen nonce is a // browser's (/authorize), and NativeExchange accepts only this one. func NativeNonce(challenge string) (string, bool) { raw, ok := decode32(challenge) if !ok { return "", false } sum := sha256.Sum256(append([]byte(nativeNonceContext), raw...)) return base64.RawURLEncoding.EncodeToString(sum[:16]), true } func decode32(s string) ([]byte, bool) { if len(s) != 43 { return nil, false } raw, err := base64.RawURLEncoding.Strict().DecodeString(s) return raw, err == nil && len(raw) == 32 && base64.RawURLEncoding.EncodeToString(raw) == s } // Verify checks an assertion against this store's pinned keys, issuer and // audience at now. It consumes and writes nothing: a native exchange // verifies first and reserves the jti only once it has decided to mint. func (s *Store) Verify(token string, now time.Time) (assertion.Claims, error) { claims, err := s.verifier.Verify(token, now) if err != nil { return assertion.Claims{}, ErrRefused } return claims, nil } // Reserve burns an assertion's (iss, jti) in its own short transaction and // commits before returning, so no caller can hold the writer across it. // It re-verifies the token, and refuses one whose jti is already reserved // or is waiting in a browser's inbox. A reserved jti that then produces no // session costs only a fresh assertion; a jti is never reserved twice, so // replay stays impossible (the native slice's accepted non-atomicity). func (s *Store) Reserve(ctx context.Context, token string, now time.Time) (assertion.Claims, error) { zero := assertion.Claims{} claims, err := s.Verify(token, now) if err != nil { return zero, err } tx, _, _, err := s.begin(ctx, now) if err != nil { return zero, err } defer tx.Rollback() jti := jtiHash(claims) var waiting int if err = tx.QueryRowContext(ctx, `SELECT COUNT(*) FROM oficina_handoff_inbox WHERE jti_hash=?`, jti).Scan(&waiting); err != nil { return zero, ErrStorage } if waiting != 0 { return zero, ErrRefused } result, err := tx.ExecContext(ctx, `INSERT INTO oficina_handoff_replay(jti_hash,expires_at) VALUES(?,?) ON CONFLICT DO NOTHING`, jti, claims.ExpiresAt) if err != nil { return zero, ErrStorage } rows, err := result.RowsAffected() if err != nil { return zero, ErrStorage } if rows != 1 { return zero, ErrRefused } if tx.Commit() != nil { return zero, ErrStorage } return claims, nil }