-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract.go
38 lines (31 loc) · 1.18 KB
/
extract.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
package jwtmw
import (
"fmt"
"net/http"
"strings"
)
// tokenFromRequest extracts a bearer token from r and returns it
// an error indicates that there was an error while extracting the
// token (e.g., invalid token, token was prefixed with wrong type, etc...)
// which will eventually cause the middleware to block the request.
// an absence of a token should return an empty token (`"", nil`) rather an error.
type tokenFromRequest func(r *http.Request) (string, error)
const (
// BearerPrefix must be set as a prefix before the actual token
BearerPrefix = "Bearer"
// BearerHeaderKey is the http header name of the token
BearerHeaderKey = "Authorization"
)
// BearerTokenFromRequest extracts a bearer token from r
// This is a naive implementation that tries to extract a token from request header.
// More advanced extractors may try to extract tokens from forms, cookies, etc.
func BearerTokenFromRequest(r *http.Request) (string, error) {
if r.Header.Get(BearerHeaderKey) == "" {
return "", nil
}
p := strings.Split(r.Header.Get(BearerHeaderKey), " ")
if len(p) == 2 && strings.EqualFold(p[0], BearerPrefix) {
return p[1], nil
}
return "", fmt.Errorf("missing or invalid token")
}