summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/dgrijalva/jwt-go/request/extractor.go
diff options
context:
space:
mode:
authorWim <wim@42.be>2017-02-18 23:00:46 +0100
committerWim <wim@42.be>2017-02-18 23:11:48 +0100
commit930b639cc9cd2d2873302f30303378c0e53816a8 (patch)
tree8cd3f1d464fb5d4e5607fe16255c35a31a9d8b62 /vendor/github.com/dgrijalva/jwt-go/request/extractor.go
parent58483ea70c2c99a352592c5e50686fb03985650e (diff)
downloadmatterbridge-msglm-930b639cc9cd2d2873302f30303378c0e53816a8.tar.gz
matterbridge-msglm-930b639cc9cd2d2873302f30303378c0e53816a8.tar.bz2
matterbridge-msglm-930b639cc9cd2d2873302f30303378c0e53816a8.zip
Update vendor
Diffstat (limited to 'vendor/github.com/dgrijalva/jwt-go/request/extractor.go')
-rw-r--r--vendor/github.com/dgrijalva/jwt-go/request/extractor.go81
1 files changed, 81 insertions, 0 deletions
diff --git a/vendor/github.com/dgrijalva/jwt-go/request/extractor.go b/vendor/github.com/dgrijalva/jwt-go/request/extractor.go
new file mode 100644
index 00000000..14414fe2
--- /dev/null
+++ b/vendor/github.com/dgrijalva/jwt-go/request/extractor.go
@@ -0,0 +1,81 @@
+package request
+
+import (
+ "errors"
+ "net/http"
+)
+
+// Errors
+var (
+ ErrNoTokenInRequest = errors.New("no token present in request")
+)
+
+// 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 Extractor interface {
+ ExtractToken(*http.Request) (string, error)
+}
+
+// Extractor for finding a token in a header. Looks at each specified
+// header in order until there's a match
+type HeaderExtractor []string
+
+func (e HeaderExtractor) ExtractToken(req *http.Request) (string, error) {
+ // loop over header names and return the first one that contains data
+ for _, header := range e {
+ if ah := req.Header.Get(header); ah != "" {
+ return ah, nil
+ }
+ }
+ return "", ErrNoTokenInRequest
+}
+
+// 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
+type ArgumentExtractor []string
+
+func (e ArgumentExtractor) ExtractToken(req *http.Request) (string, error) {
+ // Make sure form is parsed
+ req.ParseMultipartForm(10e6)
+
+ // loop over arg names and return the first one that contains data
+ for _, arg := range e {
+ if ah := req.Form.Get(arg); ah != "" {
+ return ah, nil
+ }
+ }
+
+ return "", ErrNoTokenInRequest
+}
+
+// Tries Extractors in order until one returns a token string or an error occurs
+type MultiExtractor []Extractor
+
+func (e MultiExtractor) ExtractToken(req *http.Request) (string, error) {
+ // loop over header names and return the first one that contains data
+ for _, extractor := range e {
+ if tok, err := extractor.ExtractToken(req); tok != "" {
+ return tok, nil
+ } else if err != ErrNoTokenInRequest {
+ return "", err
+ }
+ }
+ return "", ErrNoTokenInRequest
+}
+
+// Wrap an Extractor in this to post-process the value before it's handed off.
+// See AuthorizationHeaderExtractor for an example
+type PostExtractionFilter struct {
+ Extractor
+ Filter func(string) (string, error)
+}
+
+func (e *PostExtractionFilter) ExtractToken(req *http.Request) (string, error) {
+ if tok, err := e.Extractor.ExtractToken(req); tok != "" {
+ return e.Filter(tok)
+ } else {
+ return "", err
+ }
+}