2015-10-29 21:45:16 +03:00
|
|
|
package jwt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Parser struct {
|
2021-10-13 20:36:33 +03:00
|
|
|
// If populated, only these methods will be considered valid.
|
2023-02-21 16:32:25 +03:00
|
|
|
validMethods []string
|
2021-10-13 20:36:33 +03:00
|
|
|
|
|
|
|
// Use JSON Number format in JSON decoder.
|
2023-02-21 16:32:25 +03:00
|
|
|
useJSONNumber bool
|
2021-10-13 20:36:33 +03:00
|
|
|
|
|
|
|
// Skip claims validation during token parsing.
|
2023-02-21 16:32:25 +03:00
|
|
|
skipClaimsValidation bool
|
|
|
|
|
|
|
|
validator *validator
|
2021-10-13 20:36:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewParser creates a new Parser with the specified options
|
|
|
|
func NewParser(options ...ParserOption) *Parser {
|
2023-02-21 16:32:25 +03:00
|
|
|
p := &Parser{
|
|
|
|
validator: &validator{},
|
|
|
|
}
|
2021-10-13 20:36:33 +03:00
|
|
|
|
2023-02-21 16:32:25 +03:00
|
|
|
// Loop through our parsing options and apply them
|
2021-10-13 20:36:33 +03:00
|
|
|
for _, option := range options {
|
|
|
|
option(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
2015-10-29 21:45:16 +03:00
|
|
|
}
|
|
|
|
|
2021-10-15 15:48:31 +03:00
|
|
|
// Parse parses, validates, verifies the signature and returns the parsed token.
|
2015-10-29 21:45:16 +03:00
|
|
|
// keyFunc will receive the parsed token and should return the key for validating.
|
|
|
|
func (p *Parser) Parse(tokenString string, keyFunc Keyfunc) (*Token, error) {
|
2016-04-13 02:25:25 +03:00
|
|
|
return p.ParseWithClaims(tokenString, MapClaims{}, keyFunc)
|
2015-11-16 22:59:51 +03:00
|
|
|
}
|
|
|
|
|
2022-11-08 17:43:45 +03:00
|
|
|
// ParseWithClaims parses, validates, and verifies like Parse, but supplies a default object implementing the Claims
|
|
|
|
// interface. This provides default values which can be overridden and allows a caller to use their own type, rather
|
|
|
|
// than the default MapClaims implementation of Claims.
|
|
|
|
//
|
|
|
|
// Note: If you provide a custom claim implementation that embeds one of the standard claims (such as RegisteredClaims),
|
|
|
|
// make sure that a) you either embed a non-pointer version of the claims or b) if you are using a pointer, allocate the
|
|
|
|
// proper memory for it before passing in the overall claims, otherwise you might run into a panic.
|
2016-04-13 02:25:25 +03:00
|
|
|
func (p *Parser) ParseWithClaims(tokenString string, claims Claims, keyFunc Keyfunc) (*Token, error) {
|
2016-07-21 19:37:13 +03:00
|
|
|
token, parts, err := p.ParseUnverified(tokenString, claims)
|
2016-04-13 00:32:24 +03:00
|
|
|
if err != nil {
|
2016-07-21 19:37:13 +03:00
|
|
|
return token, err
|
2015-10-29 21:45:16 +03:00
|
|
|
}
|
|
|
|
|
2015-11-03 02:22:08 +03:00
|
|
|
// Verify signing method is in the required set
|
2023-02-21 16:32:25 +03:00
|
|
|
if p.validMethods != nil {
|
2015-11-03 02:22:08 +03:00
|
|
|
var signingMethodValid = false
|
|
|
|
var alg = token.Method.Alg()
|
2023-02-21 16:32:25 +03:00
|
|
|
for _, m := range p.validMethods {
|
2015-11-03 02:22:08 +03:00
|
|
|
if m == alg {
|
|
|
|
signingMethodValid = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !signingMethodValid {
|
|
|
|
// signing method is not in the listed set
|
2023-02-21 16:32:25 +03:00
|
|
|
return token, newError(fmt.Sprintf("signing method %v is invalid", alg), ErrTokenSignatureInvalid)
|
2015-11-03 02:22:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-29 21:45:16 +03:00
|
|
|
// Lookup key
|
|
|
|
var key interface{}
|
|
|
|
if keyFunc == nil {
|
|
|
|
// keyFunc was not provided. short circuiting validation
|
2023-02-21 16:32:25 +03:00
|
|
|
return token, newError("no keyfunc was provided", ErrTokenUnverifiable)
|
2015-10-29 21:45:16 +03:00
|
|
|
}
|
|
|
|
if key, err = keyFunc(token); err != nil {
|
2023-02-21 16:32:25 +03:00
|
|
|
return token, newError("error while executing keyfunc", ErrTokenUnverifiable, err)
|
2015-10-29 21:45:16 +03:00
|
|
|
}
|
|
|
|
|
2023-02-21 16:32:25 +03:00
|
|
|
// Perform signature validation
|
|
|
|
token.Signature = parts[2]
|
|
|
|
if err = token.Method.Verify(strings.Join(parts[0:2], "."), token.Signature, key); err != nil {
|
|
|
|
return token, newError("", ErrTokenSignatureInvalid, err)
|
|
|
|
}
|
2015-11-16 22:59:51 +03:00
|
|
|
|
|
|
|
// Validate Claims
|
2023-02-21 16:32:25 +03:00
|
|
|
if !p.skipClaimsValidation {
|
|
|
|
// Make sure we have at least a default validator
|
|
|
|
if p.validator == nil {
|
|
|
|
p.validator = newValidator()
|
2015-10-29 21:45:16 +03:00
|
|
|
}
|
|
|
|
|
2023-02-21 16:32:25 +03:00
|
|
|
if err := p.validator.Validate(claims); err != nil {
|
|
|
|
return token, newError("", ErrTokenInvalidClaims, err)
|
|
|
|
}
|
2015-10-29 21:45:16 +03:00
|
|
|
}
|
|
|
|
|
2023-02-21 16:32:25 +03:00
|
|
|
// No errors so far, token is valid.
|
|
|
|
token.Valid = true
|
2015-10-29 21:45:16 +03:00
|
|
|
|
2023-02-21 16:32:25 +03:00
|
|
|
return token, nil
|
2015-10-29 21:45:16 +03:00
|
|
|
}
|
2016-07-21 19:37:13 +03:00
|
|
|
|
2021-08-03 16:51:01 +03:00
|
|
|
// ParseUnverified parses the token but doesn't validate the signature.
|
2016-07-21 19:37:13 +03:00
|
|
|
//
|
2021-08-03 16:51:01 +03:00
|
|
|
// WARNING: Don't use this method unless you know what you're doing.
|
|
|
|
//
|
|
|
|
// It's only ever useful in cases where you know the signature is valid (because it has
|
|
|
|
// been checked previously in the stack) and you want to extract values from it.
|
2016-07-21 19:37:13 +03:00
|
|
|
func (p *Parser) ParseUnverified(tokenString string, claims Claims) (token *Token, parts []string, err error) {
|
|
|
|
parts = strings.Split(tokenString, ".")
|
|
|
|
if len(parts) != 3 {
|
2023-02-21 16:32:25 +03:00
|
|
|
return nil, parts, newError("token contains an invalid number of segments", ErrTokenMalformed)
|
2016-07-21 19:37:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
token = &Token{Raw: tokenString}
|
|
|
|
|
|
|
|
// parse Header
|
|
|
|
var headerBytes []byte
|
|
|
|
if headerBytes, err = DecodeSegment(parts[0]); err != nil {
|
|
|
|
if strings.HasPrefix(strings.ToLower(tokenString), "bearer ") {
|
2023-02-21 16:32:25 +03:00
|
|
|
return token, parts, newError("tokenstring should not contain 'bearer '", ErrTokenMalformed)
|
2016-07-21 19:37:13 +03:00
|
|
|
}
|
2023-02-21 16:32:25 +03:00
|
|
|
return token, parts, newError("could not base64 decode header", ErrTokenMalformed, err)
|
2016-07-21 19:37:13 +03:00
|
|
|
}
|
|
|
|
if err = json.Unmarshal(headerBytes, &token.Header); err != nil {
|
2023-02-21 16:32:25 +03:00
|
|
|
return token, parts, newError("could not JSON decode header", ErrTokenMalformed, err)
|
2016-07-21 19:37:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// parse Claims
|
|
|
|
var claimBytes []byte
|
|
|
|
token.Claims = claims
|
|
|
|
|
|
|
|
if claimBytes, err = DecodeSegment(parts[1]); err != nil {
|
2023-02-21 16:32:25 +03:00
|
|
|
return token, parts, newError("could not base64 decode claim", ErrTokenMalformed, err)
|
2016-07-21 19:37:13 +03:00
|
|
|
}
|
|
|
|
dec := json.NewDecoder(bytes.NewBuffer(claimBytes))
|
2023-02-21 16:32:25 +03:00
|
|
|
if p.useJSONNumber {
|
2016-07-21 19:37:13 +03:00
|
|
|
dec.UseNumber()
|
|
|
|
}
|
|
|
|
// JSON Decode. Special case for map type to avoid weird pointer behavior
|
|
|
|
if c, ok := token.Claims.(MapClaims); ok {
|
|
|
|
err = dec.Decode(&c)
|
|
|
|
} else {
|
|
|
|
err = dec.Decode(&claims)
|
|
|
|
}
|
|
|
|
// Handle decode error
|
|
|
|
if err != nil {
|
2023-02-21 16:32:25 +03:00
|
|
|
return token, parts, newError("could not JSON decode claim", ErrTokenMalformed, err)
|
2016-07-21 19:37:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup signature method
|
|
|
|
if method, ok := token.Header["alg"].(string); ok {
|
|
|
|
if token.Method = GetSigningMethod(method); token.Method == nil {
|
2023-02-21 16:32:25 +03:00
|
|
|
return token, parts, newError("signing method (alg) is unavailable", ErrTokenUnverifiable)
|
2016-07-21 19:37:13 +03:00
|
|
|
}
|
|
|
|
} else {
|
2023-02-21 16:32:25 +03:00
|
|
|
return token, parts, newError("signing method (alg) is unspecified", ErrTokenUnverifiable)
|
2016-07-21 19:37:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return token, parts, nil
|
|
|
|
}
|