package request
import "github.com/dgrijalva/jwt-go/request"
Utility package for extracting JWT tokens from HTTP requests.
The main function is ParseFromRequest and it's WithClaims variant. See examples for how to use the various Extractor implementations or roll your own.
Index ¶
- Variables
- func ParseFromRequest(req *http.Request, extractor Extractor, 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)
- type ArgumentExtractor
- type Extractor
- type HeaderExtractor
- type MultiExtractor
- type PostExtractionFilter
Examples ¶
Variables ¶
var AuthorizationHeaderExtractor = &PostExtractionFilter{ HeaderExtractor{"Authorization"}, stripBearerPrefixFromTokenString, }
Extract bearer token from Authorization header Uses PostExtractionFilter to strip "Bearer " prefix from header
Errors
var OAuth2Extractor = &MultiExtractor{ AuthorizationHeaderExtractor, ArgumentExtractor{"access_token"}, }
Extractor for OAuth2 access tokens. Looks in 'Authorization' header then 'access_token' argument for a token.
Functions ¶
func ParseFromRequest ¶
func ParseFromRequest(req *http.Request, extractor Extractor, keyFunc jwt.Keyfunc) (token *jwt.Token, err error)
Extract and parse a JWT token from an HTTP request. 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 the logic for extracting a token. Several useful implementations are provided.
func ParseFromRequestWithClaims ¶
func ParseFromRequestWithClaims(req *http.Request, extractor Extractor, claims jwt.Claims, keyFunc jwt.Keyfunc) (token *jwt.Token, err error)
ParseFromRequest but with custom Claims type
Types ¶
type ArgumentExtractor ¶
type ArgumentExtractor []string
Extract token from request arguments. This includes a POSTed form or
GET URL arguments. Argument names are tried in order until there's a match.
This extractor calls `ParseMultipartForm` on the request
Code:
Output:Example¶
{
req := makeExampleRequest("GET", "/", nil, url.Values{"token": {extractorTestTokenA}})
tokenString, err := ArgumentExtractor{"token"}.ExtractToken(req)
if err == nil {
fmt.Println(tokenString)
} else {
fmt.Println(err)
}
//Output: A
}
A
func (ArgumentExtractor) ExtractToken ¶
func (e ArgumentExtractor) ExtractToken(req *http.Request) (string, error)
type Extractor ¶
Interface for extracting a token from an HTTP request. The ExtractToken method should return a token string or an error. If no token is present, you must return ErrNoTokenInRequest.
type HeaderExtractor ¶
type HeaderExtractor []string
Extractor for finding a token in a header. Looks at each specified
header in order until there's a match
Code:
Output:Example¶
{
req := makeExampleRequest("GET", "/", map[string]string{"Token": exampleTokenA}, nil)
tokenString, err := HeaderExtractor{"Token"}.ExtractToken(req)
if err == nil {
fmt.Println(tokenString)
} else {
fmt.Println(err)
}
//Output: A
}
A
func (HeaderExtractor) ExtractToken ¶
func (e HeaderExtractor) ExtractToken(req *http.Request) (string, error)
type MultiExtractor ¶
type MultiExtractor []Extractor
Tries Extractors in order until one returns a token string or an error occurs
func (MultiExtractor) ExtractToken ¶
func (e MultiExtractor) ExtractToken(req *http.Request) (string, error)
type PostExtractionFilter ¶
Wrap an Extractor in this to post-process the value before it's handed off. See AuthorizationHeaderExtractor for an example
func (*PostExtractionFilter) ExtractToken ¶
func (e *PostExtractionFilter) ExtractToken(req *http.Request) (string, error)
Source Files ¶
doc.go extractor.go oauth2.go request.go
- Version
- v3.0.0+incompatible
- Published
- Jun 16, 2016
- Platform
- windows/amd64
- Imports
- 4 packages
- Last checked
- 9 hours ago –
Tools for package owners.