Skip to content

Commit a6c42be

Browse files
committed
Add "kid" to token header.
1 parent d9d4201 commit a6c42be

File tree

2 files changed

+45
-14
lines changed

2 files changed

+45
-14
lines changed

models/oauth2_application.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ func ParseOAuth2Token(jwtToken string) (*OAuth2Token, error) {
560560
func (token *OAuth2Token) SignToken() (string, error) {
561561
token.IssuedAt = time.Now().Unix()
562562
jwtToken := jwt.NewWithClaims(oauth2.DefaultSigningKey.SigningMethod(), token)
563+
oauth2.DefaultSigningKey.PreProcessToken(jwtToken)
563564
return jwtToken.SignedString(oauth2.DefaultSigningKey.SignKey())
564565
}
565566

@@ -586,5 +587,6 @@ type OIDCToken struct {
586587
func (token *OIDCToken) SignToken(signingKey oauth2.JWTSigningKey) (string, error) {
587588
token.IssuedAt = time.Now().Unix()
588589
jwtToken := jwt.NewWithClaims(signingKey.SigningMethod(), token)
590+
signingKey.PreProcessToken(jwtToken)
589591
return jwtToken.SignedString(signingKey.SignKey())
590592
}

modules/auth/oauth2/jwtsigningkey.go

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type JWTSigningKey interface {
4545
SignKey() interface{}
4646
VerifyKey() interface{}
4747
ToJWK() (map[string]string, error)
48+
PreProcessToken(*jwt.Token)
4849
}
4950

5051
type hmacSingingKey struct {
@@ -75,9 +76,25 @@ func (key hmacSingingKey) ToJWK() (map[string]string, error) {
7576
}, nil
7677
}
7778

79+
func (key hmacSingingKey) PreProcessToken(*jwt.Token) {}
80+
7881
type rsaSingingKey struct {
7982
signingMethod jwt.SigningMethod
8083
key *rsa.PrivateKey
84+
id string
85+
}
86+
87+
func newRSASingingKey(signingMethod jwt.SigningMethod, key *rsa.PrivateKey) (rsaSingingKey, error) {
88+
kid, err := createPublicKeyFingerprint(key.Public().(*rsa.PublicKey))
89+
if err != nil {
90+
return rsaSingingKey{}, err
91+
}
92+
93+
return rsaSingingKey{
94+
signingMethod,
95+
key,
96+
base64.RawURLEncoding.EncodeToString(kid),
97+
}, nil
8198
}
8299

83100
func (key rsaSingingKey) IsSymmetric() bool {
@@ -99,23 +116,36 @@ func (key rsaSingingKey) VerifyKey() interface{} {
99116
func (key rsaSingingKey) ToJWK() (map[string]string, error) {
100117
pubKey := key.key.Public().(*rsa.PublicKey)
101118

102-
kid, err := createPublicKeyFingerprint(pubKey)
103-
if err != nil {
104-
return nil, err
105-
}
106-
107119
return map[string]string{
108120
"kty": "RSA",
109121
"alg": key.SigningMethod().Alg(),
110-
"kid": base64.RawURLEncoding.EncodeToString(kid),
122+
"kid": key.id,
111123
"e": base64.RawURLEncoding.EncodeToString(big.NewInt(int64(pubKey.E)).Bytes()),
112124
"n": base64.RawURLEncoding.EncodeToString(pubKey.N.Bytes()),
113125
}, nil
114126
}
115127

128+
func (key rsaSingingKey) PreProcessToken(token *jwt.Token) {
129+
token.Header["kid"] = key.id
130+
}
131+
116132
type ecdsaSingingKey struct {
117133
signingMethod jwt.SigningMethod
118134
key *ecdsa.PrivateKey
135+
id string
136+
}
137+
138+
func newECDSASingingKey(signingMethod jwt.SigningMethod, key *ecdsa.PrivateKey) (ecdsaSingingKey, error) {
139+
kid, err := createPublicKeyFingerprint(key.Public().(*ecdsa.PublicKey))
140+
if err != nil {
141+
return ecdsaSingingKey{}, err
142+
}
143+
144+
return ecdsaSingingKey{
145+
signingMethod,
146+
key,
147+
base64.RawURLEncoding.EncodeToString(kid),
148+
}, nil
119149
}
120150

121151
func (key ecdsaSingingKey) IsSymmetric() bool {
@@ -137,21 +167,20 @@ func (key ecdsaSingingKey) VerifyKey() interface{} {
137167
func (key ecdsaSingingKey) ToJWK() (map[string]string, error) {
138168
pubKey := key.key.Public().(*ecdsa.PublicKey)
139169

140-
kid, err := createPublicKeyFingerprint(pubKey)
141-
if err != nil {
142-
return nil, err
143-
}
144-
145170
return map[string]string{
146171
"kty": "EC",
147172
"alg": key.SigningMethod().Alg(),
148-
"kid": base64.RawURLEncoding.EncodeToString(kid),
173+
"kid": key.id,
149174
"crv": pubKey.Params().Name,
150175
"x": base64.RawURLEncoding.EncodeToString(pubKey.X.Bytes()),
151176
"y": base64.RawURLEncoding.EncodeToString(pubKey.Y.Bytes()),
152177
}, nil
153178
}
154179

180+
func (key ecdsaSingingKey) PreProcessToken(token *jwt.Token) {
181+
token.Header["kid"] = key.id
182+
}
183+
155184
// createPublicKeyFingerprint creates a fingerprint of the given key.
156185
// The fingerprint is the sha256 sum of the PKIX structure of the key.
157186
func createPublicKeyFingerprint(key interface{}) ([]byte, error) {
@@ -199,13 +228,13 @@ func CreateJWTSingingKey(algorithm string, key interface{}) (JWTSigningKey, erro
199228
if !ok {
200229
return nil, jwt.ErrInvalidKeyType
201230
}
202-
return ecdsaSingingKey{signingMethod, privateKey}, nil
231+
return newECDSASingingKey(signingMethod, privateKey)
203232
case *jwt.SigningMethodRSA:
204233
privateKey, ok := key.(*rsa.PrivateKey)
205234
if !ok {
206235
return nil, jwt.ErrInvalidKeyType
207236
}
208-
return rsaSingingKey{signingMethod, privateKey}, nil
237+
return newRSASingingKey(signingMethod, privateKey)
209238
default:
210239
secret, ok := key.([]byte)
211240
if !ok {

0 commit comments

Comments
 (0)