...

Source file src/github.com/golang-jwt/jwt/request/extractor.go

Documentation: github.com/golang-jwt/jwt/request

     1  package request
     2  
     3  import (
     4  	"errors"
     5  	"net/http"
     6  )
     7  
     8  // Errors
     9  var (
    10  	ErrNoTokenInRequest = errors.New("no token present in request")
    11  )
    12  
    13  // Interface for extracting a token from an HTTP request.
    14  // The ExtractToken method should return a token string or an error.
    15  // If no token is present, you must return ErrNoTokenInRequest.
    16  type Extractor interface {
    17  	ExtractToken(*http.Request) (string, error)
    18  }
    19  
    20  // Extractor for finding a token in a header.  Looks at each specified
    21  // header in order until there's a match
    22  type HeaderExtractor []string
    23  
    24  func (e HeaderExtractor) ExtractToken(req *http.Request) (string, error) {
    25  	// loop over header names and return the first one that contains data
    26  	for _, header := range e {
    27  		if ah := req.Header.Get(header); ah != "" {
    28  			return ah, nil
    29  		}
    30  	}
    31  	return "", ErrNoTokenInRequest
    32  }
    33  
    34  // Extract token from request arguments.  This includes a POSTed form or
    35  // GET URL arguments.  Argument names are tried in order until there's a match.
    36  // This extractor calls `ParseMultipartForm` on the request
    37  type ArgumentExtractor []string
    38  
    39  func (e ArgumentExtractor) ExtractToken(req *http.Request) (string, error) {
    40  	// Make sure form is parsed
    41  	req.ParseMultipartForm(10e6)
    42  
    43  	// loop over arg names and return the first one that contains data
    44  	for _, arg := range e {
    45  		if ah := req.Form.Get(arg); ah != "" {
    46  			return ah, nil
    47  		}
    48  	}
    49  
    50  	return "", ErrNoTokenInRequest
    51  }
    52  
    53  // Tries Extractors in order until one returns a token string or an error occurs
    54  type MultiExtractor []Extractor
    55  
    56  func (e MultiExtractor) ExtractToken(req *http.Request) (string, error) {
    57  	// loop over header names and return the first one that contains data
    58  	for _, extractor := range e {
    59  		if tok, err := extractor.ExtractToken(req); tok != "" {
    60  			return tok, nil
    61  		} else if err != ErrNoTokenInRequest {
    62  			return "", err
    63  		}
    64  	}
    65  	return "", ErrNoTokenInRequest
    66  }
    67  
    68  // Wrap an Extractor in this to post-process the value before it's handed off.
    69  // See AuthorizationHeaderExtractor for an example
    70  type PostExtractionFilter struct {
    71  	Extractor
    72  	Filter func(string) (string, error)
    73  }
    74  
    75  func (e *PostExtractionFilter) ExtractToken(req *http.Request) (string, error) {
    76  	if tok, err := e.Extractor.ExtractToken(req); tok != "" {
    77  		return e.Filter(tok)
    78  	} else {
    79  		return "", err
    80  	}
    81  }
    82  

View as plain text