oficina / home Public

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

Plain git — no account needed to clone.

Download

Download this file

1// Package teammode is roster team mode for an Oficina app: the shared half
2// of what Docs and Sheets each built by hand (Home implementation/
3// team-members.md Part B §7; implementation/teammode-package.md).
4//
5// With team mode configured, Home's roster decides a person's team standing
6// on every request, and the app's own stored roles are never read for it.
7// This package holds the parts that are the same in every app:
8//
9// - the five <PREFIX>_TEAM_* variables, all or none, beside Home
10// credentials (ConfigFromEnv, Verify);
11// - the operator's provisioning record, verified against bindings built
12// only from configuration, and the bindings fingerprint (Settings);
13// - the activation marker and the roster evidence store, under the app's
14// namespace, read before any migration (State, State.BootSchema);
15// - the runtime around the roster cache, whose Check is the only way the
16// app asks it (Runtime, Runtime.Check);
17// - one request's tri-state standing, Member / NotMember / Unknown, with a
18// lease re-checked every time it is asked and never waited on (Access,
19// Guard);
20// - the GET /api/team readiness probe (Probe).
21//
22// What stays in each app: how standing maps onto its access queries, how a
23// Home-only person is allocated in its people table (through connect's
24// Unlinked hook, gated by Runtime.AdmitUnlinked), and its stream hook.
25package teammode
26
27import (
28 "crypto/sha256"
29 "encoding/base64"
30 "errors"
31 "os"
32
33 "amadan.net/oficina/home/roster"
34 "github.com/go-json-experiment/json"
35)
36
37var (
38 // ErrConfig refuses boot: configuration that is incomplete, malformed,
39 // or does not match the verified provisioning record or the marker.
40 // There is no falling back to the allowlist.
41 ErrConfig = errors.New("teammode: invalid team configuration")
42 // ErrUnavailable is "cannot decide now": storage, or the roster.
43 ErrUnavailable = errors.New("teammode: team authority unavailable")
44 // ErrNotMember is a definite answer: the roster does not list the
45 // subject on this team.
46 ErrNotMember = errors.New("teammode: not a team member")
47)
48
49// Config is the five deployment variables, never request input.
50type Config struct {
51 TeamID string
52 DirectoryOrigin string
53 ProvisioningBase64 string
54 OperatorKeysBase64 string
55 RosterToken string
56}
57
58// EnvNames are the five variables for an app's prefix, in Config's order:
59// <PREFIX>_TEAM_ID, _TEAM_DIRECTORY_ORIGIN, _TEAM_PROVISIONING_BASE64,
60// _TEAM_OPERATOR_KEYS_BASE64 and _TEAM_ROSTER_TOKEN.
61func EnvNames(prefix string) [5]string {
62 return [5]string{prefix + "_TEAM_ID", prefix + "_TEAM_DIRECTORY_ORIGIN", prefix + "_TEAM_PROVISIONING_BASE64", prefix + "_TEAM_OPERATOR_KEYS_BASE64", prefix + "_TEAM_ROSTER_TOKEN"}
63}
64
65// ConfigFromEnv reads the five variables for prefix ("DOCS", "SHEETS").
66// It is nil only when all five are absent or empty. One present and the rest
67// missing is a configuration, and an invalid one: Verify refuses it rather
68// than letting the app boot in allowlist mode.
69func ConfigFromEnv(prefix string) *Config { return ConfigFromLookup(prefix, os.Getenv) }
70
71// ConfigFromLookup is ConfigFromEnv over any lookup.
72func ConfigFromLookup(prefix string, getenv func(string) string) *Config {
73 n := EnvNames(prefix)
74 c := Config{TeamID: getenv(n[0]), DirectoryOrigin: getenv(n[1]), ProvisioningBase64: getenv(n[2]), OperatorKeysBase64: getenv(n[3]), RosterToken: getenv(n[4])}
75 if c == (Config{}) {
76 return nil
77 }
78 return &c
79}
80
81// Home is the app's validated Home credentials, as far as team mode needs
82// them: team mode exists only beside Home sign-in. Sheets builds it from
83// connect.DecodedCredentials, Docs from its own Home settings.
84type Home struct {
85 Issuer string
86 ConsumerID string
87}
88
89// Settings is a verified team configuration: the operator's provisioning
90// record checked against bindings built only from configuration.
91type Settings struct {
92 record roster.VerifiedRecord
93 token string
94 fingerprint string
95}
96
97// Bindings are the verified record's bindings.
98func (s *Settings) Bindings() roster.Bindings { return s.record.Bindings() }
99
100// Fingerprint is what the activation marker records.
101func (s *Settings) Fingerprint() string { return s.fingerprint }
102
103// Fingerprint is the marker's digest of a deployment's bindings: base64url
104// SHA-256 of their JSON. Only the bindings, so a re-issued record for the
105// same bindings (a new sequence, a rotated roster key) still matches.
106func Fingerprint(b roster.Bindings) string {
107 raw, err := json.Marshal(b)
108 if err != nil {
109 return ""
110 }
111 digest := sha256.Sum256(raw)
112 return base64.RawURLEncoding.EncodeToString(digest[:])
113}
114
115// Verify checks cfg for app at origin. A nil cfg is team mode off (nil, nil).
116// Anything else is a verified Settings or ErrConfig: every variable present,
117// Home configured, the roster token well formed, and the provisioning record
118// signed by one of the operator keys for exactly these bindings — Home's
119// issuer and consumer ID, the app, the team, the directory and this
120// instance's origin. Errors never carry a supplied value.
121func Verify(cfg *Config, home *Home, app, origin string) (*Settings, error) {
122 if cfg == nil {
123 return nil, nil
124 }
125 if home == nil || home.Issuer == "" || home.ConsumerID == "" {
126 return nil, ErrConfig
127 }
128 if cfg.TeamID == "" || cfg.DirectoryOrigin == "" || cfg.ProvisioningBase64 == "" || cfg.OperatorKeysBase64 == "" || !roster.ValidChallenge(cfg.RosterToken) {
129 return nil, ErrConfig
130 }
131 raw, ok := strictBase64(cfg.ProvisioningBase64, roster.BodyLimit)
132 if !ok {
133 return nil, ErrConfig
134 }
135 encoded, ok := strictBase64(cfg.OperatorKeysBase64, 4096)
136 if !ok {
137 return nil, ErrConfig
138 }
139 var keys []roster.Key
140 if json.Unmarshal(encoded, &keys, json.RejectUnknownMembers(true)) != nil {
141 return nil, ErrConfig
142 }
143 expected := roster.Bindings{Issuer: home.Issuer, Directory: cfg.DirectoryOrigin, Consumer: home.ConsumerID, App: app, Team: cfg.TeamID, Audience: origin}
144 record, err := roster.VerifyProvisioning(raw, keys, expected)
145 if err != nil {
146 return nil, ErrConfig
147 }
148 fingerprint := Fingerprint(record.Bindings())
149 if fingerprint == "" {
150 return nil, ErrConfig
151 }
152 return &Settings{record: record, token: cfg.RosterToken, fingerprint: fingerprint}, nil
153}
154
155// strictBase64 accepts only canonical standard base64 of at most limit bytes.
156func strictBase64(encoded string, limit int) ([]byte, bool) {
157 if encoded == "" || len(encoded) > base64.StdEncoding.EncodedLen(limit) {
158 return nil, false
159 }
160 decoded, err := base64.StdEncoding.Strict().DecodeString(encoded)
161 if err != nil || len(decoded) > limit || base64.StdEncoding.EncodeToString(decoded) != encoded {
162 return nil, false
163 }
164 return decoded, true
165}
166