-
-
Notifications
You must be signed in to change notification settings - Fork 171
/
auth0.go
68 lines (60 loc) · 1.48 KB
/
auth0.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
package provider
import (
"context"
"errors"
"strings"
"github.com/dosco/graphjin/core"
jwt "github.com/golang-jwt/jwt"
)
type Auth0Provider struct {
key interface{}
aud string
issuer string
}
func NewAuth0Provider(config JWTConfig) (*Auth0Provider, error) {
config.initFS()
key, err := getKey(config)
if err != nil {
return nil, err
}
return &Auth0Provider{
key: key,
aud: config.Audience,
issuer: config.Issuer,
}, nil
}
func (p *Auth0Provider) KeyFunc() jwt.Keyfunc {
return func(token *jwt.Token) (interface{}, error) {
return p.key, nil
}
}
func (p *Auth0Provider) VerifyAudience(claims jwt.MapClaims) bool {
if claims == nil {
return false
}
return claims.VerifyAudience(p.aud, p.aud != "")
}
func (p *Auth0Provider) VerifyIssuer(claims jwt.MapClaims) bool {
if claims == nil {
return false
}
return claims.VerifyIssuer(p.issuer, p.issuer != "")
}
func (p *Auth0Provider) SetContextValues(ctx context.Context, claims jwt.MapClaims) (context.Context, error) {
if claims == nil {
return ctx, errors.New("undefined claims")
}
sub, found := claims["sub"].(string)
if !found || sub == "" {
return ctx, errors.New("sub claim not found")
}
sp := strings.SplitN(sub, "|", 2)
if len(sub) == 2 {
ctx = context.WithValue(ctx, core.UserIDRawKey, sub)
ctx = context.WithValue(ctx, core.UserIDProviderKey, sp[0])
ctx = context.WithValue(ctx, core.UserIDKey, sp[1])
} else {
ctx = context.WithValue(ctx, core.UserIDKey, sub)
}
return ctx, nil
}