@notoofly/auth-server
v1.0.1
Published
Notoofly Auth Server - JWT verification and authorization middleware
Maintainers
Readme
@notoofly/auth-server
Production-ready JWT verification and authorization middleware for Node.js applications.
🚀 Features
- JWT Verification: Secure token validation with JWKS caching
- Framework Adapters: Native support for Express.js and Elysia
- Authorization: Role-based and permission-based access control
- Security: Race-safe fetching, input validation, error sanitization
- Performance: In-memory caching with configurable TTL
- TypeScript: Full type safety with strict typing
📦 Installation
npm install @notoofly/auth-server🔧 Quick Start
Express.js Integration
import express from 'express';
import { createAuthMiddleware, createRoleGuard } from '@notoofly/auth-server/express';
const app = express();
// Configure authentication middleware
const authMiddleware = createAuthMiddleware({
jwksUri: 'https://your-auth-domain.com/.well-known/jwks.json',
issuer: 'https://your-auth-domain.com',
audience: 'your-api',
cacheTtl: 300000, // 5 minutes
});
// Apply authentication to routes
app.use('/api', authMiddleware);
// Protected route with role guard
app.get('/api/admin', createRoleGuard('admin'), (req, res) => {
res.json({ message: 'Admin access granted' });
});
app.listen(3000);Elysia Integration
import { Elysia } from 'elysia';
import { createAuthPlugin, createRoleGuard } from '@notoofly/auth-server/elysia';
const app = new Elysia()
.use(createAuthPlugin({
jwksUri: 'https://your-auth-domain.com/.well-known/jwks.json',
issuer: 'https://your-auth-domain.com',
audience: 'your-api',
}))
.guard(
{ role: 'admin' },
({ user }) => ({ message: `Hello admin ${user.email}` })
)
.listen(3000);⚙️ Configuration
Environment Variables
Create a .env file based on .env.example:
# JWT Configuration
JWT_ISSUER=https://your-auth-domain.com
JWT_AUDIENCE=your-api-audience
JWKS_URI=https://your-auth-domain.com/.well-known/jwks.json
JWKS_CACHE_TTL=300000
# Security
NODE_ENV=production
LOG_LEVEL=info
ALLOWED_ORIGINS=https://yourdomain.com,https://app.yourdomain.com
# Rate Limiting
RATE_LIMIT_WINDOW_MS=60000
RATE_LIMIT_MAX_REQUESTS=100Configuration Options
interface JwtVerificationConfig {
jwksUri: string; // JWKS endpoint URL
issuer: string; // Expected token issuer
audience: string; // Expected token audience
cacheTtl?: number; // Cache TTL in milliseconds (default: 300000)
}🛡️ Security Features
Token Validation
- Algorithm Support: RS256 and ES256
- Claims Verification: iss, aud, exp, nbf
- Key Validation: Kid matching and key rotation support
Caching Strategy
- Race-Safe: Deduplicated concurrent requests
- Memory Efficient: Automatic cleanup of expired keys
- Configurable TTL: Default 5 minutes
Error Handling
- Sanitized Messages: Production-safe error responses
- Structured Logging: Consistent error format
- Graceful Degradation: Fallback for JWKS unavailability
📚 API Reference
Core Classes
JwtVerifier
Main JWT verification class.
import { JwtVerifier } from '@notoofly/auth-server';
const verifier = new JwtVerifier({
jwksUri: 'https://your-auth-domain.com/.well-known/jwks.json',
issuer: 'https://your-auth-domain.com',
audience: 'your-api',
});
// Verify access token
const user = await verifier.verifyAccessToken(token);Methods
verifyAccessToken(token: string): Promise<AuthUser>getUserFromToken(token: string): Promise<AuthUser>
Authorization Utilities
import {
hasRole,
hasPermission,
hasAllRoles,
hasAnyPermission,
checkGuard
} from '@notoofly/auth-server';Express Middleware
import {
createAuthMiddleware,
createRoleGuard,
createPermissionGuard,
createGuard
} from '@notoofly/auth-server/express';Elysia Plugin
import {
createAuthPlugin,
createRoleGuard,
createPermissionGuard,
createGuard
} from '@notoofly/auth-server/elysia';🔒 Best Practices
Production Deployment
Environment Configuration
NODE_ENV=production LOG_LEVEL=errorSecurity Headers
// Middleware automatically adds security headers app.use(helmet());Rate Limiting
// Configure rate limiting const rateLimit = rateLimit({ windowMs: 60000, max: 100 });Monitoring
// Add request logging app.use((req, res, next) => { console.log(`${req.method} ${req.path}`, { ip: req.ip, userAgent: req.get('User-Agent') }); next(); });
Security Considerations
- HTTPS Only: Always use HTTPS in production
- CORS Configuration: Configure allowed origins properly
- Key Rotation: Regularly rotate JWT signing keys
- Token Expiration: Use short-lived access tokens
- Error Messages: Don't expose sensitive information
🧪 Testing
# Run tests
npm test
# Run tests with coverage
npm run test:ci
# Type checking
npm run type-check
# Linting
npm run lint
npm run lint:fix📈 Performance
Benchmarks
- JWT Verification: <1ms average
- JWKS Caching: 99% hit rate
- Memory Usage: <10MB for 1000 cached keys
- Concurrent Requests: No race conditions
Optimization Tips
- Cache TTL: Balance between freshness and performance
- Connection Pooling: Reuse HTTP connections
- Memory Monitoring: Monitor cache size
- Load Balancing: Distribute across multiple instances
🔄 Migration
From v0.x to v1.0
Breaking changes:
- Package renamed to
@notoofly/auth-server - TypeScript exports now use
.jsextensions - Configuration options updated
Migration Steps
Update package.json:
{ "dependencies": { "@notoofly/auth-server": "^1.0.0" } }Update imports:
// Before import { JwtVerifier } from 'auth-server'; // After import { JwtVerifier } from '@notoofly/auth-server';Update configuration:
// Review configuration options for changes
🤝 Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Run
npm run test:ci - Submit a pull request
📄 License
MIT License – see LICENSE file for details.
