mirror of https://github.com/golang-jwt/jwt.git
add options to ParseFromRequest
This commit is contained in:
parent
27d85fe4a0
commit
6f4f904379
|
@ -4,6 +4,8 @@
|
||||||
|
|
||||||
* Added method `ParseUnverified` to allow users to split up the tasks of parsing and validation
|
* Added method `ParseUnverified` to allow users to split up the tasks of parsing and validation
|
||||||
* HMAC signing method returns `ErrInvalidKeyType` instead of `ErrInvalidKey` where appropriate
|
* HMAC signing method returns `ErrInvalidKeyType` instead of `ErrInvalidKey` where appropriate
|
||||||
|
* Added options to `request.ParseFromRequest`, which allows for an arbitrary list of modifiers to parsing behavior. Initial set include `WithClaims` and `WithParser`. Existing usage of this function will continue to work as before.
|
||||||
|
* Deprecated `ParseFromRequestWithClaims` to simplify API in the future.
|
||||||
|
|
||||||
#### 3.1.0
|
#### 3.1.0
|
||||||
|
|
||||||
|
|
|
@ -9,16 +9,60 @@ import (
|
||||||
// This behaves the same as Parse, but accepts a request and an extractor
|
// This behaves the same as Parse, but accepts a request and an extractor
|
||||||
// instead of a token string. The Extractor interface allows you to define
|
// instead of a token string. The Extractor interface allows you to define
|
||||||
// the logic for extracting a token. Several useful implementations are provided.
|
// the logic for extracting a token. Several useful implementations are provided.
|
||||||
func ParseFromRequest(req *http.Request, extractor Extractor, keyFunc jwt.Keyfunc) (token *jwt.Token, err error) {
|
//
|
||||||
return ParseFromRequestWithClaims(req, extractor, jwt.MapClaims{}, keyFunc)
|
// You can provide options to modify parsing behavior
|
||||||
|
func ParseFromRequest(req *http.Request, extractor Extractor, keyFunc jwt.Keyfunc, options ...ParseFromRequestOption) (token *jwt.Token, err error) {
|
||||||
|
// Create basic parser struct
|
||||||
|
p := &fromRequestParser{req, extractor, nil, nil}
|
||||||
|
|
||||||
|
// Handle options
|
||||||
|
for _, option := range options {
|
||||||
|
option(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set defaults
|
||||||
|
if p.claims == nil {
|
||||||
|
p.claims = jwt.MapClaims{}
|
||||||
|
}
|
||||||
|
if p.parser == nil {
|
||||||
|
p.parser = &jwt.Parser{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// perform extract
|
||||||
|
tokenString, err := p.extractor.ExtractToken(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// perform parse
|
||||||
|
return p.parser.ParseWithClaims(tokenString, p.claims, keyFunc)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseFromRequest but with custom Claims type
|
// ParseFromRequest but with custom Claims type
|
||||||
|
// DEPRECATED: use ParseFromRequest and the WithClaims option
|
||||||
func ParseFromRequestWithClaims(req *http.Request, extractor Extractor, claims jwt.Claims, keyFunc jwt.Keyfunc) (token *jwt.Token, err error) {
|
func ParseFromRequestWithClaims(req *http.Request, extractor Extractor, claims jwt.Claims, keyFunc jwt.Keyfunc) (token *jwt.Token, err error) {
|
||||||
// Extract token from request
|
return ParseFromRequest(req, extractor, keyFunc, WithClaims(claims))
|
||||||
if tokStr, err := extractor.ExtractToken(req); err == nil {
|
}
|
||||||
return jwt.ParseWithClaims(tokStr, claims, keyFunc)
|
|
||||||
} else {
|
type fromRequestParser struct {
|
||||||
return nil, err
|
req *http.Request
|
||||||
|
extractor Extractor
|
||||||
|
claims jwt.Claims
|
||||||
|
parser *jwt.Parser
|
||||||
|
}
|
||||||
|
|
||||||
|
type ParseFromRequestOption func(*fromRequestParser)
|
||||||
|
|
||||||
|
// Parse with custom claims
|
||||||
|
func WithClaims(claims jwt.Claims) ParseFromRequestOption {
|
||||||
|
return func(p *fromRequestParser) {
|
||||||
|
p.claims = claims
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse using a custom parser
|
||||||
|
func WithParser(parser *jwt.Parser) ParseFromRequestOption {
|
||||||
|
return func(p *fromRequestParser) {
|
||||||
|
p.parser = parser
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue