Skip to content

Commit 7518bd9

Browse files
committed
Added optional kid parameter.
1 parent e700002 commit 7518bd9

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

modules/auth/oauth2/jwtsigningkey.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"crypto/elliptic"
1010
"crypto/rand"
1111
"crypto/rsa"
12+
"crypto/sha256"
1213
"crypto/x509"
1314
"encoding/base64"
1415
"encoding/pem"
@@ -43,7 +44,7 @@ type JWTSigningKey interface {
4344
SigningMethod() jwt.SigningMethod
4445
SignKey() interface{}
4546
VerifyKey() interface{}
46-
ToJSON() map[string]string
47+
ToJWK() (map[string]string, error)
4748
}
4849

4950
type hmacSingingKey struct {
@@ -67,8 +68,8 @@ func (key hmacSingingKey) VerifyKey() interface{} {
6768
return key.secret
6869
}
6970

70-
func (key hmacSingingKey) ToJSON() map[string]string {
71-
return map[string]string{}
71+
func (key hmacSingingKey) ToJWK() (map[string]string, error) {
72+
return map[string]string{}, nil
7273
}
7374

7475
type rsaSingingKey struct {
@@ -92,15 +93,21 @@ func (key rsaSingingKey) VerifyKey() interface{} {
9293
return key.key.Public()
9394
}
9495

95-
func (key rsaSingingKey) ToJSON() map[string]string {
96+
func (key rsaSingingKey) ToJWK() (map[string]string, error) {
9697
pubKey := key.key.Public().(*rsa.PublicKey)
9798

99+
kid, err := createPublicKeyFingerprint(pubKey)
100+
if err != nil {
101+
return nil, err
102+
}
103+
98104
return map[string]string{
99105
"kty": "RSA",
100106
"alg": key.SigningMethod().Alg(),
107+
"kid": base64.RawURLEncoding.EncodeToString(kid),
101108
"e": base64.RawURLEncoding.EncodeToString(big.NewInt(int64(pubKey.E)).Bytes()),
102109
"n": base64.RawURLEncoding.EncodeToString(pubKey.N.Bytes()),
103-
}
110+
}, nil
104111
}
105112

106113
type ecdsaSingingKey struct {
@@ -124,16 +131,33 @@ func (key ecdsaSingingKey) VerifyKey() interface{} {
124131
return key.key.Public()
125132
}
126133

127-
func (key ecdsaSingingKey) ToJSON() map[string]string {
134+
func (key ecdsaSingingKey) ToJWK() (map[string]string, error) {
128135
pubKey := key.key.Public().(*ecdsa.PublicKey)
129136

137+
kid, err := createPublicKeyFingerprint(pubKey)
138+
if err != nil {
139+
return nil, err
140+
}
141+
130142
return map[string]string{
131143
"kty": "EC",
132144
"alg": key.SigningMethod().Alg(),
145+
"kid": base64.RawURLEncoding.EncodeToString(kid),
133146
"crv": pubKey.Params().Name,
134147
"x": base64.RawURLEncoding.EncodeToString(pubKey.X.Bytes()),
135148
"y": base64.RawURLEncoding.EncodeToString(pubKey.Y.Bytes()),
149+
}, nil
150+
}
151+
152+
func createPublicKeyFingerprint(key interface{}) ([]byte, error) {
153+
bytes, err := x509.MarshalPKIXPublicKey(key)
154+
if err != nil {
155+
return nil, err
136156
}
157+
158+
checksum := sha256.Sum256(bytes)
159+
160+
return checksum[:], nil
137161
}
138162

139163
// CreateJWTSingingKey creates a signing key from an algorithm / key pair.

routers/user/oauth.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -462,18 +462,24 @@ func OIDCWellKnown(ctx *context.Context) {
462462

463463
// OIDCKeys generates the JSON Web Key Set
464464
func OIDCKeys(ctx *context.Context) {
465-
keyJSON := oauth2.DefaultSigningKey.ToJSON()
466-
keyJSON["use"] = "sig"
465+
jwk, err := oauth2.DefaultSigningKey.ToJWK()
466+
if err != nil {
467+
log.Error("Error converting signing key to JWK: %v", err)
468+
ctx.Error(http.StatusInternalServerError)
469+
return
470+
}
471+
472+
jwk["use"] = "sig"
467473

468-
jwkSet := map[string][]map[string]string{
474+
jwks := map[string][]map[string]string{
469475
"keys": {
470-
keyJSON,
476+
jwk,
471477
},
472478
}
473479

474480
ctx.Resp.Header().Set("Content-Type", "application/json")
475481
enc := jsoniter.NewEncoder(ctx.Resp)
476-
if err := enc.Encode(jwkSet); err != nil {
482+
if err := enc.Encode(jwks); err != nil {
477483
log.Error("Failed to encode representation as json. Error: %v", err)
478484
}
479485
}

0 commit comments

Comments
 (0)