Skip to content

Commit 27de382

Browse files
authored
revert: splitting the HMAC SHA strategy (#813) (#815)
This reverts commit 576230a.
1 parent e00e96d commit 27de382

File tree

7 files changed

+57
-109
lines changed

7 files changed

+57
-109
lines changed

compose/compose_strategy.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,8 @@ type HMACSHAStrategyConfigurator interface {
3131

3232
func NewOAuth2HMACStrategy(config HMACSHAStrategyConfigurator) *oauth2.HMACSHAStrategy {
3333
return &oauth2.HMACSHAStrategy{
34-
BaseHMACSHAStrategy: &oauth2.BaseHMACSHAStrategy{
35-
Enigma: &hmac.HMACStrategy{Config: config},
36-
Config: config,
37-
},
34+
Enigma: &hmac.HMACStrategy{Config: config},
35+
Config: config,
3836
}
3937
}
4038

handler/oauth2/strategy_hmacsha.go

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ package oauth2
55

66
import (
77
"context"
8+
"fmt"
9+
"strings"
810
"time"
911

1012
"github.com/ory/x/errorsx"
@@ -13,39 +15,55 @@ import (
1315
enigma "github.com/ory/fosite/token/hmac"
1416
)
1517

16-
var _ CoreStrategy = (*BaseHMACSHAStrategy)(nil)
17-
18-
type BaseHMACSHAStrategy struct {
18+
type HMACSHAStrategy struct {
1919
Enigma *enigma.HMACStrategy
2020
Config interface {
2121
fosite.AccessTokenLifespanProvider
2222
fosite.RefreshTokenLifespanProvider
2323
fosite.AuthorizeCodeLifespanProvider
2424
}
25+
prefix *string
2526
}
2627

27-
func (h *BaseHMACSHAStrategy) AccessTokenSignature(_ context.Context, token string) string {
28+
func (h *HMACSHAStrategy) AccessTokenSignature(ctx context.Context, token string) string {
2829
return h.Enigma.Signature(token)
2930
}
30-
31-
func (h *BaseHMACSHAStrategy) RefreshTokenSignature(_ context.Context, token string) string {
31+
func (h *HMACSHAStrategy) RefreshTokenSignature(ctx context.Context, token string) string {
3232
return h.Enigma.Signature(token)
3333
}
34-
35-
func (h *BaseHMACSHAStrategy) AuthorizeCodeSignature(_ context.Context, token string) string {
34+
func (h *HMACSHAStrategy) AuthorizeCodeSignature(ctx context.Context, token string) string {
3635
return h.Enigma.Signature(token)
3736
}
3837

39-
func (h *BaseHMACSHAStrategy) GenerateAccessToken(ctx context.Context, _ fosite.Requester) (token string, signature string, err error) {
38+
func (h *HMACSHAStrategy) getPrefix(part string) string {
39+
if h.prefix == nil {
40+
prefix := "ory_%s_"
41+
h.prefix = &prefix
42+
} else if len(*h.prefix) == 0 {
43+
return ""
44+
}
45+
46+
return fmt.Sprintf(*h.prefix, part)
47+
}
48+
49+
func (h *HMACSHAStrategy) trimPrefix(token, part string) string {
50+
return strings.TrimPrefix(token, h.getPrefix(part))
51+
}
52+
53+
func (h *HMACSHAStrategy) setPrefix(token, part string) string {
54+
return h.getPrefix(part) + token
55+
}
56+
57+
func (h *HMACSHAStrategy) GenerateAccessToken(ctx context.Context, _ fosite.Requester) (token string, signature string, err error) {
4058
token, sig, err := h.Enigma.Generate(ctx)
4159
if err != nil {
4260
return "", "", err
4361
}
4462

45-
return token, sig, nil
63+
return h.setPrefix(token, "at"), sig, nil
4664
}
4765

48-
func (h *BaseHMACSHAStrategy) ValidateAccessToken(ctx context.Context, r fosite.Requester, token string) (err error) {
66+
func (h *HMACSHAStrategy) ValidateAccessToken(ctx context.Context, r fosite.Requester, token string) (err error) {
4967
var exp = r.GetSession().GetExpiresAt(fosite.AccessToken)
5068
if exp.IsZero() && r.GetRequestedAt().Add(h.Config.GetAccessTokenLifespan(ctx)).Before(time.Now().UTC()) {
5169
return errorsx.WithStack(fosite.ErrTokenExpired.WithHintf("Access token expired at '%s'.", r.GetRequestedAt().Add(h.Config.GetAccessTokenLifespan(ctx))))
@@ -55,42 +73,42 @@ func (h *BaseHMACSHAStrategy) ValidateAccessToken(ctx context.Context, r fosite.
5573
return errorsx.WithStack(fosite.ErrTokenExpired.WithHintf("Access token expired at '%s'.", exp))
5674
}
5775

58-
return h.Enigma.Validate(ctx, token)
76+
return h.Enigma.Validate(ctx, h.trimPrefix(token, "at"))
5977
}
6078

61-
func (h *BaseHMACSHAStrategy) GenerateRefreshToken(ctx context.Context, _ fosite.Requester) (token string, signature string, err error) {
79+
func (h *HMACSHAStrategy) GenerateRefreshToken(ctx context.Context, _ fosite.Requester) (token string, signature string, err error) {
6280
token, sig, err := h.Enigma.Generate(ctx)
6381
if err != nil {
6482
return "", "", err
6583
}
6684

67-
return token, sig, nil
85+
return h.setPrefix(token, "rt"), sig, nil
6886
}
6987

70-
func (h *BaseHMACSHAStrategy) ValidateRefreshToken(ctx context.Context, r fosite.Requester, token string) (err error) {
88+
func (h *HMACSHAStrategy) ValidateRefreshToken(ctx context.Context, r fosite.Requester, token string) (err error) {
7189
var exp = r.GetSession().GetExpiresAt(fosite.RefreshToken)
7290
if exp.IsZero() {
7391
// Unlimited lifetime
74-
return h.Enigma.Validate(ctx, token)
92+
return h.Enigma.Validate(ctx, h.trimPrefix(token, "rt"))
7593
}
7694

7795
if !exp.IsZero() && exp.Before(time.Now().UTC()) {
7896
return errorsx.WithStack(fosite.ErrTokenExpired.WithHintf("Refresh token expired at '%s'.", exp))
7997
}
8098

81-
return h.Enigma.Validate(ctx, token)
99+
return h.Enigma.Validate(ctx, h.trimPrefix(token, "rt"))
82100
}
83101

84-
func (h *BaseHMACSHAStrategy) GenerateAuthorizeCode(ctx context.Context, _ fosite.Requester) (token string, signature string, err error) {
102+
func (h *HMACSHAStrategy) GenerateAuthorizeCode(ctx context.Context, _ fosite.Requester) (token string, signature string, err error) {
85103
token, sig, err := h.Enigma.Generate(ctx)
86104
if err != nil {
87105
return "", "", err
88106
}
89107

90-
return token, sig, nil
108+
return h.setPrefix(token, "ac"), sig, nil
91109
}
92110

93-
func (h *BaseHMACSHAStrategy) ValidateAuthorizeCode(ctx context.Context, r fosite.Requester, token string) (err error) {
111+
func (h *HMACSHAStrategy) ValidateAuthorizeCode(ctx context.Context, r fosite.Requester, token string) (err error) {
94112
var exp = r.GetSession().GetExpiresAt(fosite.AuthorizeCode)
95113
if exp.IsZero() && r.GetRequestedAt().Add(h.Config.GetAuthorizeCodeLifespan(ctx)).Before(time.Now().UTC()) {
96114
return errorsx.WithStack(fosite.ErrTokenExpired.WithHintf("Authorize code expired at '%s'.", r.GetRequestedAt().Add(h.Config.GetAuthorizeCodeLifespan(ctx))))
@@ -100,5 +118,5 @@ func (h *BaseHMACSHAStrategy) ValidateAuthorizeCode(ctx context.Context, r fosit
100118
return errorsx.WithStack(fosite.ErrTokenExpired.WithHintf("Authorize code expired at '%s'.", exp))
101119
}
102120

103-
return h.Enigma.Validate(ctx, token)
121+
return h.Enigma.Validate(ctx, h.trimPrefix(token, "ac"))
104122
}

handler/oauth2/strategy_hmacsha_prefixed.go

Lines changed: 0 additions & 60 deletions
This file was deleted.

handler/oauth2/strategy_hmacsha_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@ import (
1717
)
1818

1919
var hmacshaStrategy = HMACSHAStrategy{
20-
BaseHMACSHAStrategy: &BaseHMACSHAStrategy{
21-
Enigma: &hmac.HMACStrategy{Config: &fosite.Config{GlobalSecret: []byte("foobarfoobarfoobarfoobarfoobarfoobarfoobarfoobar")}},
22-
Config: &fosite.Config{
23-
AccessTokenLifespan: time.Hour * 24,
24-
AuthorizeCodeLifespan: time.Hour * 24,
25-
},
20+
Enigma: &hmac.HMACStrategy{Config: &fosite.Config{GlobalSecret: []byte("foobarfoobarfoobarfoobarfoobarfoobarfoobarfoobar")}},
21+
Config: &fosite.Config{
22+
AccessTokenLifespan: time.Hour * 24,
23+
AuthorizeCodeLifespan: time.Hour * 24,
2624
},
2725
}
2826

handler/openid/flow_hybrid_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@ import (
2727
)
2828

2929
var hmacStrategy = &oauth2.HMACSHAStrategy{
30-
BaseHMACSHAStrategy: &oauth2.BaseHMACSHAStrategy{
31-
Enigma: &hmac.HMACStrategy{
32-
Config: &fosite.Config{
33-
GlobalSecret: []byte("some-super-cool-secret-that-nobody-knows-nobody-knows"),
34-
},
30+
Enigma: &hmac.HMACStrategy{
31+
Config: &fosite.Config{
32+
GlobalSecret: []byte("some-super-cool-secret-that-nobody-knows-nobody-knows"),
3533
},
3634
},
3735
}

handler/pkce/handler_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,9 @@ func (m *mockCodeStrategy) ValidateAuthorizeCode(ctx context.Context, requester
3838
func TestPKCEHandleAuthorizeEndpointRequest(t *testing.T) {
3939
var config fosite.Config
4040
h := &Handler{
41-
Storage: storage.NewMemoryStore(),
42-
AuthorizeCodeStrategy: &oauth2.HMACSHAStrategy{
43-
BaseHMACSHAStrategy: new(oauth2.BaseHMACSHAStrategy),
44-
},
45-
Config: &config,
41+
Storage: storage.NewMemoryStore(),
42+
AuthorizeCodeStrategy: new(oauth2.HMACSHAStrategy),
43+
Config: &config,
4644
}
4745
w := fosite.NewAuthorizeResponse()
4846
r := fosite.NewAuthorizeRequest()

integration/helper_setup_test.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,17 +173,15 @@ func newJWTBearerAppClient(ts *httptest.Server) *clients.JWTBearer {
173173
}
174174

175175
var hmacStrategy = &oauth2.HMACSHAStrategy{
176-
BaseHMACSHAStrategy: &oauth2.BaseHMACSHAStrategy{
177-
Enigma: &hmac.HMACStrategy{
178-
Config: &fosite.Config{
179-
GlobalSecret: []byte("some-super-cool-secret-that-nobody-knows"),
180-
},
181-
},
176+
Enigma: &hmac.HMACStrategy{
182177
Config: &fosite.Config{
183-
AccessTokenLifespan: accessTokenLifespan,
184-
AuthorizeCodeLifespan: authCodeLifespan,
178+
GlobalSecret: []byte("some-super-cool-secret-that-nobody-knows"),
185179
},
186180
},
181+
Config: &fosite.Config{
182+
AccessTokenLifespan: accessTokenLifespan,
183+
AuthorizeCodeLifespan: authCodeLifespan,
184+
},
187185
}
188186

189187
var defaultRSAKey = gen.MustRSAKey()

0 commit comments

Comments
 (0)