github.com/trustbloc/kms-go@v1.1.2/doc/jose/common_test.go (about)

     1  /*
     2  Copyright SecureKey Technologies Inc. All Rights Reserved.
     3  SPDX-License-Identifier: Apache-2.0
     4  */
     5  
     6  package jose
     7  
     8  import (
     9  	"crypto/ed25519"
    10  	"crypto/rand"
    11  	"encoding/json"
    12  	"testing"
    13  
    14  	"github.com/go-jose/go-jose/v3"
    15  	"github.com/stretchr/testify/require"
    16  
    17  	"github.com/trustbloc/kms-go/doc/jose/jwk"
    18  )
    19  
    20  func TestHeaders_GetJWK(t *testing.T) {
    21  	headers := Headers{}
    22  
    23  	pubKey, _, err := ed25519.GenerateKey(rand.Reader)
    24  	require.NoError(t, err)
    25  
    26  	jwkKey := jwk.JWK{
    27  		JSONWebKey: jose.JSONWebKey{
    28  			Key:       pubKey,
    29  			KeyID:     "kid",
    30  			Algorithm: "EdDSA",
    31  		},
    32  	}
    33  
    34  	jwkBytes, err := json.Marshal(&jwkKey)
    35  	require.NoError(t, err)
    36  
    37  	var jwkMap map[string]interface{}
    38  
    39  	err = json.Unmarshal(jwkBytes, &jwkMap)
    40  	require.NoError(t, err)
    41  
    42  	headers["jwk"] = jwkMap
    43  
    44  	parsedJWK, ok := headers.JWK()
    45  	require.True(t, ok)
    46  	require.NotNil(t, parsedJWK)
    47  
    48  	// jwk is not present
    49  	delete(headers, "jwk")
    50  	parsedJWK, ok = headers.JWK()
    51  	require.False(t, ok)
    52  	require.Nil(t, parsedJWK)
    53  
    54  	// jwk is not a map
    55  	headers["jwk"] = "not a map"
    56  	parsedJWK, ok = headers.JWK()
    57  	require.False(t, ok)
    58  	require.Nil(t, parsedJWK)
    59  }