From a8ba658b631a825d609fb1eba081fdbd5199c054 Mon Sep 17 00:00:00 2001 From: Kevin Franklin Kim Date: Wed, 17 Nov 2021 11:36:36 +0100 Subject: [PATCH] refactor: make key props public --- jwt/jwt.go | 6 +++--- jwt/jwtkey.go | 18 +++++++++--------- jwt/keyfunc.go | 8 ++++---- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/jwt/jwt.go b/jwt/jwt.go index cfa5591..baba89b 100644 --- a/jwt/jwt.go +++ b/jwt/jwt.go @@ -28,7 +28,7 @@ func WithDeprecatedKeys(v ...Key) Option { return func(o *JWT) { deprecatedKeys := make(map[string]Key, len(v)) for _, key := range deprecatedKeys { - deprecatedKeys[key.id] = key + deprecatedKeys[key.ID] = key } o.DeprecatedKeys = deprecatedKeys } @@ -53,8 +53,8 @@ func New(key Key, opts ...Option) *JWT { func (j *JWT) GetSignedToken(claims jwt.Claims) (string, error) { // create token token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims) - token.Header["kid"] = j.Key.id - return token.SignedString(j.Key.private) + token.Header["kid"] = j.Key.ID + return token.SignedString(j.Key.Private) } func (j *JWT) ParseWithClaims(token string, claims jwt.Claims) (*jwt.Token, error) { diff --git a/jwt/jwtkey.go b/jwt/jwtkey.go index a5bd5d6..6c4c7d2 100644 --- a/jwt/jwtkey.go +++ b/jwt/jwtkey.go @@ -12,20 +12,20 @@ import ( ) type Key struct { - // id (required) represents the key identifier e.g. the md5 representation of the public key - id string - // public (required) rsa key - public *rsa.PublicKey - // private (optional) rsa key - private *rsa.PrivateKey + // ID (required) represents the key identifier e.g. the md5 representation of the public key + ID string + // Public (required) rsa key + Public *rsa.PublicKey + // Private (optional) rsa key + Private *rsa.PrivateKey } // NewKey return a new Key func NewKey(id string, public *rsa.PublicKey, private *rsa.PrivateKey) Key { return Key{ - id: id, - public: public, - private: private, + ID: id, + Public: public, + Private: private, } } diff --git a/jwt/keyfunc.go b/jwt/keyfunc.go index b1adabd..c6d46fe 100644 --- a/jwt/keyfunc.go +++ b/jwt/keyfunc.go @@ -15,11 +15,11 @@ func DefaultKeyFunc(key Key, deprecatedKeys map[string]Key) jwt.Keyfunc { } else if kidString, ok := kid.(string); !ok { return nil, errors.New("invalid key identifier type") } else if oldKey, ok := deprecatedKeys[kidString]; ok { - return oldKey.public, nil - } else if kidString == key.id { - return key.public, nil + return oldKey.Public, nil + } else if kidString == key.ID { + return key.Public, nil } else { - return nil, errors.New("unknown key identifier: " + kidString + " (" + key.id + ")") + return nil, errors.New("unknown key identifier: " + kidString + " (" + key.ID + ")") } } }