feat(bouncer): stamp auth_time at sign-in and carry it unchanged through refresh (C-CR-02)

- MintAudience stamps auth_time equal to iat; refreshAudience copies the
  presented token's value and never renews it (absent stays absent)
- bouncer.ClaimAuthTime and bouncer.VerifyAuthTime for plugins, with the
  same signature, audience and expiry checks as VerifyClaims
- TestAuthTimeSmoke traces mint, refresh and read
This commit is contained in:
Jakub Zych
2026-10-23 16:13:49 +02:00
parent 60a86677f2
commit c14238b94b
6 changed files with 182 additions and 19 deletions

View File

@@ -21,9 +21,16 @@ const (
AudienceBackend = "backend"
)
// ClaimAuthTime is the JWT claim holding the time of the sign-in that started
// the session (OIDC Core's auth_time, NumericDate seconds). Every direct mint
// is a sign-in and stamps it equal to iat; a refresh copies it unchanged, so
// a "fresh sign-in" check compares auth_time, never iat.
const ClaimAuthTime = "auth_time"
type registeredClaims struct {
jwt.RegisteredClaims
Prv string `json:"prv,omitempty"`
Prv string `json:"prv,omitempty"`
AuthTime *jwt.NumericDate `json:"auth_time,omitempty"`
}
// Mint signs a frontend-audience HS256 token. iss is the minting endpoint URL.
@@ -33,8 +40,17 @@ func Mint(secret, sub, issuerURL string, ttl time.Duration) (string, string, err
}
// MintAudience signs an HS256 token for audience. Frontend tokens keep the PHP
// prv lock-subject; backend tokens omit it.
// prv lock-subject; backend tokens omit it. Every MintAudience call is a
// sign-in, so the token's auth_time (ClaimAuthTime) equals its iat.
func MintAudience(secret, sub, issuerURL string, ttl time.Duration, audience string) (string, string, error) {
now := time.Now()
return mintAudience(secret, sub, issuerURL, ttl, audience, now, now)
}
// mintAudience signs the token issued at now. authTime, when non-zero,
// becomes the auth_time claim; a zero authTime mints a token without one. A
// sign-in passes the same instant as now and authTime, so auth_time equals iat.
func mintAudience(secret, sub, issuerURL string, ttl time.Duration, audience string, now, authTime time.Time) (string, string, error) {
if strings.TrimSpace(secret) == "" {
return "", "", fmt.Errorf("bouncer: jwt secret is empty")
}
@@ -45,7 +61,6 @@ func MintAudience(secret, sub, issuerURL string, ttl time.Duration, audience str
if err != nil {
return "", "", err
}
now := time.Now()
prv := ""
if audience == AudienceUser {
prv = prvHash
@@ -62,6 +77,9 @@ func MintAudience(secret, sub, issuerURL string, ttl time.Duration, audience str
},
Prv: prv,
}
if !authTime.IsZero() {
claims.AuthTime = jwt.NewNumericDate(authTime)
}
signed, err := jwt.NewWithClaims(jwt.SigningMethodHS256, claims).SignedString([]byte(secret))
if err != nil {
return "", "", err