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 jws parsing #247

Merged
merged 2 commits into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 15 additions & 7 deletions crypto/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/goccy/go-json"
"github.com/lestrrat-go/jwx/jwa"
"github.com/lestrrat-go/jwx/jwk"
"github.com/lestrrat-go/jwx/jws"
"github.com/lestrrat-go/jwx/jwt"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -95,13 +96,6 @@ func (sv *JWTSigner) SignJWT(kvs map[string]interface{}) ([]byte, error) {
if err := t.Set(jwt.IssuerKey, kid); err != nil {
return nil, fmt.Errorf("could not set iss with provided value: %s", kid)
}
if err := t.Set(jwk.KeyIDKey, kid); err != nil {
return nil, fmt.Errorf("could not set kid with provided value: %s", kid)
}
alg := sv.Key.Algorithm()
if err := t.Set(jwk.AlgorithmKey, alg); err != nil {
return nil, fmt.Errorf("could not set alg with value: %s", alg)
}
iat := time.Now().Unix()
if err := t.Set(jwt.IssuedAtKey, iat); err != nil {
return nil, fmt.Errorf("could not set iat with value: %d", iat)
Expand Down Expand Up @@ -146,6 +140,20 @@ func (*JWTVerifier) ParseJWT(token string) (jwt.Token, error) {
return parsed, nil
}

// ParseJWS attempts to pull of a single signature from a token, containing its headers
func (*JWTVerifier) ParseJWS(token string) (*jws.Signature, error) {
parsed, err := jws.Parse([]byte(token))
if err != nil {
logrus.WithError(err).Error("could not parse JWS")
return nil, err
}
signatures := parsed.Signatures()
if len(signatures) != 1 {
return nil, fmt.Errorf("expected 1 signature, got %d", len(signatures))
}
return signatures[0], nil
}

// VerifyAndParseJWT attempts to turn a string into a jwt.Token and verify its signature using the verifier
func (sv *JWTVerifier) VerifyAndParseJWT(token string) (jwt.Token, error) {
parsed, err := jwt.Parse([]byte(token), jwt.WithVerify(jwa.SignatureAlgorithm(sv.Algorithm()), sv.Key))
Expand Down
10 changes: 9 additions & 1 deletion crypto/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package crypto
import (
"testing"

"github.com/lestrrat-go/jwx/jwt"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -79,7 +80,7 @@ func TestSignVerifyGenericJWT(t *testing.T) {
assert.True(t, ok)
assert.EqualValues(t, "abcd", gotID)

gotJTI, ok := parsed.Get("jti")
gotJTI, ok := parsed.Get(jwt.JwtIDKey)
assert.True(t, ok)
assert.EqualValues(t, "1234", gotJTI)

Expand All @@ -89,6 +90,13 @@ func TestSignVerifyGenericJWT(t *testing.T) {

_, err = verifier.VerifyAndParseJWT(string(token))
assert.NoError(t, err)

// parse out the headers
jws, err := verifier.ParseJWS(string(token))
assert.NoError(t, err)
assert.NotEmpty(t, jws)
assert.EqualValues(t, "EdDSA", jws.ProtectedHeaders().Algorithm())
assert.EqualValues(t, "did:example:123#key-0", jws.ProtectedHeaders().KeyID())
}

func getTestVectorKey0Signer(t *testing.T) JWTSigner {
Expand Down