// Package teammode is roster team mode for an Oficina app: the shared half // of what Docs and Sheets each built by hand (Home implementation/ // team-members.md Part B §7; implementation/teammode-package.md). // // With team mode configured, Home's roster decides a person's team standing // on every request, and the app's own stored roles are never read for it. // This package holds the parts that are the same in every app: // // - the five _TEAM_* variables, all or none, beside Home // credentials (ConfigFromEnv, Verify); // - the operator's provisioning record, verified against bindings built // only from configuration, and the bindings fingerprint (Settings); // - the activation marker and the roster evidence store, under the app's // namespace, read before any migration (State, State.BootSchema); // - the runtime around the roster cache, whose Check is the only way the // app asks it (Runtime, Runtime.Check); // - one request's tri-state standing, Member / NotMember / Unknown, with a // lease re-checked every time it is asked and never waited on (Access, // Guard); // - the GET /api/team readiness probe (Probe). // // What stays in each app: how standing maps onto its access queries, how a // Home-only person is allocated in its people table (through connect's // Unlinked hook, gated by Runtime.AdmitUnlinked), and its stream hook. package teammode import ( "crypto/sha256" "encoding/base64" "errors" "os" "amadan.net/oficina/home/roster" "github.com/go-json-experiment/json" ) var ( // ErrConfig refuses boot: configuration that is incomplete, malformed, // or does not match the verified provisioning record or the marker. // There is no falling back to the allowlist. ErrConfig = errors.New("teammode: invalid team configuration") // ErrUnavailable is "cannot decide now": storage, or the roster. ErrUnavailable = errors.New("teammode: team authority unavailable") // ErrNotMember is a definite answer: the roster does not list the // subject on this team. ErrNotMember = errors.New("teammode: not a team member") ) // Config is the five deployment variables, never request input. type Config struct { TeamID string DirectoryOrigin string ProvisioningBase64 string OperatorKeysBase64 string RosterToken string } // EnvNames are the five variables for an app's prefix, in Config's order: // _TEAM_ID, _TEAM_DIRECTORY_ORIGIN, _TEAM_PROVISIONING_BASE64, // _TEAM_OPERATOR_KEYS_BASE64 and _TEAM_ROSTER_TOKEN. func EnvNames(prefix string) [5]string { return [5]string{prefix + "_TEAM_ID", prefix + "_TEAM_DIRECTORY_ORIGIN", prefix + "_TEAM_PROVISIONING_BASE64", prefix + "_TEAM_OPERATOR_KEYS_BASE64", prefix + "_TEAM_ROSTER_TOKEN"} } // ConfigFromEnv reads the five variables for prefix ("DOCS", "SHEETS"). // It is nil only when all five are absent or empty. One present and the rest // missing is a configuration, and an invalid one: Verify refuses it rather // than letting the app boot in allowlist mode. func ConfigFromEnv(prefix string) *Config { return ConfigFromLookup(prefix, os.Getenv) } // ConfigFromLookup is ConfigFromEnv over any lookup. func ConfigFromLookup(prefix string, getenv func(string) string) *Config { n := EnvNames(prefix) c := Config{TeamID: getenv(n[0]), DirectoryOrigin: getenv(n[1]), ProvisioningBase64: getenv(n[2]), OperatorKeysBase64: getenv(n[3]), RosterToken: getenv(n[4])} if c == (Config{}) { return nil } return &c } // Home is the app's validated Home credentials, as far as team mode needs // them: team mode exists only beside Home sign-in. Sheets builds it from // connect.DecodedCredentials, Docs from its own Home settings. type Home struct { Issuer string ConsumerID string } // Settings is a verified team configuration: the operator's provisioning // record checked against bindings built only from configuration. type Settings struct { record roster.VerifiedRecord token string fingerprint string } // Bindings are the verified record's bindings. func (s *Settings) Bindings() roster.Bindings { return s.record.Bindings() } // Fingerprint is what the activation marker records. func (s *Settings) Fingerprint() string { return s.fingerprint } // Fingerprint is the marker's digest of a deployment's bindings: base64url // SHA-256 of their JSON. Only the bindings, so a re-issued record for the // same bindings (a new sequence, a rotated roster key) still matches. func Fingerprint(b roster.Bindings) string { raw, err := json.Marshal(b) if err != nil { return "" } digest := sha256.Sum256(raw) return base64.RawURLEncoding.EncodeToString(digest[:]) } // Verify checks cfg for app at origin. A nil cfg is team mode off (nil, nil). // Anything else is a verified Settings or ErrConfig: every variable present, // Home configured, the roster token well formed, and the provisioning record // signed by one of the operator keys for exactly these bindings — Home's // issuer and consumer ID, the app, the team, the directory and this // instance's origin. Errors never carry a supplied value. func Verify(cfg *Config, home *Home, app, origin string) (*Settings, error) { if cfg == nil { return nil, nil } if home == nil || home.Issuer == "" || home.ConsumerID == "" { return nil, ErrConfig } if cfg.TeamID == "" || cfg.DirectoryOrigin == "" || cfg.ProvisioningBase64 == "" || cfg.OperatorKeysBase64 == "" || !roster.ValidChallenge(cfg.RosterToken) { return nil, ErrConfig } raw, ok := strictBase64(cfg.ProvisioningBase64, roster.BodyLimit) if !ok { return nil, ErrConfig } encoded, ok := strictBase64(cfg.OperatorKeysBase64, 4096) if !ok { return nil, ErrConfig } var keys []roster.Key if json.Unmarshal(encoded, &keys, json.RejectUnknownMembers(true)) != nil { return nil, ErrConfig } expected := roster.Bindings{Issuer: home.Issuer, Directory: cfg.DirectoryOrigin, Consumer: home.ConsumerID, App: app, Team: cfg.TeamID, Audience: origin} record, err := roster.VerifyProvisioning(raw, keys, expected) if err != nil { return nil, ErrConfig } fingerprint := Fingerprint(record.Bindings()) if fingerprint == "" { return nil, ErrConfig } return &Settings{record: record, token: cfg.RosterToken, fingerprint: fingerprint}, nil } // strictBase64 accepts only canonical standard base64 of at most limit bytes. func strictBase64(encoded string, limit int) ([]byte, bool) { if encoded == "" || len(encoded) > base64.StdEncoding.EncodedLen(limit) { return nil, false } decoded, err := base64.StdEncoding.Strict().DecodeString(encoded) if err != nil || len(decoded) > limit || base64.StdEncoding.EncodeToString(decoded) != encoded { return nil, false } return decoded, true }