dkforest

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

commit e43dbac6ca1e71b86f3a8e550a7fd40b4d531ea6
parent 540a51f66ea19424aef590c56197c3df0e51fa69
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Sat,  3 Dec 2022 23:41:42 -0500

cleanup

Diffstat:
Mpkg/web/handlers/handlers.go | 26+++++++++++++-------------
Mpkg/web/handlers/utils/utils.go | 10+++++++---
2 files changed, 20 insertions(+), 16 deletions(-)

diff --git a/pkg/web/handlers/handlers.go b/pkg/web/handlers/handlers.go @@ -414,7 +414,7 @@ func SessionsGpgTwoFactorHandler(c echo.Context) error { } pgpTokenCache.Delete(user.ID) partialAuthCache.Delete(partialAuthCookie.Value) - c.SetCookie(createPartialSessionCookie("", -1)) + c.SetCookie(hutils.DeleteCookie("partial-auth-token")) if string(user.TwoFactorSecret) != "" { token := utils.GenerateToken32() @@ -468,7 +468,7 @@ func SessionsGpgSignTwoFactorHandler(c echo.Context) error { } pgpTokenCache.Delete(user.ID) partialAuthCache.Delete(partialAuthCookie.Value) - c.SetCookie(createPartialSessionCookie("", -1)) + c.SetCookie(hutils.DeleteCookie("partial-auth-token")) if string(user.TwoFactorSecret) != "" { token := utils.GenerateToken32() @@ -511,7 +511,7 @@ func SessionsTwoFactorHandler(c echo.Context) error { } partialAuthCache.Delete(partialAuthCookie.Value) - c.SetCookie(createPartialSessionCookie("", -1)) + c.SetCookie(hutils.DeleteCookie("partial-auth-token")) return completeLogin(c, user, c.RealIP()) } @@ -543,7 +543,7 @@ func SessionsTwoFactorRecoveryHandler(c echo.Context) error { } partialAuthCache.Delete(partialAuthCookie.Value) - c.SetCookie(createPartialSessionCookie("", -1)) + c.SetCookie(hutils.DeleteCookie("partial-auth-token")) return completeLogin(c, user, c.RealIP()) } @@ -564,7 +564,7 @@ func LogoutHandler(ctx echo.Context) error { } } database.CreateSecurityLog(authUser.ID, database.LogoutSecurityLog) - ctx.SetCookie(createSessionCookie("", -1)) + ctx.SetCookie(hutils.DeleteCookie("auth-token")) managers.ActiveUsers.RemoveUser(authUser.ID) if authUser.Temp { if err := database.DB.Where("id = ?", authUser.ID).Unscoped().Delete(&database.User{}).Error; err != nil { @@ -878,7 +878,7 @@ func signupHandler(c echo.Context) error { // If SignupFakeEnabled is enabled, we always say the account was created, but we do not create it. if config.SignupFakeEnabled.IsTrue() { - c.SetCookie(createSignupCookie("", -1)) + c.SetCookie(hutils.DeleteCookie("signup-token")) return c.Render(http.StatusOK, "flash", FlashResponse{"Your account has been created", "/login", "alert-success"}) } @@ -927,7 +927,7 @@ func signupHandler(c echo.Context) error { } } - c.SetCookie(createSignupCookie("", -1)) + c.SetCookie(hutils.DeleteCookie("signup-token")) return c.Render(http.StatusOK, "flash", FlashResponse{"Your account has been created", "/login", "alert-success"}) } @@ -1064,7 +1064,7 @@ func ForgotPasswordHandler(c echo.Context) error { pgpTokenCache.Delete(userID) partialRecoveryCache.Delete(partialRecoveryCookie.Value) - c.SetCookie(createPartialRecoveryCookie("", -1)) + c.SetCookie(hutils.DeleteCookie("partial-recovery-token")) token2 := utils.GenerateToken32() partialRecovery2Cache.Set(token2, userID, cache1.DefaultExpiration) @@ -1108,7 +1108,7 @@ func ForgotPasswordHandler(c echo.Context) error { database.CreateSecurityLog(user.ID, database.PasswordRecoverySecurityLog) partialRecovery2Cache.Delete(partialRecovery2Cookie.Value) - c.SetCookie(createPartialRecovery2Cookie("", -1)) + c.SetCookie(hutils.DeleteCookie("partial-recovery2-token")) return c.Render(http.StatusFound, "flash", FlashResponse{Message: "Password reset done", Redirect: "/login"}) } @@ -3168,7 +3168,7 @@ func changePasswordForm(c echo.Context, data settingsPasswordData) error { if err := authUser.ChangePassword(hashedPassword); err != nil { logrus.Error(err) } - c.SetCookie(createSessionCookie("", -1)) + c.SetCookie(hutils.DeleteCookie("auth-token")) database.CreateSecurityLog(authUser.ID, database.ChangePasswordSecurityLog) return c.Render(http.StatusFound, "flash", FlashResponse{Message: "Password changed successfully", Redirect: "/login"}) } @@ -3205,7 +3205,7 @@ func changeDuressPasswordForm(c echo.Context, data settingsPasswordData) error { if err := authUser.ChangeDuressPassword(hashedPassword); err != nil { logrus.Error(err) } - c.SetCookie(createSessionCookie("", -1)) + c.SetCookie(hutils.DeleteCookie("auth-token")) database.CreateSecurityLog(authUser.ID, database.ChangeDuressPasswordSecurityLog) return c.Render(http.StatusFound, "flash", FlashResponse{Message: "Password changed successfully", Redirect: "/login"}) } @@ -3516,7 +3516,7 @@ func GpgTwoFactorAuthenticationToggleHandler(c echo.Context) error { if err := database.DeleteUserSessions(authUser.ID); err != nil { logrus.Error(err) } - c.SetCookie(createSessionCookie("", -1)) + c.SetCookie(hutils.DeleteCookie("auth-token")) authUser.GpgTwoFactorEnabled = true authUser.GpgTwoFactorMode = utils.DoParseBool(c.Request().PostFormValue("gpg_two_factor_mode")) authUser.DoSave() @@ -3569,7 +3569,7 @@ func TwoFactorAuthenticationVerifyHandler(c echo.Context) error { if err := database.DeleteUserSessions(authUser.ID); err != nil { logrus.Error(err) } - c.SetCookie(createSessionCookie("", -1)) + c.SetCookie(hutils.DeleteCookie("auth-token")) authUser.TwoFactorSecret = database.EncryptedString(twoFactor.key.Secret()) authUser.TwoFactorRecovery = string(h) if err := authUser.Save(); err != nil { diff --git a/pkg/web/handlers/utils/utils.go b/pkg/web/handlers/utils/utils.go @@ -29,6 +29,10 @@ func CreateCookie(name, value string, maxAge int64) *http.Cookie { return cookie } +func DeleteCookie(name string) *http.Cookie { + return CreateCookie(name, "", -1) +} + func GetRoomCookie(c echo.Context, roomID int64) (*http.Cookie, error) { return c.Cookie("room_" + utils.FormatInt64(roomID) + "_auth") } @@ -38,8 +42,8 @@ func GetRoomKeyCookie(c echo.Context, roomID int64) (*http.Cookie, error) { } func DeleteRoomCookie(c echo.Context, roomID int64) { - c.SetCookie(CreateCookie("room_"+utils.FormatInt64(roomID)+"_auth", "", -1)) - c.SetCookie(CreateCookie("room_"+utils.FormatInt64(roomID)+"_key", "", -1)) + c.SetCookie(DeleteCookie("room_" + utils.FormatInt64(roomID) + "_auth")) + c.SetCookie(DeleteCookie("room_" + utils.FormatInt64(roomID) + "_key")) } func CreateRoomCookie(c echo.Context, roomID int64, v, key string) { @@ -52,7 +56,7 @@ func GetGistCookie(c echo.Context, gistUUID string) (*http.Cookie, error) { } func DeleteGistCookie(c echo.Context, gistUUID string) { - c.SetCookie(CreateCookie("gist_"+gistUUID+"_auth", "", -1)) + c.SetCookie(DeleteCookie("gist_" + gistUUID + "_auth")) } func CreateGistCookie(c echo.Context, gistUUID, v string) {