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
@@ -3,9 +3,11 @@ package identity
|
||||
import (
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
|
||||
"lincodex/backend/internal/domain"
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
)
|
||||
|
||||
@@ -20,6 +22,7 @@ const (
|
||||
labelEmailBucket = "lincodex/identity/email-bucket/v1"
|
||||
labelClientBucket = "lincodex/identity/client-bucket/v1"
|
||||
labelTokenBucket = "lincodex/identity/verification-token-bucket/v1"
|
||||
labelSessionCSRF = "lincodex/identity/session-csrf/v1"
|
||||
)
|
||||
|
||||
// Keyring computes the keyed digests of the anonymous identity operations:
|
||||
@@ -33,6 +36,7 @@ type Keyring struct {
|
||||
bucketKey []byte
|
||||
clientKey []byte
|
||||
tokenKey []byte
|
||||
sessionCSRFKey []byte
|
||||
}
|
||||
|
||||
// NewKeyring derives the purpose keys from secret.
|
||||
@@ -46,6 +50,7 @@ func NewKeyring(secret []byte) (*Keyring, error) {
|
||||
bucketKey: derive(secret, labelEmailBucket),
|
||||
clientKey: derive(secret, labelClientBucket),
|
||||
tokenKey: derive(secret, labelTokenBucket),
|
||||
sessionCSRFKey: derive(secret, labelSessionCSRF),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -99,3 +104,26 @@ func (keyring *Keyring) VerificationTokenBucket(rawToken string) string {
|
||||
mac.Write([]byte(rawToken))
|
||||
return "identity:verification-token:" + hex.EncodeToString(mac.Sum(nil))
|
||||
}
|
||||
|
||||
// SessionCSRF derives a synchronizer token for exactly one session. The
|
||||
// purpose key and message are both domain-separated from every other keyring
|
||||
// use, and only the encoded result is returned to the HTTP adapter.
|
||||
func (keyring *Keyring) SessionCSRF(token domain.SecretToken) string {
|
||||
if token.IsZero() {
|
||||
return ""
|
||||
}
|
||||
mac := hmac.New(sha256.New, keyring.sessionCSRFKey)
|
||||
mac.Write([]byte(labelSessionCSRF))
|
||||
mac.Write([]byte{0})
|
||||
mac.Write([]byte(token.Reveal()))
|
||||
return base64.RawURLEncoding.EncodeToString(mac.Sum(nil))
|
||||
}
|
||||
|
||||
// VerifySessionCSRF compares the supplied synchronizer token in constant
|
||||
// time with the token bound to this session.
|
||||
func (keyring *Keyring) VerifySessionCSRF(token domain.SecretToken, supplied string) bool {
|
||||
if token.IsZero() {
|
||||
return false
|
||||
}
|
||||
return hmac.Equal([]byte(keyring.SessionCSRF(token)), []byte(supplied))
|
||||
}
|
||||
Reference in new issue
Block a user