Skip to content

Commit 38d498c

Browse files
committed
feat(examples): add cryptographic and HTTP server examples ✨
- Add salt-hash examples demonstrating crypto operations and key rotation - Add HTTP server example with JWT authentication and role-based access - Include comprehensive README documentation for both example categories - Provide zero-dependency examples using only Node.js built-in modules - Show real-world integration patterns for Secure-JWT library
1 parent 2482614 commit 38d498c

File tree

5 files changed

+1071
-0
lines changed

5 files changed

+1071
-0
lines changed

examples/http-server/README.md

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# 🌐 HTTP Server Examples
2+
3+
This directory contains HTTP server examples demonstrating how to use Secure-JWT in real-world scenarios.
4+
5+
## 📚 Examples
6+
7+
### 🚀 Simple Server (`simple-server.ts`)
8+
9+
A complete HTTP server implementation using only Node.js built-in modules - no external dependencies!
10+
11+
**Features:**
12+
- **Login endpoint** - Authenticate users and issue JWT tokens
13+
- **Protected endpoints** - Verify JWT tokens for access control
14+
- **Role-based access** - Admin-only endpoints
15+
- **Token verification** - Validate existing tokens
16+
- **Health check** - Server status and JWT configuration
17+
- **CORS support** - Cross-origin requests
18+
- **Error handling** - Comprehensive error responses
19+
20+
**Endpoints:**
21+
- `POST /login` - Login with username/password
22+
- `GET /protected` - Access protected resource
23+
- `GET /profile` - Get user profile
24+
- `GET /admin` - Admin only access
25+
- `POST /verify` - Verify token
26+
- `GET /health` - Health check
27+
28+
---
29+
30+
## 🚀 Quick Start
31+
32+
1. **Start the server:**
33+
```bash
34+
npx tsx examples/http-server/simple-server.ts
35+
```
36+
37+
2. **Test the endpoints:**
38+
```bash
39+
# Login
40+
curl -X POST http://localhost:3000/login \
41+
-H "Content-Type: application/json" \
42+
-d '{"username":"admin","password":"admin123"}'
43+
44+
# Use the token from login response
45+
curl -H "Authorization: Bearer <your-token>" \
46+
http://localhost:3000/protected
47+
```
48+
49+
## 👥 Test Users
50+
51+
| Username | Password | Role |
52+
|----------|----------|-------|
53+
| admin | admin123 | admin |
54+
| user | user123 | user |
55+
| john | john123 | user |
56+
57+
## 🔒 Security Features
58+
59+
- **Authenticated Encryption** - All tokens are encrypted with AES-256-GCM
60+
- **Key Derivation** - Uses PBKDF2 for secure key generation
61+
- **Version Control** - Prevents downgrade attacks
62+
- **Input Validation** - Comprehensive validation at every layer
63+
- **Error Handling** - Secure error responses without information leakage
64+
65+
## 🏭 Production Considerations
66+
67+
- **Change the secret key** - Use a strong, random secret in production
68+
- **Use environment variables** - Store secrets securely
69+
- **Add rate limiting** - Prevent brute force attacks
70+
- **Use HTTPS** - Encrypt all communications
71+
- **Add logging** - Monitor authentication events
72+
- **Database integration** - Replace mock user database
73+
74+
## 📝 Example Responses
75+
76+
### ✅ Login Success
77+
```json
78+
{
79+
"message": "Login successful",
80+
"token": "eyJlbmNyeXB0ZWQiOiI...",
81+
"user": {
82+
"userId": 1,
83+
"username": "admin",
84+
"role": "admin"
85+
},
86+
"expiresIn": "1h"
87+
}
88+
```
89+
90+
### 🔐 Protected Resource Access
91+
```json
92+
{
93+
"message": "Access granted to protected resource",
94+
"user": {
95+
"userId": 1,
96+
"username": "admin",
97+
"role": "admin"
98+
},
99+
"data": {
100+
"users": [...],
101+
"stats": {...}
102+
}
103+
}
104+
```
105+
106+
### ❌ Error Response
107+
```json
108+
{
109+
"error": "Invalid or expired token",
110+
"details": null,
111+
"timestamp": "2024-01-15T10:30:00.000Z"
112+
}
113+
```
114+
115+
## 💡 Why This Example?
116+
117+
This example demonstrates:
118+
- **Real-world usage** - How JWT authentication works in practice
119+
- **Zero dependencies** - Pure Node.js implementation
120+
- **Security best practices** - Proper token handling and validation
121+
- **Production patterns** - Error handling, CORS, health checks
122+
- **Role-based access** - Different permission levels
123+
124+
Perfect for understanding how to integrate Secure-JWT into your applications! 🚀

0 commit comments

Comments
 (0)