forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/golang-jwt/jwt/v4/parser.go

300 lines
6.0 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
package jwt
import (
"bytes"
"encoding/json"
"fmt"
"strings"
)
type Parser struct {
2024-02-18 10:42:21 +00:00
// If populated, only these methods will be considered valid.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Deprecated: In future releases, this field will not be exported anymore and should be set with an option to NewParser instead.
2024-02-18 10:42:21 +00:00
ValidMethods []string
// Use JSON Number format in JSON decoder.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Deprecated: In future releases, this field will not be exported anymore and should be set with an option to NewParser instead.
2024-02-18 10:42:21 +00:00
UseJSONNumber bool
// Skip claims validation during token parsing.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Deprecated: In future releases, this field will not be exported anymore and should be set with an option to NewParser instead.
2024-02-18 10:42:21 +00:00
SkipClaimsValidation bool
}
// NewParser creates a new Parser with the specified options
2024-02-18 10:42:21 +00:00
func NewParser(options ...ParserOption) *Parser {
2024-02-18 10:42:21 +00:00
p := &Parser{}
// loop through our parsing options and apply them
2024-02-18 10:42:21 +00:00
for _, option := range options {
2024-02-18 10:42:21 +00:00
option(p)
2024-02-18 10:42:21 +00:00
}
return p
2024-02-18 10:42:21 +00:00
}
// Parse parses, validates, verifies the signature and returns the parsed token.
2024-02-18 10:42:21 +00:00
// keyFunc will receive the parsed token and should return the key for validating.
2024-02-18 10:42:21 +00:00
func (p *Parser) Parse(tokenString string, keyFunc Keyfunc) (*Token, error) {
2024-02-18 10:42:21 +00:00
return p.ParseWithClaims(tokenString, MapClaims{}, keyFunc)
2024-02-18 10:42:21 +00:00
}
// ParseWithClaims parses, validates, and verifies like Parse, but supplies a default object implementing the Claims
2024-02-18 10:42:21 +00:00
// interface. This provides default values which can be overridden and allows a caller to use their own type, rather
2024-02-18 10:42:21 +00:00
// than the default MapClaims implementation of Claims.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Note: If you provide a custom claim implementation that embeds one of the standard claims (such as RegisteredClaims),
2024-02-18 10:42:21 +00:00
// make sure that a) you either embed a non-pointer version of the claims or b) if you are using a pointer, allocate the
2024-02-18 10:42:21 +00:00
// proper memory for it before passing in the overall claims, otherwise you might run into a panic.
2024-02-18 10:42:21 +00:00
func (p *Parser) ParseWithClaims(tokenString string, claims Claims, keyFunc Keyfunc) (*Token, error) {
2024-02-18 10:42:21 +00:00
token, parts, err := p.ParseUnverified(tokenString, claims)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return token, err
2024-02-18 10:42:21 +00:00
}
// Verify signing method is in the required set
2024-02-18 10:42:21 +00:00
if p.ValidMethods != nil {
2024-02-18 10:42:21 +00:00
var signingMethodValid = false
2024-02-18 10:42:21 +00:00
var alg = token.Method.Alg()
2024-02-18 10:42:21 +00:00
for _, m := range p.ValidMethods {
2024-02-18 10:42:21 +00:00
if m == alg {
2024-02-18 10:42:21 +00:00
signingMethodValid = true
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !signingMethodValid {
2024-02-18 10:42:21 +00:00
// signing method is not in the listed set
2024-02-18 10:42:21 +00:00
return token, NewValidationError(fmt.Sprintf("signing method %v is invalid", alg), ValidationErrorSignatureInvalid)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Lookup key
2024-02-18 10:42:21 +00:00
var key interface{}
2024-02-18 10:42:21 +00:00
if keyFunc == nil {
2024-02-18 10:42:21 +00:00
// keyFunc was not provided. short circuiting validation
2024-02-18 10:42:21 +00:00
return token, NewValidationError("no Keyfunc was provided.", ValidationErrorUnverifiable)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if key, err = keyFunc(token); err != nil {
2024-02-18 10:42:21 +00:00
// keyFunc returned an error
2024-02-18 10:42:21 +00:00
if ve, ok := err.(*ValidationError); ok {
2024-02-18 10:42:21 +00:00
return token, ve
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return token, &ValidationError{Inner: err, Errors: ValidationErrorUnverifiable}
2024-02-18 10:42:21 +00:00
}
vErr := &ValidationError{}
// Validate Claims
2024-02-18 10:42:21 +00:00
if !p.SkipClaimsValidation {
2024-02-18 10:42:21 +00:00
if err := token.Claims.Valid(); err != nil {
// If the Claims Valid returned an error, check if it is a validation error,
2024-02-18 10:42:21 +00:00
// If it was another error type, create a ValidationError with a generic ClaimsInvalid flag set
2024-02-18 10:42:21 +00:00
if e, ok := err.(*ValidationError); !ok {
2024-02-18 10:42:21 +00:00
vErr = &ValidationError{Inner: err, Errors: ValidationErrorClaimsInvalid}
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
vErr = e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Perform validation
2024-02-18 10:42:21 +00:00
token.Signature = parts[2]
2024-02-18 10:42:21 +00:00
if err = token.Method.Verify(strings.Join(parts[0:2], "."), token.Signature, key); err != nil {
2024-02-18 10:42:21 +00:00
vErr.Inner = err
2024-02-18 10:42:21 +00:00
vErr.Errors |= ValidationErrorSignatureInvalid
2024-02-18 10:42:21 +00:00
}
if vErr.valid() {
2024-02-18 10:42:21 +00:00
token.Valid = true
2024-02-18 10:42:21 +00:00
return token, nil
2024-02-18 10:42:21 +00:00
}
return token, vErr
2024-02-18 10:42:21 +00:00
}
// ParseUnverified parses the token but doesn't validate the signature.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// WARNING: Don't use this method unless you know what you're doing.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// It's only ever useful in cases where you know the signature is valid (because it has
2024-02-18 10:42:21 +00:00
// been checked previously in the stack) and you want to extract values from it.
2024-02-18 10:42:21 +00:00
func (p *Parser) ParseUnverified(tokenString string, claims Claims) (token *Token, parts []string, err error) {
2024-02-18 10:42:21 +00:00
parts = strings.Split(tokenString, ".")
2024-02-18 10:42:21 +00:00
if len(parts) != 3 {
2024-02-18 10:42:21 +00:00
return nil, parts, NewValidationError("token contains an invalid number of segments", ValidationErrorMalformed)
2024-02-18 10:42:21 +00:00
}
token = &Token{Raw: tokenString}
// parse Header
2024-02-18 10:42:21 +00:00
var headerBytes []byte
2024-02-18 10:42:21 +00:00
if headerBytes, err = DecodeSegment(parts[0]); err != nil {
2024-02-18 10:42:21 +00:00
if strings.HasPrefix(strings.ToLower(tokenString), "bearer ") {
2024-02-18 10:42:21 +00:00
return token, parts, NewValidationError("tokenstring should not contain 'bearer '", ValidationErrorMalformed)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return token, parts, &ValidationError{Inner: err, Errors: ValidationErrorMalformed}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if err = json.Unmarshal(headerBytes, &token.Header); err != nil {
2024-02-18 10:42:21 +00:00
return token, parts, &ValidationError{Inner: err, Errors: ValidationErrorMalformed}
2024-02-18 10:42:21 +00:00
}
// parse Claims
2024-02-18 10:42:21 +00:00
var claimBytes []byte
2024-02-18 10:42:21 +00:00
token.Claims = claims
if claimBytes, err = DecodeSegment(parts[1]); err != nil {
2024-02-18 10:42:21 +00:00
return token, parts, &ValidationError{Inner: err, Errors: ValidationErrorMalformed}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
dec := json.NewDecoder(bytes.NewBuffer(claimBytes))
2024-02-18 10:42:21 +00:00
if p.UseJSONNumber {
2024-02-18 10:42:21 +00:00
dec.UseNumber()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// JSON Decode. Special case for map type to avoid weird pointer behavior
2024-02-18 10:42:21 +00:00
if c, ok := token.Claims.(MapClaims); ok {
2024-02-18 10:42:21 +00:00
err = dec.Decode(&c)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
err = dec.Decode(&claims)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// Handle decode error
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return token, parts, &ValidationError{Inner: err, Errors: ValidationErrorMalformed}
2024-02-18 10:42:21 +00:00
}
// Lookup signature method
2024-02-18 10:42:21 +00:00
if method, ok := token.Header["alg"].(string); ok {
2024-02-18 10:42:21 +00:00
if token.Method = GetSigningMethod(method); token.Method == nil {
2024-02-18 10:42:21 +00:00
return token, parts, NewValidationError("signing method (alg) is unavailable.", ValidationErrorUnverifiable)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
return token, parts, NewValidationError("signing method (alg) is unspecified.", ValidationErrorUnverifiable)
2024-02-18 10:42:21 +00:00
}
return token, parts, nil
2024-02-18 10:42:21 +00:00
}