Derive every key from the signing key in one place (closes #315)
check / check (push) Canceled after 0s
check / check (push) Canceled after 0s
seal.DeriveKeys derives the session hash and block keys, the encrypted URL key and the CSRF key from the signing key, with their salts declared beside it, and returns them as one seal.Keys. internal/providers calls it once at startup and provides the value to the fx graph. The session manager, the encrypted URL generator and the CSRF protection each take their own key. session.NewManager, encurl.NewGenerator, newCSRFProtect and handlers.New return no error, as nothing in them can fail now. Each key is derived as before, with seal.DeriveKey and the same salt. The signing key itself stays the HMAC key of signed URLs and the login password. README.md says under signing_key what it protects. Model: opus-5-5
This commit was merged in pull request #332.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
// Package providers builds from the config the parts pixad serves images
|
||||
// and logins with, and provides them to the fx graph:
|
||||
// - the keys derived from the signing key
|
||||
// - the image cache, with its default limit
|
||||
// - the fetcher
|
||||
// - the processor
|
||||
@@ -27,6 +28,7 @@ import (
|
||||
"sneak.berlin/go/pixa/internal/imageprocessor"
|
||||
"sneak.berlin/go/pixa/internal/imgcache"
|
||||
"sneak.berlin/go/pixa/internal/logger"
|
||||
"sneak.berlin/go/pixa/internal/seal"
|
||||
"sneak.berlin/go/pixa/internal/session"
|
||||
)
|
||||
|
||||
@@ -35,6 +37,7 @@ import (
|
||||
// the cache registers its counters on.
|
||||
func Constructors() []any {
|
||||
return []any{
|
||||
newKeys,
|
||||
newCache,
|
||||
newFetcherConfig,
|
||||
httpfetcher.New,
|
||||
@@ -45,6 +48,12 @@ func Constructors() []any {
|
||||
}
|
||||
}
|
||||
|
||||
// newKeys derives the keys of seal.Keys from the signing key. fx calls it
|
||||
// once, at startup.
|
||||
func newKeys(cfg *config.Config) (seal.Keys, error) {
|
||||
return seal.DeriveKeys(cfg.SigningKey)
|
||||
}
|
||||
|
||||
// cacheParams defines dependencies for newCache.
|
||||
type cacheParams struct {
|
||||
fx.In
|
||||
@@ -229,22 +238,23 @@ func newImageService(
|
||||
type sessionManagerParams struct {
|
||||
fx.In
|
||||
|
||||
Config *config.Config
|
||||
Keys seal.Keys
|
||||
Database *database.Database
|
||||
}
|
||||
|
||||
// newSessionManager builds the session manager. Session cookies are
|
||||
// always Secure, HttpOnly and SameSite=Strict. The random value that each
|
||||
// logout replaces is saved in the database, so it outlasts a restart.
|
||||
// newSessionManager builds the session manager with its two keys. Session
|
||||
// cookies are always Secure, HttpOnly and SameSite=Strict. The random value
|
||||
// that each logout replaces is saved in the database, so it outlasts a
|
||||
// restart.
|
||||
func newSessionManager(
|
||||
_ fx.Lifecycle, params sessionManagerParams,
|
||||
) (*session.Manager, error) {
|
||||
return session.NewManagerWithDatabase(
|
||||
context.Background(), params.Config.SigningKey, params.Database.DB())
|
||||
return session.NewManagerWithDatabase(context.Background(),
|
||||
params.Keys.SessionHashKey, params.Keys.SessionBlockKey,
|
||||
params.Database.DB())
|
||||
}
|
||||
|
||||
// newEncryptedURLGenerator builds the encrypted URL generator, with a key
|
||||
// derived from the signing key.
|
||||
func newEncryptedURLGenerator(cfg *config.Config) (*encurl.Generator, error) {
|
||||
return encurl.NewGenerator(cfg.SigningKey)
|
||||
// newEncryptedURLGenerator builds the encrypted URL generator with its key.
|
||||
func newEncryptedURLGenerator(keys seal.Keys) *encurl.Generator {
|
||||
return encurl.NewGenerator(keys.EncryptedURLKey)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user