From 80b3b62044e7bb95e1f096fa2dfc0cad85fd10b6 Mon Sep 17 00:00:00 2001 From: Oleg Kovalov Date: Fri, 8 May 2020 00:30:53 +0200 Subject: [PATCH] v2 release changes (#43) --- algo.go | 2 -- audience.go | 2 +- audience_test.go | 1 - claims.go | 16 ++++++++-------- errors.go | 3 +++ jwt.go | 18 +++++++++--------- numeric_date.go | 4 ++-- numeric_date_test.go | 1 - 8 files changed, 23 insertions(+), 24 deletions(-) diff --git a/algo.go b/algo.go index c865421..6d981d3 100644 --- a/algo.go +++ b/algo.go @@ -22,8 +22,6 @@ type Algorithm string // Algorithm names for signing and verifying. const ( - NoEncryption Algorithm = "none" - EdDSA Algorithm = "EdDSA" HS256 Algorithm = "HS256" diff --git a/audience.go b/audience.go index 0c24a65..6b311a0 100644 --- a/audience.go +++ b/audience.go @@ -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) { diff --git a/audience_test.go b/audience_test.go index 722be72..3eb853d 100644 --- a/audience_test.go +++ b/audience_test.go @@ -60,7 +60,6 @@ func TestAudienceUnmarshalMalformed(t *testing.T) { if err == nil { t.Error("want err") } - } f(``) diff --git a/claims.go b/claims.go index 528012b..fbf1134 100644 --- a/claims.go +++ b/claims.go @@ -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 @@ -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) } diff --git a/errors.go b/errors.go index d5ba91a..971e39e 100644 --- a/errors.go +++ b/errors.go @@ -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") diff --git a/jwt.go b/jwt.go index b7fb75d..0b34fba 100644 --- a/jwt.go +++ b/jwt.go @@ -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 `.`). -func (t Token) SecureString() string { +func (t *Token) SecureString() string { dot := bytes.LastIndexByte(t.raw, '.') return string(t.raw[:dot]) + `.` } // 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 } @@ -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)) diff --git a/numeric_date.go b/numeric_date.go index 28882bd..c358e05 100644 --- a/numeric_date.go +++ b/numeric_date.go @@ -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)) diff --git a/numeric_date_test.go b/numeric_date_test.go index 0397fe4..55941cc 100644 --- a/numeric_date_test.go +++ b/numeric_date_test.go @@ -56,7 +56,6 @@ func TestNumericDateUnmarshalMalformed(t *testing.T) { if err == nil { t.Error("want err") } - } f(``)