-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcredential_jws_test.go
146 lines (107 loc) · 4.77 KB
/
credential_jws_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package verifiable
import (
"crypto/rand"
"crypto/rsa"
"encoding/base64"
"encoding/json"
"strings"
"testing"
"github.com/go-jose/go-jose/v3"
"github.com/go-jose/go-jose/v3/jwt"
"github.com/stretchr/testify/require"
"github.com/trustbloc/vc-go/proof/testsupport"
"github.com/trustbloc/kms-go/spi/kms"
)
func TestV1JWTCredClaimsMarshalJWS(t *testing.T) {
proofCreator, proofChecker := testsupport.NewKMSSigVerPair(t, kms.RSARS256Type, "did:example:76e12ec712ebc6f1c221ebfeb1f#key1")
vc, err := parseTestCredential(t, []byte(v1ValidCredential), WithDisabledProofCheck())
require.NoError(t, err)
jwtClaims, err := vc.JWTClaims(true)
require.NoError(t, err)
t.Run("Marshal signed JWT", func(t *testing.T) {
jws, err := jwtClaims.MarshalJWSString(RS256, proofCreator, "did:example:76e12ec712ebc6f1c221ebfeb1f#key1")
require.NoError(t, err)
jwtVC, err := ParseCredential([]byte(jws), WithProofChecker(proofChecker))
require.NoError(t, err)
require.Equal(t, vc.stringJSON(t), jsonObjectToString(t, jwtVC.ToRawJSON()))
})
}
func TestV2JWTCredClaimsMarshalJWS(t *testing.T) {
proofCreator, proofChecker := testsupport.NewKMSSigVerPair(t, kms.RSARS256Type, "did:example:76e12ec712ebc6f1c221ebfeb1f#key1")
vc, err := parseTestCredential(t, []byte(v2ValidCredential), WithDisabledProofCheck())
require.NoError(t, err)
jwtClaims, err := vc.JWTClaims(true)
require.NoError(t, err)
t.Run("Marshal signed JWT", func(t *testing.T) {
jws, err := jwtClaims.MarshalJWSString(RS256, proofCreator, "did:example:76e12ec712ebc6f1c221ebfeb1f#key1")
require.NoError(t, err)
jwtVC, err := ParseCredential([]byte(jws), WithProofChecker(proofChecker))
require.NoError(t, err)
require.Equal(t, vc.stringJSON(t), jsonObjectToString(t, jwtVC.ToRawJSON()))
})
}
type invalidCredClaims struct {
*jwt.Claims
Credential int `json:"vc,omitempty"`
}
func TestV1CredJWSDecoderUnmarshal(t *testing.T) {
verificationKeyID := "did:example:76e12ec712ebc6f1c221ebfeb1f#key1"
otherVerificationKeyID := "did:example:76e12ec712ebc6f1c221ebfeb1f#key2"
proofCreators, proofChecker := testsupport.NewKMSSignersAndVerifier(t, []*testsupport.SigningKey{
{Type: kms.RSARS256, PublicKeyID: verificationKeyID},
{Type: kms.RSARS256, PublicKeyID: otherVerificationKeyID},
})
validJWS := createRS256JWS(t, []byte(jwtTestCredential), proofCreators[0], verificationKeyID, false)
jwsWithWrongKey := createRS256JWS(t, []byte(jwtTestCredential), proofCreators[0], otherVerificationKeyID, false)
t.Run("Successful JWS decoding", func(t *testing.T) {
jwtVC, err := ParseCredential(validJWS, WithProofChecker(proofChecker))
require.NoError(t, err)
vc, err := parseTestCredential(t, []byte(jwtTestCredential), WithDisabledProofCheck())
require.NoError(t, err)
require.Equal(t, vc.stringJSON(t), jsonObjectToString(t, jwtVC.ToRawJSON()))
})
t.Run("JWS with 'iss' and issuer.id miss match", func(t *testing.T) {
original := strings.Split(string(validJWS), ".")
var claims map[string]interface{}
claimsBytes, err := base64.RawURLEncoding.DecodeString(original[1])
require.NoError(t, err)
require.NoError(t, json.Unmarshal(claimsBytes, &claims))
claims["iss"] = "did:example:other_issuer"
claimsBytes, err = json.Marshal(claims)
require.NoError(t, err)
jwsWithMissMatch := original[0] + "." + base64.RawURLEncoding.EncodeToString(claimsBytes) + "." + original[2]
_, err = ParseCredential([]byte(jwsWithMissMatch), WithProofChecker(proofChecker))
require.ErrorContains(t, err, "iss(did:example:other_issuer) claim and "+
"vc.issuer.id(did:example:76e12ec712ebc6f1c221ebfeb1f) missmatch")
})
t.Run("Invalid serialized JWS", func(t *testing.T) {
_, err := ParseCredential([]byte(`"invalid JWS"`), WithProofChecker(proofChecker))
require.Error(t, err)
require.Contains(t, err.Error(), "unsupported credential format")
})
t.Run("Invalid format of \"vc\" claim", func(t *testing.T) {
privKey, err := rsa.GenerateKey(rand.Reader, 2048)
require.NoError(t, err)
key := jose.SigningKey{Algorithm: jose.RS256, Key: privKey}
signer, err := jose.NewSigner(key, &jose.SignerOptions{})
require.NoError(t, err)
claims := &invalidCredClaims{
Claims: &jwt.Claims{},
Credential: 55, // "vc" claim of invalid format
}
jwtCompact, err := jwt.Signed(signer).Claims(claims).CompactSerialize()
require.NoError(t, err)
_, err = ParseCredential([]byte(jwtCompact), WithProofChecker(proofChecker))
require.Error(t, err)
require.Contains(t, err.Error(), "decode new JWT credential")
})
t.Run("Invalid signature of JWS", func(t *testing.T) {
_, err := ParseCredential(jwsWithWrongKey, WithProofChecker(proofChecker))
require.Error(t, err)
require.Contains(t, err.Error(), "JWS proof check")
})
}