@ahhaohho/auth-middleware
Version:
Shared authentication middleware with Passport.js for ahhaohho microservices
251 lines (185 loc) • 5.24 kB
Markdown
ㅈ
Shared authentication middleware with Passport.js for ahhaohho microservices.
- ✅ Passport.js JWT authentication strategy
- ✅ Multi-key JWT verification with fallback support
- ✅ Redis-based token blacklist
- ✅ AWS Secrets Manager integration
- ✅ Express middleware ready
```bash
npm install @ahhaohho/auth-middleware
```
Or add to `package.json`:
```json
{
"dependencies": {
"@ahhaohho/auth-middleware": "^1.0.2"
}
}
```
```bash
npm install git+ssh://git@github.com:Future-Lab-META/auth-middleware.git#v1.0.2
```
Or add to `package.json`:
```json
{
"dependencies": {
"@ahhaohho/auth-middleware": "git+ssh://git@github.com:Future-Lab-META/auth-middleware.git#v1.0.2"
}
}
```
```javascript
const express = require('express');
const { authenticateJWT, authenticateRefresh } = require('@ahhaohho/auth-middleware');
const app = express();
// Environment variables required
// AWS_REGION=ap-northeast-2
// REDIS_HOST=your-redis-host
// REDIS_PORT=6379
// JWT_SECRET_NAME=your-secret-name
// Protected routes
app.get('/api/verify', authenticateJWT, (req, res) => {
res.json({
userId: req.user.userId,
userRole: req.user.userRole
});
});
app.get('/api/refresh', authenticateRefresh, (req, res) => {
// Generate new access token
res.json({ newAccessToken: '...' });
});
app.listen(3000);
```
```bash
AWS_REGION=ap-northeast-2
REDIS_HOST=your-redis-host
REDIS_PORT=6379
JWT_SECRET_NAME=your-secret-name
ELASTICACHE_ENDPOINT=your-elasticache-endpoint
REDIS_TLS=true
```
- **REDIS_HOST**: If set, takes priority over ELASTICACHE_ENDPOINT
- **ELASTICACHE_ENDPOINT**: Used only when REDIS_HOST is not set
- **TLS Auto-detection**:
- TLS is automatically disabled for `localhost` and `127.0.0.1`
- TLS is automatically enabled when using ELASTICACHE_ENDPOINT (without REDIS_HOST)
- Use `REDIS_TLS=true` to force enable TLS for any host
```
Request with JWT
↓
authenticateJWT middleware
↓
Extract token from Authorization header
↓
Verify with current JWT key
↓ (if fails)
Fallback to previous JWT key
↓
Check Redis blacklist
↓
Inject user data to req.user
↓
Next middleware
```
Supports seamless JWT key rotation:
- Verifies with current key first
- Falls back to previous key if current fails
- Allows zero-downtime key rotation
Uses Redis to maintain revoked tokens:
- Stores blacklisted tokens per user
- Automatically expires with token TTL
- Checked on every authentication
Passport.js middleware for JWT authentication.
**Headers:**
- `Authorization: Bearer <access_token>`
**Sets:**
- `req.user`: `{ userId, userRole, phoneNumber }`
**Errors:**
- 401: Unauthorized (invalid or expired token)
- 500: Authentication error
Passport.js middleware for refresh token authentication.
**Headers:**
- `Refresh-Token: Bearer <refresh_token>`
**Sets:**
- `req.user`: `{ userId, userRole, phoneNumber }`
**Errors:**
- 401: Invalid refresh token
- 500: Token refresh error
```
auth-middleware/
├── src/
│ ├── index.js
│ ├── strategies/
│ │ ├── jwt.strategy.js
│ │ └── refresh.strategy.js
│ ├── middleware/
│ │ └── auth.js
│ ├── utils/
│ │ ├── jwtValidator.js
│ │ ├── blacklist.js
│ │ └── secretManager.js
│ └── config/
│ └── redis.js
├── package.json
└── README.md
```
```bash
git clone git@github.com:Future-Lab-META/auth-middleware.git
cd auth-middleware
npm install
npm link
npm link @ahhaohho/auth-middleware
```
This package follows [Semantic Versioning](https://semver.org/).
```bash
npm version patch
npm version minor
npm version major
git push origin main --tags
```
```bash
npm install @ahhaohho/auth-middleware@1.0.2
npm install git+ssh://git@github.com:Future-Lab-META/auth-middleware.git#v1.0.2
```
Or in `package.json`:
```json
{
"dependencies": {
"@ahhaohho/auth-middleware": "1.0.2"
}
}
```
See [MIGRATION.md](./MIGRATION.md) for detailed migration guide from HTTP-based authentication to Passport.js.
MIT