feat(identity): register accounts and resend verification mail
This commit is contained in:
1 parent
5e202ac972
commit
4436f626a3
55 files changed
+3227
-88
No files matched your search
@@ -33,6 +33,7 @@ func consumerTestConfig(t *testing.T) config.Config {
|
||||
settings, err := config.LoadFromEnv(func(key string) (string, bool) {
|
||||
value, ok := map[string]string{
|
||||
"SEAWEEDFS_ACCESS_KEY": "test-access",
|
||||
"IDENTITY_KEY_SECRET": "test-identity-key-secret-0123456789ab",
|
||||
"SEAWEEDFS_SECRET_KEY": "test-secret",
|
||||
"EMAIL_PROVIDER": "disabled",
|
||||
"EMAIL_FROM": "no-reply@example.com",
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
package bootstrap
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/netip"
|
||||
"time"
|
||||
|
||||
"lincodex/backend/config"
|
||||
"lincodex/backend/internal/adapter/primary/http/problem"
|
||||
v1 "lincodex/backend/internal/adapter/primary/http/v1"
|
||||
"lincodex/backend/internal/adapter/secondary/argon2id"
|
||||
"lincodex/backend/internal/adapter/secondary/postgres"
|
||||
"lincodex/backend/internal/adapter/secondary/prometheus"
|
||||
redisadapter "lincodex/backend/internal/adapter/secondary/redis"
|
||||
"lincodex/backend/internal/domain"
|
||||
"lincodex/backend/internal/usecase/consent"
|
||||
"lincodex/backend/internal/usecase/identity"
|
||||
"lincodex/backend/internal/usecase/notification"
|
||||
"lincodex/backend/internal/usecase/notification/mailtemplate"
|
||||
)
|
||||
|
||||
// identityPoolMaxConns bounds the connections of the registration requests:
|
||||
// each holds one for a short transaction, after the password hash is done.
|
||||
const identityPoolMaxConns = 8
|
||||
|
||||
// newTrustedProxies hands the configured reverse proxies to the router.
|
||||
func newTrustedProxies(settings config.Config) []netip.Prefix { return settings.TrustedProxyCIDRs }
|
||||
|
||||
// newAPIHandler composes the handler of the public API from the business
|
||||
// areas that are built. An area without a handler answers 501.
|
||||
func newAPIHandler(health *v1.HealthHandler, problems *problem.Responder, identityHandler *v1.IdentityHandler) *v1.Handler {
|
||||
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.
|
||||
func newIdentityHandler(
|
||||
ctx context.Context,
|
||||
settings config.Config,
|
||||
logger *slog.Logger,
|
||||
cache *redisadapter.Cache,
|
||||
metrics *prometheus.Metrics,
|
||||
) (*v1.IdentityHandler, func(), error) {
|
||||
if settings.DatabaseURL == "" {
|
||||
logger.Warn("registration is not served because DATABASE_URL is not set")
|
||||
return nil, func() {}, nil
|
||||
}
|
||||
keyring, err := newIdentityKeyring(settings, logger)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
renderer, err := mailtemplate.NewRenderer(settings.AppPublicURL)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("bootstrap.newIdentityHandler - create mail renderer: %w", err)
|
||||
}
|
||||
// New deliveries are pinned to the transport and account configured now.
|
||||
account, err := domain.NewMailProviderAccount(string(settings.EmailProvider), settings.EmailAccount)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("bootstrap.newIdentityHandler - mail provider account: %w", err)
|
||||
}
|
||||
hasher, err := argon2id.NewHasher(settings.PasswordHashConcurrency)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("bootstrap.newIdentityHandler - create password hasher: %w", err)
|
||||
}
|
||||
pool, err := postgres.NewSizedPool(ctx, settings.DatabaseURL, identityPoolMaxConns)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("bootstrap.newIdentityHandler - create pool: %w", err)
|
||||
}
|
||||
transactor := postgres.NewTransactor(pool)
|
||||
registration, err := identity.NewRegistration(identity.RegistrationDependencies{
|
||||
Transactor: transactor,
|
||||
ConsentTexts: postgres.NewConsentStore(pool),
|
||||
Idempotency: postgres.NewIdentityStore(pool).IdempotencyRecords(),
|
||||
Consents: consent.NewService(transactor, time.Now),
|
||||
Credentials: identity.NewCredentials(hasher),
|
||||
Mailer: notification.NewServiceMailer(notification.NewTemplateRenderer(renderer), account, time.Now),
|
||||
Limiter: cache,
|
||||
Keyring: keyring,
|
||||
Clock: time.Now,
|
||||
Metrics: metrics,
|
||||
}, identity.RegistrationSettings{
|
||||
MailMinInterval: settings.VerificationMailMinInterval,
|
||||
MailDailyLimit: int64(settings.VerificationMailDailyLimit),
|
||||
ClientHourlyLimit: int64(settings.IdentityClientHourlyLimit),
|
||||
})
|
||||
if err != nil {
|
||||
pool.Close()
|
||||
return nil, nil, fmt.Errorf("bootstrap.newIdentityHandler - create registration: %w", err)
|
||||
}
|
||||
// The HTTP server stops before this cleanup runs, so no request uses the
|
||||
// pool while it closes.
|
||||
return v1.NewIdentityHandler(registration), 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.
|
||||
func newIdentityKeyring(settings config.Config, logger *slog.Logger) (*identity.Keyring, error) {
|
||||
secret := []byte(settings.IdentityKeySecret)
|
||||
if len(secret) == 0 {
|
||||
secret = make([]byte, identity.MinKeySecretBytes)
|
||||
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")
|
||||
}
|
||||
keyring, err := identity.NewKeyring(secret)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("bootstrap.newIdentityKeyring - create keyring: %w", err)
|
||||
}
|
||||
return keyring, nil
|
||||
}
|
||||
@@ -30,6 +30,7 @@ func loadMailConfig(t *testing.T, mail map[string]string) config.Config {
|
||||
t.Helper()
|
||||
values := map[string]string{
|
||||
"SEAWEEDFS_ACCESS_KEY": "test-access",
|
||||
"IDENTITY_KEY_SECRET": "test-identity-key-secret-0123456789ab",
|
||||
"SEAWEEDFS_SECRET_KEY": "test-secret",
|
||||
"EMAIL_FROM": "Lincodex <" + testSenderAddress + ">",
|
||||
"APP_PUBLIC_URL": "http://localhost:8080",
|
||||
|
||||
@@ -44,7 +44,9 @@ func initialize(
|
||||
metrics.New,
|
||||
newHealthHandler,
|
||||
problem.NewResponder,
|
||||
v1.NewHandler,
|
||||
newAPIHandler,
|
||||
newTrustedProxies,
|
||||
newIdentityHandler,
|
||||
v1.NewDenyAllSecurity,
|
||||
httpadapter.NewRouter,
|
||||
newOutboxRelay,
|
||||
|
||||
@@ -45,24 +45,18 @@ func initialize(ctx context.Context, config2 config.Config, logger *slog.Logger,
|
||||
readiness := newReadiness(healthChecker, cache, storage)
|
||||
healthHandler := newHealthHandler(readiness, config2)
|
||||
responder := problem.NewResponder(logger)
|
||||
handler := v1.NewHandler(healthHandler, responder)
|
||||
denyAllSecurity := v1.NewDenyAllSecurity()
|
||||
metrics := prometheus.New()
|
||||
httpHandler, err := httpadapter.NewRouter(healthHandler, handler, denyAllSecurity, responder, metrics, logger)
|
||||
identityHandler, cleanup4, err := newIdentityHandler(ctx, config2, logger, cache, metrics)
|
||||
if err != nil {
|
||||
cleanup3()
|
||||
cleanup2()
|
||||
cleanup()
|
||||
return nil, nil, err
|
||||
}
|
||||
relay, cleanup4, err := newOutboxRelay(ctx, config2, logger, metrics)
|
||||
if err != nil {
|
||||
cleanup3()
|
||||
cleanup2()
|
||||
cleanup()
|
||||
return nil, nil, err
|
||||
}
|
||||
bootstrapEventConsumers, cleanup5, err := newEventConsumers(ctx, config2, logger, metrics)
|
||||
handler := newAPIHandler(healthHandler, responder, identityHandler)
|
||||
denyAllSecurity := v1.NewDenyAllSecurity()
|
||||
v := newTrustedProxies(config2)
|
||||
httpHandler, err := httpadapter.NewRouter(healthHandler, handler, denyAllSecurity, responder, metrics, logger, v)
|
||||
if err != nil {
|
||||
cleanup4()
|
||||
cleanup3()
|
||||
@@ -70,7 +64,15 @@ func initialize(ctx context.Context, config2 config.Config, logger *slog.Logger,
|
||||
cleanup()
|
||||
return nil, nil, err
|
||||
}
|
||||
worker, cleanup6, err := newJobWorker(ctx, config2, logger, metrics)
|
||||
relay, cleanup5, err := newOutboxRelay(ctx, config2, logger, metrics)
|
||||
if err != nil {
|
||||
cleanup4()
|
||||
cleanup3()
|
||||
cleanup2()
|
||||
cleanup()
|
||||
return nil, nil, err
|
||||
}
|
||||
bootstrapEventConsumers, cleanup6, err := newEventConsumers(ctx, config2, logger, metrics)
|
||||
if err != nil {
|
||||
cleanup5()
|
||||
cleanup4()
|
||||
@@ -79,7 +81,7 @@ func initialize(ctx context.Context, config2 config.Config, logger *slog.Logger,
|
||||
cleanup()
|
||||
return nil, nil, err
|
||||
}
|
||||
mailsenderWorker, cleanup7, err := newMailSenderWorker(ctx, config2, logger, metrics, mailSender)
|
||||
worker, cleanup7, err := newJobWorker(ctx, config2, logger, metrics)
|
||||
if err != nil {
|
||||
cleanup6()
|
||||
cleanup5()
|
||||
@@ -89,7 +91,7 @@ func initialize(ctx context.Context, config2 config.Config, logger *slog.Logger,
|
||||
cleanup()
|
||||
return nil, nil, err
|
||||
}
|
||||
sampler, cleanup8, err := newDeliveryMetrics(ctx, config2, logger, metrics)
|
||||
mailsenderWorker, cleanup8, err := newMailSenderWorker(ctx, config2, logger, metrics, mailSender)
|
||||
if err != nil {
|
||||
cleanup7()
|
||||
cleanup6()
|
||||
@@ -100,8 +102,21 @@ func initialize(ctx context.Context, config2 config.Config, logger *slog.Logger,
|
||||
cleanup()
|
||||
return nil, nil, err
|
||||
}
|
||||
sampler, cleanup9, err := newDeliveryMetrics(ctx, config2, logger, metrics)
|
||||
if err != nil {
|
||||
cleanup8()
|
||||
cleanup7()
|
||||
cleanup6()
|
||||
cleanup5()
|
||||
cleanup4()
|
||||
cleanup3()
|
||||
cleanup2()
|
||||
cleanup()
|
||||
return nil, nil, err
|
||||
}
|
||||
application := NewApplication(config2, httpHandler, metrics, logger, mailSender, relay, bootstrapEventConsumers, worker, mailsenderWorker, sampler)
|
||||
return application, func() {
|
||||
cleanup9()
|
||||
cleanup8()
|
||||
cleanup7()
|
||||
cleanup6()
|
||||
|
||||
Reference in new issue
Block a user