Skip to content

Commit

Permalink
Added jti as parameter to claims helper to privide better interface t…
Browse files Browse the repository at this point in the history
…o developers

Signed-off-by: Alexander Widerberg <[email protected]>
  • Loading branch information
leetal committed Jan 19, 2016
1 parent 26f41a0 commit bde3822
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 12 deletions.
8 changes: 4 additions & 4 deletions enigma/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestRejectsAlgAndTypHeader(t *testing.T) {
{"typ": "foo"},
{"typ": "foo", "alg": "foo"},
} {
claims, _ := jwthelper.NewClaimsContext("fosite", "peter", "group0",
claims, _ := jwthelper.NewClaimsContext("fosite", "peter", "group0", "",
time.Now().Add(time.Hour), time.Now(), time.Now(), make(map[string]interface{}))

j := JWTEnigma{
Expand All @@ -68,7 +68,7 @@ func TestRejectsAlgAndTypHeader(t *testing.T) {
}

func TestGenerateJWT(t *testing.T) {
claims, err := jwthelper.NewClaimsContext("fosite", "peter", "group0",
claims, err := jwthelper.NewClaimsContext("fosite", "peter", "group0", "",
time.Now().Add(time.Hour), time.Now(), time.Now(), make(map[string]interface{}))

j := JWTEnigma{
Expand Down Expand Up @@ -109,7 +109,7 @@ func TestGenerateJWT(t *testing.T) {
j.PrivateKey = []byte(TestCertificates[0][1])

// Lets validate the exp claim
claims, err = jwthelper.NewClaimsContext("fosite", "peter", "group0",
claims, err = jwthelper.NewClaimsContext("fosite", "peter", "group0", "",
time.Now().Add(-time.Hour), time.Now(), time.Now(), make(map[string]interface{}))

token, sig, err = j.Generate(claims, make(map[string]interface{}))
Expand All @@ -121,7 +121,7 @@ func TestGenerateJWT(t *testing.T) {
require.NotNil(t, err, "%s", err)

// Lets validate the nbf claim
claims, err = jwthelper.NewClaimsContext("fosite", "peter", "group0",
claims, err = jwthelper.NewClaimsContext("fosite", "peter", "group0", "",
time.Now().Add(time.Hour), time.Now().Add(time.Hour), time.Now(), make(map[string]interface{}))

token, sig, err = j.Generate(claims, make(map[string]interface{}))
Expand Down
8 changes: 6 additions & 2 deletions enigma/jwthelper/claims.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var reservedClaimNames = map[string]string{
}

// NewClaimsContext : Dezignated initializer of the ClaimsContext handler
func NewClaimsContext(issuer string, subject string, audience string,
func NewClaimsContext(issuer string, subject string, audience string, tokenId string,
expiresAt time.Time, notBefore time.Time, issuedAt time.Time,
userClaims map[string]interface{}) (*ClaimsContext, error) {

Expand All @@ -50,7 +50,11 @@ func NewClaimsContext(issuer string, subject string, audience string,
allClaims["nbf"] = notBefore.Unix()
allClaims["aud"] = audience
allClaims["exp"] = expiresAt.Unix()
allClaims["jti"] = uuid.New()
if tokenId != "" {
allClaims["jti"] = tokenId
} else {
allClaims["jti"] = uuid.New()
}

return &allClaims, nil
}
Expand Down
6 changes: 3 additions & 3 deletions enigma/jwthelper/claims_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func TestValidClaimsContext(t *testing.T) {
userClaims := ClaimsContext{"user-id": "123456", "custom-time": 1453066866, "custom-time-f": 1631.083, "custom-date": time.Date(2016, time.January, 17, 19, 00, 00, 00, &time.Location{})}
ctx, err := NewClaimsContext("fosite/auth", "Peter", "[email protected]", time.Now().Add(time.Hour), time.Now(), time.Now(), userClaims)
ctx, err := NewClaimsContext("fosite/auth", "Peter", "[email protected]", "", time.Now().Add(time.Hour), time.Now(), time.Now(), userClaims)
assert.Nil(t, err)

assert.Equal(t, "fosite/auth", ctx.GetIssuer())
Expand Down Expand Up @@ -41,11 +41,11 @@ func TestValidClaimsContext(t *testing.T) {

func TestInvalidClaimsContext(t *testing.T) {
userClaims := ClaimsContext{"sub": "the \"sub\" field cannot be passed to claims context since it's a reserved claim"}
claimsCtx, err := NewClaimsContext("fosite/auth", "Peter", "[email protected]", time.Now().Add(time.Hour), time.Now(), time.Now(), userClaims)
claimsCtx, err := NewClaimsContext("fosite/auth", "Peter", "[email protected]", "", time.Now().Add(time.Hour), time.Now(), time.Now(), userClaims)
assert.NotNil(t, err)

userClaims = ClaimsContext{"alt": ""}
claimsCtx, err = NewClaimsContext("fosite/auth", "Peter", "[email protected]", time.Now().Add(-time.Hour), time.Now().Add(time.Hour), time.Now(), userClaims)
claimsCtx, err = NewClaimsContext("fosite/auth", "Peter", "[email protected]", "", time.Now().Add(-time.Hour), time.Now().Add(time.Hour), time.Now(), userClaims)
assert.Nil(t, err)

assert.True(t, claimsCtx.AssertExpired())
Expand Down
4 changes: 2 additions & 2 deletions fosite-example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func tokenEndpoint(rw http.ResponseWriter, req *http.Request) {

if typeof(*selectedStrategy) == "strategy.JWTStrategy" {
// JWT
claims, _ := jwthelper.NewClaimsContext("fosite", "peter", "group0",
claims, _ := jwthelper.NewClaimsContext("fosite", "peter", "group0", "",
time.Now().Add(time.Hour), time.Now(), time.Now(), make(map[string]interface{}))

mySessionData := strategy.JWTSession{
Expand Down Expand Up @@ -239,7 +239,7 @@ func authEndpoint(rw http.ResponseWriter, req *http.Request) {

if typeof(*selectedStrategy) == "strategy.JWTStrategy" {
// JWT
claims, _ := jwthelper.NewClaimsContext("fosite", "peter", "group0",
claims, _ := jwthelper.NewClaimsContext("fosite", "peter", "group0", "",
time.Now().Add(time.Hour), time.Now(), time.Now(), make(map[string]interface{}))

mySessionData := strategy.JWTSession{
Expand Down
2 changes: 1 addition & 1 deletion handler/core/strategy/strategy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var j = &JWTStrategy{
},
}

var claims, claimsErr = jwthelper.NewClaimsContext("fosite", "peter", "group0",
var claims, claimsErr = jwthelper.NewClaimsContext("fosite", "peter", "group0", "",
time.Now().Add(time.Hour), time.Now(), time.Now(), make(map[string]interface{}))

var r = &fosite.Request{
Expand Down

0 comments on commit bde3822

Please sign in to comment.