|
| 1 | +package jwt |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestVerifyAud(t *testing.T) { |
| 8 | + var nilInterface interface{} |
| 9 | + var nilListInterface []interface{} |
| 10 | + var intListInterface interface{} = []int{1,2,3} |
| 11 | + type test struct{ |
| 12 | + Name string |
| 13 | + MapClaims MapClaims |
| 14 | + Expected bool |
| 15 | + Comparison string |
| 16 | + Required bool |
| 17 | + } |
| 18 | + tests := []test{ |
| 19 | + // Matching Claim in aud |
| 20 | + // Required = true |
| 21 | + { Name: "String Aud matching required", MapClaims: MapClaims{"aud": "example.com"}, Expected: true , Required: true, Comparison: "example.com"}, |
| 22 | + { Name: "[]String Aud with match required", MapClaims: MapClaims{"aud": []string{"example.com", "example.example.com"}}, Expected: true, Required: true, Comparison: "example.com"}, |
| 23 | + |
| 24 | + // Required = false |
| 25 | + { Name: "String Aud with match not required", MapClaims: MapClaims{"aud": "example.com"}, Expected: true , Required: false, Comparison: "example.com"}, |
| 26 | + { Name: "Empty String Aud with match not required", MapClaims: MapClaims{}, Expected: true , Required: false, Comparison: "example.com"}, |
| 27 | + { Name: "Empty String Aud with match not required", MapClaims: MapClaims{"aud": ""}, Expected: true , Required: false, Comparison: "example.com"}, |
| 28 | + { Name: "Nil String Aud with match not required", MapClaims: MapClaims{"aud": nil}, Expected: true , Required: false, Comparison: "example.com"}, |
| 29 | + |
| 30 | + { Name: "[]String Aud with match not required", MapClaims: MapClaims{"aud": []string{"example.com", "example.example.com"}}, Expected: true, Required: false, Comparison: "example.com"}, |
| 31 | + { Name: "Empty []String Aud with match not required", MapClaims: MapClaims{"aud": []string{}}, Expected: true, Required: false, Comparison: "example.com"}, |
| 32 | + |
| 33 | + // Non-Matching Claim in aud |
| 34 | + // Required = true |
| 35 | + { Name: "String Aud without match required", MapClaims: MapClaims{"aud": "not.example.com"}, Expected: false, Required: true, Comparison: "example.com"}, |
| 36 | + { Name: "Empty String Aud without match required", MapClaims: MapClaims{"aud": ""}, Expected: false, Required: true, Comparison: "example.com"}, |
| 37 | + { Name: "[]String Aud without match required", MapClaims: MapClaims{"aud": []string{"not.example.com", "example.example.com"}}, Expected: false, Required: true, Comparison: "example.com"}, |
| 38 | + { Name: "Empty []String Aud without match required", MapClaims: MapClaims{"aud": []string{""}}, Expected: false, Required: true, Comparison: "example.com"}, |
| 39 | + { Name: "String Aud without match not required", MapClaims: MapClaims{"aud": "not.example.com"}, Expected: false, Required: true, Comparison: "example.com"}, |
| 40 | + { Name: "Empty String Aud without match not required", MapClaims: MapClaims{"aud": ""}, Expected: false, Required: true, Comparison: "example.com"}, |
| 41 | + { Name: "[]String Aud without match not required", MapClaims: MapClaims{"aud": []string{"not.example.com", "example.example.com"}}, Expected: false, Required: true, Comparison: "example.com"}, |
| 42 | + |
| 43 | + // Required = false |
| 44 | + { Name: "Empty []String Aud without match required", MapClaims: MapClaims{"aud": []string{""}}, Expected: false, Required: true, Comparison: "example.com"}, |
| 45 | + |
| 46 | + // []interface{} |
| 47 | + { Name: "Empty []interface{} Aud without match required", MapClaims: MapClaims{"aud": nilListInterface}, Expected: true, Required: false, Comparison: "example.com"}, |
| 48 | + { Name: "[]interface{} Aud wit match required", MapClaims: MapClaims{"aud": []interface{}{"a", "foo", "example.com"}}, Expected: true, Required: true, Comparison: "example.com"}, |
| 49 | + { Name: "[]interface{} Aud wit match but invalid types", MapClaims: MapClaims{"aud": []interface{}{"a", 5, "example.com"}}, Expected: false, Required: true, Comparison: "example.com"}, |
| 50 | + { Name: "[]interface{} Aud int wit match required", MapClaims: MapClaims{"aud": intListInterface}, Expected: false, Required: true, Comparison: "example.com"}, |
| 51 | + |
| 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 | + |
| 58 | + |
| 59 | + for _, test := range tests { |
| 60 | + t.Run(test.Name, func(t *testing.T) { |
| 61 | + got := test.MapClaims.VerifyAudience(test.Comparison, test.Required) |
| 62 | + |
| 63 | + if got != test.Expected { |
| 64 | + t.Errorf("Expected %v, got %v", test.Expected, got) |
| 65 | + } |
| 66 | + }) |
| 67 | + } |
| 68 | +} |
0 commit comments