2021-01-26 16:36:53 +01:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-01-26 16:36:53 +01:00
|
|
|
|
|
|
|
package session
|
|
|
|
|
2021-12-20 15:12:26 +01:00
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2024-04-25 13:22:32 +02:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
2021-12-20 15:12:26 +01:00
|
|
|
"gitea.com/go-chi/session"
|
|
|
|
)
|
|
|
|
|
2021-01-26 16:36:53 +01: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 13:22:32 +02:00
|
|
|
ID() string
|
|
|
|
Release() error
|
|
|
|
Flush() error
|
|
|
|
Destroy(http.ResponseWriter, *http.Request) error
|
2021-01-26 16:36:53 +01:00
|
|
|
}
|
2021-12-20 15:12:26 +01:00
|
|
|
|
|
|
|
// RegenerateSession regenerates the underlying session and returns the new store
|
|
|
|
func RegenerateSession(resp http.ResponseWriter, req *http.Request) (Store, error) {
|
2024-04-19 06:03:53 +02:00
|
|
|
for _, f := range BeforeRegenerateSession {
|
|
|
|
f(resp, req)
|
|
|
|
}
|
2024-04-25 13:22:32 +02: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 15:12:26 +01:00
|
|
|
}
|
2024-04-19 06:03:53 +02:00
|
|
|
|
|
|
|
// BeforeRegenerateSession is a list of functions that are called before a session is regenerated.
|
|
|
|
var BeforeRegenerateSession []func(http.ResponseWriter, *http.Request)
|