2016-01-15 01:09:27 +03:00
|
|
|
package request
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/dgrijalva/jwt-go"
|
|
|
|
"net/http"
|
2016-04-08 23:58:29 +03:00
|
|
|
)
|
|
|
|
|
2016-01-15 01:09:27 +03:00
|
|
|
// Try to find the token in an http.Request.
|
|
|
|
// This method will call ParseMultipartForm if there's no token in the header.
|
|
|
|
// Currently, it looks in the Authorization header as well as
|
|
|
|
// looking for an 'access_token' request parameter in req.Form.
|
2016-06-07 01:27:44 +03:00
|
|
|
func ParseFromRequest(req *http.Request, extractor Extractor, keyFunc jwt.Keyfunc) (token *jwt.Token, err error) {
|
|
|
|
return ParseFromRequestWithClaims(req, extractor, jwt.MapClaims{}, keyFunc)
|
2016-04-08 21:57:11 +03:00
|
|
|
}
|
2016-01-15 01:09:27 +03:00
|
|
|
|
2016-06-07 01:27:44 +03:00
|
|
|
func ParseFromRequestWithClaims(req *http.Request, extractor Extractor, claims jwt.Claims, keyFunc jwt.Keyfunc) (token *jwt.Token, err error) {
|
|
|
|
// Extract token from request
|
|
|
|
tokStr, err := extractor.ExtractToken(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-01-15 01:09:27 +03:00
|
|
|
}
|
|
|
|
|
2016-06-07 01:27:44 +03:00
|
|
|
return jwt.ParseWithClaims(tokStr, claims, keyFunc)
|
2016-01-15 01:09:27 +03:00
|
|
|
}
|