Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactors errors to use go 1.13 style #100

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 38 additions & 36 deletions claims.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package jwt

import (
"crypto/subtle"
"fmt"
"time"

"github.com/hashicorp/go-multierror"
)

// Claims must just have a Valid method that determines
Expand Down Expand Up @@ -49,32 +50,32 @@ type RegisteredClaims struct {
// As well, if any of the above claims are not in the token, it will still
// be considered a valid claim.
func (c RegisteredClaims) Valid() error {
vErr := new(ValidationError)
now := TimeFunc()
result := &multierror.Error{}
result.ErrorFormat = ValidationErrorFormat

now := TimeFunc()
// The claims below are optional, by default, so if they are set to the
// default value in Go, let's not fail the verification for them.
if !c.VerifyExpiresAt(now, false) {
delta := now.Sub(c.ExpiresAt.Time)
vErr.Inner = fmt.Errorf("token is expired by %v", delta)
vErr.Errors |= ValidationErrorExpired
result = multierror.Append(result, &ExpiredError{
ExpiredAt: c.ExpiresAt.Time,
AttemptedAt: now,
})
}

if !c.VerifyIssuedAt(now, false) {
vErr.Inner = fmt.Errorf("token used before issued")
vErr.Errors |= ValidationErrorIssuedAt
result = multierror.Append(result, &UsedBeforeIssuedError{
IssuedAt: c.IssuedAt.Time,
AttemptedAt: now,
})
}

if !c.VerifyNotBefore(now, false) {
vErr.Inner = fmt.Errorf("token is not valid yet")
vErr.Errors |= ValidationErrorNotValidYet
}

if vErr.valid() {
return nil
result = multierror.Append(result, &NotYetValidError{
ValidAt: c.NotBefore.Time,
AttemptedAt: now,
})
}

return vErr
return result.ErrorOrNil()
}

// VerifyAudience compares the aud claim against cmp.
Expand Down Expand Up @@ -136,32 +137,33 @@ type StandardClaims struct {
// As well, if any of the above claims are not in the token, it will still
// be considered a valid claim.
func (c StandardClaims) Valid() error {
vErr := new(ValidationError)
now := TimeFunc().Unix()
result := &multierror.Error{}
result.ErrorFormat = ValidationErrorFormat

now := TimeFunc()
nowUnix := now.Unix()
// The claims below are optional, by default, so if they are set to the
// default value in Go, let's not fail the verification for them.
if !c.VerifyExpiresAt(now, false) {
delta := time.Unix(now, 0).Sub(time.Unix(c.ExpiresAt, 0))
vErr.Inner = fmt.Errorf("token is expired by %v", delta)
vErr.Errors |= ValidationErrorExpired
}

if !c.VerifyIssuedAt(now, false) {
vErr.Inner = fmt.Errorf("token used before issued")
vErr.Errors |= ValidationErrorIssuedAt
if !c.VerifyExpiresAt(nowUnix, false) {
result = multierror.Append(result, &ExpiredError{
ExpiredAt: time.Unix(c.ExpiresAt, 0),
AttemptedAt: now,
})
}

if !c.VerifyNotBefore(now, false) {
vErr.Inner = fmt.Errorf("token is not valid yet")
vErr.Errors |= ValidationErrorNotValidYet
if !c.VerifyIssuedAt(nowUnix, false) {
result = multierror.Append(result, &UsedBeforeIssuedError{
IssuedAt: time.Unix(c.IssuedAt, 0),
AttemptedAt: now,
})
}

if vErr.valid() {
return nil
if !c.VerifyNotBefore(nowUnix, false) {
result = multierror.Append(result, &NotYetValidError{
ValidAt: time.Unix(c.NotBefore, 0),
AttemptedAt: now,
})
}

return vErr
return result.ErrorOrNil()
}

// VerifyAudience compares the aud claim against cmp.
Expand Down
12 changes: 7 additions & 5 deletions ecdsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import (
"crypto"
"crypto/ecdsa"
"crypto/rand"
"errors"
"math/big"
)

var (
// Sadly this is missing from crypto/ecdsa compared to crypto/rsa
ErrECDSAVerification = errors.New("crypto/ecdsa: verification error")
// Sadly this is missing from crypto/ecdsa compared to crypto/rsa
)

// SigningMethodECDSA implements the ECDSA family of signing methods.
Expand Down Expand Up @@ -74,7 +72,9 @@ func (m *SigningMethodECDSA) Verify(signingString, signature string, key interfa
}

if len(sig) != 2*m.KeySize {
return ErrECDSAVerification
return &SignatureVerificationError{
Algorithm: m.Name,
}
}

r := big.NewInt(0).SetBytes(sig[:m.KeySize])
Expand All @@ -92,7 +92,9 @@ func (m *SigningMethodECDSA) Verify(signingString, signature string, key interfa
return nil
}

return ErrECDSAVerification
return &SignatureVerificationError{
Algorithm: m.Name,
}
}

// Sign implements token signing for the SigningMethod.
Expand Down
8 changes: 2 additions & 6 deletions ed25519.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package jwt

import (
"errors"

"crypto"
"crypto/ed25519"
"crypto/rand"
)

var (
ErrEd25519Verification = errors.New("ed25519: verification error")
)
var ()

// SigningMethodEd25519 implements the EdDSA family.
// Expects ed25519.PrivateKey for signing and ed25519.PublicKey for verification
Expand Down Expand Up @@ -55,7 +51,7 @@ func (m *SigningMethodEd25519) Verify(signingString, signature string, key inter

// Verify the signature
if !ed25519.Verify(ed25519Key, []byte(signingString), sig) {
return ErrEd25519Verification
return &SignatureVerificationError{Algorithm: "EdDSA"}
}

return nil
Expand Down
Loading