...

Source file src/github.com/golang-jwt/jwt/v5/map_claims_test.go

Documentation: github.com/golang-jwt/jwt/v5

     1  package jwt
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  )
     7  
     8  func TestVerifyAud(t *testing.T) {
     9  	var nilInterface interface{}
    10  	var nilListInterface []interface{}
    11  	var intListInterface interface{} = []int{1, 2, 3}
    12  	type test struct {
    13  		Name       string
    14  		MapClaims  MapClaims
    15  		Expected   bool
    16  		Comparison string
    17  		Required   bool
    18  	}
    19  	tests := []test{
    20  		// Matching Claim in aud
    21  		// Required = true
    22  		{Name: "String Aud matching required", MapClaims: MapClaims{"aud": "example.com"}, Expected: true, Required: true, Comparison: "example.com"},
    23  		{Name: "[]String Aud with match required", MapClaims: MapClaims{"aud": []string{"example.com", "example.example.com"}}, Expected: true, Required: true, Comparison: "example.com"},
    24  
    25  		// Required = false
    26  		{Name: "String Aud with match not required", MapClaims: MapClaims{"aud": "example.com"}, Expected: true, Required: false, Comparison: "example.com"},
    27  		{Name: "Empty String Aud with match not required", MapClaims: MapClaims{}, Expected: true, Required: false, Comparison: "example.com"},
    28  		{Name: "Empty String Aud with match not required", MapClaims: MapClaims{"aud": ""}, Expected: true, Required: false, Comparison: "example.com"},
    29  		{Name: "Nil String Aud with match not required", MapClaims: MapClaims{"aud": nil}, Expected: true, Required: false, Comparison: "example.com"},
    30  
    31  		{Name: "[]String Aud with match not required", MapClaims: MapClaims{"aud": []string{"example.com", "example.example.com"}}, Expected: true, Required: false, Comparison: "example.com"},
    32  		{Name: "Empty []String Aud with match not required", MapClaims: MapClaims{"aud": []string{}}, Expected: true, Required: false, Comparison: "example.com"},
    33  
    34  		// Non-Matching Claim in aud
    35  		// Required = true
    36  		{Name: "String Aud without match required", MapClaims: MapClaims{"aud": "not.example.com"}, Expected: false, Required: true, Comparison: "example.com"},
    37  		{Name: "Empty String Aud without match required", MapClaims: MapClaims{"aud": ""}, Expected: false, Required: true, Comparison: "example.com"},
    38  		{Name: "[]String Aud without match required", MapClaims: MapClaims{"aud": []string{"not.example.com", "example.example.com"}}, Expected: false, Required: true, Comparison: "example.com"},
    39  		{Name: "Empty []String Aud without match required", MapClaims: MapClaims{"aud": []string{""}}, Expected: false, Required: true, Comparison: "example.com"},
    40  		{Name: "String Aud without match not required", MapClaims: MapClaims{"aud": "not.example.com"}, Expected: false, Required: true, Comparison: "example.com"},
    41  		{Name: "Empty String Aud without match not required", MapClaims: MapClaims{"aud": ""}, Expected: false, Required: true, Comparison: "example.com"},
    42  		{Name: "[]String Aud without match not required", MapClaims: MapClaims{"aud": []string{"not.example.com", "example.example.com"}}, Expected: false, Required: true, Comparison: "example.com"},
    43  
    44  		// Required = false
    45  		{Name: "Empty []String Aud without match required", MapClaims: MapClaims{"aud": []string{""}}, Expected: true, Required: false, Comparison: "example.com"},
    46  
    47  		// []interface{}
    48  		{Name: "Empty []interface{} Aud without match required", MapClaims: MapClaims{"aud": nilListInterface}, Expected: true, Required: false, Comparison: "example.com"},
    49  		{Name: "[]interface{} Aud with match required", MapClaims: MapClaims{"aud": []interface{}{"a", "foo", "example.com"}}, Expected: true, Required: true, Comparison: "example.com"},
    50  		{Name: "[]interface{} Aud with match but invalid types", MapClaims: MapClaims{"aud": []interface{}{"a", 5, "example.com"}}, Expected: false, Required: true, Comparison: "example.com"},
    51  		{Name: "[]interface{} Aud int with match required", MapClaims: MapClaims{"aud": intListInterface}, Expected: false, Required: true, Comparison: "example.com"},
    52  
    53  		// interface{}
    54  		{Name: "Empty interface{} Aud without match not required", MapClaims: MapClaims{"aud": nilInterface}, Expected: true, Required: false, Comparison: "example.com"},
    55  	}
    56  
    57  	for _, test := range tests {
    58  		t.Run(test.Name, func(t *testing.T) {
    59  			var opts []ParserOption
    60  
    61  			if test.Required {
    62  				opts = append(opts, WithAudience(test.Comparison))
    63  			}
    64  
    65  			validator := NewValidator(opts...)
    66  			got := validator.Validate(test.MapClaims)
    67  
    68  			if (got == nil) != test.Expected {
    69  				t.Errorf("Expected %v, got %v", test.Expected, (got == nil))
    70  			}
    71  		})
    72  	}
    73  }
    74  
    75  func TestMapclaimsVerifyIssuedAtInvalidTypeString(t *testing.T) {
    76  	mapClaims := MapClaims{
    77  		"iat": "foo",
    78  	}
    79  	want := false
    80  	got := NewValidator(WithIssuedAt()).Validate(mapClaims)
    81  	if want != (got == nil) {
    82  		t.Fatalf("Failed to verify claims, wanted: %v got %v", want, (got == nil))
    83  	}
    84  }
    85  
    86  func TestMapclaimsVerifyNotBeforeInvalidTypeString(t *testing.T) {
    87  	mapClaims := MapClaims{
    88  		"nbf": "foo",
    89  	}
    90  	want := false
    91  	got := NewValidator().Validate(mapClaims)
    92  	if want != (got == nil) {
    93  		t.Fatalf("Failed to verify claims, wanted: %v got %v", want, (got == nil))
    94  	}
    95  }
    96  
    97  func TestMapclaimsVerifyExpiresAtInvalidTypeString(t *testing.T) {
    98  	mapClaims := MapClaims{
    99  		"exp": "foo",
   100  	}
   101  	want := false
   102  	got := NewValidator().Validate(mapClaims)
   103  
   104  	if want != (got == nil) {
   105  		t.Fatalf("Failed to verify claims, wanted: %v got %v", want, (got == nil))
   106  	}
   107  }
   108  
   109  func TestMapClaimsVerifyExpiresAtExpire(t *testing.T) {
   110  	exp := time.Now()
   111  	mapClaims := MapClaims{
   112  		"exp": float64(exp.Unix()),
   113  	}
   114  	want := false
   115  	got := NewValidator(WithTimeFunc(func() time.Time {
   116  		return exp
   117  	})).Validate(mapClaims)
   118  	if want != (got == nil) {
   119  		t.Fatalf("Failed to verify claims, wanted: %v got %v", want, (got == nil))
   120  	}
   121  
   122  	got = NewValidator(WithTimeFunc(func() time.Time {
   123  		return exp.Add(1 * time.Second)
   124  	})).Validate(mapClaims)
   125  	if want != (got == nil) {
   126  		t.Fatalf("Failed to verify claims, wanted: %v got %v", want, (got == nil))
   127  	}
   128  
   129  	want = true
   130  	got = NewValidator(WithTimeFunc(func() time.Time {
   131  		return exp.Add(-1 * time.Second)
   132  	})).Validate(mapClaims)
   133  	if want != (got == nil) {
   134  		t.Fatalf("Failed to verify claims, wanted: %v got %v", want, (got == nil))
   135  	}
   136  }
   137  
   138  func TestMapClaims_parseString(t *testing.T) {
   139  	type args struct {
   140  		key string
   141  	}
   142  	tests := []struct {
   143  		name    string
   144  		m       MapClaims
   145  		args    args
   146  		want    string
   147  		wantErr bool
   148  	}{
   149  		{
   150  			name: "missing key",
   151  			m:    MapClaims{},
   152  			args: args{
   153  				key: "mykey",
   154  			},
   155  			want:    "",
   156  			wantErr: false,
   157  		},
   158  		{
   159  			name: "wrong key type",
   160  			m:    MapClaims{"mykey": 4},
   161  			args: args{
   162  				key: "mykey",
   163  			},
   164  			want:    "",
   165  			wantErr: true,
   166  		},
   167  		{
   168  			name: "correct key type",
   169  			m:    MapClaims{"mykey": "mystring"},
   170  			args: args{
   171  				key: "mykey",
   172  			},
   173  			want:    "mystring",
   174  			wantErr: false,
   175  		},
   176  	}
   177  	for _, tt := range tests {
   178  		t.Run(tt.name, func(t *testing.T) {
   179  			got, err := tt.m.parseString(tt.args.key)
   180  			if (err != nil) != tt.wantErr {
   181  				t.Errorf("MapClaims.parseString() error = %v, wantErr %v", err, tt.wantErr)
   182  				return
   183  			}
   184  			if got != tt.want {
   185  				t.Errorf("MapClaims.parseString() = %v, want %v", got, tt.want)
   186  			}
   187  		})
   188  	}
   189  }
   190  

View as plain text