forked from dgrijalva/jwt-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from gospotcheck/support-audience-array
Support audience claim as string or array
- Loading branch information
Showing
4 changed files
with
207 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package jwt | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
// Test StandardClaims instances with an audience value populated in a string, []string and []interface{} | ||
var audienceValue = "Aud" | ||
var unmatchedAudienceValue = audienceValue + "Test" | ||
var claimWithAudience = []StandardClaims{ | ||
{ | ||
audienceValue, | ||
123123, | ||
"Id", | ||
12312, | ||
"Issuer", | ||
12312, | ||
"Subject", | ||
}, | ||
{ | ||
[]string{audienceValue, unmatchedAudienceValue}, | ||
123123, | ||
"Id", | ||
12312, | ||
"Issuer", | ||
12312, | ||
"Subject", | ||
}, | ||
{ | ||
[]interface{}{audienceValue, unmatchedAudienceValue}, | ||
123123, | ||
"Id", | ||
12312, | ||
"Issuer", | ||
12312, | ||
"Subject", | ||
}, | ||
} | ||
|
||
// Test StandardClaims instances with no audiences within empty []string and []interface{} collections. | ||
var claimWithoutAudience = []StandardClaims{ | ||
{ | ||
nil, | ||
123123, | ||
"Id", | ||
12312, | ||
"Issuer", | ||
12312, | ||
"Subject", | ||
}, | ||
{ | ||
[]string{}, | ||
123123, | ||
"Id", | ||
12312, | ||
"Issuer", | ||
12312, | ||
"Subject", | ||
}, | ||
{ | ||
[]interface{}{}, | ||
123123, | ||
"Id", | ||
12312, | ||
"Issuer", | ||
12312, | ||
"Subject", | ||
}, | ||
} | ||
|
||
func TestExtractAudienceWithAudienceValues(t *testing.T) { | ||
for _, data := range claimWithAudience { | ||
var aud = ExtractAudience(&data) | ||
if len(aud) == 0 || aud[0] != audienceValue { | ||
t.Errorf("The audience value was not extracted properly") | ||
} | ||
} | ||
} | ||
|
||
func TestExtractAudience_WithoutAudienceValues(t *testing.T) { | ||
for _, data := range claimWithoutAudience { | ||
var aud = ExtractAudience(&data) | ||
if len(aud) != 0 { | ||
t.Errorf("An audience value should not have been extracted") | ||
} | ||
} | ||
} | ||
|
||
var audWithValues = [][]string{ | ||
[]string{audienceValue}, | ||
[]string{"Aud1", "Aud2", audienceValue}, | ||
} | ||
|
||
var audWithLackingOriginalValue = [][]string{ | ||
[]string{}, | ||
[]string{audienceValue + "1"}, | ||
[]string{"Aud1", "Aud2", audienceValue + "1"}, | ||
} | ||
|
||
func TestVerifyAud_ShouldVerifyExists(t *testing.T) { | ||
for _, data := range audWithValues { | ||
if !verifyAud(data, audienceValue, true) { | ||
t.Errorf("The audience value was not verified properly") | ||
} | ||
} | ||
} | ||
|
||
func TestVerifyAud_ShouldVerifyDoesNotExist(t *testing.T) { | ||
for _, data := range audWithValues { | ||
if !verifyAud(data, audienceValue, true) { | ||
t.Errorf("The audience value was not verified properly") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package jwt | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
var audFixedValue = "Aud" | ||
var audClaimsMapsWithValues = []MapClaims{ | ||
{ | ||
"aud": audFixedValue, | ||
}, | ||
{ | ||
"aud": []string{audFixedValue}, | ||
}, | ||
{ | ||
"aud": []interface{}{audFixedValue}, | ||
}, | ||
} | ||
|
||
var audClaimsMapsWithoutValues = []MapClaims{ | ||
{}, | ||
{ | ||
"aud": []string{}, | ||
}, | ||
{ | ||
"aud": []interface{}{}, | ||
}, | ||
} | ||
|
||
// Verifies that for every form of the "aud" field, the audFixedValue is always verifiable | ||
func TestVerifyAudienceWithVerifiableValues(t *testing.T) { | ||
for _, data := range audClaimsMapsWithValues { | ||
if !data.VerifyAudience(audFixedValue, true) { | ||
t.Errorf("The audience value was not extracted properly") | ||
} | ||
} | ||
} | ||
|
||
// Verifies that for every empty form of the "aud" field, the audFixedValue cannot be verified | ||
func TestVerifyAudienceWithoutVerifiableValues(t *testing.T) { | ||
for _, data := range audClaimsMapsWithoutValues { | ||
if data.VerifyAudience(audFixedValue, true) { | ||
t.Errorf("The audience should not verify") | ||
} | ||
} | ||
} |