github.com/hyperledger/aries-framework-go@v0.3.2/pkg/doc/verifiable/presentation_jwt_unsecured_test.go (about) 1 /* 2 Copyright SecureKey Technologies Inc. All Rights Reserved. 3 SPDX-License-Identifier: Apache-2.0 4 */ 5 6 package verifiable 7 8 import ( 9 "testing" 10 11 "github.com/go-jose/go-jose/v3/jwt" 12 "github.com/stretchr/testify/require" 13 14 "github.com/hyperledger/aries-framework-go/pkg/doc/jose" 15 ) 16 17 func TestJWTPresClaims_MarshalUnsecuredJWT(t *testing.T) { 18 vp, err := newTestPresentation(t, []byte(validPresentation)) 19 require.NoError(t, err) 20 21 jws := createCredUnsecuredJWT(t, vp) 22 23 _, rawVC, err := decodeVPFromUnsecuredJWT(jws) 24 25 require.NoError(t, err) 26 require.Equal(t, vp.stringJSON(t), rawVC.stringJSON(t)) 27 } 28 29 func TestDecodeVPFromUnsecuredJWT(t *testing.T) { 30 t.Run("Successful unsecured JWT decoding", func(t *testing.T) { 31 vp, err := newTestPresentation(t, []byte(validPresentation)) 32 require.NoError(t, err) 33 34 jws := createCredUnsecuredJWT(t, vp) 35 36 vpDecodedBytes, vpRaw, err := decodeVPFromUnsecuredJWT(jws) 37 require.NoError(t, err) 38 require.NotNil(t, vpDecodedBytes) 39 require.Equal(t, vp.stringJSON(t), vpRaw.stringJSON(t)) 40 }) 41 42 t.Run("Invalid serialized unsecured JWT", func(t *testing.T) { 43 vpBytes, vpRaw, err := decodeVPFromUnsecuredJWT("invalid JWS") 44 require.Error(t, err) 45 require.Contains(t, err.Error(), "decode Verifiable Presentation JWT claims") 46 require.Nil(t, vpBytes) 47 require.Nil(t, vpRaw) 48 }) 49 50 t.Run("Invalid format of \"vp\" claim", func(t *testing.T) { 51 claims := &invalidPresClaims{ 52 Claims: &jwt.Claims{}, 53 Presentation: 55, // "vp" claim of invalid format 54 } 55 56 rawJWT, err := marshalUnsecuredJWT(jose.Headers{}, claims) 57 require.NoError(t, err) 58 59 vpBytes, vpRaw, err := decodeVPFromUnsecuredJWT(rawJWT) 60 require.Error(t, err) 61 require.Contains(t, err.Error(), "decode Verifiable Presentation JWT claims") 62 require.Nil(t, vpBytes) 63 require.Nil(t, vpRaw) 64 }) 65 } 66 67 func createCredUnsecuredJWT(t *testing.T, vp *Presentation) string { 68 claims, err := newJWTPresClaims(vp, []string{}, false) 69 require.NoError(t, err) 70 require.NotNil(t, claims) 71 72 unsecuredJWT, err := claims.MarshalUnsecuredJWT() 73 require.NoError(t, err) 74 75 return unsecuredJWT 76 }