Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BEDS 444/prolong sessions #1012

Merged
merged 2 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion backend/pkg/api/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import (
"github.com/gorilla/csrf"
)

var day time.Duration = time.Hour * 24
var sessionDuration time.Duration = day * 365

func newSessionManager(cfg *types.Config) *scs.SessionManager {
// TODO: replace redis with user db down the line (or replace sessions with oauth2)
pool := &redis.Pool{
Expand All @@ -23,7 +26,7 @@ func newSessionManager(cfg *types.Config) *scs.SessionManager {
}

scs := scs.New()
scs.Lifetime = time.Hour * 24 * 7
scs.Lifetime = sessionDuration
scs.Cookie.Name = "session_id"
scs.Cookie.HttpOnly = true
scs.Cookie.Persist = true
Expand All @@ -42,6 +45,19 @@ func newSessionManager(cfg *types.Config) *scs.SessionManager {
return scs
}

// returns a middleware that extends the session expiration if the session is older than 1 day
func getSlidingSessionExpirationMiddleware(scs *scs.SessionManager) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
deadline := scs.Deadline(r.Context()) // unauthenticated requests have deadline set to now+sessionDuration
if time.Until(deadline) < sessionDuration-day {
scs.SetDeadline(r.Context(), time.Now().Add(sessionDuration).UTC()) // setting to utc because library also does that internally
}
next.ServeHTTP(w, r)
})
}
}

// returns goriila/csrf middleware with the given config settings
func getCsrfProtectionMiddleware(cfg *types.Config) func(http.Handler) http.Handler {
csrfBytes, err := hex.DecodeString(cfg.Frontend.CsrfAuthKey)
Expand Down
14 changes: 7 additions & 7 deletions backend/pkg/api/data_access/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"time"

"github.com/go-faker/faker/v4"
"github.com/go-faker/faker/v4/pkg/interfaces"
"github.com/go-faker/faker/v4/pkg/options"
"github.com/gobitfly/beaconchain/pkg/api/enums"
"github.com/gobitfly/beaconchain/pkg/api/types"
t "github.com/gobitfly/beaconchain/pkg/api/types"
commontypes "github.com/gobitfly/beaconchain/pkg/commons/types"
"github.com/gobitfly/beaconchain/pkg/userservice"
Expand Down Expand Up @@ -55,7 +55,7 @@ func randomEthDecimal() decimal.Decimal {
// must pass a pointer to the data
func commonFakeData(a interface{}) error {
// TODO fake decimal.Decimal
return faker.FakeData(a, options.WithRandomMapAndSliceMaxSize(5))
return faker.FakeData(a, options.WithRandomMapAndSliceMaxSize(5), options.WithRandomFloatBoundaries(interfaces.RandomFloatBoundary{Start: 0, End: 1}))
}

func (d *DummyService) StartDataAccessServices() {
Expand Down Expand Up @@ -393,7 +393,7 @@ func (d *DummyService) GetValidatorDashboardRocketPoolMinipools(ctx context.Cont
}

func (d *DummyService) GetAllNetworks() ([]t.NetworkInfo, error) {
return []types.NetworkInfo{
return []t.NetworkInfo{
{
ChainId: 1,
Name: "ethereum",
Expand All @@ -412,8 +412,8 @@ func (d *DummyService) GetAllNetworks() ([]t.NetworkInfo, error) {
}, nil
}

func (d *DummyService) GetAllClients() ([]types.ClientInfo, error) {
return []types.ClientInfo{
func (d *DummyService) GetAllClients() ([]t.ClientInfo, error) {
return []t.ClientInfo{
// execution_layer
{
Id: 0,
Expand Down Expand Up @@ -756,8 +756,8 @@ func (d *DummyService) GetValidatorDashboardMobileWidget(ctx context.Context, da
return getDummyStruct[t.MobileWidgetData]()
}

func (d *DummyService) GetUserMachineMetrics(ctx context.Context, userID uint64, limit int, offset int) (*types.MachineMetricsData, error) {
data, err := getDummyStruct[types.MachineMetricsData]()
func (d *DummyService) GetUserMachineMetrics(ctx context.Context, userID uint64, limit int, offset int) (*t.MachineMetricsData, error) {
data, err := getDummyStruct[t.MachineMetricsData]()
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion backend/pkg/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func NewApiRouter(dataAccessor dataaccess.DataAccessor, dummy dataaccess.DataAcc
legacyRouter := apiRouter.PathPrefix("/v1").Subrouter()
internalRouter := apiRouter.PathPrefix("/i").Subrouter()
sessionManager := newSessionManager(cfg)
internalRouter.Use(sessionManager.LoadAndSave)
internalRouter.Use(sessionManager.LoadAndSave, getSlidingSessionExpirationMiddleware(sessionManager))

if !(cfg.Frontend.CsrfInsecure || cfg.Frontend.Debug) {
internalRouter.Use(getCsrfProtectionMiddleware(cfg), csrfInjecterMiddleware)
Expand Down
Loading