diff --git a/map_claims.go b/map_claims.go index 90ab6bea..bcc37b15 100644 --- a/map_claims.go +++ b/map_claims.go @@ -13,15 +13,23 @@ type MapClaims map[string]interface{} // Compares the aud claim against cmp. // If required is false, this method will return true if the value matches or is unset func (m MapClaims) VerifyAudience(cmp string, req bool) bool { - aud, ok := m["aud"].([]string) - if !ok { - strAud, ok := m["aud"].(string) - if !ok { - return false + var aud []string + switch v := m["aud"].(type) { + case []string: + aud = v + case []interface{}: + for _, a := range v { + vs, ok := a.(string) + if !ok { + return false + } + aud = append(aud, vs) } - aud = append(aud, strAud) + case string: + aud = append(aud, v) + default: + return false } - return verifyAud(aud, cmp, req) } diff --git a/map_claims_test.go b/map_claims_test.go index d5255a4d..47715d41 100644 --- a/map_claims_test.go +++ b/map_claims_test.go @@ -2,7 +2,7 @@ package jwt import "testing" -func Test_mapClaims_list_aud(t *testing.T){ +func Test_mapClaims_list_aud(t *testing.T) { mapClaims := MapClaims{ "aud": []string{"foo"}, } @@ -13,7 +13,18 @@ func Test_mapClaims_list_aud(t *testing.T){ t.Fatalf("Failed to verify claims, wanted: %v got %v", want, got) } } -func Test_mapClaims_string_aud(t *testing.T){ +func Test_mapClaims_list_interface_aud(t *testing.T) { + mapClaims := MapClaims{ + "aud": []interface{}{"foo"}, + } + want := true + got := mapClaims.VerifyAudience("foo", true) + + if want != got { + t.Fatalf("Failed to verify claims, wanted: %v got %v", want, got) + } +} +func Test_mapClaims_string_aud(t *testing.T) { mapClaims := MapClaims{ "aud": "foo", } @@ -25,7 +36,7 @@ func Test_mapClaims_string_aud(t *testing.T){ } } -func Test_mapClaims_list_aud_no_match(t *testing.T){ +func Test_mapClaims_list_aud_no_match(t *testing.T) { mapClaims := MapClaims{ "aud": []string{"bar"}, } @@ -36,7 +47,7 @@ func Test_mapClaims_list_aud_no_match(t *testing.T){ t.Fatalf("Failed to verify claims, wanted: %v got %v", want, got) } } -func Test_mapClaims_string_aud_fail(t *testing.T){ +func Test_mapClaims_string_aud_fail(t *testing.T) { mapClaims := MapClaims{ "aud": "bar", } @@ -48,9 +59,8 @@ func Test_mapClaims_string_aud_fail(t *testing.T){ } } -func Test_mapClaims_string_aud_no_claim(t *testing.T){ - mapClaims := MapClaims{ - } +func Test_mapClaims_string_aud_no_claim(t *testing.T) { + mapClaims := MapClaims{} want := false got := mapClaims.VerifyAudience("foo", true) @@ -59,13 +69,12 @@ func Test_mapClaims_string_aud_no_claim(t *testing.T){ } } -func Test_mapClaims_string_aud_no_claim_not_required(t *testing.T){ - mapClaims := MapClaims{ - } +func Test_mapClaims_string_aud_no_claim_not_required(t *testing.T) { + mapClaims := MapClaims{} want := false got := mapClaims.VerifyAudience("foo", false) if want != got { t.Fatalf("Failed to verify claims, wanted: %v got %v", want, got) } -} \ No newline at end of file +}