forked from hyperledger-archives/aries-framework-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcredential_jwt_unsecured_test.go
77 lines (59 loc) · 1.97 KB
/
credential_jwt_unsecured_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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package verifiable
import (
"encoding/json"
"testing"
"github.com/square/go-jose/v3/jwt"
"github.com/stretchr/testify/require"
"github.com/hyperledger/aries-framework-go/pkg/doc/jose"
)
func TestCredentialJWTClaimsMarshallingToUnsecuredJWT(t *testing.T) {
vc, err := parseTestCredential(t, []byte(validCredential))
require.NoError(t, err)
jwtClaims, err := vc.JWTClaims(true)
require.NoError(t, err)
sJWT, err := jwtClaims.MarshalUnsecuredJWT()
require.NoError(t, err)
require.NotNil(t, sJWT)
vcBytes, err := decodeCredJWTUnsecured(sJWT)
require.NoError(t, err)
vcRaw := new(rawCredential)
err = json.Unmarshal(vcBytes, &vcRaw)
require.NoError(t, err)
require.NoError(t, err)
require.Equal(t, vc.stringJSON(t), vcRaw.stringJSON(t))
}
func TestCredUnsecuredJWTDecoderParseJWTClaims(t *testing.T) {
t.Run("Successful unsecured JWT decoding", func(t *testing.T) {
vc, err := parseTestCredential(t, []byte(validCredential))
require.NoError(t, err)
jwtClaims, err := vc.JWTClaims(true)
require.NoError(t, err)
sJWT, err := jwtClaims.MarshalUnsecuredJWT()
require.NoError(t, err)
decodedCred, err := decodeCredJWTUnsecured(sJWT)
require.NoError(t, err)
require.NotNil(t, decodedCred)
})
t.Run("Invalid serialized unsecured JWT", func(t *testing.T) {
vcBytes, err := decodeCredJWTUnsecured("invalid JWS")
require.Error(t, err)
require.Contains(t, err.Error(), "parse VC in JWT Unsecured form")
require.Nil(t, vcBytes)
})
t.Run("Invalid format of \"vc\" claim", func(t *testing.T) {
claims := &invalidCredClaims{
Claims: &jwt.Claims{},
Credential: 55, // "vc" claim of invalid format
}
rawJWT, err := marshalUnsecuredJWT(jose.Headers{}, claims)
require.NoError(t, err)
vcBytes, err := decodeCredJWTUnsecured(rawJWT)
require.Error(t, err)
require.Contains(t, err.Error(), "unmarshal VC JWT claims")
require.Nil(t, vcBytes)
})
}