oficina / home Public

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

Plain git — no account needed to clone.

Download

Download this file

1package home
2
3import (
4 "database/sql"
5 "errors"
6 "net/http"
7
8 "amadan.net/oficina/home/handoff"
9 "amadan.net/rastrillo/rastrillo/assertion"
10)
11
12var errMintPolicy = errors.New("home: mint refused by policy")
13
14// mintPolicy is the one rule for whether Home mints an assertion for a
15// consumer from a sign-in method. /authorize (handleAssertion) and the
16// native assert both call it, so a rule added here holds on both doors.
17// It is empty in slice A; Contra's passkey-only rule lands here later (§7).
18//
19// A variable rather than a function so a test can install a refusing
20// policy and watch both doors refuse.
21var mintPolicy = func(consumer, method string) error { return nil }
22
23// nativeAssert is POST assert {aud, challenge} → {assertion} (§7 step 1).
24// aud is an origin, matched exactly against the registered consumers. The
25// nonce is derived here from the caller's challenge, never taken from it.
26func (a *App) nativeAssert(tx *sql.Tx, g nativeGrant, body []byte, now int64) (int, any) {
27 var in struct {
28 Aud string `json:"aud"`
29 Challenge string `json:"challenge"`
30 }
31 if !decodeNative(body, &in) {
32 return http.StatusBadRequest, map[string]string{"error": "request"}
33 }
34 // The nonce is handoff's native derivation, the one every
35 // NativeExchange recomputes; the caller supplies only the challenge.
36 nonce, ok := handoff.NativeNonce(in.Challenge)
37 if !ok {
38 return http.StatusBadRequest, map[string]string{"error": "request"}
39 }
40 consumer := ""
41 for id, origin := range a.consumers() {
42 if origin == in.Aud {
43 consumer = id
44 break
45 }
46 }
47 if consumer == "" || consumer == operatorConfirmConsumer || a.runtime == nil || a.runtime.signer == nil {
48 return http.StatusBadRequest, map[string]string{"error": "audience"}
49 }
50 if mintPolicy(consumer, g.method) != nil {
51 return http.StatusForbidden, map[string]string{"error": "policy"}
52 }
53 id, err := assertion.RandomID()
54 if err != nil {
55 return 0, nil
56 }
57 token, err := assertion.Sign(a.runtime.signer, assertion.Claims{
58 Version: 1,
59 Issuer: a.origins.Credential,
60 Audience: in.Aud,
61 Subject: g.person,
62 KeyID: a.runtime.keyID,
63 ID: id,
64 Nonce: nonce,
65 IssuedAt: now,
66 ExpiresAt: now + 120,
67 AuthTime: g.authTime,
68 })
69 if err != nil {
70 return 0, nil
71 }
72 return http.StatusOK, map[string]string{"assertion": token}
73}
74
75// nativeSignout is POST signout: the device's own grant ends (§10). The app
76// signed this request before deleting its key, so it arrives after the key
77// is gone; the proof's 30-second window bounds how late.
78func (a *App) nativeSignout(tx *sql.Tx, g nativeGrant, body []byte, now int64) (int, any) {
79 var in struct{}
80 if !decodeNative(body, &in) {
81 return http.StatusBadRequest, map[string]string{"error": "request"}
82 }
83 if _, err := tx.Exec("UPDATE home_id_grants SET ended_at=?,end_reason='signout' WHERE grant_hash=? AND ended_at IS NULL", now, g.hash); err != nil {
84 return 0, nil
85 }
86 return http.StatusOK, map[string]string{}
87}
88