Skip to content
This repository was archived by the owner on Aug 10, 2021. It is now read-only.

Commit 5b2d2b5

Browse files
authored
Merge pull request #8 from yehted/fix/validate_audience_type_issue
add check for []interface{} type when validating audience
2 parents 9162a5a + 36a2dce commit 5b2d2b5

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

map_claims.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,23 @@ type MapClaims map[string]interface{}
1313
// Compares the aud claim against cmp.
1414
// If required is false, this method will return true if the value matches or is unset
1515
func (m MapClaims) VerifyAudience(cmp string, req bool) bool {
16-
aud, ok := m["aud"].([]string)
17-
if !ok {
18-
strAud, ok := m["aud"].(string)
19-
if !ok {
20-
return false
16+
var aud []string
17+
switch v := m["aud"].(type) {
18+
case []string:
19+
aud = v
20+
case []interface{}:
21+
for _, a := range v {
22+
vs, ok := a.(string)
23+
if !ok {
24+
return false
25+
}
26+
aud = append(aud, vs)
2127
}
22-
aud = append(aud, strAud)
28+
case string:
29+
aud = append(aud, v)
30+
default:
31+
return false
2332
}
24-
2533
return verifyAud(aud, cmp, req)
2634
}
2735

map_claims_test.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package jwt
22

33
import "testing"
44

5-
func Test_mapClaims_list_aud(t *testing.T){
5+
func Test_mapClaims_list_aud(t *testing.T) {
66
mapClaims := MapClaims{
77
"aud": []string{"foo"},
88
}
@@ -13,7 +13,18 @@ func Test_mapClaims_list_aud(t *testing.T){
1313
t.Fatalf("Failed to verify claims, wanted: %v got %v", want, got)
1414
}
1515
}
16-
func Test_mapClaims_string_aud(t *testing.T){
16+
func Test_mapClaims_list_interface_aud(t *testing.T) {
17+
mapClaims := MapClaims{
18+
"aud": []interface{}{"foo"},
19+
}
20+
want := true
21+
got := mapClaims.VerifyAudience("foo", true)
22+
23+
if want != got {
24+
t.Fatalf("Failed to verify claims, wanted: %v got %v", want, got)
25+
}
26+
}
27+
func Test_mapClaims_string_aud(t *testing.T) {
1728
mapClaims := MapClaims{
1829
"aud": "foo",
1930
}
@@ -25,7 +36,7 @@ func Test_mapClaims_string_aud(t *testing.T){
2536
}
2637
}
2738

28-
func Test_mapClaims_list_aud_no_match(t *testing.T){
39+
func Test_mapClaims_list_aud_no_match(t *testing.T) {
2940
mapClaims := MapClaims{
3041
"aud": []string{"bar"},
3142
}
@@ -36,7 +47,7 @@ func Test_mapClaims_list_aud_no_match(t *testing.T){
3647
t.Fatalf("Failed to verify claims, wanted: %v got %v", want, got)
3748
}
3849
}
39-
func Test_mapClaims_string_aud_fail(t *testing.T){
50+
func Test_mapClaims_string_aud_fail(t *testing.T) {
4051
mapClaims := MapClaims{
4152
"aud": "bar",
4253
}
@@ -48,9 +59,8 @@ func Test_mapClaims_string_aud_fail(t *testing.T){
4859
}
4960
}
5061

51-
func Test_mapClaims_string_aud_no_claim(t *testing.T){
52-
mapClaims := MapClaims{
53-
}
62+
func Test_mapClaims_string_aud_no_claim(t *testing.T) {
63+
mapClaims := MapClaims{}
5464
want := false
5565
got := mapClaims.VerifyAudience("foo", true)
5666

@@ -59,13 +69,12 @@ func Test_mapClaims_string_aud_no_claim(t *testing.T){
5969
}
6070
}
6171

62-
func Test_mapClaims_string_aud_no_claim_not_required(t *testing.T){
63-
mapClaims := MapClaims{
64-
}
72+
func Test_mapClaims_string_aud_no_claim_not_required(t *testing.T) {
73+
mapClaims := MapClaims{}
6574
want := false
6675
got := mapClaims.VerifyAudience("foo", false)
6776

6877
if want != got {
6978
t.Fatalf("Failed to verify claims, wanted: %v got %v", want, got)
7079
}
71-
}
80+
}

0 commit comments

Comments
 (0)