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

@@ -203,24 +203,34 @@ func VerifyClaimsAudience(tokenString, secret, audience string) (sub string, iat
return verifyClaims(tokenString, secret, audience)
}
func verifyClaims(tokenString, secret, audience string) (sub string, iat, exp time.Time, jti string, err error) {
if strings.TrimSpace(secret) == "" {
return "", time.Time{}, time.Time{}, "", fmt.Errorf("bouncer: jwt secret is empty")
// VerifyAuthTime verifies a token exactly as VerifyClaims does for
// AudienceUser (a frontend token without aud is accepted) and as
// VerifyClaimsAudience does for any other audience: HS256 signature,
// audience, a present and unexpired exp, and a subject. It returns the
// subject and the auth_time claim (ClaimAuthTime), the time of the sign-in
// that started the session. ok is false when the token carries no numeric
// auth_time, for example one minted before the claim existed. A "fresh
// sign-in" check compares authTime, never iat, because a refresh renews iat.
func VerifyAuthTime(tokenString, secret, audience string) (sub string, authTime time.Time, ok bool, err error) {
if strings.TrimSpace(audience) == "" {
return "", time.Time{}, false, fmt.Errorf("bouncer: jwt audience is empty")
}
parser := jwt.NewParser(jwt.WithValidMethods([]string{"HS256"}), jwt.WithExpirationRequired())
claims := jwt.MapClaims{}
_, err = parser.ParseWithClaims(tokenString, claims, func(t *jwt.Token) (any, error) {
return []byte(secret), nil
})
expected := audience
if audience == AudienceUser {
expected = ""
}
claims, sub, err := parseVerified(tokenString, secret, expected)
if err != nil {
return "", time.Time{}, time.Time{}, "", mapJWTError(err)
return "", time.Time{}, false, err
}
if !frontendAudienceOK(claims, audience) {
return "", time.Time{}, time.Time{}, "", errors.New(msgBadSignature)
}
sub = subject(claims)
if sub == "" {
return "", time.Time{}, time.Time{}, "", errors.New(msgRequiredClaims)
authTime, ok = claimTime(claims, ClaimAuthTime)
return sub, authTime, ok, nil
}
func verifyClaims(tokenString, secret, audience string) (sub string, iat, exp time.Time, jti string, err error) {
claims, sub, err := parseVerified(tokenString, secret, audience)
if err != nil {
return "", time.Time{}, time.Time{}, "", err
}
iat, _ = claimTime(claims, "iat")
exp, _ = claimTime(claims, "exp")
@@ -228,6 +238,30 @@ func verifyClaims(tokenString, secret, audience string) (sub string, iat, exp ti
return sub, iat, exp, jti, nil
}
// parseVerified runs the shared verification of verifyClaims and returns
// the claims and the subject. audience "" accepts a frontend token with no
// aud or with AudienceUser; any other value requires that audience.
func parseVerified(tokenString, secret, audience string) (jwt.MapClaims, string, error) {
if strings.TrimSpace(secret) == "" {
return nil, "", fmt.Errorf("bouncer: jwt secret is empty")
}
parser := jwt.NewParser(jwt.WithValidMethods([]string{"HS256"}), jwt.WithExpirationRequired())
claims := jwt.MapClaims{}
if _, err := parser.ParseWithClaims(tokenString, claims, func(t *jwt.Token) (any, error) {
return []byte(secret), nil
}); err != nil {
return nil, "", mapJWTError(err)
}
if !frontendAudienceOK(claims, audience) {
return nil, "", errors.New(msgBadSignature)
}
sub := subject(claims)
if sub == "" {
return nil, "", errors.New(msgRequiredClaims)
}
return claims, sub, nil
}
// frontendAudienceOK accepts a missing audience when expected is empty
// (legacy frontend tokens) and otherwise requires expected.
func frontendAudienceOK(claims jwt.MapClaims, expected string) bool {