Skip to content

Commit

Permalink
add signerdid to jwt (#159)
Browse files Browse the repository at this point in the history
* bringing web5 spec commit up to date

* adding signerdid to jwt decoded struct

* parse header kid to get signerdid, put it in decodedjwt
  • Loading branch information
Jiyoon Koo authored Jul 26, 2024
1 parent c22ede2 commit 24a79d9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
9 changes: 8 additions & 1 deletion jwt/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func Decode(jwt string) (Decoded, error) {

header, err := jws.DecodeHeader(parts[0])
if err != nil {
return Decoded{}, err
return Decoded{}, fmt.Errorf("malformed JWT. Failed to decode header: %w", err)
}

claimsBytes, err := base64.RawURLEncoding.DecodeString(parts[1])
Expand All @@ -41,11 +41,17 @@ func Decode(jwt string) (Decoded, error) {
return Decoded{}, fmt.Errorf("malformed JWT. Failed to decode signature: %w", err)
}

signerDid, err := did.Parse(header.KID)
if err != nil {
return Decoded{}, fmt.Errorf("malformed JWT. Failed to parse signer DID: %w", err)
}

return Decoded{
Header: header,
Claims: claims,
Signature: signature,
Parts: parts,
SignerDID: signerDid,
}, nil
}

Expand Down Expand Up @@ -134,6 +140,7 @@ type Decoded struct {
Claims Claims
Signature []byte
Parts []string
SignerDID did.DID
}

// Verify verifies a JWT (JSON Web Token)
Expand Down
27 changes: 27 additions & 0 deletions jwt/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,30 @@ func Test_Decode_Empty(t *testing.T) {
assert.Error(t, err)
assert.Equal(t, jwt.Decoded{}, decoded)
}

func Test_Decode_Works(t *testing.T) {
vcJwt := `eyJhbGciOiJFZERTQSIsImtpZCI6ImRpZDpqd2s6ZXlKcmRIa2lPaUpQUzFBaUxDSmpjbllpT2lKRlpESTFOVEU1SWl3aWVDSTZJbkY0VjFGS2F6RTJSbWhCZWtOQlRsRktaR1F5UTFkRldrcE9lbXBSYjNGSmRYWk5SbUpVWjFKTVNFRWlmUSMwIiwidHlwIjoiS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hvc3Qva2JjLnNjaGVtYS5qc29uIn1dfX0.VwvrU5Lmv3rn9rzXB0OCxe-MtE5R0876pXsXNLRuQjoqSNB5tBv_12NqrobwA-LkMzFwzdQ5-LWJni6grGdXCQ`
decoded, err := jwt.Decode(vcJwt)
assert.NoError(t, err)
assert.Equal(t, decoded.Header.ALG, "EdDSA")
assert.Equal(t, decoded.Header.KID, "did:jwk:eyJrdHkiOiJPS1AiLCJjcnYiOiJFZDI1NTE5IiwieCI6InF4V1FKazE2RmhBekNBTlFKZGQyQ1dFWkpOempRb3FJdXZNRmJUZ1JMSEEifQ#0")
assert.NotZero(t, decoded.SignerDID)
}

func Test_Decode_Bad_Header(t *testing.T) {
vcJwt := `kakaHeader.eyJleHAiOjE3MjQ1MzQwNTAsImlzcyI6ImRpZDpqd2s6ZXlKcmRIa2lPaUpQUzFBaUxDSmpjbllpT2lKRlpESTFOVEU1SWl3aWVDSTZJbkY0VjFGS2F6RTJSbWhCZWtOQlRsRktaR1F5UTFkRldrcE9lbXBSYjNGSmRYWk5SbUpVWjFKTVNFRWlmUSIsImp0aSI6InVybjp2Yzp1dWlkOjlkMzdmMzY3LWE4ZDctNDY4Zi05NGYwLTk1NzAxNzBkNzZhNCIsIm5iZiI6MTcyMTk0MjA1MCwidmMiOnsiQGNvbnRleHQiOlsiaHR0cHM6Ly93d3cudzMub3JnLzIwMTgvY3JlZGVudGlhbHMvdjEiXSwidHlwZSI6WyJWZXJpZmlhYmxlQ3JlZGVudGlhbCJdLCJpc3N1ZXIiOiJkaWQ6andrOmV5SnJkSGtpT2lKUFMxQWlMQ0pqY25ZaU9pSkZaREkxTlRFNUlpd2llQ0k2SW5GNFYxRkthekUyUm1oQmVrTkJUbEZLWkdReVExZEZXa3BPZW1wUmIzRkpkWFpOUm1KVVoxSk1TRUVpZlEiLCJjcmVkZW50aWFsU3ViamVjdCI6eyJpc3N1ZXIiOiJkaWQ6andrOmV5SnJkSGtpT2lKUFMxQWlMQ0pqY25ZaU9pSkZaRE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