(t *testing.T)
| 6 | ) |
| 7 | |
| 8 | func TestVerifyAud(t *testing.T) { |
| 9 | var nilInterface any |
| 10 | var nilListInterface []any |
| 11 | var intListInterface any = []int{1, 2, 3} |
| 12 | type test struct { |
| 13 | Name string |
| 14 | MapClaims MapClaims |
| 15 | Expected bool |
| 16 | Comparison []string |
| 17 | MatchAllAud bool |
| 18 | Required bool |
| 19 | } |
| 20 | tests := []test{ |
| 21 | // Matching Claim in aud |
| 22 | // Required = true |
| 23 | {Name: "String Aud matching required", MapClaims: MapClaims{"aud": "example.com"}, Expected: true, Required: true, Comparison: []string{"example.com"}}, |
| 24 | {Name: "[]String Aud with match required", MapClaims: MapClaims{"aud": []string{"example.com", "example.example.com"}}, Expected: true, Required: true, Comparison: []string{"example.com"}}, |
| 25 | {Name: "[]String Aud with []match any required", MapClaims: MapClaims{"aud": []string{"example.com", "example.example.com"}}, Expected: true, Required: true, Comparison: []string{"example.com", "auth.example.com"}}, |
| 26 | {Name: "[]String Aud with []match all required", MapClaims: MapClaims{"aud": []string{"example.com", "example.example.com"}}, Expected: true, Required: true, Comparison: []string{"example.com", "example.example.com"}, MatchAllAud: true}, |
| 27 | |
| 28 | // Required = false |
| 29 | {Name: "String Aud with match not required", MapClaims: MapClaims{"aud": "example.com"}, Expected: true, Required: false, Comparison: []string{"example.com"}}, |
| 30 | {Name: "Empty String Aud with match not required", MapClaims: MapClaims{}, Expected: true, Required: false, Comparison: []string{"example.com"}}, |
| 31 | {Name: "Empty String Aud with match not required", MapClaims: MapClaims{"aud": ""}, Expected: true, Required: false, Comparison: []string{"example.com"}}, |
| 32 | {Name: "Nil String Aud with match not required", MapClaims: MapClaims{"aud": nil}, Expected: true, Required: false, Comparison: []string{"example.com"}}, |
| 33 | |
| 34 | {Name: "[]String Aud with match not required", MapClaims: MapClaims{"aud": []string{"example.com", "example.example.com"}}, Expected: true, Required: false, Comparison: []string{"example.com"}}, |
| 35 | {Name: "Empty []String Aud with match not required", MapClaims: MapClaims{"aud": []string{}}, Expected: true, Required: false, Comparison: []string{"example.com"}}, |
| 36 | |
| 37 | // Non-Matching Claim in aud |
| 38 | // Required = true |
| 39 | {Name: "String Aud without match required", MapClaims: MapClaims{"aud": "not.example.com"}, Expected: false, Required: true, Comparison: []string{"example.com"}}, |
| 40 | {Name: "Empty String Aud without match required", MapClaims: MapClaims{"aud": ""}, Expected: false, Required: true, Comparison: []string{"example.com"}}, |
| 41 | {Name: "[]String Aud without match required", MapClaims: MapClaims{"aud": []string{"not.example.com", "example.example.com"}}, Expected: false, Required: true, Comparison: []string{"example.com"}}, |
| 42 | {Name: "Empty []String Aud without match required", MapClaims: MapClaims{"aud": []string{""}}, Expected: false, Required: true, Comparison: []string{"example.com"}}, |
| 43 | {Name: "String Aud without match not required", MapClaims: MapClaims{"aud": "not.example.com"}, Expected: false, Required: true, Comparison: []string{"example.com"}}, |
| 44 | {Name: "Empty String Aud without match not required", MapClaims: MapClaims{"aud": ""}, Expected: false, Required: true, Comparison: []string{"example.com"}}, |
| 45 | {Name: "[]String Aud without match not required", MapClaims: MapClaims{"aud": []string{"not.example.com", "example.example.com"}}, Expected: false, Required: true, Comparison: []string{"example.com"}}, |
| 46 | |
| 47 | // Required = false |
| 48 | {Name: "Empty []String Aud without match required", MapClaims: MapClaims{"aud": []string{""}}, Expected: true, Required: false, Comparison: []string{"example.com"}}, |
| 49 | |
| 50 | // []any |
| 51 | {Name: "Empty []interface{} Aud without match required", MapClaims: MapClaims{"aud": nilListInterface}, Expected: true, Required: false, Comparison: []string{"example.com"}}, |
| 52 | {Name: "[]interface{} Aud with match required", MapClaims: MapClaims{"aud": []any{"a", "foo", "example.com"}}, Expected: true, Required: true, Comparison: []string{"example.com"}}, |
| 53 | {Name: "[]interface{} Aud with match but invalid types", MapClaims: MapClaims{"aud": []any{"a", 5, "example.com"}}, Expected: false, Required: true, Comparison: []string{"example.com"}}, |
| 54 | {Name: "[]interface{} Aud int with match required", MapClaims: MapClaims{"aud": intListInterface}, Expected: false, Required: true, Comparison: []string{"example.com"}}, |
| 55 | |
| 56 | // any |
| 57 | {Name: "Empty interface{} Aud without match not required", MapClaims: MapClaims{"aud": nilInterface}, Expected: true, Required: false, Comparison: []string{"example.com"}}, |
| 58 | } |
| 59 | |
| 60 | for _, test := range tests { |
| 61 | t.Run(test.Name, func(t *testing.T) { |
| 62 | var opts []ParserOption |
| 63 | |
| 64 | if test.Required && test.MatchAllAud { |
| 65 | opts = append(opts, WithAllAudiences(test.Comparison...)) |
nothing calls this directly
no test coverage detected