add kid header to JWT

This commit is contained in:
Finn 2024-09-30 14:47:46 -07:00
parent 6c10f85fb5
commit a2e959dabb
2 changed files with 27 additions and 1 deletions

View file

@ -40,6 +40,7 @@ type JWTSigningKey interface {
SignKey() any SignKey() any
VerifyKey() any VerifyKey() any
ToJWK() (map[string]string, error) ToJWK() (map[string]string, error)
KID() string
PreProcessToken(*jwt.Token) PreProcessToken(*jwt.Token)
} }
@ -71,6 +72,10 @@ func (key hmacSigningKey) ToJWK() (map[string]string, error) {
}, nil }, nil
} }
func (key hmacSigningKey) KID() string {
return ""
}
func (key hmacSigningKey) PreProcessToken(*jwt.Token) {} func (key hmacSigningKey) PreProcessToken(*jwt.Token) {}
type rsaSingingKey struct { type rsaSingingKey struct {
@ -120,6 +125,10 @@ func (key rsaSingingKey) ToJWK() (map[string]string, error) {
}, nil }, nil
} }
func (key rsaSingingKey) KID() string {
return key.id
}
func (key rsaSingingKey) PreProcessToken(token *jwt.Token) { func (key rsaSingingKey) PreProcessToken(token *jwt.Token) {
token.Header["kid"] = key.id token.Header["kid"] = key.id
} }
@ -171,6 +180,10 @@ func (key eddsaSigningKey) ToJWK() (map[string]string, error) {
}, nil }, nil
} }
func (key eddsaSigningKey) KID() string {
return key.id
}
func (key eddsaSigningKey) PreProcessToken(token *jwt.Token) { func (key eddsaSigningKey) PreProcessToken(token *jwt.Token) {
token.Header["kid"] = key.id token.Header["kid"] = key.id
} }
@ -223,6 +236,10 @@ func (key ecdsaSingingKey) ToJWK() (map[string]string, error) {
}, nil }, nil
} }
func (key ecdsaSingingKey) KID() string {
return key.id
}
func (key ecdsaSingingKey) PreProcessToken(token *jwt.Token) { func (key ecdsaSingingKey) PreProcessToken(token *jwt.Token) {
token.Header["kid"] = key.id token.Header["kid"] = key.id
} }

View file

@ -162,7 +162,7 @@ func (o oidcRoutes) getToken(ctx *ArtifactContext) {
"nbf": jwt.NewNumericDate(iat), "nbf": jwt.NewNumericDate(iat),
"exp": jwt.NewNumericDate(iat.Add(time.Minute * 15)), "exp": jwt.NewNumericDate(iat.Add(time.Minute * 15)),
"iat": jwt.NewNumericDate(iat), "iat": jwt.NewNumericDate(iat),
}) }, addTokenHeaders(o.signingKey))
signedJWT, err := token.SignedString(o.signingKey.SignKey()) signedJWT, err := token.SignedString(o.signingKey.SignKey())
if err != nil { if err != nil {
@ -212,3 +212,12 @@ func (o oidcRoutes) getOpenIDConfiguration(resp http.ResponseWriter, req *http.R
return return
} }
} }
func addTokenHeaders(key jwtx.JWTSigningKey) jwt.TokenOption {
return func(t *jwt.Token) {
kid := key.KID()
if kid != "" {
t.Header["kid"] = kid
}
}
}