Add user session management and auditing#36933
Draft
elesiuta wants to merge 1 commit intogo-gitea:mainfrom
Draft
Add user session management and auditing#36933elesiuta wants to merge 1 commit intogo-gitea:mainfrom
elesiuta wants to merge 1 commit intogo-gitea:mainfrom
Conversation
bircni
reviewed
Mar 20, 2026
| ip = host | ||
| } | ||
| if err := auth_model.CreateUserSession(req.Context(), &auth_model.UserSession{ | ||
| ID: sess.ID(), |
Contributor
There was a problem hiding this comment.
shouldnt this be newSess.ID() ?
The stored ID won't match the live session, so these records can never be correctly looked up or revoked.
| db.RegisterModel(new(UserSession)) | ||
| } | ||
|
|
||
| // CreateUserSession inserts a new user session record |
Contributor
There was a problem hiding this comment.
Why this comment? AI overexplaining?
| return db.Insert(ctx, session) | ||
| } | ||
|
|
||
| // GetUserSessionByID returns a single session by its ID |
| ) | ||
|
|
||
| // VirtualSessionProvider represents a shadowed session provider implementation. | ||
| // It wraps a real session provider and adds "tombstone" tracking for destroyed |
| LastIP: "10.0.0.1", | ||
| })) | ||
|
|
||
| // Update with same IP — only LastAccessUnix should change |
| return | ||
| } | ||
|
|
||
| // Create tracked user session record |
Contributor
There was a problem hiding this comment.
you added these lines 4x?
- routers/web/auth/auth.go::handleSignInFull
- routers/web/auth/auth.go::autoSignIn
- routers/web/auth/oauth.go::handleOAuth2SignIn
- services/auth/auth.go::handleSignIn
| } | ||
|
|
||
| // GetUserSessionsByUserID returns all sessions for a user, ordered by creation time descending | ||
| func GetUserSessionsByUserID(ctx context.Context, userID int64) ([]*UserSession, error) { |
Contributor
There was a problem hiding this comment.
This loads every session record (including old ended ones)
Is that really wanted?
| } | ||
|
|
||
| ctx.Data["Sessions"] = sessions | ||
| ctx.Data["SessionsTotal"] = len(sessions) |
Contributor
There was a problem hiding this comment.
Could be used in the template with len .Sessions ?
| // It deletes: | ||
| // - Sessions that were logged out more than retentionSeconds ago | ||
| // - Abandoned sessions (never logged out) whose last activity is older than maxLifetime + retentionSeconds | ||
| func CleanupExpiredUserSessions(ctx context.Context, retentionSeconds, maxLifetime int64) error { |
Contributor
There was a problem hiding this comment.
why not use time.Duration - Manually converting at the call site is not good - look at how other functions do that already
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
user_sessiontable stores metadata for each authenticated sessionIDUserIDLoginIPLastIPPrevIPUserAgentLoginMethodAuthTokenIDCreatedUnixLastAccessUnixLogoutUnixScreenshots