dkforest

A forum and chat platform (onion)
git clone https://git.dasho.dev/n0tr1v/dkforest.git
Log | Files | Refs | LICENSE

commit bf50a78d86aa570858cf5d457f1737b4632efd62
parent b4b5b6fbc1c76f1530ad5dd07402c13ed6dfa579
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Sun, 18 Dec 2022 01:41:46 -0800

strongly type encrypted cookie payload

Diffstat:
Mpkg/web/handlers/handlers.go | 35+++++++++++++++++++++--------------
1 file changed, 21 insertions(+), 14 deletions(-)

diff --git a/pkg/web/handlers/handlers.go b/pkg/web/handlers/handlers.go @@ -765,19 +765,26 @@ func tmpSignupHandler(c echo.Context) error { return waitPageWrapper(c, signupHandler, hutils.WaitCookieName) } +type WaitPageCookiePayload struct { + Token string + Count int64 + Now int64 + Unix int64 +} + func waitPageWrapper(c echo.Context, clb echo.HandlerFunc, cookieName string) error { start := time.Now().UnixNano() var signupToken string - if payload, err := hutils.EncCookie[map[string]string](c, cookieName); err != nil { + if 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() - payload := map[string]string{ - "token": signupToken, - "count": "1", - "now": utils.FormatInt64(time.Now().UnixMilli()), - "unix": utils.FormatInt64(time.Now().Unix() + waitTime - 1), // unix time at which the wait time is over + payload := WaitPageCookiePayload{ + Token: signupToken, + Count: 1, + Now: time.Now().UnixMilli(), + Unix: time.Now().Unix() + waitTime - 1, // unix time at which the wait time is over } c.SetCookie(hutils.CreateEncCookie(cookieName, payload, utils.OneMinuteSecs*5)) @@ -790,11 +797,11 @@ func waitPageWrapper(c echo.Context, clb echo.HandlerFunc, cookieName string) er } else { // Cookie was found, incr counter then call callback - signupToken = payload["token"] - start = utils.DoParseInt64(payload["now"]) + signupToken = payload.Token + start = payload.Now if c.Request().Method == http.MethodGet { - count := utils.DoParseInt64(payload["count"]) - unix := utils.DoParseInt64(payload["unix"]) + count := payload.Count + unix := payload.Unix // If you reload the page before the wait time is over, we kill the circuit. if time.Now().Unix() < unix { @@ -810,10 +817,10 @@ func waitPageWrapper(c echo.Context, clb echo.HandlerFunc, cookieName string) er c.SetCookie(hutils.CreateEncCookie(cookieName, payload, utils.OneMinuteSecs)) return c.String(http.StatusFound, "You tried to reload the page too many times. Now you have to wait one minute.") } - newPayload := map[string]string{ - "count": utils.FormatInt64(count + 1), - "now": utils.FormatInt64(time.Now().UnixMilli()), - "token": signupToken, + newPayload := WaitPageCookiePayload{ + Count: count + 1, + Now: time.Now().UnixMilli(), + Token: signupToken, } c.SetCookie(hutils.CreateEncCookie(cookieName, newPayload, utils.OneMinuteSecs*5)) }