-
-
Notifications
You must be signed in to change notification settings - Fork 177
/
generic.go
59 lines (51 loc) · 1.23 KB
/
generic.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
package provider
import (
"context"
"errors"
jwt "github.com/dgrijalva/jwt-go"
"github.com/dosco/graphjin/core"
)
type GenericProvider struct {
key interface{}
aud string
issuer string
}
func NewGenericProvider(config JWTConfig) (*GenericProvider, error) {
key, err := getKey(config)
if err != nil {
return nil, err
}
return &GenericProvider{
key: key,
aud: config.Audience,
issuer: config.Issuer,
}, nil
}
func (p *GenericProvider) KeyFunc() jwt.Keyfunc {
return func(token *jwt.Token) (interface{}, error) {
return p.key, nil
}
}
func (p *GenericProvider) VerifyAudience(claims jwt.MapClaims) bool {
if claims == nil {
return false
}
return claims.VerifyAudience(p.aud, p.aud != "")
}
func (p *GenericProvider) VerifyIssuer(claims jwt.MapClaims) bool {
if claims == nil {
return false
}
return claims.VerifyIssuer(p.issuer, p.issuer != "")
}
func (p *GenericProvider) 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 {
return ctx, errors.New("subject claim not found")
}
ctx = context.WithValue(ctx, core.UserIDKey, sub)
return ctx, nil
}