2021-01-26 15:36:53 +00:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-01-26 15:36:53 +00:00
|
|
|
|
|
|
|
package session
|
|
|
|
|
2021-12-20 14:12:26 +00:00
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2024-04-25 11:22:32 +00:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
2021-12-20 14:12:26 +00:00
|
|
|
"gitea.com/go-chi/session"
|
|
|
|
)
|
|
|
|
|
2021-01-26 15:36:53 +00:00
|
|
|
// Store represents a session store
|
|
|
|
type Store interface {
|
2023-07-04 18:36:08 +00:00
|
|
|
Get(any) any
|
|
|
|
Set(any, any) error
|
|
|
|
Delete(any) error
|
2024-04-25 11:22:32 +00:00
|
|
|
ID() string
|
|
|
|
Release() error
|
|
|
|
Flush() error
|
|
|
|
Destroy(http.ResponseWriter, *http.Request) error
|
2021-01-26 15:36:53 +00: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 04:03:53 +00:00
|
|
|
for _, f := range BeforeRegenerateSession {
|
|
|
|
f(resp, req)
|
|
|
|
}
|
2024-04-25 11:22:32 +00: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 04:03:53 +00:00
|
|
|
|
|
|
|
// BeforeRegenerateSession is a list of functions that are called before a session is regenerated.
|
|
|
|
var BeforeRegenerateSession []func(http.ResponseWriter, *http.Request)
|