Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add asymmetric JWT signing #16010

Merged
merged 16 commits into from
Jun 17, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added optional kid parameter.
KN4CK3R committed May 30, 2021
commit 7518bd919be41f06c53683dca3cd1fe7bd5492c2
36 changes: 30 additions & 6 deletions modules/auth/oauth2/jwtsigningkey.go
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ import (
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/pem"
@@ -43,7 +44,7 @@ type JWTSigningKey interface {
SigningMethod() jwt.SigningMethod
SignKey() interface{}
VerifyKey() interface{}
ToJSON() map[string]string
ToJWK() (map[string]string, error)
}

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

func (key hmacSingingKey) ToJSON() map[string]string {
return map[string]string{}
func (key hmacSingingKey) ToJWK() (map[string]string, error) {
return map[string]string{}, nil
}

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

func (key rsaSingingKey) ToJSON() map[string]string {
func (key rsaSingingKey) ToJWK() (map[string]string, error) {
pubKey := key.key.Public().(*rsa.PublicKey)

kid, err := createPublicKeyFingerprint(pubKey)
if err != nil {
return nil, err
}

return map[string]string{
"kty": "RSA",
"alg": key.SigningMethod().Alg(),
"kid": base64.RawURLEncoding.EncodeToString(kid),
"e": base64.RawURLEncoding.EncodeToString(big.NewInt(int64(pubKey.E)).Bytes()),
"n": base64.RawURLEncoding.EncodeToString(pubKey.N.Bytes()),
}
}, nil
}

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

func (key ecdsaSingingKey) ToJSON() map[string]string {
func (key ecdsaSingingKey) ToJWK() (map[string]string, error) {
pubKey := key.key.Public().(*ecdsa.PublicKey)

kid, err := createPublicKeyFingerprint(pubKey)
if err != nil {
return nil, err
}

return map[string]string{
"kty": "EC",
"alg": key.SigningMethod().Alg(),
"kid": base64.RawURLEncoding.EncodeToString(kid),
"crv": pubKey.Params().Name,
"x": base64.RawURLEncoding.EncodeToString(pubKey.X.Bytes()),
"y": base64.RawURLEncoding.EncodeToString(pubKey.Y.Bytes()),
}, nil
}

func createPublicKeyFingerprint(key interface{}) ([]byte, error) {
bytes, err := x509.MarshalPKIXPublicKey(key)
if err != nil {
return nil, err
}

checksum := sha256.Sum256(bytes)

return checksum[:], nil
}

// CreateJWTSingingKey creates a signing key from an algorithm / key pair.
16 changes: 11 additions & 5 deletions routers/user/oauth.go
Original file line number Diff line number Diff line change
@@ -462,18 +462,24 @@ func OIDCWellKnown(ctx *context.Context) {

// OIDCKeys generates the JSON Web Key Set
func OIDCKeys(ctx *context.Context) {
keyJSON := oauth2.DefaultSigningKey.ToJSON()
keyJSON["use"] = "sig"
jwk, err := oauth2.DefaultSigningKey.ToJWK()
if err != nil {
log.Error("Error converting signing key to JWK: %v", err)
ctx.Error(http.StatusInternalServerError)
return
}

jwk["use"] = "sig"

jwkSet := map[string][]map[string]string{
jwks := map[string][]map[string]string{
"keys": {
keyJSON,
jwk,
},
}

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