forked from mirror/jwt
move error types and constants out to separate file
This commit is contained in:
parent
c48cfd5d97
commit
3dd0a21a31
|
@ -0,0 +1,43 @@
|
||||||
|
package jwt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Error constants
|
||||||
|
var (
|
||||||
|
ErrInvalidKey = errors.New("key is invalid or of invalid type")
|
||||||
|
ErrHashUnavailable = errors.New("the requested hash function is unavailable")
|
||||||
|
ErrNoTokenInRequest = errors.New("no token present in request")
|
||||||
|
)
|
||||||
|
|
||||||
|
// The errors that might occur when parsing and validating a token
|
||||||
|
const (
|
||||||
|
ValidationErrorMalformed uint32 = 1 << iota // Token is malformed
|
||||||
|
ValidationErrorUnverifiable // Token could not be verified because of signing problems
|
||||||
|
ValidationErrorSignatureInvalid // Signature validation failed
|
||||||
|
ValidationErrorExpired // Exp validation failed
|
||||||
|
ValidationErrorNotValidYet // NBF validation failed
|
||||||
|
)
|
||||||
|
|
||||||
|
// The error from Parse if token is not valid
|
||||||
|
type ValidationError struct {
|
||||||
|
err string
|
||||||
|
Errors uint32 // bitfield. see ValidationError... constants
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validation error is an error type
|
||||||
|
func (e ValidationError) Error() string {
|
||||||
|
if e.err == "" {
|
||||||
|
return "token is invalid"
|
||||||
|
}
|
||||||
|
return e.err
|
||||||
|
}
|
||||||
|
|
||||||
|
// No errors
|
||||||
|
func (e *ValidationError) valid() bool {
|
||||||
|
if e.Errors > 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
39
jwt.go
39
jwt.go
|
@ -3,7 +3,6 @@ package jwt
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -20,13 +19,6 @@ var TimeFunc = time.Now
|
||||||
// Header of the token (such as `kid`) to identify which key to use.
|
// Header of the token (such as `kid`) to identify which key to use.
|
||||||
type Keyfunc func(*Token) (interface{}, error)
|
type Keyfunc func(*Token) (interface{}, error)
|
||||||
|
|
||||||
// Error constants
|
|
||||||
var (
|
|
||||||
ErrInvalidKey = errors.New("key is invalid or of invalid type")
|
|
||||||
ErrHashUnavailable = errors.New("the requested hash function is unavailable")
|
|
||||||
ErrNoTokenInRequest = errors.New("no token present in request")
|
|
||||||
)
|
|
||||||
|
|
||||||
// A JWT Token. Different fields will be used depending on whether you're
|
// A JWT Token. Different fields will be used depending on whether you're
|
||||||
// creating or parsing/verifying a token.
|
// creating or parsing/verifying a token.
|
||||||
type Token struct {
|
type Token struct {
|
||||||
|
@ -167,37 +159,6 @@ func Parse(tokenString string, keyFunc Keyfunc) (*Token, error) {
|
||||||
return token, vErr
|
return token, vErr
|
||||||
}
|
}
|
||||||
|
|
||||||
// The errors that might occur when parsing and validating a token
|
|
||||||
const (
|
|
||||||
ValidationErrorMalformed uint32 = 1 << iota // Token is malformed
|
|
||||||
ValidationErrorUnverifiable // Token could not be verified because of signing problems
|
|
||||||
ValidationErrorSignatureInvalid // Signature validation failed
|
|
||||||
ValidationErrorExpired // Exp validation failed
|
|
||||||
ValidationErrorNotValidYet // NBF validation failed
|
|
||||||
)
|
|
||||||
|
|
||||||
// The error from Parse if token is not valid
|
|
||||||
type ValidationError struct {
|
|
||||||
err string
|
|
||||||
Errors uint32 // bitfield. see ValidationError... constants
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validation error is an error type
|
|
||||||
func (e ValidationError) Error() string {
|
|
||||||
if e.err == "" {
|
|
||||||
return "token is invalid"
|
|
||||||
}
|
|
||||||
return e.err
|
|
||||||
}
|
|
||||||
|
|
||||||
// No errors
|
|
||||||
func (e *ValidationError) valid() bool {
|
|
||||||
if e.Errors > 0 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// Try to find the token in an http.Request.
|
// Try to find the token in an http.Request.
|
||||||
// This method will call ParseMultipartForm if there's no token in the header.
|
// This method will call ParseMultipartForm if there's no token in the header.
|
||||||
// Currently, it looks in the Authorization header as well as
|
// Currently, it looks in the Authorization header as well as
|
||||||
|
|
Loading…
Reference in New Issue