
Product
Secure Your AI-Generated Code with Socket MCP
Socket MCP brings real-time security checks to AI-generated code, helping developers catch risky dependencies before they enter the codebase.
This module provides a robust and secure implementation of user session management based on **database-backed token authentication**, with features like caching, session expiration, user agent validation, and session invalidation.
****# π‘οΈ Nilva User Session Management
This module provides a robust and secure implementation of user session management based on database-backed token authentication, with features like caching, session expiration, user agent validation, and session invalidation.
nilva_session/ β βββ models.py # UserSession model βββ serializers.py # SessionSerializer , TokenSessionObtainPairSerializer βββ authentication.py # Custom authentication βββ utils.py # Token utilities βββ urls.py # Endpoint routes βββ apis.py # API views βββ admin.py # Django admin customization
UserSession
This model keeps track of each authenticated session for a user.
id
(UUID): Unique identifier for the session.user
(ForeignKey): Link to the authenticated user.key
(CharField): A unique session token (used for authentication).expire_at
(DateTimeField): Expiration timestamp of the session.is_active
(Boolean): Indicates if the session is currently valid.last_online
(DateTimeField): Last activity timestamp.last_ip
(GenericIPAddressField): IP address of the user.user_agent
(TextField): Raw user agent string from the request.detail
(TextField): Additional context (e.g., reason for suspension).created_at
(DateTimeField): Session creation timestamp.is_expired
: Checks if the session has expired and updates last_online
.user_agent_data
: Parses and returns user agent info (OS, browser, device).get_cache_key(key)
: Generates a cache key for storing/retrieving the session key.get_cache_key_by_session_id(session_id)
: Generates a cache key for storing/retrieving the session id.invalidate_cache(key)
: Deletes the cached session from memory.invalidate_cache_by_session_id(session_id)
: Deletes the cached session from memory.SessionSerializer
Serializes session data for API representation.
session_id
: Read-only primary key (UUID).user_id
: Associated user ID.user_agent_data
: Structured data from the user agent string.last_online
: Last activity timestamp.last_ip
: User's IP address.created_at
: Creation time of the session.UserSessionDBTokenAuthentication
Custom authentication class replacing TokenAuthentication
.
authenticate
: Extracts token and authenticates the user.authenticate_credentials
: Loads session from cache or DB.check_user_agent
: Suspends session if user agent changes.create_user_session_from_request(user, request)
: Factory to create a session from request meta.UserSessionDBJwtAuthentication
Custom authentication class replacing JWTAuthentication
.
authenticate
: Extracts token and authenticates the user.get_user
: get user from session .check_user_agent
: Suspends session if user agent changes.create_user_session_from_request(user, request)
: Factory to create a session from request meta.utils.py
Reusable utility logic for session creation and management.
random_token_generator(size)
: Generates a random token of given size.token_generator()
: Generates session token using app settings.token_expire_at_generator()
: Returns expiration time using settings.hash_data(data)
: Hashes a string with SHA-256.ListDestroyActiveSessionsApi
RESTful endpoint to list and delete sessions.
GET /active/
: List all active sessions for the authenticated user.DELETE /active/?token_id=...
: Log out (deactivate) one or more sessions.IsAuthenticated
: User must be logged in to interact with this endpoint.UserSessionAdmin
Django Admin panel configuration for UserSession
.
id
, key
, user
, expire_at
, etc.).Follow these steps to integrate and use the Nilva User Session Management system in your Django project:
INSTALLED_APPS
In your settings.py
:
INSTALLED_APPS = [ ... 'nilva_session', ]
To enable token-based session authentication, add the following to your settings.py
:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'nilva_session.backends.UserSessionDBTokenAuthentication',
'nilva_session.backends.UserSessionDBJwtAuthentication',
...
],
}****
FAQs
This module provides a robust and secure implementation of user session management based on **database-backed token authentication**, with features like caching, session expiration, user agent validation, and session invalidation.
We found that nilva-session demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.Β It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket MCP brings real-time security checks to AI-generated code, helping developers catch risky dependencies before they enter the codebase.
Security News
As vulnerability data bottlenecks grow, the federal government is formally investigating NISTβs handling of the National Vulnerability Database.
Research
Security News
Socketβs Threat Research Team has uncovered 60 npm packages using post-install scripts to silently exfiltrate hostnames, IP addresses, DNS servers, and user directories to a Discord-controlled endpoint.