Skip to content

Commit

Permalink
v2 release changes (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg authored May 7, 2020
1 parent 77ee493 commit 80b3b62
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 24 deletions.
2 changes: 0 additions & 2 deletions algo.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ type Algorithm string

// Algorithm names for signing and verifying.
const (
NoEncryption Algorithm = "none"

EdDSA Algorithm = "EdDSA"

HS256 Algorithm = "HS256"
Expand Down
2 changes: 1 addition & 1 deletion audience.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (a Audience) MarshalJSON() ([]byte, error) {
func (a *Audience) UnmarshalJSON(b []byte) error {
var v interface{}
if err := json.Unmarshal(b, &v); err != nil {
return err
return ErrAudienceInvalidFormat
}

switch v := v.(type) {
Expand Down
1 change: 0 additions & 1 deletion audience_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ func TestAudienceUnmarshalMalformed(t *testing.T) {
if err == nil {
t.Error("want err")
}

}

f(``)
Expand Down
16 changes: 8 additions & 8 deletions claims.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type StandardClaims struct {
}

// IsForAudience reports whether token has a given audience.
func (sc StandardClaims) IsForAudience(audience string) bool {
func (sc *StandardClaims) IsForAudience(audience string) bool {
for _, aud := range sc.Audience {
if constTimeEqual(aud, audience) {
return true
Expand All @@ -48,37 +48,37 @@ func (sc StandardClaims) IsForAudience(audience string) bool {
}

// IsIssuer reports whether token has a given issuer.
func (sc StandardClaims) IsIssuer(issuer string) bool {
func (sc *StandardClaims) IsIssuer(issuer string) bool {
return constTimeEqual(sc.Issuer, issuer)
}

// IsSubject reports whether token has a given subject.
func (sc StandardClaims) IsSubject(subject string) bool {
func (sc *StandardClaims) IsSubject(subject string) bool {
return constTimeEqual(sc.Subject, subject)
}

// IsID reports whether token has a given id.
func (sc StandardClaims) IsID(id string) bool {
func (sc *StandardClaims) IsID(id string) bool {
return constTimeEqual(sc.ID, id)
}

// IsValidExpiresAt reports whether a token isn't expired at a given time.
func (sc StandardClaims) IsValidExpiresAt(now time.Time) bool {
func (sc *StandardClaims) IsValidExpiresAt(now time.Time) bool {
return sc.ExpiresAt == nil || sc.ExpiresAt.After(now)
}

// IsValidNotBefore reports whether a token isn't used before a given time.
func (sc StandardClaims) IsValidNotBefore(now time.Time) bool {
func (sc *StandardClaims) IsValidNotBefore(now time.Time) bool {
return sc.NotBefore == nil || sc.NotBefore.Before(now)
}

// IsValidIssuedAt reports whether a token was created before a given time.
func (sc StandardClaims) IsValidIssuedAt(now time.Time) bool {
func (sc *StandardClaims) IsValidIssuedAt(now time.Time) bool {
return sc.IssuedAt == nil || sc.IssuedAt.Before(now)
}

// IsValidAt reports whether a token is valid at a given time.
func (sc StandardClaims) IsValidAt(now time.Time) bool {
func (sc *StandardClaims) IsValidAt(now time.Time) bool {
return sc.IsValidExpiresAt(now) && sc.IsValidNotBefore(now) && sc.IsValidIssuedAt(now)
}

Expand Down
3 changes: 3 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ const (
// ErrAudienceInvalidFormat indicates that audience format is not valid.
ErrAudienceInvalidFormat = Error("jwt: audience format is not valid")

// ErrDateInvalidFormat indicates that date format is not valid.
ErrDateInvalidFormat = Error("jwt: date is not valid")

// ErrAlgorithmMismatch indicates that token is signed by another algorithm.
ErrAlgorithmMismatch = Error("jwt: token is signed by another algorithm")

Expand Down
18 changes: 9 additions & 9 deletions jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,44 @@ type Token struct {
claims json.RawMessage
}

func (t Token) String() string {
func (t *Token) String() string {
return string(t.raw)
}

// SecureString returns token without a signature (replaced with `.<signature>`).
func (t Token) SecureString() string {
func (t *Token) SecureString() string {
dot := bytes.LastIndexByte(t.raw, '.')
return string(t.raw[:dot]) + `.<signature>`
}

// Raw returns token's raw bytes.
func (t Token) Raw() []byte {
func (t *Token) Raw() []byte {
return t.raw
}

// Header returns token's header.
func (t Token) Header() Header {
func (t *Token) Header() Header {
return t.header
}

// RawHeader returns token's header raw bytes.
func (t Token) RawHeader() []byte {
func (t *Token) RawHeader() []byte {
dot := bytes.IndexByte(t.raw, '.')
return t.raw[:dot]
}

// RawClaims returns token's claims as a raw bytes.
func (t Token) RawClaims() []byte {
func (t *Token) RawClaims() []byte {
return t.claims
}

// Payload returns token's payload.
func (t Token) Payload() []byte {
func (t *Token) Payload() []byte {
return t.payload
}

// Signature returns token's signature.
func (t Token) Signature() []byte {
func (t *Token) Signature() []byte {
return t.signature
}

Expand All @@ -67,7 +67,7 @@ type Header struct {
}

// MarshalJSON implements the json.Marshaler interface.
func (h Header) MarshalJSON() (data []byte, err error) {
func (h *Header) MarshalJSON() (data []byte, err error) {
buf := bytes.Buffer{}
buf.WriteString(`{"alg":"`)
buf.WriteString(string(h.Algorithm))
Expand Down
4 changes: 2 additions & 2 deletions numeric_date.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ func (t NumericDate) MarshalJSON() ([]byte, error) {
func (t *NumericDate) UnmarshalJSON(data []byte) error {
var value json.Number
if err := json.Unmarshal(data, &value); err != nil {
return err
return ErrDateInvalidFormat
}
f, err := value.Float64()
if err != nil {
return err
return ErrDateInvalidFormat
}
sec, dec := math.Modf(f)
ts := time.Unix(int64(sec), int64(dec*1e9))
Expand Down
1 change: 0 additions & 1 deletion numeric_date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ func TestNumericDateUnmarshalMalformed(t *testing.T) {
if err == nil {
t.Error("want err")
}

}

f(``)
Expand Down

0 comments on commit 80b3b62

Please sign in to comment.