2023-05-08 09:36:54 +00:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2023-11-22 16:26:21 +00:00
|
|
|
auth_model "code.gitea.io/gitea/models/auth"
|
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2023-05-08 09:36:54 +00:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2023-11-22 16:26:21 +00:00
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2023-05-08 09:36:54 +00:00
|
|
|
"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)
|
|
|
|
}
|
2023-11-22 16:26:21 +00:00
|
|
|
|
|
|
|
// 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
|
2024-08-11 03:13:01 +00:00
|
|
|
lookup, validator, err := auth_model.GenerateAuthToken(ctx, u.ID, timeutil.TimeStampNow().Add(int64(days)), auth_model.LongTermAuthorization)
|
2023-11-22 16:26:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ctx.SetSiteCookie(setting.CookieRememberName, lookup+":"+validator, days)
|
|
|
|
return nil
|
|
|
|
}
|