forked from hyperledger-archives/aries-framework-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpresentation_jws_test.go
129 lines (98 loc) · 3.5 KB
/
presentation_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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package verifiable
import (
"crypto/rand"
"crypto/rsa"
"testing"
"github.com/square/go-jose/v3"
"github.com/square/go-jose/v3/jwt"
"github.com/stretchr/testify/require"
"github.com/hyperledger/aries-framework-go/pkg/doc/signature/verifier"
"github.com/hyperledger/aries-framework-go/pkg/kms"
)
func TestJWTPresClaims_MarshalJWS(t *testing.T) {
vp, err := newTestPresentation(t, []byte(validPresentation))
require.NoError(t, err)
signer, err := newCryptoSigner(kms.RSARS256Type)
require.NoError(t, err)
jws := createCredJWS(t, vp, signer)
_, rawVC, err := decodeVPFromJWS(jws, true, holderPublicKeyFetcher(signer.PublicKeyBytes()))
require.NoError(t, err)
require.Equal(t, vp.stringJSON(t), rawVC.stringJSON(t))
}
type invalidPresClaims struct {
*jwt.Claims
Presentation int `json:"vp,omitempty"`
}
func TestUnmarshalPresJWSClaims(t *testing.T) {
holderSigner, err := newCryptoSigner(kms.RSARS256Type)
require.NoError(t, err)
testFetcher := holderPublicKeyFetcher(holderSigner.PublicKeyBytes())
t.Run("Successful JWS decoding", func(t *testing.T) {
vp, err := newTestPresentation(t, []byte(validPresentation))
require.NoError(t, err)
jws := createCredJWS(t, vp, holderSigner)
claims, err := unmarshalPresJWSClaims(jws, true, testFetcher)
require.NoError(t, err)
require.Equal(t, vp.stringJSON(t), claims.Presentation.stringJSON(t))
})
t.Run("Invalid serialized JWS", func(t *testing.T) {
claims, err := unmarshalPresJWSClaims("invalid JWS", true, testFetcher)
require.Error(t, err)
require.Contains(t, err.Error(), "parse JWT")
require.Nil(t, claims)
})
t.Run("Invalid format of \"vp\" 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 := &invalidPresClaims{
Claims: &jwt.Claims{},
Presentation: 55, // "vp" claim of invalid format
}
token, err := jwt.Signed(signer).Claims(claims).CompactSerialize()
require.NoError(t, err)
uc, err := unmarshalPresJWSClaims(token, true, testFetcher)
require.Error(t, err)
require.Contains(t, err.Error(), "parse JWT")
require.Nil(t, uc)
})
t.Run("Invalid signature of JWS", func(t *testing.T) {
vp, err := newTestPresentation(t, []byte(validPresentation))
require.NoError(t, err)
jws := createCredJWS(t, vp, holderSigner)
uc, err := unmarshalPresJWSClaims(jws, true, func(issuerID, keyID string) (*verifier.PublicKey, error) {
// use public key of VC Issuer (while expecting to use the ones of VP Holder)
issuerSigner, errSigner := newCryptoSigner(kms.RSARS256Type)
require.NoError(t, errSigner)
return &verifier.PublicKey{
Type: kms.RSARS256,
Value: issuerSigner.PublicKeyBytes(),
}, nil
})
require.Error(t, err)
require.Contains(t, err.Error(), "parse JWT")
require.Nil(t, uc)
})
}
func createCredJWS(t *testing.T, vp *Presentation, signer Signer) string {
claims, err := newJWTPresClaims(vp, []string{}, false)
require.NoError(t, err)
require.NotNil(t, claims)
jws, err := claims.MarshalJWS(RS256, signer, "any")
require.NoError(t, err)
return jws
}
func holderPublicKeyFetcher(pubKeyBytes []byte) PublicKeyFetcher {
return func(issuerID, keyID string) (*verifier.PublicKey, error) {
return &verifier.PublicKey{
Type: kms.RSARS256,
Value: pubKeyBytes,
}, nil
}
}