Documentation
¶
Overview ¶
Package egauthtest holds the test-only helpers of egauth: the development configuration, the principal builder, the manual clock, a capturing mailbox and a fake OIDC provider. It is the only sanctioned home of Insecure opt-outs and of the root test hooks, and the G15 guard fails any import of it from a non-test file, so a helper copied into a main package fails CI.
Stability ¶
Stability class: experimental. No SemVer guarantee: the API may change or be removed in any release. See docs/adr/0001-v1-scope-and-stability-classes.md.
Index ¶
- Constants
- func Config[C any]() egauth.Config[C]
- func New[C any](cfg egauth.Config[C], clk *Clock, idp *IdP) (*egauth.Auth[C], error)
- func NewPrincipal[C any](spec PrincipalSpec) (egauth.Principal[C], error)
- func NewResourceServer[C any](cfg egauth.ResourceConfig, clk *Clock, roots *x509.CertPool) (*egauth.ResourceServer[C], error)
- func ReferenceTime() time.Time
- type Clock
- type IDToken
- type IdP
- type IdPUser
- type Mail
- type Mailbox
- func (m *Mailbox) AccountExistsNotice(_ context.Context, n egauth.NoticeMessage) error
- func (m *Mailbox) EmailChangeLink(_ context.Context, l egauth.LinkMessage) error
- func (m *Mailbox) EmailVerificationLink(_ context.Context, l egauth.LinkMessage) error
- func (m *Mailbox) GoString() string
- func (m *Mailbox) Last(kind string) (Mail, bool)
- func (m *Mailbox) LogValue() slog.Value
- func (m *Mailbox) LoginCode(_ context.Context, c egauth.CodeMessage) error
- func (m *Mailbox) MagicLink(_ context.Context, l egauth.LinkMessage) error
- func (m *Mailbox) Messages() []Mail
- func (m *Mailbox) PasswordResetLink(_ context.Context, l egauth.LinkMessage) error
- func (m *Mailbox) RecoveryEmailLink(_ context.Context, l egauth.LinkMessage) error
- func (m *Mailbox) RegistrationLink(_ context.Context, l egauth.LinkMessage) error
- func (m *Mailbox) SecurityNotice(_ context.Context, n egauth.SecurityNoticeMessage) error
- func (m *Mailbox) String() string
- type PrincipalSpec
- type Resolution
Constants ¶
const TestOptOutReason = "egauthtest development configuration; never use in production"
TestOptOutReason is the reason every opt-out of Config acknowledges; Posture reports it.
Variables ¶
This section is empty.
Functions ¶
func Config ¶
Config returns a loopback development configuration: stateful sessions, the password module with a Mailbox as Email, and the opt-outs a fast test needs (no rate limit). Storage is left empty because this package cannot import the stores; assign it before New:
cfg := egauthtest.Config[struct{}]()
cfg.Storage = memstore.New(cfg.KEK)
Read captured mail back with cfg.Email.(*egauthtest.Mailbox). Every call draws a fresh KEK from crypto/rand, so data sealed under one Config is unreadable under another: keep cfg.KEK to reopen a store. Every opt-out carries TestOptOutReason and is accepted by New only while every origin and audience is loopback; a public deployment must acknowledge it with its own reason.
func New ¶
New is egauth.New for a journey: every component of the Auth reads clk instead of the wall clock, which must be the clock the storage was built with (memstore.NewWithClock), and its outbound client trusts the certificate of idp. Either may be nil to keep the wall clock or the system roots. The SSRF dial guard stays in force, so a loopback provider still needs Insecure.OutboundPrivateNetwork in cfg.
func NewPrincipal ¶
func NewPrincipal[C any](spec PrincipalSpec) (egauth.Principal[C], error)
NewPrincipal builds the Principal described by spec without running any route gate: an API-key Principal when spec.Machine is set, a session Principal otherwise. Use egauth.Principal's zero value for Anonymous. It fails when Custom is not a C or the spec cannot form a caller.
func NewResourceServer ¶
func NewResourceServer[C any](cfg egauth.ResourceConfig, clk *Clock, roots *x509.CertPool) (*egauth.ResourceServer[C], error)
NewResourceServer is egauth.NewResourceServer for a journey: the server reads clk, trusts the certificates in roots, and may reach an Auth on loopback, as an httptest Auth is. Either may be nil to keep the wall clock or the system roots.
func ReferenceTime ¶
ReferenceTime is the instant NewClock(time.Time{}) starts at and the default instant of every time in a PrincipalSpec.
Types ¶
type Clock ¶
type Clock struct {
// contains filtered or unexported fields
}
Clock is a manually driven clock: it never advances on its own and is safe for concurrent use.
func (*Clock) Library ¶
Library returns the library clock that reads c, observing every Set and Advance. Pass it to memstore.NewWithClock so that store-side expiry follows c.
type IdP ¶
type IdP struct {
// contains filtered or unexported fields
}
IdP is a fake OpenID Connect provider on a loopback TLS server: discovery, authorization, token, userinfo and JWKS endpoints, ID tokens signed with a throwaway ES256 key. It is as strict as a conforming provider so that a client regression fails: authorization requires state, nonce and an S256 PKCE challenge; the token endpoint requires the authorization_code grant, the verifier and client authentication (client_secret_post or client_secret_basic) by the client the code was issued to. The browser sign-in is simulated: Authorize immediately redirects back as the current user.
func NewIdP ¶
NewIdP starts the fake provider and stops it when t ends. Times in ID tokens come from clk, or from the wall clock when clk is nil, which is what an Auth without a test clock checks them against. The initial user is subject "idp-user-1" with a verified email.
func (*IdP) ClientSecret ¶
ClientSecret returns the client secret the token endpoint requires with ClientID, by client_secret_post or client_secret_basic. It is public and protects nothing.
func (*IdP) HTTPClient ¶
HTTPClient returns a client that trusts the provider's certificate, follows no redirect and gives up after 60 seconds.
func (*IdP) Provider ¶
func (p *IdP) Provider(name string) federation.Provider
Provider returns the egauth federation spec of the fake provider under name, trusting its verified emails. It carries Issuer and JWKSURL, so it needs no discovery.
type Mail ¶
type Mail struct {
Kind string
To string
Link string
Code string
RevertLink string
DeliveryID string
}
Mail is one message a Mailbox captured. Link, Code and RevertLink are the working secrets, so never log a Mail.
func (Mail) MarshalJSON ¶
MarshalJSON renders m with its secrets redacted.
type Mailbox ¶
type Mailbox struct {
// contains filtered or unexported fields
}
Mailbox is an egauth.EmailDeliverer that records every message instead of sending it. It is safe for concurrent use.
func (*Mailbox) AccountExistsNotice ¶
func (*Mailbox) EmailChangeLink ¶
func (*Mailbox) EmailVerificationLink ¶
func (*Mailbox) Last ¶
Last returns the most recent message of kind, and false when there is none. Kind is the method name of egauth.EmailDeliverer, for example "PasswordResetLink".
func (*Mailbox) PasswordResetLink ¶
func (*Mailbox) RecoveryEmailLink ¶
func (*Mailbox) RegistrationLink ¶
func (*Mailbox) SecurityNotice ¶
type PrincipalSpec ¶
type PrincipalSpec struct {
Subject principal.Subject
Lineage uuid.UUID
UsedFactors []principal.Factor
PasskeyID []byte
SecurityEpoch uint64
MustChange bool
Remembered bool
ReturnPath string
Transport principal.Transport
Provider string
PrimaryAt time.Time
SecondAt time.Time
CreatedAt time.Time
ClaimsFetchedAt time.Time
Roles []string
Groups []string
Scopes []string
Custom any
Machine *egauth.APIKeyInfo
}
PrincipalSpec describes the Principal NewPrincipal builds. Every zero field takes a default: the single tenant and a fixed user, the password factor, cookie transport, and ReferenceTime for PrimaryAt, CreatedAt and ClaimsFetchedAt.
type Resolution ¶
type Resolution struct {
// contains filtered or unexported fields
}
Resolution is the read-only view of a configuration after the defaults registry was applied.
func Resolved ¶
func Resolved[C any](cfg egauth.Config[C]) Resolution
Resolved applies the library defaults to a copy of cfg without building an Auth, so a test can read back that a default took effect. Paths are dotted field names with [i] for elements, for example "Policy.IdleTimeout" or "Modules.APIKeys.Kinds[0]".
func (Resolution) Applied ¶
func (r Resolution) Applied() []string
Applied returns the sorted paths filled from the defaults registry.
func (Resolution) Defaulted ¶
func (r Resolution) Defaulted(path string) bool
Defaulted reports whether the field at path was filled from the defaults registry.
func (Resolution) Get ¶
func (r Resolution) Get(path string) (any, bool)
Get returns the value at path after defaults, and false for an unknown path.
func (Resolution) Problems ¶
func (r Resolution) Problems() []string
Problems returns every validation failure as "<field>: <problem>".