@@ -5,6 +5,8 @@ package oauth2
55
66import (
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}
0 commit comments