feat(identity): add server sessions and audited mail recovery

This commit is contained in:
TakuyaYagam1 committed 2026-10-07 11:03:46 +03:00
1 parent 20c03288d6
commit 4ee53d9a74
58 files changed
+5359 -567

No files matched your search

+36 -12
View File
@@ -35,10 +35,10 @@ func newAPIHandler(health *v1.HealthHandler, problems *problem.Responder, identi
return v1.NewHandler(health, problems, v1.WithIdentity(identityHandler))
}
// newIdentityHandler builds the registration operations and the PostgreSQL
// pool they own. Nothing connects here, so an unavailable database never
// blocks startup. It returns no handler when DATABASE_URL is unset, and the
// operations then answer 501. The application clock is time.Now.
// newIdentityHandler builds the registration and session operations and the
// PostgreSQL pool they own. Nothing connects here, so an unavailable database
// never blocks startup. It returns no handler when DATABASE_URL is unset, and
// the operations then answer 501. The application clock is time.Now.
func newIdentityHandler(
ctx context.Context,
settings config.Config,
@@ -47,7 +47,7 @@ func newIdentityHandler(
metrics *prometheus.Metrics,
) (*v1.IdentityHandler, func(), error) {
if settings.DatabaseURL == "" {
logger.Warn("registration is not served because DATABASE_URL is not set")
logger.Warn("identity operations are not served because DATABASE_URL is not set")
return nil, func() {}, nil
}
keyring, err := newIdentityKeyring(settings, logger)
@@ -110,16 +110,40 @@ func newIdentityHandler(
pool.Close()
return nil, nil, fmt.Errorf("bootstrap.newIdentityHandler - create confirmation: %w", err)
}
sessions, err := identity.NewSessionService(identity.SessionDependencies{
Transactor: transactor,
Users: store.Users(),
Sessions: store.Sessions(),
Credentials: credentials,
Limiter: cache,
Keyring: keyring,
Clock: time.Now,
Metrics: metrics,
Logger: logger,
}, identity.SessionSettings{
ClientHourlyLimit: int64(settings.IdentityClientHourlyLimit),
EmailHourlyLimit: int64(settings.IdentityLoginEmailHourlyLimit),
IdleTimeout: settings.IdentitySessionIdleTimeout,
AbsoluteLifetime: settings.IdentitySessionAbsoluteLifetime,
})
if err != nil {
pool.Close()
return nil, nil, fmt.Errorf("bootstrap.newIdentityHandler - create sessions: %w", err)
}
// The HTTP server stops before this cleanup runs, so no request uses the
// pool while it closes.
return v1.NewIdentityHandler(registration, v1.WithEmailConfirmation(confirmation, settings.AppPublicURL)), pool.Close, nil
return v1.NewIdentityHandler(registration,
v1.WithEmailConfirmation(confirmation, settings.AppPublicURL),
v1.WithSessionService(sessions, settings.AppPublicURL),
), pool.Close, nil
}
// newIdentityKeyring keys the idempotency scopes and rate limit buckets with
// IDENTITY_KEY_SECRET. Only with IDENTITY_ALLOW_EPHEMERAL_KEY, which the
// configuration check demands for an unset secret, a random one is drawn for
// this process: idempotency records and counters written before a restart no
// longer match, and instances of one deployment do not agree.
// newIdentityKeyring derives purpose keys for idempotency scopes, rate-limit
// buckets, and session CSRF tokens from IDENTITY_KEY_SECRET. Only with
// IDENTITY_ALLOW_EPHEMERAL_KEY, which the configuration check demands for an
// unset secret, a random one is drawn for this process. Idempotency records and
// counters no longer match after restart, and CSRF tokens differ between
// restarts and instances.
func newIdentityKeyring(settings config.Config, logger *slog.Logger) (*identity.Keyring, error) {
secret := []byte(settings.IdentityKeySecret)
if len(secret) == 0 {
@@ -127,7 +151,7 @@ func newIdentityKeyring(settings config.Config, logger *slog.Logger) (*identity.
if _, err := rand.Read(secret); err != nil {
return nil, fmt.Errorf("bootstrap.newIdentityKeyring - generate secret: %w", err)
}
logger.Warn("IDENTITY_ALLOW_EPHEMERAL_KEY is set and IDENTITY_KEY_SECRET is not; a random secret is used, so idempotency records and rate limits do not survive a restart")
logger.Warn("IDENTITY_ALLOW_EPHEMERAL_KEY is set and IDENTITY_KEY_SECRET is not; a random secret is used, so idempotency records and rate limits do not survive a restart and session CSRF tokens differ between restarts and instances")
}
keyring, err := identity.NewKeyring(secret)
if err != nil {
+6 -2
View File
@@ -39,7 +39,7 @@ func initialize(
wire.Bind(new(inbound.Readiness), new(*health.Readiness)),
wire.Bind(new(httpmiddleware.RequestMetrics), new(*metrics.Metrics)),
wire.Bind(new(apiadapter.Handler), new(*v1.Handler)),
wire.Bind(new(apiadapter.SecurityHandler), new(*v1.DenyAllSecurity)),
wire.Bind(new(apiadapter.SecurityHandler), new(*v1.SessionSecurity)),
newReadiness,
metrics.New,
newHealthHandler,
@@ -47,7 +47,7 @@ func initialize(
newAPIHandler,
newTrustedProxies,
newIdentityHandler,
v1.NewDenyAllSecurity,
newSessionSecurity,
httpadapter.NewRouter,
newOutboxRelay,
newEventConsumers,
@@ -60,6 +60,10 @@ func initialize(
return nil, nil, nil
}
func newSessionSecurity(identityHandler *v1.IdentityHandler) *v1.SessionSecurity {
return identityHandler.SessionSecurity()
}
func newHealthHandler(readiness inbound.Readiness, config config.Config) *v1.HealthHandler {
return v1.NewHealthHandler(readiness, config.ReadinessTimeout)
}
+6 -2
View File
@@ -54,9 +54,9 @@ func initialize(ctx context.Context, config2 config.Config, logger *slog.Logger,
return nil, nil, err
}
handler := newAPIHandler(healthHandler, responder, identityHandler)
denyAllSecurity := v1.NewDenyAllSecurity()
sessionSecurity := newSessionSecurity(identityHandler)
v := newTrustedProxies(config2)
httpHandler, err := httpadapter.NewRouter(healthHandler, handler, denyAllSecurity, responder, metrics, logger, v)
httpHandler, err := httpadapter.NewRouter(healthHandler, handler, sessionSecurity, responder, metrics, logger, v)
if err != nil {
cleanup4()
cleanup3()
@@ -130,6 +130,10 @@ func initialize(ctx context.Context, config2 config.Config, logger *slog.Logger,
// wire.go:
func newSessionSecurity(identityHandler *v1.IdentityHandler) *v1.SessionSecurity {
return identityHandler.SessionSecurity()
}
func newHealthHandler(readiness inbound.Readiness, config2 config.Config) *v1.HealthHandler {
return v1.NewHealthHandler(readiness, config2.ReadinessTimeout)
}