...

Source file src/edge-infra.dev/pkg/edge/edgeencrypt/bearer_token_test.go

Documentation: edge-infra.dev/pkg/edge/edgeencrypt

     1  package edgeencrypt
     2  
     3  import (
     4  	"crypto/rand"
     5  	"crypto/rsa"
     6  	"testing"
     7  
     8  	"github.com/golang-jwt/jwt"
     9  	"github.com/google/uuid"
    10  )
    11  
    12  func TestBearerToken(t *testing.T) {
    13  	channelID := uuid.NewString()
    14  	channelName := "my-channel"
    15  	privateKey, err := rsa.GenerateKey(rand.Reader, RSA2048)
    16  	if err != nil {
    17  		t.Fatalf("failed to generate private key: %v", err)
    18  	}
    19  	publicKey := &privateKey.PublicKey
    20  
    21  	token, err := CreateToken(jwt.SigningMethodRS256, privateKey, DefaultDuration, channelID, channelName, Encryption, "my-banner")
    22  	if err != nil {
    23  		t.Errorf("CreateToken failed: %v", err)
    24  	}
    25  
    26  	claims, err := FromToken(publicKey, token)
    27  	if err != nil {
    28  		t.Errorf("FromToken failed: %v", err)
    29  	}
    30  
    31  	if !claims.ValidChannel(channelID) {
    32  		t.Errorf("ChannelID mismatch: %s != %s", claims.ChannelID, channelID)
    33  	}
    34  
    35  	if !claims.ValidChannelName(channelName) {
    36  		t.Errorf("ChannelName mismatch: %s != %s", claims.Channel, channelName)
    37  	}
    38  
    39  	if !claims.HasRole(Encryption) {
    40  		t.Errorf("Expected Role encryption: %v", claims.Role)
    41  	}
    42  }
    43  
    44  func TestPrintToken(t *testing.T) {
    45  	t.SkipNow()
    46  	// change the channelID and role to test different scenarios
    47  	channelID := uuid.NewString()
    48  	channelName := "store256"
    49  	role := Encryption
    50  
    51  	privateKey, err := rsa.GenerateKey(rand.Reader, RSA2048)
    52  	if err != nil {
    53  		t.Fatalf("failed to generate private key: %v", err)
    54  	}
    55  	publicKey := &privateKey.PublicKey
    56  
    57  	token, err := CreateToken(jwt.SigningMethodRS256, privateKey, DefaultDuration, channelID, channelName, role, "my-banner")
    58  	if err != nil {
    59  		t.Errorf("CreateToken failed: %v", err)
    60  	}
    61  
    62  	pk, err := ConvertRSAPublicKeyToPEM(publicKey)
    63  	if err != nil {
    64  		t.Fatalf("failed to convert public key to string: %v", err)
    65  	}
    66  	pvKey := ConvertRSAPrivateKeyToPEM(privateKey)
    67  
    68  	t.Logf("Private Key: \n%s", pvKey)
    69  	t.Logf("Public Key: \n%s", pk)
    70  	t.Logf("Token: \n%s", token)
    71  }
    72  

View as plain text