-
-
Notifications
You must be signed in to change notification settings - Fork 367
/
strategy_jwt.go
133 lines (111 loc) · 3.97 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package oauth2
import (
"context"
"strings"
"time"
"github.com/pkg/errors"
"github.com/ory/fosite"
"github.com/ory/fosite/token/jwt"
"github.com/ory/x/errorsx"
)
// DefaultJWTStrategy is a JWT RS256 strategy.
type DefaultJWTStrategy struct {
jwt.Signer
HMACSHAStrategy CoreStrategy
Config interface {
fosite.AccessTokenIssuerProvider
fosite.JWTScopeFieldProvider
}
}
func (h DefaultJWTStrategy) signature(token string) string {
split := strings.Split(token, ".")
if len(split) != 3 {
return ""
}
return split[2]
}
func (h DefaultJWTStrategy) AccessTokenSignature(ctx context.Context, token string) string {
return h.signature(token)
}
func (h *DefaultJWTStrategy) GenerateAccessToken(ctx context.Context, requester fosite.Requester) (token string, signature string, err error) {
return h.generate(ctx, fosite.AccessToken, requester)
}
func (h *DefaultJWTStrategy) ValidateAccessToken(ctx context.Context, _ fosite.Requester, token string) error {
_, err := validate(ctx, h.Signer, token)
return err
}
func (h DefaultJWTStrategy) RefreshTokenSignature(ctx context.Context, token string) string {
return h.HMACSHAStrategy.RefreshTokenSignature(ctx, token)
}
func (h DefaultJWTStrategy) AuthorizeCodeSignature(ctx context.Context, token string) string {
return h.HMACSHAStrategy.AuthorizeCodeSignature(ctx, token)
}
func (h *DefaultJWTStrategy) GenerateRefreshToken(ctx context.Context, req fosite.Requester) (token string, signature string, err error) {
return h.HMACSHAStrategy.GenerateRefreshToken(ctx, req)
}
func (h *DefaultJWTStrategy) ValidateRefreshToken(ctx context.Context, req fosite.Requester, token string) error {
return h.HMACSHAStrategy.ValidateRefreshToken(ctx, req, token)
}
func (h *DefaultJWTStrategy) GenerateAuthorizeCode(ctx context.Context, req fosite.Requester) (token string, signature string, err error) {
return h.HMACSHAStrategy.GenerateAuthorizeCode(ctx, req)
}
func (h *DefaultJWTStrategy) ValidateAuthorizeCode(ctx context.Context, req fosite.Requester, token string) error {
return h.HMACSHAStrategy.ValidateAuthorizeCode(ctx, req, token)
}
func validate(ctx context.Context, jwtStrategy jwt.Signer, token string) (t *jwt.Token, err error) {
t, err = jwtStrategy.Decode(ctx, token)
if err == nil {
err = t.Claims.Valid()
return
}
var e *jwt.ValidationError
if err != nil && errors.As(err, &e) {
err = errorsx.WithStack(toRFCErr(e).WithWrap(err).WithDebug(err.Error()))
}
return
}
func toRFCErr(v *jwt.ValidationError) *fosite.RFC6749Error {
switch {
case v == nil:
return nil
case v.Has(jwt.ValidationErrorMalformed):
return fosite.ErrInvalidTokenFormat
case v.Has(jwt.ValidationErrorUnverifiable | jwt.ValidationErrorSignatureInvalid):
return fosite.ErrTokenSignatureMismatch
case v.Has(jwt.ValidationErrorExpired):
return fosite.ErrTokenExpired
case v.Has(jwt.ValidationErrorAudience |
jwt.ValidationErrorIssuedAt |
jwt.ValidationErrorIssuer |
jwt.ValidationErrorNotValidYet |
jwt.ValidationErrorId |
jwt.ValidationErrorClaimsInvalid):
return fosite.ErrTokenClaim
default:
return fosite.ErrRequestUnauthorized
}
}
func (h *DefaultJWTStrategy) generate(ctx context.Context, tokenType fosite.TokenType, requester fosite.Requester) (string, string, error) {
if jwtSession, ok := requester.GetSession().(JWTSessionContainer); !ok {
return "", "", errors.Errorf("Session must be of type JWTSessionContainer but got type: %T", requester.GetSession())
} else if jwtSession.GetJWTClaims() == nil {
return "", "", errors.New("GetTokenClaims() must not be nil")
} else {
claims := jwtSession.GetJWTClaims().
With(
jwtSession.GetExpiresAt(tokenType),
requester.GetGrantedScopes(),
requester.GetGrantedAudience(),
).
WithDefaults(
time.Now().UTC(),
h.Config.GetAccessTokenIssuer(ctx),
).
WithScopeField(
h.Config.GetJWTScopeField(ctx),
)
return h.Signer.Generate(ctx, claims.ToMapClaims(), jwtSession.GetJWTHeader())
}
}