forked from ory/fosite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
strategy_jwt.go
84 lines (66 loc) · 2.36 KB
/
strategy_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
package oauth2
import (
"strings"
"github.com/ory-am/fosite"
"github.com/ory-am/fosite/token/jwt"
"github.com/pkg/errors"
"golang.org/x/net/context"
)
// RS256JWTStrategy is a JWT RS256 strategy.
type RS256JWTStrategy struct {
*jwt.RS256JWTStrategy
}
func (h RS256JWTStrategy) signature(token string) string {
split := strings.Split(token, ".")
if len(split) != 3 {
return ""
}
return split[2]
}
func (h RS256JWTStrategy) AccessTokenSignature(token string) string {
return h.signature(token)
}
func (h RS256JWTStrategy) RefreshTokenSignature(token string) string {
return h.signature(token)
}
func (h RS256JWTStrategy) AuthorizeCodeSignature(token string) string {
return h.signature(token)
}
func (h *RS256JWTStrategy) GenerateAccessToken(_ context.Context, requester fosite.Requester) (token string, signature string, err error) {
return h.generate(requester)
}
func (h *RS256JWTStrategy) ValidateAccessToken(_ context.Context, _ fosite.Requester, token string) error {
return h.validate(token)
}
func (h *RS256JWTStrategy) GenerateRefreshToken(_ context.Context, requester fosite.Requester) (token string, signature string, err error) {
return h.generate(requester)
}
func (h *RS256JWTStrategy) ValidateRefreshToken(_ context.Context, _ fosite.Requester, token string) error {
return h.validate(token)
}
func (h *RS256JWTStrategy) GenerateAuthorizeCode(_ context.Context, requester fosite.Requester) (token string, signature string, err error) {
return h.generate(requester)
}
func (h *RS256JWTStrategy) ValidateAuthorizeCode(_ context.Context, requester fosite.Requester, token string) error {
return h.validate(token)
}
func (h *RS256JWTStrategy) validate(token string) error {
t, err := h.RS256JWTStrategy.Decode(token)
if err != nil {
return err
}
claims := jwt.JWTClaimsFromMap(t.Claims)
if claims.IsNotYetValid() || claims.IsExpired() {
return errors.New("Token claims did not validate")
}
return nil
}
func (h *RS256JWTStrategy) generate(requester fosite.Requester) (string, string, error) {
if jwtSession, ok := requester.GetSession().(JWTSessionContainer); !ok {
return "", "", errors.New("Session must be of type JWTSessionContainer")
} else if jwtSession.GetJWTClaims() == nil {
return "", "", errors.New("GetTokenClaims() must not be nil")
} else {
return h.RS256JWTStrategy.Generate(jwtSession.GetJWTClaims(), jwtSession.GetJWTHeader())
}
}