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