mcpField
S
session-management

session-management

github.com/aj-geddesSecurity374463 installsUpdated 34w ago
Repository

About session-management

A skill for implementing session management across backend frameworks, covering secure token handling, persistence, refresh, logout, and CSRF protection. It includes guidance on JWT, Redis storage, middleware, and session maintenance.

Key features

  1. 01Secure token handling
  2. 02Session persistence
  3. 03Token refresh mechanisms
  4. 04Logout procedures
  5. 05CSRF protection
  6. 06Session cleanup guidance

Use cases

  1. 01Implementing user authentication
  2. 02Managing session state and context
  3. 03Handling JWT refresh cycles
  4. 04Protecting state-changing requests from CSRF

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/aj-geddes/useful-ai-prompts --skill session-management

After installing, type/session-managementin Claude Code to confirm it loaded.

Bundled files

References and scripts installed along with SKILL.md.

references/
csrf-protection.md
jwt-token-generation-and-validation.md
nodejsexpress-jwt-implementation.md
session-cleanup-and-maintenance.md
session-middleware-chain.md
session-storage-with-redis.md
token-refresh-endpoint.md
scripts/
security-checklist.sh

Details

From the project README

Session Management

Table of Contents

Overview

Implement comprehensive session management systems with secure token handling, session persistence, token refresh mechanisms, proper logout procedures, and CSRF protection across different backend frameworks.

When to Use

  • Implementing user authentication systems
  • Managing session state and user context
  • Handling JWT token refresh cycles
  • Implementing logout functionality
  • Protecting against CSRF attacks
  • Managing session expiration and cleanup

Quick Start

Minimal working example:

# Python/Flask Example
from flask import current_app
from datetime import datetime, timedelta
import jwt
import os

class TokenManager:
    def __init__(self, secret_key=None):
        self.secret_key = secret_key or os.getenv('JWT_SECRET')
        self.algorithm = 'HS256'
        self.access_token_expires_hours = 1
        self.refresh_token_expires_days = 7

    def generate_tokens(self, user_id, email, role='user'):
        """Generate both access and refresh tokens"""
        now = datetime.utcnow()

        # Access token
        access_payload = {
            'user_id': user_id,
            'email': email,
            'role': role,
            'type': 'access',
            'iat': now,
            'exp': now + timedelta(hours=self.access_token_expires_hours)
// ... (see reference guides for full implementation)

Reference Guides

Detailed implementations in the references/ directory:

GuideContents
JWT Token Generation and ValidationJWT Token Generation and Validation
Node.js/Express JWT ImplementationNode.js/Express JWT Implementation
Session Storage with RedisSession Storage with Redis
CSRF ProtectionCSRF Protection
Session Middleware ChainSession Middleware Chain
Token Refresh EndpointToken Refresh Endpoint
Session Cleanup and MaintenanceSession Cleanup and Maintenance

Best Practices

✅ DO
  • Use HTTPS for all session transmission
  • Implement secure cookies (httpOnly, sameSite, secure flags)
  • Use JWT with proper expiration times
  • Implement token refresh mechanism
  • Store refresh tokens securely
  • Validate tokens on every request
  • Use strong secret keys
  • Implement session timeout
  • Log authentication events
  • Clear session data on logout
  • Use CSRF tokens for state-changing requests
❌ DON'T
  • Store sensitive data in tokens
  • Use short secret keys
  • Transmit tokens in URLs
  • Ignore token expiration
  • Reuse token secrets across environments
  • Store tokens in localStorage (use httpOnly cookies)
  • Implement session without HTTPS
  • Forget to validate token signatures
  • Expose session IDs in logs
  • Use predictable session IDs

Comments

Markdown supported