Skip to content

Commit

Permalink
new verifier from key (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabe authored Nov 7, 2022
1 parent 7346cda commit be34aab
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
8 changes: 8 additions & 0 deletions crypto/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ type JWTVerifier struct {
jwk.Key
}

func NewJWTVerifier(kid string, key crypto.PublicKey) (*JWTVerifier, error) {
privateKeyJWK, err := PublicKeyToJWK(key)
if err != nil {
return nil, err
}
return NewJWTVerifierFromKey(kid, privateKeyJWK)
}

func NewJWTVerifierFromJWK(kid string, key PublicKeyJWK) (*JWTVerifier, error) {
gotJWK, alg, err := jwtVerifier(kid, key)
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion crypto/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestSignVerifyJWTForEachSupportedKeyType(t *testing.T) {
for _, test := range tests {
t.Run(string(test.kt), func(t *testing.T) {
// generate a new key based on the given key type
_, privKey, err := GenerateKeyByKeyType(test.kt)
pubKey, privKey, err := GenerateKeyByKeyType(test.kt)
assert.NoError(t, err)
assert.NotEmpty(t, privKey)

Expand All @@ -94,6 +94,10 @@ func TestSignVerifyJWTForEachSupportedKeyType(t *testing.T) {
assert.NoError(t, err)
assert.NotEmpty(t, verifier)

sameVerifier, err := NewJWTVerifier(testKID, pubKey)
assert.NoError(t, err)
assert.Equal(t, verifier, sameVerifier)

err = verifier.VerifyJWT(string(token))
assert.NoError(t, err)
})
Expand Down

0 comments on commit be34aab

Please sign in to comment.