-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathauth.go
74 lines (63 loc) · 1.99 KB
/
auth.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
62
63
64
65
66
67
68
69
70
71
72
73
74
package api
import (
"context"
"crypto/rsa"
"io/ioutil"
"net/http"
jwt "github.com/dgrijalva/jwt-go"
"github.com/sirupsen/logrus"
)
// requireAuthentication checks incoming requests for tokens presented using the Authorization header
func (a *API) requireAuthentication(w http.ResponseWriter, r *http.Request) (context.Context, error) {
logrus.Info("Getting auth token")
token, err := a.extractBearerToken(w, r)
if err != nil {
return nil, err
}
logrus.Infof("Parsing JWT claims: %v", token)
return a.parseJWTClaims(token, r)
}
func (a *API) extractBearerToken(w http.ResponseWriter, r *http.Request) (string, error) {
authHeader := r.Header.Get("Authorization")
if authHeader == "" {
return "", unauthorizedError("This endpoint requires a Bearer token")
}
matches := bearerRegexp.FindStringSubmatch(authHeader)
if len(matches) != 2 {
return "", unauthorizedError("This endpoint requires a Bearer token")
}
return matches[1], nil
}
func (a *API) parseJWTClaims(bearer string, r *http.Request) (context.Context, error) {
config := getConfig(r.Context())
p := jwt.Parser{ValidMethods: []string{jwt.SigningMethodHS256.Name, jwt.SigningMethodRS256.Name}}
token, err := p.ParseWithClaims(bearer, &GatewayClaims{}, func(token *jwt.Token) (interface{}, error) {
signMethod := config.JWT.Method
if signMethod == "" {
signMethod = "HS256" // don't break backwards compatibility
}
switch signMethod {
case "HS256":
return []byte(config.JWT.Secret), nil
case "RS256":
return loadRSAPublicKeyFromDisk(config.JWT.Keyfile), nil
default:
return nil, unauthorizedError("Invalid Signing Method: %s", signMethod)
}
})
if err != nil {
return nil, unauthorizedError("Invalid token: %v", err)
}
return withToken(r.Context(), token), nil
}
func loadRSAPublicKeyFromDisk(location string) *rsa.PublicKey {
keyData, e := ioutil.ReadFile(location)
if e != nil {
panic(e.Error())
}
key, e := jwt.ParseRSAPublicKeyFromPEM(keyData)
if e != nil {
panic(e.Error())
}
return key
}