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.
Supports for the aud claim as string or array
This was created based on the following PR: dgrijalva#286 As per the JWT spec, the aud claim field can be either a single string value or an array of strings. jwt-go would completely drop array values as the StandardClaims struct's Audience field is a string value and the value is dropped upon deserialization.
- Loading branch information
1 parent
3af4c74
commit d3af071
Showing
4 changed files
with
195 additions
and
15 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,105 @@ | ||
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 aduences within empty []string and []interface{} collections. | ||
var claimWithoutAudience = []StandardClaims{ | ||
{ | ||
[]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") | ||
} | ||
} | ||
} |