feat(identity): add server sessions and audited mail recovery
This commit is contained in:
1 parent
20c03288d6
commit
4ee53d9a74
58 files changed
+5359
-567
No files matched your search
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user