@@ -6,12 +6,12 @@ import (
66 "net/http"
77
88 "github.com/authgear/authgear-server/pkg/api/apierrors"
9- "github.com/authgear/authgear-server/pkg/lib/config"
109 "github.com/authgear/authgear-server/pkg/util/duration"
1110 "github.com/authgear/authgear-server/pkg/util/httputil"
1211)
1312
14- type CookieFactory interface {
13+ type CookieManager interface {
14+ GetCookie (r * http.Request , def * httputil.CookieDef ) (* http.Cookie , error )
1515 ValueCookie (def * httputil.CookieDef , value string ) * http.Cookie
1616 ClearCookie (def * httputil.CookieDef ) * http.Cookie
1717}
@@ -20,86 +20,68 @@ type SessionCookieDef struct {
2020 Def * httputil.CookieDef
2121}
2222
23- func NewSessionCookieDef (httpCfg * config. HTTPConfig ) SessionCookieDef {
23+ func NewSessionCookieDef () SessionCookieDef {
2424 def := & httputil.CookieDef {
25- Name : httpCfg . CookiePrefix + "web_session" ,
25+ NameSuffix : "web_session" ,
2626 Path : "/" ,
2727 AllowScriptAccess : false ,
2828 SameSite : http .SameSiteNoneMode , // For resumption after redirecting from OAuth providers
2929 MaxAge : nil , // Use HTTP session cookie; expires when browser closes
3030 }
31-
32- if httpCfg .CookieDomain != nil {
33- def .Domain = * httpCfg .CookieDomain
34- }
35-
3631 return SessionCookieDef {Def : def }
3732}
3833
3934type ErrorCookieDef struct {
4035 Def * httputil.CookieDef
4136}
4237
43- func NewErrorCookieDef (httpCfg * config. HTTPConfig ) ErrorCookieDef {
38+ func NewErrorCookieDef () ErrorCookieDef {
4439 def := & httputil.CookieDef {
45- Name : httpCfg . CookiePrefix + "web_err" ,
40+ NameSuffix : "web_err" ,
4641 Path : "/" ,
4742 AllowScriptAccess : false ,
4843 SameSite : http .SameSiteLaxMode ,
4944 MaxAge : nil , // Use HTTP session cookie; expires when browser closes
5045 }
51-
52- if httpCfg .CookieDomain != nil {
53- def .Domain = * httpCfg .CookieDomain
54- }
55-
5646 return ErrorCookieDef {Def : def }
5747}
5848
5949type SignedUpCookieDef struct {
6050 Def * httputil.CookieDef
6151}
6252
63- func NewSignedUpCookieDef (httpCfg * config. HTTPConfig ) SignedUpCookieDef {
53+ func NewSignedUpCookieDef () SignedUpCookieDef {
6454 long := int (duration .Long .Seconds ())
6555 def := & httputil.CookieDef {
66- Name : httpCfg . CookiePrefix + "signed_up" ,
56+ NameSuffix : "signed_up" ,
6757 Path : "/" ,
6858 AllowScriptAccess : false ,
6959 SameSite : http .SameSiteLaxMode ,
7060 MaxAge : & long ,
7161 }
72-
73- if httpCfg .CookieDomain != nil {
74- def .Domain = * httpCfg .CookieDomain
75- }
76-
7762 return SignedUpCookieDef {Def : def }
7863}
7964
8065type ErrorCookie struct {
81- Cookie ErrorCookieDef
82- CookieFactory CookieFactory
66+ Cookie ErrorCookieDef
67+ Cookies CookieManager
8368}
8469
8570type ClientIDCookieDef struct {
8671 Def * httputil.CookieDef
8772}
8873
89- func NewClientIDCookieDef (httpCfg * config. HTTPConfig ) ClientIDCookieDef {
74+ func NewClientIDCookieDef () ClientIDCookieDef {
9075 def := & httputil.CookieDef {
91- Name : "client_id" ,
92- Path : "/" ,
93- SameSite : http .SameSiteNoneMode ,
94- }
95- if httpCfg .CookieDomain != nil {
96- def .Domain = * httpCfg .CookieDomain
76+ NameSuffix : "client_id" ,
77+ Path : "/" ,
78+ SameSite : http .SameSiteNoneMode ,
9779 }
9880 return ClientIDCookieDef {Def : def }
9981}
10082
10183func (c * ErrorCookie ) GetError (r * http.Request ) (* apierrors.APIError , bool ) {
102- cookie , err := r . Cookie ( c .Cookie .Def . Name )
84+ cookie , err := c . Cookies . GetCookie ( r , c .Cookie .Def )
10385 if err != nil || cookie .Value == "" {
10486 return nil , false
10587 }
@@ -117,7 +99,7 @@ func (c *ErrorCookie) GetError(r *http.Request) (*apierrors.APIError, bool) {
11799}
118100
119101func (c * ErrorCookie ) ResetError () * http.Cookie {
120- cookie := c .CookieFactory .ClearCookie (c .Cookie .Def )
102+ cookie := c .Cookies .ClearCookie (c .Cookie .Def )
121103 return cookie
122104}
123105
@@ -128,6 +110,6 @@ func (c *ErrorCookie) SetError(value *apierrors.APIError) (*http.Cookie, error)
128110 }
129111
130112 cookieValue := base64 .RawURLEncoding .EncodeToString (data )
131- cookie := c .CookieFactory .ValueCookie (c .Cookie .Def , cookieValue )
113+ cookie := c .Cookies .ValueCookie (c .Cookie .Def , cookieValue )
132114 return cookie , nil
133115}
0 commit comments