-
-
Notifications
You must be signed in to change notification settings - Fork 367
/
strategy_hmacsha_prefixed.go
71 lines (55 loc) · 2.22 KB
/
strategy_hmacsha_prefixed.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
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package oauth2
import (
"context"
"fmt"
"strings"
enigma "github.com/ory/fosite/token/hmac"
"github.com/ory/fosite"
)
var _ CoreStrategy = (*HMACSHAStrategy)(nil)
type HMACSHAStrategy struct {
*HMACSHAStrategyUnPrefixed
}
func NewHMACSHAStrategy(
enigma *enigma.HMACStrategy,
config LifespanConfigProvider,
) *HMACSHAStrategy {
return &HMACSHAStrategy{
HMACSHAStrategyUnPrefixed: NewHMACSHAStrategyUnPrefixed(enigma, config),
}
}
func (h *HMACSHAStrategy) getPrefix(part string) string {
return fmt.Sprintf("ory_%s_", part)
}
func (h *HMACSHAStrategy) trimPrefix(token, part string) string {
return strings.TrimPrefix(token, h.getPrefix(part))
}
func (h *HMACSHAStrategy) setPrefix(token, part string) string {
if token == "" {
return ""
}
return h.getPrefix(part) + token
}
func (h *HMACSHAStrategy) GenerateAccessToken(ctx context.Context, r fosite.Requester) (token string, signature string, err error) {
token, sig, err := h.HMACSHAStrategyUnPrefixed.GenerateAccessToken(ctx, r)
return h.setPrefix(token, "at"), sig, err
}
func (h *HMACSHAStrategy) ValidateAccessToken(ctx context.Context, r fosite.Requester, token string) (err error) {
return h.HMACSHAStrategyUnPrefixed.ValidateAccessToken(ctx, r, h.trimPrefix(token, "at"))
}
func (h *HMACSHAStrategy) GenerateRefreshToken(ctx context.Context, r fosite.Requester) (token string, signature string, err error) {
token, sig, err := h.HMACSHAStrategyUnPrefixed.GenerateRefreshToken(ctx, r)
return h.setPrefix(token, "rt"), sig, err
}
func (h *HMACSHAStrategy) ValidateRefreshToken(ctx context.Context, r fosite.Requester, token string) (err error) {
return h.HMACSHAStrategyUnPrefixed.ValidateRefreshToken(ctx, r, h.trimPrefix(token, "rt"))
}
func (h *HMACSHAStrategy) GenerateAuthorizeCode(ctx context.Context, r fosite.Requester) (token string, signature string, err error) {
token, sig, err := h.HMACSHAStrategyUnPrefixed.GenerateAuthorizeCode(ctx, r)
return h.setPrefix(token, "ac"), sig, err
}
func (h *HMACSHAStrategy) ValidateAuthorizeCode(ctx context.Context, r fosite.Requester, token string) (err error) {
return h.HMACSHAStrategyUnPrefixed.ValidateAuthorizeCode(ctx, r, h.trimPrefix(token, "ac"))
}