java.lang.Object
net.i2p.servlet.filters.SessionManager
Simple session manager for console authentication. Stores sessions in memory with expiration.
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classOne user's failed login count.private static classOne logged-in console session.Field Summary
Modifier and TypeFieldDescriptionprivate final ScheduledExecutorServiceScheduler for the cleanup jobs.private final Map<String, SessionManager.FailedLogin> Failed logins, by user name.private static final LogLogger for the console sessions.private final Map<String, SessionManager.Session> The active sessions, by token.private static final intInterval between cleanup runs.private static final longWindow the failed logins count in.private static final SessionManagerThe single instance of the filter.private static final intFailed logins before a lockout.private static final SecureRandomRandom source for the session tokens.static final StringSession attribute under which the console stores the logged-in username.static final StringName of the cookie carrying the session token to the console.private static final intTime an idle session stays valid.Constructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptionprivate voidRemoves the sessions past their expiry.private voidDrops the failed logins outside the window.voidClear failed login attempts for an IP after successful login.createSession(String username) Create a new session for a user with default 30 min expiry.createSession(String username, long expiryMs) Create a new session for a user with custom expiry.private StringGenerates a random session token.static SessionManagerReturns the single session manager, creating it on first use.voidinvalidateSession(String token) Drops a session token, so validateSession() stops accepting it.booleanCheck if IP is blocked due to too many failed attempts.voidRecord a failed login attempt from an IP address.voidrestoreSession(String token, String username, long expiresAt) Restore a persisted session with its original token.voidshutdown()Stops the cleanup scheduler, ending periodic session expiry.validateSession(String token) Validate a session token.
Field Details
_log
Logger for the console sessions.SESSION_TIMEOUT_MS
private static final int SESSION_TIMEOUT_MSTime an idle session stays valid.- See Also:
CLEANUP_INTERVAL_MS
private static final int CLEANUP_INTERVAL_MSInterval between cleanup runs.- See Also:
INSTANCE
The single instance of the filter.RANDOM
Random source for the session tokens._sessions
The active sessions, by token._failedLogins
Failed logins, by user name._cleanup
Scheduler for the cleanup jobs.MAX_FAILED_ATTEMPTS
private static final int MAX_FAILED_ATTEMPTSFailed logins before a lockout.- See Also:
FAILURE_WINDOW_MS
private static final long FAILURE_WINDOW_MSWindow the failed logins count in.- See Also:
SESSION_COOKIE_NAME
Name of the cookie carrying the session token to the console.- See Also:
SESSION_ATTR_USER
Session attribute under which the console stores the logged-in username.- See Also:
Constructor Details
SessionManager
private SessionManager()Builds the singleton and starts the cleanup.
Method Details
getInstance
Returns the single session manager, creating it on first use.- Returns:
- the singleton instance holding the live session table
createSession
Create a new session for a user with default 30 min expiry.- Parameters:
username- the user the session is issued to, returned byvalidateSession(String)- Returns:
- the 64-character hex session token to hand to the client
createSession
Create a new session for a user with custom expiry.- Parameters:
username- the user the session is issued to, returned by validateSession()expiryMs- expiry time in milliseconds, or -1 for no expiry (forever)- Returns:
- the 64-character hex session token to hand to the client
validateSession
restoreSession
Restore a persisted session with its original token. Used on router restart to maintain login sessions.- Parameters:
token- the original session token from persistenceusername- the user the restored session belongs to, returned by validateSession()expiresAt- absolute expiry time, or -1 for no expiry
invalidateSession
Drops a session token, so validateSession() stops accepting it.- Parameters:
token- the token to drop from the session table; null is ignored
generateToken
cleanupExpiredSessions
private void cleanupExpiredSessions()Removes the sessions past their expiry.shutdown
public void shutdown()Stops the cleanup scheduler, ending periodic session expiry.recordFailedLogin
Record a failed login attempt from an IP address.- Parameters:
ip- the client address the attempt came from, used as the counter key
isBlocked
Check if IP is blocked due to too many failed attempts.- Parameters:
ip- the client address to test against the recent-failure counters- Returns:
- true if blocked
clearFailedLogins
Clear failed login attempts for an IP after successful login.- Parameters:
ip- the client address whose failure counter is discarded
cleanupFailedLogins
private void cleanupFailedLogins()Drops the failed logins outside the window.