2021-01-26 23:36:53 +08:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-01-26 23:36:53 +08:00
|
|
|
|
|
|
|
package session
|
|
|
|
|
2021-12-20 14:12:26 +00:00
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2024-04-25 19:22:32 +08:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
2021-12-20 14:12:26 +00:00
|
|
|
"gitea.com/go-chi/session"
|
|
|
|
)
|
|
|
|
|
2021-01-26 23:36:53 +08:00
|
|
|
// Store represents a session store
|
|
|
|
type Store interface {
|
2023-07-04 20:36:08 +02:00
|
|
|
Get(any) any
|
|
|
|
Set(any, any) error
|
|
|
|
Delete(any) error
|
2024-04-25 19:22:32 +08:00
|
|
|
ID() string
|
|
|
|
Release() error
|
|
|
|
Flush() error
|
|
|
|
Destroy(http.ResponseWriter, *http.Request) error
|
2021-01-26 23:36:53 +08:00
|
|
|
}
|
2021-12-20 14:12:26 +00:00
|
|
|
|
|
|
|
// RegenerateSession regenerates the underlying session and returns the new store
|
|
|
|
func RegenerateSession(resp http.ResponseWriter, req *http.Request) (Store, error) {
|
2024-04-19 12:03:53 +08:00
|
|
|
for _, f := range BeforeRegenerateSession {
|
|
|
|
f(resp, req)
|
|
|
|
}
|
2024-04-25 19:22:32 +08:00
|
|
|
if setting.IsInTesting {
|
|
|
|
if store, ok := req.Context().Value(MockStoreContextKey).(*MockStore); ok {
|
|
|
|
return store, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return session.RegenerateSession(resp, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetContextSession(req *http.Request) Store {
|
|
|
|
if setting.IsInTesting {
|
|
|
|
if store, ok := req.Context().Value(MockStoreContextKey).(*MockStore); ok {
|
|
|
|
return store
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return session.GetSession(req)
|
2021-12-20 14:12:26 +00:00
|
|
|
}
|
2024-04-19 12:03:53 +08:00
|
|
|
|
|
|
|
// BeforeRegenerateSession is a list of functions that are called before a session is regenerated.
|
|
|
|
var BeforeRegenerateSession []func(http.ResponseWriter, *http.Request)
|