Skip to content

Add user session management and auditing#36933

Draft
elesiuta wants to merge 1 commit intogo-gitea:mainfrom
elesiuta:session-management
Draft

Add user session management and auditing#36933
elesiuta wants to merge 1 commit intogo-gitea:mainfrom
elesiuta:session-management

Conversation

@elesiuta
Copy link
Contributor

Field Type Description
ID VARCHAR(64) pk go-chi session key
UserID INT64, indexed user who owns the session
LoginIP VARCHAR(45) IP at login time
LastIP VARCHAR(45) most recent IP seen
PrevIP VARCHAR(45) IP before LastIP (shift on change)
UserAgent TEXT browser user-agent at login
LoginMethod VARCHAR(64) "password", "oauth2:github", etc.
AuthTokenID VARCHAR(64) remember-me token ID (if applicable)
CreatedUnix TIMESTAMP, indexed login time
LastAccessUnix TIMESTAMP, indexed last activity (throttled ~5min)
LogoutUnix TIMESTAMP, indexed 0=active, else when session ended

Screenshots

Screenshot 1 Screenshot 2 Screenshot 3 Screenshot 4

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Mar 20, 2026
@github-actions github-actions bot added modifies/go Pull requests that update Go code modifies/templates This PR modifies the template files modifies/migrations docs-update-needed The document needs to be updated synchronously labels Mar 20, 2026
ip = host
}
if err := auth_model.CreateUserSession(req.Context(), &auth_model.UserSession{
ID: sess.ID(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this comment? AI overexplaining?

return db.Insert(ctx, session)
}

// GetUserSessionByID returns a single session by its ID
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

)

// VirtualSessionProvider represents a shadowed session provider implementation.
// It wraps a real session provider and adds "tombstone" tracking for destroyed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need?

LastIP: "10.0.0.1",
}))

// Update with same IP — only LastAccessUnix should change
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need of the comments?

return
}

// Create tracked user session record
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This loads every session record (including old ended ones)
Is that really wanted?

}

ctx.Data["Sessions"] = sessions
ctx.Data["SessionsTotal"] = len(sessions)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not use time.Duration - Manually converting at the call site is not good - look at how other functions do that already

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs-update-needed The document needs to be updated synchronously lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. modifies/go Pull requests that update Go code modifies/migrations modifies/templates This PR modifies the template files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] Option to view and manage all active sessions User session managment and audit

3 participants