This repository has been archived by the owner on May 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 995
Added logic to MapClaims to allow 'aud' to be an array of strings #315
Open
danapsimer
wants to merge
2
commits into
dgrijalva:master
Choose a base branch
from
danapsimer:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -13,7 +13,13 @@ 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, _ := m["aud"].(string) | ||
var aud []string | ||
switch exp := m["aud"].(type) { | ||
case string: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wrong Indentation |
||
aud = []string{exp} | ||
case []string: | ||
aud = exp | ||
} | ||
return verifyAud(aud, cmp, req) | ||
} | ||
|
||
|
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,120 @@ | ||
package jwt_test | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/dgrijalva/jwt-go" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestMapClaims_Valid(t *testing.T) { | ||
now := time.Now() | ||
oneMinuteFromNow := json.Number(fmt.Sprint(now.Add(time.Minute).Unix())) | ||
oneMinuteAgo := json.Number(fmt.Sprint(now.Add(-time.Minute).Unix())) | ||
twoMinutesAgo := json.Number(fmt.Sprint(now.Add(-2 * time.Minute).Unix())) | ||
thirtySecondFromNow := json.Number(fmt.Sprint(now.Add(30*time.Second).Unix())) | ||
nowStr := json.Number(fmt.Sprint(now.Unix())) | ||
validClaims := jwt.MapClaims{ | ||
"exp": oneMinuteFromNow, | ||
"iat": nowStr, | ||
"nbf": nowStr, | ||
} | ||
assert.NoError(t, validClaims.Valid()) | ||
expiredClaims := jwt.MapClaims{ | ||
"exp": oneMinuteAgo, | ||
"iat": twoMinutesAgo, | ||
"nbf": twoMinutesAgo, | ||
} | ||
assert.Error(t, expiredClaims.Valid()) | ||
notYetValidClaims := jwt.MapClaims{ | ||
"exp": oneMinuteFromNow, | ||
"iat": nowStr, | ||
"nbf": thirtySecondFromNow, | ||
} | ||
assert.Error(t, notYetValidClaims.Valid()) | ||
notYetIssuedClaims := jwt.MapClaims{ | ||
"exp": oneMinuteFromNow, | ||
"iat": thirtySecondFromNow, | ||
"nbf": thirtySecondFromNow, | ||
} | ||
assert.Error(t, notYetIssuedClaims.Valid()) | ||
} | ||
|
||
func TestMapClaims_Valid_Float(t *testing.T) { | ||
now := time.Now() | ||
oneMinuteFromNow := float64(now.Add(time.Minute).Unix()) | ||
oneMinuteAgo := float64(now.Add(-time.Minute).Unix()) | ||
twoMinutesAgo := float64(now.Add(-2 * time.Minute).Unix()) | ||
thirtySecondFromNow := float64(now.Add(30*time.Second).Unix()) | ||
nowStr := float64(now.Unix()) | ||
validClaims := jwt.MapClaims{ | ||
"exp": oneMinuteFromNow, | ||
"iat": nowStr, | ||
"nbf": nowStr, | ||
} | ||
assert.NoError(t, validClaims.Valid()) | ||
expiredClaims := jwt.MapClaims{ | ||
"exp": oneMinuteAgo, | ||
"iat": twoMinutesAgo, | ||
"nbf": twoMinutesAgo, | ||
} | ||
assert.Error(t, expiredClaims.Valid()) | ||
notYetValidClaims := jwt.MapClaims{ | ||
"exp": oneMinuteFromNow, | ||
"iat": nowStr, | ||
"nbf": thirtySecondFromNow, | ||
} | ||
assert.Error(t, notYetValidClaims.Valid()) | ||
notYetIssuedClaims := jwt.MapClaims{ | ||
"exp": oneMinuteFromNow, | ||
"iat": thirtySecondFromNow, | ||
"nbf": thirtySecondFromNow, | ||
} | ||
assert.Error(t, notYetIssuedClaims.Valid()) | ||
} | ||
|
||
func TestMapClaims_VerifyAudience(t *testing.T) { | ||
joe := "joe" | ||
jill := "jill" | ||
jack := "jack" | ||
|
||
claims := jwt.MapClaims{} | ||
assert.True(t, claims.VerifyAudience(joe, false)) | ||
assert.False(t, claims.VerifyAudience(joe, true)) | ||
|
||
claims = jwt.MapClaims{"aud":[]string{}} | ||
assert.True(t, claims.VerifyAudience(joe, false)) | ||
assert.False(t, claims.VerifyAudience(joe, true)) | ||
|
||
claims = jwt.MapClaims{ | ||
"aud": joe, | ||
} | ||
assert.True(t, claims.VerifyAudience(joe, false)) | ||
assert.False(t, claims.VerifyAudience(jill, false)) | ||
assert.True(t, claims.VerifyAudience(joe, true)) | ||
assert.False(t, claims.VerifyAudience(jill, true)) | ||
|
||
claims = jwt.MapClaims{ | ||
"aud": []string {joe, jill}, | ||
} | ||
assert.True(t, claims.VerifyAudience(joe, false)) | ||
assert.True(t, claims.VerifyAudience(joe, true)) | ||
assert.False(t, claims.VerifyAudience(jack, false)) | ||
assert.False(t, claims.VerifyAudience(jack, true)) | ||
} | ||
|
||
func TestMapClaims_VerifyIssuer(t *testing.T) { | ||
claims := jwt.MapClaims{} | ||
assert.True(t, claims.VerifyIssuer("service1", false)) | ||
assert.False(t, claims.VerifyIssuer("service1", true)) | ||
|
||
claims = jwt.MapClaims{"iss": "service1"} | ||
assert.True(t, claims.VerifyIssuer("service1", false)) | ||
assert.True(t, claims.VerifyIssuer("service1", true)) | ||
|
||
claims = jwt.MapClaims{"iss": "service2"} | ||
assert.False(t, claims.VerifyIssuer("service1", false)) | ||
assert.False(t, claims.VerifyIssuer("service1", true)) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
auds == nil
is not necessary,len(auds)
covers both cases: https://play.golang.org/p/rzZoRNYYo9N