Repository Public
1// Package identity implements accounts, passkeys (WebAuthn), sessions,2// devices and single-use invitations. It is the only code that creates or3// validates credentials; HTTP handlers are thin wrappers around it.4package identity56import (7 "net/netip"8 "time"910 "github.com/google/uuid"1112 "clustr.spboucher.ai/mcc/internal/authz"13)1415type User struct {16 ID uuid.UUID17 Handle string18 DisplayName string19 Role authz.Role20 Locale string21 WebAuthnHandle []byte22 CreatedAt time.Time23 DisabledAt *time.Time24}2526type Device struct {27 ID uuid.UUID28 UserID uuid.UUID29 Kind string // web | macos | ios30 Label string31 Model string32 OSVersion string33 AppVersion string34 CreatedAt time.Time35 LastSeenAt *time.Time36 RevokedAt *time.Time37 HasPush bool38}3940type Session struct {41 ID uuid.UUID42 Kind string // cookie | bearer43 User User44 DeviceID uuid.UUID45 PasskeyID *uuid.UUID46 CreatedAt time.Time47 LastSeenAt time.Time48 AuthenticatedAt time.Time49 IdleExpiresAt time.Time50 AbsoluteExpiresAt time.Time51 IP string52 UserAgent string53 RevokedAt *time.Time54}5556func (s *Session) Subject() authz.Subject {57 return authz.Subject{UserID: s.User.ID.String(), Role: s.User.Role}58}5960type Passkey struct {61 ID uuid.UUID62 UserID uuid.UUID63 Label string64 CreatedAt time.Time65 LastUsedAt *time.Time66 BackupEligible bool67 BackupState bool68 Transports []string69 RevokedAt *time.Time70 AAGUID []byte // set by ListAllPasskeys only; nil when the authenticator did not provide one71}7273// ClientInfo describes the client performing a ceremony.74type ClientInfo struct {75 Kind string // web | macos | ios76 DeviceID *uuid.UUID77 Label string78 Model string79 OSVersion string80 AppVersion string81}8283func (c ClientInfo) valid() bool { return c.Kind == "web" || c.Kind == "macos" || c.Kind == "ios" }8485// ReqMeta carries request facts recorded in sessions and audit entries.86type ReqMeta struct {87 IP *netip.Addr88 UserAgent string89 Correlation uuid.UUID90}9192// AuthResult is returned by a successful sign-in. Exactly one of93// SessionToken (web cookie) or AccessToken+RefreshToken (native) is set.94type AuthResult struct {95 User User96 Session Session97 Device Device98 SessionToken string99 AccessToken string100 AccessExpiresAt time.Time101 RefreshToken string102}103104type Invitation struct {105 ID uuid.UUID106 Purpose string // new_account | link | recovery107 Role authz.Role108 Handle string109 DisplayName string110 TargetUserID *uuid.UUID111 Note string112 CreatedBy *uuid.UUID113 CreatedByName string114 CreatedAt time.Time115 ExpiresAt time.Time116 ConsumedAt *time.Time117 RevokedAt *time.Time118 CompletedAt *time.Time119 FailedAttempts int120 ResultingUserID *uuid.UUID121}122123// Status summarizes an invitation for administrators.124func (i Invitation) Status(now time.Time) string {125 switch {126 case i.CompletedAt != nil:127 return "completed"128 case i.RevokedAt != nil:129 return "revoked"130 case i.ConsumedAt != nil:131 return "in_progress"132 case !now.Before(i.ExpiresAt):133 return "expired"134 }135 return "pending"136}137138// SetupTx is the short-lived transaction opened by exchanging an invitation token.139type SetupTx struct {140 ID uuid.UUID141 Invitation Invitation142 ExpiresAt time.Time143}144145const (146 maxInvitationTTL = 24 * time.Hour147 defaultInvitationTTL = 15 * time.Minute148 setupTxTTL = 10 * time.Minute149 challengeTTL = 5 * time.Minute150 maxSetupFailures = 5151 bearerAbsolute = 180 * 24 * time.Hour152 touchEvery = 30 * time.Second153)154