2022-05-12 16:12:22 +01:00
|
|
|
// Copyright (C) 2022 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellitedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-06-27 19:10:48 +01:00
|
|
|
"database/sql"
|
|
|
|
"errors"
|
2022-05-12 16:12:22 +01:00
|
|
|
"time"
|
|
|
|
|
2023-08-19 06:52:13 +01:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2022-05-12 16:12:22 +01:00
|
|
|
"storj.io/common/uuid"
|
2023-08-19 06:52:13 +01:00
|
|
|
"storj.io/private/dbutil/pgutil"
|
2022-05-12 16:12:22 +01:00
|
|
|
"storj.io/storj/satellite/console/consoleauth"
|
|
|
|
"storj.io/storj/satellite/satellitedb/dbx"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ensures that *webappSessions implements consoleauth.WebappSessions.
|
|
|
|
var _ consoleauth.WebappSessions = (*webappSessions)(nil)
|
|
|
|
|
|
|
|
type webappSessions struct {
|
2023-06-27 19:10:48 +01:00
|
|
|
db *satelliteDB
|
2022-05-12 16:12:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create creates a webapp session and returns the session info.
|
|
|
|
func (db *webappSessions) Create(ctx context.Context, id, userID uuid.UUID, address, userAgent string, expiresAt time.Time) (session consoleauth.WebappSession, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
dbxSession, err := db.db.Create_WebappSession(ctx, dbx.WebappSession_Id(id.Bytes()), dbx.WebappSession_UserId(userID.Bytes()),
|
|
|
|
dbx.WebappSession_IpAddress(address), dbx.WebappSession_UserAgent(userAgent), dbx.WebappSession_ExpiresAt(expiresAt))
|
|
|
|
if err != nil {
|
|
|
|
return session, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return getSessionFromDBX(dbxSession)
|
|
|
|
}
|
|
|
|
|
2022-07-19 10:26:18 +01:00
|
|
|
// UpdateExpiration updates the expiration time of the session.
|
|
|
|
func (db *webappSessions) UpdateExpiration(ctx context.Context, sessionID uuid.UUID, expiresAt time.Time) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
_, err = db.db.Update_WebappSession_By_Id(
|
|
|
|
ctx,
|
|
|
|
dbx.WebappSession_Id(sessionID.Bytes()),
|
|
|
|
dbx.WebappSession_Update_Fields{
|
|
|
|
ExpiresAt: dbx.WebappSession_ExpiresAt(expiresAt),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-12 16:12:22 +01:00
|
|
|
// GetBySessionID gets the session info from the session ID.
|
|
|
|
func (db *webappSessions) GetBySessionID(ctx context.Context, sessionID uuid.UUID) (session consoleauth.WebappSession, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
dbxSession, err := db.db.Get_WebappSession_By_Id(ctx, dbx.WebappSession_Id(sessionID.Bytes()))
|
|
|
|
if err != nil {
|
|
|
|
return session, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return getSessionFromDBX(dbxSession)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAllByUserID gets all webapp sessions with userID.
|
|
|
|
func (db *webappSessions) GetAllByUserID(ctx context.Context, userID uuid.UUID) (sessions []consoleauth.WebappSession, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
dbxSessions, err := db.db.All_WebappSession_By_UserId(ctx, dbx.WebappSession_UserId(userID.Bytes()))
|
|
|
|
for _, dbxs := range dbxSessions {
|
|
|
|
s, err := getSessionFromDBX(dbxs)
|
|
|
|
if err != nil {
|
|
|
|
return sessions, err
|
|
|
|
}
|
|
|
|
sessions = append(sessions, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
return sessions, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteBySessionID deletes a webapp session by ID.
|
|
|
|
func (db *webappSessions) DeleteBySessionID(ctx context.Context, sessionID uuid.UUID) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
_, err = db.db.Delete_WebappSession_By_Id(ctx, dbx.WebappSession_Id(sessionID.Bytes()))
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteAllByUserID deletes all webapp sessions by user ID.
|
|
|
|
func (db *webappSessions) DeleteAllByUserID(ctx context.Context, userID uuid.UUID) (deleted int64, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
return db.db.Delete_WebappSession_By_UserId(ctx, dbx.WebappSession_UserId(userID.Bytes()))
|
|
|
|
}
|
|
|
|
|
2023-06-27 19:10:48 +01:00
|
|
|
// DeleteExpired deletes all sessions that have expired before the provided timestamp.
|
|
|
|
func (db *webappSessions) DeleteExpired(ctx context.Context, now time.Time, asOfSystemTimeInterval time.Duration, pageSize int) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
if pageSize <= 0 {
|
|
|
|
return Error.New("expected page size to be positive; got %d", pageSize)
|
|
|
|
}
|
|
|
|
|
2023-08-19 06:52:13 +01:00
|
|
|
var pageCursor uuid.UUID
|
|
|
|
selected := make([]uuid.UUID, pageSize)
|
2023-06-27 19:10:48 +01:00
|
|
|
aost := db.db.impl.AsOfSystemInterval(asOfSystemTimeInterval)
|
|
|
|
for {
|
|
|
|
// Select the ID beginning this page of records
|
|
|
|
err := db.db.QueryRowContext(ctx, `
|
|
|
|
SELECT id FROM webapp_sessions
|
|
|
|
`+aost+`
|
|
|
|
WHERE id > $1 AND expires_at < $2
|
|
|
|
ORDER BY id LIMIT 1
|
|
|
|
`, pageCursor, now).Scan(&pageCursor)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2023-08-19 06:52:13 +01:00
|
|
|
// Select page of records
|
|
|
|
rows, err := db.db.QueryContext(ctx, `
|
2023-06-27 19:10:48 +01:00
|
|
|
SELECT id FROM webapp_sessions
|
|
|
|
`+aost+`
|
2023-08-19 06:52:13 +01:00
|
|
|
WHERE id >= $1 ORDER BY id LIMIT $2
|
|
|
|
`, pageCursor, pageSize)
|
2023-06-27 19:10:48 +01:00
|
|
|
if err != nil {
|
2023-08-19 06:52:13 +01:00
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var i int
|
|
|
|
for i = 0; rows.Next(); i++ {
|
|
|
|
if err = rows.Scan(&selected[i]); err != nil {
|
2023-06-27 19:10:48 +01:00
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
2023-08-19 06:52:13 +01:00
|
|
|
}
|
|
|
|
if err = errs.Combine(rows.Err(), rows.Close()); err != nil {
|
2023-06-27 19:10:48 +01:00
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2023-08-19 06:52:13 +01:00
|
|
|
// Delete all expired records in the page
|
2023-06-27 19:10:48 +01:00
|
|
|
_, err = db.db.ExecContext(ctx, `
|
|
|
|
DELETE FROM webapp_sessions
|
2023-08-19 06:52:13 +01:00
|
|
|
WHERE id = ANY($1)
|
|
|
|
AND expires_at < $2
|
|
|
|
`, pgutil.UUIDArray(selected[:i]), now)
|
2023-06-27 19:10:48 +01:00
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2023-08-19 06:52:13 +01:00
|
|
|
if i < pageSize {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-27 19:10:48 +01:00
|
|
|
// Advance the cursor to the next page
|
2023-08-19 06:52:13 +01:00
|
|
|
pageCursor = selected[i-1]
|
2023-06-27 19:10:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-12 16:12:22 +01:00
|
|
|
func getSessionFromDBX(dbxSession *dbx.WebappSession) (consoleauth.WebappSession, error) {
|
|
|
|
id, err := uuid.FromBytes(dbxSession.Id)
|
|
|
|
if err != nil {
|
|
|
|
return consoleauth.WebappSession{}, err
|
|
|
|
}
|
|
|
|
userID, err := uuid.FromBytes(dbxSession.UserId)
|
|
|
|
if err != nil {
|
|
|
|
return consoleauth.WebappSession{}, err
|
|
|
|
}
|
|
|
|
return consoleauth.WebappSession{
|
|
|
|
ID: id,
|
|
|
|
UserID: userID,
|
|
|
|
Address: dbxSession.IpAddress,
|
|
|
|
UserAgent: dbxSession.UserAgent,
|
|
|
|
Status: dbxSession.Status,
|
|
|
|
ExpiresAt: dbxSession.ExpiresAt,
|
|
|
|
}, nil
|
|
|
|
}
|