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

252 lines
4.7 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 {
ValidMethods []string // If populated, only these methods will be considered valid
UseJSONNumber bool // Use JSON Number format in JSON decoder
SkipClaimsValidation bool // Skip claims validation during token parsing
2024-02-18 10:42:21 +00:00
}
// Parse, validate, and return a 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
// If everything is kosher, err will be nil
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
}
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
}
// 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
// This method parses the token but doesn't validate the signature. It's only
2024-02-18 10:42:21 +00:00
// 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
2024-02-18 10:42:21 +00:00
// 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
}