This repository has been archived by the owner on Dec 20, 2019. It is now read-only.
/
handler.go
61 lines (52 loc) · 1.77 KB
/
handler.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package tokeninfo
import (
"net/http"
"strings"
)
const accessTokenParameter = "access_token"
// A Handler is a regular http.Handler that can be queried for its capacity of handling a
// Request. The Match function should be as fast as possible verifying if the Request matches
type Handler interface {
http.Handler
Match(r *http.Request) bool
}
type routingHandler struct {
defaultHandler http.Handler
routes []Handler
}
// NewHandler returns an http.Handler that contains a default http.Handler and a variadic argument
// of TokenInfoHandlers that will selectively handle specific requests
func NewHandler(def http.Handler, r ...Handler) http.Handler {
return &routingHandler{defaultHandler: def, routes: r}
}
// ServeHTTP will go through the list of TokenInfoHandlers and test for a match for the current Request.
// The first TokenInfoHandler to match will handle the request.
// If none of them can handle the request, it is handled by the default Handler
func (rh *routingHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if h := rh.matchRequest(req); h != nil {
h.ServeHTTP(w, req)
} else {
rh.defaultHandler.ServeHTTP(w, req)
}
}
func (rh *routingHandler) matchRequest(r *http.Request) http.Handler {
for _, h := range rh.routes {
if h.Match(r) {
return h
}
}
return nil
}
// AccessTokenFromRequest can be used to extract an Access Token from an http.Request
// via the standard headers/parameters
// Ref:
// https://tools.ietf.org/html/rfc6749#section-5.1
// https://tools.ietf.org/html/rfc6750#section-2.1
func AccessTokenFromRequest(req *http.Request) string {
if h := req.Header.Get("Authorization"); h != "" {
if strings.HasPrefix(strings.ToLower(h), "bearer ") {
return h[7:]
}
}
return req.FormValue(accessTokenParameter)
}