commit 978225796ae20f2f85a4ed4cbd8be885b37aa5a7
parent bf50a78d86aa570858cf5d457f1737b4632efd62
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Sun, 18 Dec 2022 01:46:42 -0800
avoid useless reencryption
Diffstat:
2 files changed, 8 insertions(+), 8 deletions(-)
diff --git a/pkg/web/handlers/handlers.go b/pkg/web/handlers/handlers.go
@@ -776,7 +776,7 @@ func waitPageWrapper(c echo.Context, clb echo.HandlerFunc, cookieName string) er
start := time.Now().UnixNano()
var signupToken string
- if payload, err := hutils.EncCookie[WaitPageCookiePayload](c, cookieName); err != nil {
+ if cc, payload, err := hutils.EncCookie[WaitPageCookiePayload](c, cookieName); err != nil {
// No cookie found, we create one and display the waiting page.
waitTime := utils.Random(5, 15)
signupToken = utils.GenerateToken10()
@@ -814,7 +814,7 @@ func waitPageWrapper(c echo.Context, clb echo.HandlerFunc, cookieName string) er
// If the wait time is over, and you reload the protected page more than 4 times, we make you wait 1min
if count >= 4 {
- c.SetCookie(hutils.CreateEncCookie(cookieName, payload, utils.OneMinuteSecs))
+ c.SetCookie(hutils.CreateCookie(cookieName, cc.Value, utils.OneMinuteSecs))
return c.String(http.StatusFound, "You tried to reload the page too many times. Now you have to wait one minute.")
}
newPayload := WaitPageCookiePayload{
diff --git a/pkg/web/handlers/utils/utils.go b/pkg/web/handlers/utils/utils.go
@@ -55,25 +55,25 @@ func CreateEncCookie(name string, value any, maxAge int64) *http.Cookie {
}
// EncCookie gets back the value of an encrypted cookie
-func EncCookie[T any](c echo.Context, name string) (T, error) {
+func EncCookie[T any](c echo.Context, name string) (*http.Cookie, T, error) {
var zero T
cc, err := c.Cookie(name)
if err != nil {
- return zero, err
+ return nil, zero, err
}
val, err := base64.URLEncoding.DecodeString(cc.Value)
if err != nil {
- return zero, err
+ return nil, zero, err
}
v, err := utils.DecryptAES(val, []byte(config.Global.MasterKey()))
if err != nil {
- return zero, err
+ return nil, zero, err
}
var out T
if err := json.Unmarshal(v, &out); err != nil {
- return zero, err
+ return nil, zero, err
}
- return out, nil
+ return cc, out, nil
}
func DeleteCookie(name string) *http.Cookie {