forked from infobloxopen/atlas-app-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.go
93 lines (83 loc) · 2.74 KB
/
jwt.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
92
93
package auth
import (
"context"
"errors"
"fmt"
jwt "github.com/dgrijalva/jwt-go"
"github.com/grpc-ecosystem/go-grpc-middleware/auth"
)
const (
// MultiTenancyField the field name for a specific tenant
MultiTenancyField = "account_id"
// DefaultTokenType is the name of the authorization token (e.g. "Bearer"
// or "token")
DefaultTokenType = "Bearer"
)
var (
errMissingField = errors.New("unable to get field from token")
errMissingToken = errors.New("unable to get token from context")
errInvalidAssertion = errors.New("unable to assert token as jwt.MapClaims")
// multiTenancyVariants all possible multi-tenant names
multiTenancyVariants = []string{
MultiTenancyField,
"AccountID",
}
)
// GetJWTFieldWithTokenType gets the JWT from a context and returns the
// specified field. The user must provide a token type, which prefixes the
// token itself (e.g. "Bearer" or "token")
func GetJWTFieldWithTokenType(ctx context.Context, tokenType, tokenField string, keyfunc jwt.Keyfunc) (string, error) {
token, err := getToken(ctx, tokenType, keyfunc)
if err != nil {
return "", errMissingToken
}
claims, ok := token.Claims.(jwt.MapClaims)
if !ok {
return "", errInvalidAssertion
}
jwtField, ok := claims[tokenField]
if !ok {
return "", errMissingField
}
return fmt.Sprint(jwtField), nil
}
// GetJWTField gets the JWT from a context and returns the specified field
// using the DefaultTokenName
func GetJWTField(ctx context.Context, tokenField string, keyfunc jwt.Keyfunc) (string, error) {
return GetJWTFieldWithTokenType(ctx, DefaultTokenType, tokenField, keyfunc)
}
// GetAccountID gets the JWT from a context and returns the AccountID field
func GetAccountID(ctx context.Context, keyfunc jwt.Keyfunc) (string, error) {
for _, tenantField := range multiTenancyVariants {
if val, err := GetJWTField(ctx, tenantField, keyfunc); err == nil {
return val, nil
}
}
return "", errMissingField
}
// getToken parses the token into a jwt.Token type from the grpc metadata.
// WARNING: if keyfunc is nil, the token will get parsed but not verified
// because it has been checked previously in the stack. More information
// here: https://godoc.org/github.com/dgrijalva/jwt-go#Parser.ParseUnverified
func getToken(ctx context.Context, tokenField string, keyfunc jwt.Keyfunc) (jwt.Token, error) {
if ctx == nil {
return jwt.Token{}, errMissingToken
}
tokenStr, err := grpc_auth.AuthFromMD(ctx, tokenField)
if err != nil {
return jwt.Token{}, err
}
parser := jwt.Parser{}
if keyfunc != nil {
token, err := parser.Parse(tokenStr, keyfunc)
if err != nil {
return jwt.Token{}, err
}
return *token, nil
}
token, _, err := parser.ParseUnverified(tokenStr, jwt.MapClaims{})
if err != nil {
return jwt.Token{}, err
}
return *token, nil
}