Skip to main content

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.

Project description

****# 🛡️ 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.


📁 File Structure

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

📦 Components Overview

1. Model: UserSession

This model keeps track of each authenticated session for a user.

Fields:

  • 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.

Methods:

  • 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.

2. Serializer: SessionSerializer

Serializes session data for API representation.

Fields:

  • 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.

3. Authentication: UserSessionDBTokenAuthentication

Custom authentication class replacing TokenAuthentication.

Features:

  • Checks session token validity (via DB and cache).
  • Validates the user agent; suspends session if it changes.
  • Caches active sessions to improve performance.
  • Automatically saves updated session data if IP changes.

Important Methods:

  • 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.

4. Authentication: UserSessionDBJwtAuthentication

Custom authentication class replacing JWTAuthentication.

Features:

  • Checks session token validity (via DB and cache).
  • Validates the user agent; suspends session if it changes.
  • Caches active sessions to improve performance.
  • Automatically saves updated session data if IP changes.

Important Methods:

  • 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.

5. Utility Functions: 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.

6. API View: ListDestroyActiveSessionsApi

RESTful endpoint to list and delete sessions.

Features:

  • GET /active/: List all active sessions for the authenticated user.
  • DELETE /active/?token_id=...: Log out (deactivate) one or more sessions.

Permissions:

  • IsAuthenticated: User must be logged in to interact with this endpoint.

7. Admin: UserSessionAdmin

Django Admin panel configuration for UserSession.

Features:

  • Displays key session info (id, key, user, expire_at, etc.).
  • Read-only fields: prevents changing sensitive data post-creation.
  • Filters: View active/inactive sessions easily.

🚀 Usage Guide

Follow these steps to integrate and use the Nilva User Session Management system in your Django project:


8. Add App to INSTALLED_APPS

In your settings.py:

INSTALLED_APPS = [ ... 'nilva_session', ]


🔧 Configuration

9. Configure REST Framework Authentication

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',
        ...
    ],
}****

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nilva_session-0.1.4.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nilva_session-0.1.4-py3-none-any.whl (15.7 kB view details)

Uploaded Python 3

File details

Details for the file nilva_session-0.1.4.tar.gz.

File metadata

  • Download URL: nilva_session-0.1.4.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.11.2 Linux/6.1.0-34-amd64

File hashes

Hashes for nilva_session-0.1.4.tar.gz
Algorithm Hash digest
SHA256 7bbb1ee97b33a1db47e83ef52ae8b48ce9f90994454b5f1ddba4b38b4a3db612
MD5 3e24767d3171029e2636f088a831cb58
BLAKE2b-256 65d1f9fe2eae3606c659bccc3ed5c3ca12b122e0ae4848231382fb16a0ac374f

See more details on using hashes here.

File details

Details for the file nilva_session-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: nilva_session-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 15.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.11.2 Linux/6.1.0-34-amd64

File hashes

Hashes for nilva_session-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 fd2bdfc0ec1d4ba1d3f77b6c66de1f6b2fd1375e5f2c49f501935833f21d6f2b
MD5 3f1d446134063bff10b2cb46918baf37
BLAKE2b-256 0fc40613c14903279bcefc8668f8240ebcd28c699f589085f2f6b51000323c1e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page