github.com/qlik-oss/gopherciser@v0.18.6/connection/jwtconnection_test.go (about)

     1  package connection
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/qlik-oss/gopherciser/logger"
     8  	"github.com/qlik-oss/gopherciser/session"
     9  	"github.com/qlik-oss/gopherciser/synced"
    10  	"github.com/qlik-oss/gopherciser/users"
    11  )
    12  
    13  func TestParsing(t *testing.T) {
    14  	// simple claims
    15  	stClaims, err := synced.New(`{
    16  			"user": "{{.UserName}}",
    17  			"directory": "{{.Directory}}"
    18  		}`)
    19  	if err != nil {
    20  		t.Fatal(err)
    21  	}
    22  
    23  	settings := ConnectJWTSettings{
    24  		Claims: *stClaims,
    25  	}
    26  
    27  	user := &users.User{
    28  		UserName:  "mytestuser_1",
    29  		Directory: "mydirectory",
    30  	}
    31  
    32  	sessionState := &session.State{
    33  		User: user,
    34  		LogEntry: &logger.LogEntry{
    35  			Session: &logger.SessionEntry{},
    36  			Action:  &logger.ActionEntry{},
    37  		},
    38  	}
    39  
    40  	claims, err := settings.executeClaimsTemplates(sessionState)
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  
    45  	expected := user.UserName
    46  	key := "user"
    47  	value := fmt.Sprintf("%v", claims[key])
    48  	validate(t, key, value, expected)
    49  
    50  	expected = user.Directory
    51  	key = "directory"
    52  	value = fmt.Sprintf("%v", claims[key])
    53  	validate(t, key, value, expected)
    54  
    55  	stClaims, err = synced.New(`{
    56  			"iat": {{now.Unix}},
    57  			"exp": {{(now.Add 18000000000000).Unix}},
    58  			"iss" : "https://qlik.api.interal",
    59  			"aud" : "qlik.api",
    60  			"sub": "custom",
    61  			"name": "{{.UserName}}",
    62  			"groups": ["group1", "group for user {{.UserName}}"]
    63  		}`)
    64  	if err != nil {
    65  		t.Fatal(err)
    66  	}
    67  
    68  	// advanced claims
    69  	settings = ConnectJWTSettings{
    70  		Claims: *stClaims,
    71  	}
    72  
    73  	claims, err = settings.executeClaimsTemplates(sessionState)
    74  	if err != nil {
    75  		t.Fatal(err)
    76  	}
    77  
    78  	expected = "https://qlik.api.interal"
    79  	key = "iss"
    80  	value = fmt.Sprintf("%v", claims[key])
    81  	validate(t, key, value, expected)
    82  
    83  	expected = "qlik.api"
    84  	key = "aud"
    85  	value = fmt.Sprintf("%v", claims[key])
    86  	validate(t, key, value, expected)
    87  
    88  	expected = "custom"
    89  	key = "sub"
    90  	value = fmt.Sprintf("%v", claims[key])
    91  	validate(t, key, value, expected)
    92  
    93  	expected = user.UserName
    94  	key = "name"
    95  	value = fmt.Sprintf("%v", claims[key])
    96  	validate(t, key, value, expected)
    97  
    98  	expected = fmt.Sprintf("%v", []string{"group1", fmt.Sprintf("group for user %s", user.UserName)})
    99  	key = "groups"
   100  	value = fmt.Sprintf("%v", claims[key])
   101  	validate(t, key, value, expected)
   102  
   103  	key = "iat"
   104  	if claims[key] == nil {
   105  		t.Error(key, "not set")
   106  	} else {
   107  		v, ok := claims[key].(float64)
   108  		if !ok {
   109  			t.Error(key, "not a number")
   110  		}
   111  		if v < 1 {
   112  			t.Error(key, "not set correctly, value:", v)
   113  		}
   114  	}
   115  
   116  	key = "exp"
   117  	if claims[key] == nil {
   118  		t.Error(key, "not set")
   119  	} else {
   120  		v, ok := claims[key].(float64)
   121  		if !ok {
   122  			t.Error(key, "not a number")
   123  		}
   124  		if v < 1 {
   125  			t.Error(key, "not set correctly, value:", v)
   126  		}
   127  	}
   128  
   129  	stJWTHeader, err := synced.New("{\"kid\":\"{{.UserName}}-Key\"}")
   130  	if err != nil {
   131  		t.Fatal(err)
   132  	}
   133  	// test jwt header
   134  	settings.JwtHeader = *stJWTHeader
   135  	jwtHeader, err := settings.executeJWTHeaderTemplates(sessionState)
   136  	if err != nil {
   137  		t.Fatal("failed parsing jwtheader", err)
   138  	}
   139  
   140  	expected = fmt.Sprintf("%s-Key", user.UserName)
   141  	key = "kid"
   142  	value = fmt.Sprintf("%v", jwtHeader[key])
   143  	validate(t, key, value, expected)
   144  }
   145  
   146  func validate(t *testing.T, key, value, expected string) {
   147  	t.Helper()
   148  
   149  	if value != expected {
   150  		t.Errorf("key<%s> expected<%s> got<%s>", key, expected, value)
   151  	}
   152  }