-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathtoken_extraction.go
47 lines (38 loc) · 1.14 KB
/
token_extraction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package auth0
import (
"net/http"
"gopkg.in/square/go-jose.v2/jwt"
"strings"
"errors"
)
var (
ErrTokenNotFound = errors.New("Token not found")
)
// RequestTokenExtractor can extract a JWT
// from a request.
type RequestTokenExtractor interface {
Extract(r *http.Request) (*jwt.JSONWebToken, error)
}
// RequestTokenExtractorFunc function conforming
// to the RequestTokenExtractor interface.
type RequestTokenExtractorFunc func(r *http.Request) (*jwt.JSONWebToken, error)
// Extract calls f(r)
func (f RequestTokenExtractorFunc) Extract(r *http.Request) (*jwt.JSONWebToken, error) {
return f(r)
}
// FromHeader looks for the request in the
// authentication header or call ParseMultipartForm
// if not present.
func FromHeader(r *http.Request) (*jwt.JSONWebToken, error) {
raw, err := fromHeader(r)
if err != nil {
return nil, err
}
return jwt.ParseSigned(string(raw))
}
func fromHeader(r *http.Request) ([]byte, error) {
if authorizationHeader := r.Header.Get("Authorization"); len(authorizationHeader) > 7 && strings.EqualFold(authorizationHeader[0:7], "BEARER ") {
return []byte(authorizationHeader[7:]), nil
}
return nil, ErrTokenNotFound
}