github.com/xmidt-org/webpa-common@v1.11.9/secure/setup_test.go (about)

     1  package secure
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/SermoDigital/jose/crypto"
     6  	"github.com/SermoDigital/jose/jws"
     7  	"github.com/SermoDigital/jose/jwt"
     8  	"github.com/xmidt-org/webpa-common/resource"
     9  	"github.com/xmidt-org/webpa-common/secure/key"
    10  	"os"
    11  	"testing"
    12  )
    13  
    14  const (
    15  	publicKeyFileName  = "jwt-key.pub"
    16  	privateKeyFileName = "jwt-key"
    17  )
    18  
    19  var (
    20  	publicKeyFileURI  string
    21  	publicKeyResolver key.Resolver
    22  
    23  	privateKeyFileURI  string
    24  	privateKeyResolver key.Resolver
    25  
    26  	// ripped these test claims from the SATS swagger example
    27  	testClaims = jws.Claims{
    28  		"valid":        true,
    29  		"capabilities": []interface{}{"x1:webpa:api:.*:post"},
    30  		"allowedResources": map[string]interface{}{
    31  			"allowedDeviceIds":         []interface{}{"1641529834193109183"},
    32  			"allowedPartners":          []interface{}{"comcast, cox"},
    33  			"allowedServiceAccountIds": []interface{}{"4924346887352567847"},
    34  		},
    35  	}
    36  
    37  	testJWT           jwt.JWT
    38  	testSerializedJWT []byte
    39  )
    40  
    41  func TestMain(m *testing.M) {
    42  	os.Exit(func() int {
    43  		currentDirectory, err := os.Getwd()
    44  		if err != nil {
    45  			fmt.Fprintf(os.Stderr, "Unable to obtain current working directory: %s\n", err)
    46  			return 1
    47  		}
    48  
    49  		publicKeyFileURI = fmt.Sprintf("%s/%s", currentDirectory, publicKeyFileName)
    50  		privateKeyFileURI = fmt.Sprintf("%s/%s", currentDirectory, privateKeyFileName)
    51  
    52  		privateKeyResolver, err = (&key.ResolverFactory{
    53  			Factory: resource.Factory{URI: privateKeyFileURI},
    54  			Purpose: key.PurposeSign,
    55  		}).NewResolver()
    56  
    57  		if err != nil {
    58  			fmt.Fprintf(os.Stderr, "Unable to create private key resolver: %s\n", err)
    59  			return 1
    60  		}
    61  
    62  		publicKeyResolver, err = (&key.ResolverFactory{
    63  			Factory: resource.Factory{URI: publicKeyFileURI},
    64  			Purpose: key.PurposeVerify,
    65  		}).NewResolver()
    66  
    67  		if err != nil {
    68  			fmt.Fprintf(os.Stderr, "Unable to create public key resolver: %s\n", err)
    69  			return 1
    70  		}
    71  
    72  		pair, err := privateKeyResolver.ResolveKey("")
    73  		if err != nil {
    74  			fmt.Fprintf(os.Stderr, "Unable to resolve private key: %s\n", err)
    75  			return 1
    76  		}
    77  
    78  		// generate a unique JWT for each run of the tests
    79  		// this also exercises our secure/key infrastructure
    80  		testJWT = jws.NewJWT(testClaims, crypto.SigningMethodRS256)
    81  		testSerializedJWT, err = testJWT.Serialize(pair.Private())
    82  		if err != nil {
    83  			fmt.Fprintf(os.Stderr, "Unable to serialize test JWT: %s\n", err)
    84  			return 1
    85  		}
    86  
    87  		return m.Run()
    88  	}())
    89  }