Skip to main content

Authentication & Authorization

The authentication utilities provide comprehensive authentication and authorization capabilities specifically designed for edge environments, supporting JWT tokens, API keys, session management, and OAuth flows.

Features

  • JWT Management: Secure JWT token generation, verification, and refresh
  • API Key Management: HMAC-based API key generation and validation
  • Session Management: Distributed session handling with automatic cleanup
  • OAuth Support: OAuth 2.0 client and server implementations
  • Security Features: Token blacklisting, rotation, and secure storage
  • Edge Optimized: Designed for low-latency edge environments
  • Audit Logging: Comprehensive authentication event logging

Quick Start

JWTManager

Secure JWT token management with automatic refresh and blacklisting capabilities.

Constructor Options

Token Generation

Token Verification

Token Refresh

Token Blacklisting

APIKeyManager

HMAC-based API key generation and validation with quota management.

Constructor Options

API Key Generation

API Key Validation

API Key Management

EdgeSessionManager

Distributed session management with automatic cleanup and security features.

Constructor Options

Session Creation

Session Retrieval

Session Updates

Session Destruction

Session Cookies

OAuthManager

OAuth 2.0 client and server implementations for edge environments.

OAuth Client

OAuth Server

Security Features

Token Rotation

Audit Logging

Middleware Integration

Authentication Middleware

Session Middleware

Advanced Examples

Complete Authentication Flow

Performance Considerations

JWT Performance

  • Use HS256 for best performance in edge environments
  • Keep payload size minimal
  • Cache verification results when possible
  • Use short expiration times for sensitive operations

Session Performance

  • Use distributed storage (KV, Redis) for multi-region deployments
  • Implement session cleanup to prevent storage bloat
  • Use appropriate TTL values based on application needs
  • Consider session compression for large session data

API Key Performance

  • Cache validation results with short TTL
  • Use efficient storage backends
  • Implement quota checking with minimal database calls
  • Consider rate limiting for API key validation endpoints

Platform-Specific Notes

Cloudflare Workers

  • Compatible with Cloudflare KV for distributed storage
  • Supports Durable Objects for advanced session management
  • Use Web Crypto API for cryptographic operations

Vercel Edge Functions

  • Compatible with Edge Config for storage
  • Supports environment variables for secrets
  • Use Vercel KV for distributed sessions

Deno Deploy

  • Native performance with Deno runtime
  • Compatible with Deno KV for storage
  • Supports all modern cryptographic APIs

Security Best Practices

Token Security

  • Use strong, randomly generated secrets
  • Implement token rotation policies
  • Set appropriate expiration times
  • Use HTTPS for all authentication endpoints

Session Security

  • Use secure, httpOnly cookies
  • Implement CSRF protection
  • Set appropriate session timeouts
  • Clean up expired sessions regularly

API Key Security

  • Use HMAC for key validation
  • Implement key rotation policies
  • Set reasonable quota limits
  • Monitor key usage patterns

Testing

Run authentication tests with:

API Reference

JWTManager Methods

  • generate(payload, options) - Generate JWT token
  • verify(token, options) - Verify JWT token
  • decode(token) - Decode token without verification
  • refresh(refreshToken) - Refresh access token
  • blacklist(token) - Blacklist token
  • isBlacklisted(token) - Check if token is blacklisted

APIKeyManager Methods

  • generate(options) - Generate API key
  • validate(key, permission) - Validate API key
  • checkQuota(key) - Check API key quota
  • list(userId) - List API keys for user
  • update(keyId, updates) - Update API key
  • revoke(keyId) - Revoke API key
  • rotate(keyId) - Rotate API key

EdgeSessionManager Methods

  • create(data, userId) - Create session
  • get(sessionId) - Get session data
  • getFromRequest(request) - Get session from request
  • update(sessionId, data) - Update session
  • extend(sessionId, ttl) - Extend session TTL
  • touch(sessionId) - Update last access time
  • destroy(sessionId) - Destroy session
  • destroyUserSessions(userId) - Destroy all user sessions
  • setCookie(response, sessionId) - Set session cookie
  • clearCookie(response) - Clear session cookie

Contributing

When contributing to authentication utilities:
  1. Maintain security best practices
  2. Add comprehensive tests for security features
  3. Update documentation for API changes
  4. Follow OWASP security guidelines
  5. Test across all supported platforms

License

MIT