github.com/hyperledger/aries-framework-go@v0.3.2/pkg/doc/verifiable/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/stretchr/testify/require"
    12  
    13  	"github.com/hyperledger/aries-framework-go/pkg/doc/jose"
    14  )
    15  
    16  func TestUnsecuredJWT(t *testing.T) {
    17  	headers := jose.Headers{"alg": "none"}
    18  	claims := map[string]interface{}{"sub": "user123", "productIds": []interface{}{1., 2.}}
    19  
    20  	serializedJWT, err := marshalUnsecuredJWT(headers, claims)
    21  	require.NoError(t, err)
    22  	require.NotEmpty(t, serializedJWT)
    23  
    24  	var claimsParsed map[string]interface{}
    25  	err = unmarshalUnsecuredJWT(serializedJWT, &claimsParsed)
    26  	require.NoError(t, err)
    27  
    28  	require.Equal(t, claims, claimsParsed)
    29  
    30  	// marshal with invalid claims
    31  	invalidClaims := map[string]interface{}{"error": map[chan int]interface{}{make(chan int): 6}}
    32  	serializedJWT, err = marshalUnsecuredJWT(headers, invalidClaims)
    33  	require.Error(t, err)
    34  	require.Contains(t, err.Error(), "marshal unsecured JWT")
    35  	require.Empty(t, serializedJWT)
    36  
    37  	// unmarshal invalid JWT
    38  	err = unmarshalUnsecuredJWT("not a valid compact serialized JWT", &claimsParsed)
    39  	require.Error(t, err)
    40  	require.Contains(t, err.Error(), "marshal unsecured JWT")
    41  	require.Empty(t, serializedJWT)
    42  }