Skip to content

Commit

Permalink
fix #8
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Mar 9, 2022
1 parent da1ee79 commit 5ed1b49
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
21 changes: 17 additions & 4 deletions claims.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package jwt
import (
"encoding/json"
"errors"
"fmt"
"time"
)

Expand Down Expand Up @@ -62,8 +63,8 @@ type claimsSecondChance struct {
IssuedAt json.Number `json:"iat,omitempty"`
Expiry json.Number `json:"exp,omitempty"`
ID string `json:"jti,omitempty"`
Issuer string `json:"iss,omitempty"`
Subject string `json:"sub,omitempty"`
Issuer interface{} `json:"iss,omitempty"`
Subject interface{} `json:"sub,omitempty"`
Audience Audience `json:"aud,omitempty"`
}

Expand All @@ -77,12 +78,24 @@ func (c claimsSecondChance) toClaims() Claims {
IssuedAt: int64(iat),
Expiry: int64(exp),
ID: c.ID,
Issuer: c.Issuer,
Subject: c.Subject,
Issuer: getStr(c.Issuer),
Subject: getStr(c.Subject),
Audience: c.Audience,
}
}

func getStr(v interface{}) string {
if v == nil {
return ""
}

if s, ok := v.(string); ok {
return s
} else {
return fmt.Sprintf("%v", v)
}
}

// Audience represents the "aud" standard JWT claim.
// See the `Claims` structure for details.
type Audience []string
Expand Down
15 changes: 15 additions & 0 deletions claims_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,18 @@ func TestMaxAgeMap(t *testing.T) {
// test no panic if nil.
MaxAgeMap(maxAge, nil)
}

func TestClaimsSubAsInt(t *testing.T) {
secret := "secret"
token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOjEyMywibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.QzFnWiase0tPyeNzn8ecl-kVfDVEZ1ctbf9ztM0Qjqg"

verifiedToken, err := Verify(HS256, []byte(secret), []byte(token))
if err != nil {
t.Fatal(err)
}

expectedClaims := Claims{NotBefore: 0, IssuedAt: 1516239022, Expiry: 0, ID: "", Issuer: "", Subject: "123", Audience: nil}
if !reflect.DeepEqual(verifiedToken.StandardClaims, expectedClaims) {
t.Fatalf("expected: %#+v but got: %#+v\n", expectedClaims, verifiedToken.StandardClaims)
}
}

0 comments on commit 5ed1b49

Please sign in to comment.