-
Notifications
You must be signed in to change notification settings - Fork 1
/
bearerware.go
91 lines (80 loc) · 1.83 KB
/
bearerware.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package bearerware
import (
"errors"
"fmt"
"strings"
jwt "github.com/dgrijalva/jwt-go"
)
const (
authHeader = "authorization"
bearer = "bearer "
bearerLen = len(bearer)
invalidRequest = "invalid_request"
invalidToken = "invalid_token"
//insufficientScope = "insufficient_scope"
errSigningMethod = `Expected signing method %s but token is signed using %s`
)
var (
errRestricted = errors.New("Bearer realm=Restricted")
errBearerFormat = errors.New(
"Authorization header format must be Bearer {token}",
)
errTokenInvalid = errors.New("Token is invalid")
)
type jwtError struct {
err error
code string
}
func (j *jwtError) Error() string {
s := errRestricted.Error()
if len(j.code) > 0 {
s += fmt.Sprintf(`,error="%s",error_description="%s"`, j.code, j.err)
}
return s
}
func isBearerToken(s string) bool {
return len(s) > bearerLen && strings.ToLower(s)[:bearerLen] == bearer
}
func tokenFromBearer(s string) (string, bool) {
if isBearerToken(s) {
return s[bearerLen:], true
}
return "", false
}
//CheckJWT checks if the JWT was issued by this app
func validJWTFromString(
token string,
keyFunc jwt.Keyfunc,
signingMethod jwt.SigningMethod,
) (*jwt.Token, error) {
var (
err error
parsedToken *jwt.Token
)
if len(token) == 0 {
return nil, &jwtError{
err: errBearerFormat,
code: invalidRequest,
}
}
parsedToken, err = jwt.Parse(token, keyFunc)
if err != nil {
return nil, &jwtError{
err: fmt.Errorf("Error parsing token: %v", err),
code: invalidToken,
}
}
if alg := parsedToken.Header["alg"]; alg != signingMethod.Alg() {
return nil, &jwtError{
err: fmt.Errorf(errSigningMethod, signingMethod.Alg(), alg),
code: invalidToken,
}
}
if !parsedToken.Valid {
return nil, &jwtError{
err: errTokenInvalid,
code: invalidToken,
}
}
return parsedToken, nil
}