Source identity
| Field | Recorded value |
|---|---|
| Source type | GitHub source |
| Original record | Janady13/missionmatch / internal/modules/auth/dto.go |
| Date | Not specified in source |
| Repository | Janady13/missionmatch |
| Revision | 720d47fae476b0d38477ae4401697d372b1ea2bc |
| Branch | feature/opportunity-to-outcome |
| Path or archive member | internal/modules/auth/dto.go |
| Original SHA-256 | c3b03bb36326e7a007dafc850f022903f5af3f18decf825448bd57c697a0c595 |
| Git blob SHA | fa46ea4e6b0f0c837aeb37790cad06eb9a06ee52 |
| Reading-copy SHA-256 | c3b03bb36326e7a007dafc850f022903f5af3f18decf825448bd57c697a0c595 |
Recovered source transcript
Download complete source text ↓215 lines · 10.2 KB
// Package auth is the first party authentication and accounts module. It produces a verified
// (org_id, user_id, role) from an opaque server side session and feeds that, and only that, into the
// tenant resolver, replacing the dev X-Org-ID header. The cryptographic primitives live in
// internal/platform/security; this package wires them to the database (through the SECURITY DEFINER
// functions in migration 0009) and to HTTP.
package auth
import (
"github.com/missionworkforce/missionmatch/internal/platform/audit"
"github.com/missionworkforce/missionmatch/internal/platform/orgsettings"
"github.com/missionworkforce/missionmatch/internal/platform/security"
"github.com/missionworkforce/missionmatch/internal/platform/tenant"
)
// Cookie names. The session cookie uses the __Host- prefix, which a browser only accepts when the
// cookie is Secure, Path=/, and has no Domain, so it cannot be planted by a sibling subdomain. The
// prefix and the Secure attribute must agree: a __Host- cookie without Secure is silently dropped by
// the browser. So the name is chosen to match the Secure flag (the secure name in production, a plain
// name only for plain http local dev). Production config requires CookieSecure true.
const (
SessionCookie = "__Host-mm_session"
CSRFCookie = "__Host-mm_csrf"
CSRFHeader = "X-CSRF-Token"
sessionCookieInsecure = "mm_session"
csrfCookieInsecure = "mm_csrf"
)
// Deps is everything the service needs, assembled in cmd/api from config. Keys never have defaults;
// they come from the secrets file through config.
type Deps struct {
DB *tenant.DB
Audit *audit.Writer
Hasher *security.Hasher
Tagger *security.Tagger
TOTP security.TOTP
TOTPKEKs map[int][]byte // key_version to AES 256 key, for decrypt
TOTPKEKActive int // version new secrets are sealed under
TOTPIssuer string
IdleSeconds int
AbsoluteSeconds int
PendingSeconds int
InviteSeconds int
CookieSecure bool
MFADisabled bool // pilot only: when true, login and invite accept mint an active session with no TOTP step. The zero value (false) keeps MFA mandatory, so a Deps built without setting it is secure by default.
// OperatorMFADisabled stands down the operator only second factor of amendment 6 decision 1. Like
// MFADisabled the ZERO VALUE keeps the rule ON, so a Deps built without setting it is secure by default
// and every test that does not mention it proves the amendment's behaviour rather than the exception.
// Config refuses it in a production MM_ENV.
OperatorMFADisabled bool
// Settings resolves the organization's own mail configuration so an invitation can be delivered from the
// customer's address rather than handed to an administrator to forward. NIL IS A SUPPORTED STATE and
// means the pilot behaviour: the one time link is returned in the response and a human delivers it.
Settings *orgsettings.Store
// AppBaseURL is the public address of the web application, used to build the activation link in an
// invitation. Empty means no link is built and the raw code is sent with an instruction instead, because
// a guessed hostname in a credential bearing email is worse than none.
AppBaseURL string
}
// LoginInput is a password login attempt.
type LoginInput struct {
Email string `json:"email"`
Password string `json:"password"`
}
// LoginResult tells the SPA whether to show the enroll or the verify step. Token is set as the
// session cookie by the handler, never returned in the body. When MFARequired is false the issued
// session is already active, so the SPA skips the MFA step and goes straight to the app.
type LoginResult struct {
Token string `json:"-"`
MFAEnrolled bool `json:"mfa_enrolled"`
MFARequired bool `json:"mfa_required"`
}
// CodeInput carries a submitted six digit TOTP code or a recovery code.
type CodeInput struct {
Code string `json:"code"`
}
// EnrollResult returns the provisioning material shown once during enrollment.
type EnrollResult struct {
OTPAuthURI string `json:"otpauth_uri"`
Secret string `json:"secret"`
}
// ActivateResult returns the recovery codes shown exactly once on activation. Token is the new active
// session cookie.
type ActivateResult struct {
Token string `json:"-"`
RecoveryCodes []string `json:"recovery_codes"`
}
// TokenResult is a bare new session token for the handler to set as a cookie.
type TokenResult struct {
Token string `json:"-"`
}
// InviteInput is an owner or admin inviting a user into their org.
type InviteInput struct {
Email string `json:"email"`
FullName string `json:"full_name"`
Role string `json:"role"`
// Tools is the product set the new account starts with. Carried on the invite so creating an account
// and granting its access are ONE operation: entitlement fails closed, so an account created without
// this would exist but see nothing, and the operator would have to notice and fix it in a second step.
// Bounded by the same rule as a later change: the inviter may only confer tools they hold themselves.
Tools []string `json:"tools"`
}
// InviteResult returns the one time activation link token for manual delivery at pilot.
type InviteResult struct {
// Token is the one time activation link, and it is EMPTY when Delivered is true. Once the credential has
// reached the invited person by mail there is no reason for a second copy to exist in an administrator's
// browser.
Token string `json:"token,omitempty"`
UserID string `json:"user_id"`
// Delivered reports that the organization's own mail server accepted the message.
Delivered bool `json:"delivered"`
// DeliveredTo echoes the address it went to, so the administrator can see it reached the right person.
DeliveredTo string `json:"delivered_to,omitempty"`
// DeliveryNote explains why the link came back instead of being mailed. It is present only when delivery
// did not happen, and it is guidance for the administrator, never an error: the invitation succeeded
// either way.
DeliveryNote string `json:"delivery_note,omitempty"`
}
// AcceptInput redeems an invite or activation link and sets the password.
type AcceptInput struct {
Token string `json:"token"`
Password string `json:"password"`
}
// ChangePasswordInput changes the authenticated user's password.
type ChangePasswordInput struct {
OldPassword string `json:"old_password"`
NewPassword string `json:"new_password"`
}
// RoleChangeInput sets a target user's role.
type RoleChangeInput struct {
Role string `json:"role"`
}
// StatusChangeInput enables or disables a target user.
type StatusChangeInput struct {
Status string `json:"status"`
}
// SessionInfo is the SPA view of the current session. No secrets.
type SessionInfo struct {
UserID string `json:"user_id"`
OrgID string `json:"org_id"`
Role string `json:"role"`
MFAEnrolled bool `json:"mfa_enrolled"`
// Capabilities is the caller's resolved permission list, computed from the same matrix the server
// enforces. It exists so the interface stops gating on role name comparisons, which duplicate the
// matrix in TypeScript where it drifts, and cannot express a permission that does not map cleanly
// onto one role.
//
// The server remains the only authority. This list decides what is OFFERED, never what is allowed,
// and every endpoint keeps its own gate.
Capabilities []string `json:"capabilities"`
// Tools is the caller's granted product set: which of missionMATCH, screenMATCH, slateMATCH, and
// recruitMATCH exist for them at all. Separate from Capabilities because role answers what a person
// may DO and this answers what they may SEE, and the two vary independently between clients.
//
// Like Capabilities this decides what is OFFERED, never what is allowed: each tool's route group
// carries its own server side gate, so a hidden tool is refused at the API even if the interface is
// bypassed entirely.
Tools []string `json:"tools"`
// OrgTools is what the ORGANIZATION is entitled to, which is a different question from what this
// caller holds, and amendment 6 decision 13 needs both to say WHY a control is disabled: the tenant
// never bought the product, or the actor personally does not hold it. Sending it from the server means
// the two reasons are computed from one source rather than guessed in TypeScript.
//
// There is no organization level product entitlement table yet (the open design point recorded in the
// ledger), so today every tenant is entitled to all four and this carries the full vocabulary. When the
// Phase 5A operator console lands the per tool entitlement, only this value narrows and no client
// changes, which is the whole reason it is a field rather than a constant in the bundle.
OrgTools []string `json:"org_tools"`
// The caller's own identity, so the profile screen renders current values without a second endpoint and
// so a name change propagates everywhere the identity renders on the next session read. Amendment 6
// decision 4 and acceptance item 36.
FullName string `json:"full_name"`
Title string `json:"title"`
Phone string `json:"phone"`
}
// UserSummary is one row of the org user list. No secrets.
type UserSummary struct {
ID string `json:"id"`
Email string `json:"email"`
FullName string `json:"full_name"`
Role string `json:"role"`
Status string `json:"status"`
MFAEnrolled bool `json:"mfa_enrolled"`
// Tools is this user's granted product set, so the People screen can show and edit what they SEE
// alongside what they may do. Always present, empty rather than null, so the client can iterate.
Tools []string `json:"tools"`
// LastLoginAt was the other half of a two part defect. The column was never written, and it was also
// never returned, so fixing only the write would have left the People screen still showing "never
// signed in" for everyone and the fix would have looked like it failed.
LastLoginAt *string `json:"last_login_at,omitempty"`
}
// ToolsChangeInput replaces a user's tool entitlement. The field is the COMPLETE intended set, not a
// delta: the result is exactly these tools, so one request can express "these and no others" and a client
// that has drifted from the server cannot re grant something it believed was already absent.
type ToolsChangeInput struct {
Tools []string `json:"tools"`
}