dkforest

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

commit 099d15a232f1bede2df37978f2fa5ab99aed35ac
parent fc17a5cbfdc0669f7332a8f511cbcf3b5361e298
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Wed, 14 Dec 2022 10:42:50 -0500

cleanup var names

Diffstat:
Mpkg/web/handlers/handlers.go | 47+++++++++++++++++++++++------------------------
1 file changed, 23 insertions(+), 24 deletions(-)

diff --git a/pkg/web/handlers/handlers.go b/pkg/web/handlers/handlers.go @@ -30,7 +30,7 @@ import ( "time" "unicode/utf8" - cache1 "dkforest/pkg/cache" + "dkforest/pkg/cache" "dkforest/pkg/captcha" "dkforest/pkg/global" armor1 "filippo.io/age/armor" @@ -95,7 +95,7 @@ func firstUseHandler(c echo.Context) error { return c.Redirect(http.StatusFound, "/") } -var tempLoginCache = cache1.New[TempLoginCaptcha](3*time.Minute, 3*time.Minute) +var tempLoginCache = cache.New[TempLoginCaptcha](3*time.Minute, 3*time.Minute) var tempLoginStore = captcha.NewMemoryStore(captcha.CollectNum, 3*time.Minute) type TempLoginCaptcha struct { @@ -199,7 +199,7 @@ func HomeHandler(c echo.Context) error { // partialAuthCache keep track of partial auth token -> user id. // When a user login and have 2fa enabled, we create a "partial" auth cookie. // The token can be used to complete the 2fa authentication. -var partialAuthCache = cache1.New[PartialAuthItem](10*time.Minute, time.Hour) +var partialAuthCache = cache.New[PartialAuthItem](10*time.Minute, time.Hour) type PartialAuthItem struct { UserID database.UserID @@ -291,15 +291,15 @@ func loginHandler(c echo.Context) error { if user.GpgTwoFactorEnabled { token := utils.GenerateToken32() if user.GpgTwoFactorMode { - partialAuthCache.Set(token, PartialAuthItem{user.ID, PgpSignStep}, cache1.DefaultExpiration) + partialAuthCache.Set(token, PartialAuthItem{user.ID, PgpSignStep}, cache.DefaultExpiration) return SessionsGpgSignTwoFactorHandler(c, true, token) } - partialAuthCache.Set(token, PartialAuthItem{user.ID, PgpStep}, cache1.DefaultExpiration) + partialAuthCache.Set(token, PartialAuthItem{user.ID, PgpStep}, cache.DefaultExpiration) return SessionsGpgTwoFactorHandler(c, true, token) } else if string(user.TwoFactorSecret) != "" { token := utils.GenerateToken32() - partialAuthCache.Set(token, PartialAuthItem{user.ID, TwoFactorStep}, cache1.DefaultExpiration) + partialAuthCache.Set(token, PartialAuthItem{user.ID, TwoFactorStep}, cache.DefaultExpiration) return SessionsTwoFactorHandler(c, true, token) } @@ -433,7 +433,7 @@ func SessionsGpgTwoFactorHandler(c echo.Context, step1 bool, token string) error if string(user.TwoFactorSecret) != "" { token := utils.GenerateToken32() - partialAuthCache.Set(token, PartialAuthItem{user.ID, TwoFactorStep}, cache1.DefaultExpiration) + partialAuthCache.Set(token, PartialAuthItem{user.ID, TwoFactorStep}, cache.DefaultExpiration) return SessionsTwoFactorHandler(c, true, token) } @@ -477,7 +477,7 @@ func SessionsGpgSignTwoFactorHandler(c echo.Context, step1 bool, token string) e if string(user.TwoFactorSecret) != "" { token := utils.GenerateToken32() - partialAuthCache.Set(token, PartialAuthItem{user.ID, TwoFactorStep}, cache1.DefaultExpiration) + partialAuthCache.Set(token, PartialAuthItem{user.ID, TwoFactorStep}, cache.DefaultExpiration) return SessionsTwoFactorHandler(c, true, token) } @@ -660,8 +660,7 @@ func metaCss(token string) []byte { return buf.Bytes() } -//var signupCache = cache.New(5*time.Minute, 5*time.Minute) -var signupCache1 = cache1.New[SignupInfo](5*time.Minute, 5*time.Minute) +var signupCache = cache.New[SignupInfo](5*time.Minute, 5*time.Minute) type SignupInfo struct { ScreenWidth string @@ -702,7 +701,7 @@ func SignalCss(c echo.Context) error { token := c.Param("signupToken") signal := c.Param("signal") var info SignupInfo - if val, found := signupCache1.Get(token); found { + if val, found := signupCache.Get(token); found { info = val } else { info = SignupInfo{} @@ -716,7 +715,7 @@ func SignalCss(c echo.Context) error { info.HelvaticaLoaded = true } info.UpdatedAt = time.Now().Format(time.RFC3339) - signupCache1.Set(token, info, cache1.DefaultExpiration) + signupCache.Set(token, info, cache.DefaultExpiration) return c.NoContent(http.StatusOK) } @@ -806,7 +805,7 @@ func signupHandler(c echo.Context) error { hbCookie, hbCookieErr := c.Cookie(hutils.HBCookieName) hasHBCookie := hbCookieErr == nil && hbCookie.Value != "" - signupInfo, _ := signupCache1.Get(signupToken) + signupInfo, _ := signupCache.Get(signupToken) data.HasSolvedCaptcha = signupInfo.hasSolvedCaptcha if !signupInfo.hasSolvedCaptcha { @@ -832,7 +831,7 @@ func signupHandler(c echo.Context) error { signupInfo.hasSolvedCaptcha = true data.HasSolvedCaptcha = signupInfo.hasSolvedCaptcha - signupCache1.Set(signupToken, signupInfo, cache1.DefaultExpiration) + signupCache.Set(signupToken, signupInfo, cache.DefaultExpiration) config.SignupSucceed.Inc() @@ -913,7 +912,7 @@ func validateCaptcha(c echo.Context) error { // Since the user is not authenticated in any of these steps, we need to guard each steps and ensure the user can access it legitimately. // partialRecoveryCache keeps track of users that are in the process of recovering their password and the step they're at. var ( - partialRecoveryCache = cache1.New[PartialRecoveryItem](10*time.Minute, time.Hour) + partialRecoveryCache = cache.New[PartialRecoveryItem](10*time.Minute, time.Hour) ) type PartialRecoveryItem struct { @@ -995,7 +994,7 @@ func ForgotPasswordHandler(c echo.Context) error { } token := utils.GenerateToken32() - partialRecoveryCache.Set(token, PartialRecoveryItem{user.ID, RecoveryCaptchaCompleted}, cache1.DefaultExpiration) + partialRecoveryCache.Set(token, PartialRecoveryItem{user.ID, RecoveryCaptchaCompleted}, cache.DefaultExpiration) data.Token = token data.Step = 2 @@ -1037,7 +1036,7 @@ func ForgotPasswordHandler(c echo.Context) error { } pgpTokenCache.Delete(userID) - partialRecoveryCache.Set(token, PartialRecoveryItem{userID, RecoveryGpgValidated}, cache1.DefaultExpiration) + partialRecoveryCache.Set(token, PartialRecoveryItem{userID, RecoveryGpgValidated}, cache.DefaultExpiration) data.Token = token data.Step = 3 @@ -3304,8 +3303,8 @@ type ValueTokenCache struct { PKey string // age/pgp public key } -var ageTokenCache = cache1.NewWithKey[database.UserID, ValueTokenCache](10*time.Minute, time.Hour) -var pgpTokenCache = cache1.NewWithKey[database.UserID, ValueTokenCache](10*time.Minute, time.Hour) +var ageTokenCache = cache.NewWithKey[database.UserID, ValueTokenCache](10*time.Minute, time.Hour) +var pgpTokenCache = cache.NewWithKey[database.UserID, ValueTokenCache](10*time.Minute, time.Hour) func SettingsPGPHandler(c echo.Context) error { authUser := c.Get("authUser").(*database.User) @@ -3468,7 +3467,7 @@ func AddAgeHandler(c echo.Context) error { } // twoFactorCache ... -var twoFactorCache = cache1.NewWithKey[database.UserID, twoFactorObj](10*time.Minute, time.Hour) +var twoFactorCache = cache.NewWithKey[database.UserID, twoFactorObj](10*time.Minute, time.Hour) type twoFactorObj struct { key *otp.Key @@ -3576,7 +3575,7 @@ func TwoFactorAuthenticationVerifyHandler(c echo.Context) error { data.QRCode = getImgStr(img) data.Secret = key.Secret() data.RecoveryCode = recovery - twoFactorCache.Set(authUser.ID, twoFactorObj{key, recovery}, cache1.DefaultExpiration) + twoFactorCache.Set(authUser.ID, twoFactorObj{key, recovery}, cache.DefaultExpiration) return c.Render(http.StatusOK, "two-factor-authentication-verify", data) } @@ -3714,7 +3713,7 @@ func TorchessDownloadsHandler(c echo.Context) error { return c.Render(http.StatusOK, "torchess-downloads", data) } -var flagValidationCache = cache1.NewWithKey[database.UserID, bool](time.Minute, time.Hour) +var flagValidationCache = cache.NewWithKey[database.UserID, bool](time.Minute, time.Hour) // VipDownloadsHandler ... func VipDownloadsHandler(c echo.Context) error { @@ -4033,8 +4032,8 @@ func GetFileContentType(out io.ReadSeeker) (string, error) { return contentType, nil } -var byteRoadSignUpSessionCache = cache1.New[bool](10*time.Minute, 10*time.Minute) -var byteRoadUsersCountCache = cache1.NewWithKey[database.UserID, ByteRoadPayload](5*time.Minute, 10*time.Minute) +var byteRoadSignUpSessionCache = cache.New[bool](10*time.Minute, 10*time.Minute) +var byteRoadUsersCountCache = cache.NewWithKey[database.UserID, ByteRoadPayload](5*time.Minute, 10*time.Minute) type ByteRoadPayload struct { Count int64