mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-11 11:28:02 -05:00
fe3b294f7b
- The current architecture is inherently insecure, because you can construct the 'secret' cookie value with values that are available in the database. Thus provides zero protection when a database is dumped/leaked. - This patch implements a new architecture that's inspired from: [Paragonie Initiative](https://paragonie.com/blog/2015/04/secure-authentication-php-with-long-term-persistence#secure-remember-me-cookies). - Integration testing is added to ensure the new mechanism works. - Removes a setting, because it's not used anymore. (cherry picked from commiteff097448b
) [GITEA] rework long-term authentication (squash) add migration Reminder: the migration is run via integration tests as explained in the commit "[DB] run all Forgejo migrations in integration tests" (cherry picked from commit4accf7443c
) (cherry picked from commit 99d06e344ebc3b50bafb2ac4473dd95f057d1ddc) (cherry picked from commitd8bc98a8f0
) (cherry picked from commit6404845df9
) (cherry picked from commit72bdd4f3b9
) (cherry picked from commit4b01bb0ce8
) (cherry picked from commitc26ac31816
) (cherry picked from commit8d2dab94a6
) Conflicts: routers/web/auth/auth.go https://codeberg.org/forgejo/forgejo/issues/2158
56 lines
1.7 KiB
Go
56 lines
1.7 KiB
Go
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package context
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
auth_model "code.gitea.io/gitea/models/auth"
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
"code.gitea.io/gitea/modules/setting"
|
|
"code.gitea.io/gitea/modules/timeutil"
|
|
"code.gitea.io/gitea/modules/web/middleware"
|
|
)
|
|
|
|
const CookieNameFlash = "gitea_flash"
|
|
|
|
func removeSessionCookieHeader(w http.ResponseWriter) {
|
|
cookies := w.Header()["Set-Cookie"]
|
|
w.Header().Del("Set-Cookie")
|
|
for _, cookie := range cookies {
|
|
if strings.HasPrefix(cookie, setting.SessionConfig.CookieName+"=") {
|
|
continue
|
|
}
|
|
w.Header().Add("Set-Cookie", cookie)
|
|
}
|
|
}
|
|
|
|
// SetSiteCookie convenience function to set most cookies consistently
|
|
// CSRF and a few others are the exception here
|
|
func (ctx *Context) SetSiteCookie(name, value string, maxAge int) {
|
|
middleware.SetSiteCookie(ctx.Resp, name, value, maxAge)
|
|
}
|
|
|
|
// DeleteSiteCookie convenience function to delete most cookies consistently
|
|
// CSRF and a few others are the exception here
|
|
func (ctx *Context) DeleteSiteCookie(name string) {
|
|
middleware.SetSiteCookie(ctx.Resp, name, "", -1)
|
|
}
|
|
|
|
// GetSiteCookie returns given cookie value from request header.
|
|
func (ctx *Context) GetSiteCookie(name string) string {
|
|
return middleware.GetSiteCookie(ctx.Req, name)
|
|
}
|
|
|
|
// SetLTACookie will generate a LTA token and add it as an cookie.
|
|
func (ctx *Context) SetLTACookie(u *user_model.User) error {
|
|
days := 86400 * setting.LogInRememberDays
|
|
lookup, validator, err := auth_model.GenerateAuthToken(ctx, u.ID, timeutil.TimeStampNow().Add(int64(days)))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ctx.SetSiteCookie(setting.CookieRememberName, lookup+":"+validator, days)
|
|
return nil
|
|
}
|