@chdlabs/my-shield 0.2.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,370 @@
1
+ /**
2
+ * Shield OTP Admin Verification Gate
3
+ *
4
+ * Provides a zero-dependency MFA challenge gate for the Shield Admin Dashboard
5
+ * and sensitive governance endpoints. Uses cryptographically secure OTP generation,
6
+ * timing-safe HMAC validation, rate limiting, and a self-contained dark-mode challenge UI.
7
+ *
8
+ * @author ChdLabs
9
+ * @license MIT
10
+ */
11
+ import crypto from 'node:crypto';
12
+
13
+ export class OTPGate {
14
+ /**
15
+ * @param {Object} [options={}]
16
+ * @param {string} [options.adminEmail]
17
+ * @param {string} [options.jwtSecret] Secret for HMAC signing (auto-generated if omitted)
18
+ * @param {string} [options.cookieName='shield_session']
19
+ * @param {number} [options.codeTtlMs=300000] 5 minutes default
20
+ * @param {number} [options.sessionTtlMs=86400000] 24 hours default
21
+ * @param {number} [options.maxAttempts=5]
22
+ * @param {Function} [options.sendOtp] Async delivery function: async (email, code) => void
23
+ * @param {Object} [options.logger]
24
+ */
25
+ constructor(options = {}) {
26
+ this.adminEmail = options.adminEmail || 'admin@localhost';
27
+ this.secret = options.jwtSecret || options.secret || crypto.randomBytes(32).toString('hex');
28
+ this.cookieName = options.cookieName || 'shield_session';
29
+ this.codeTtlMs = options.codeTtlMs || 5 * 60 * 1000;
30
+ this.sessionTtlMs = options.sessionTtlMs || 24 * 60 * 60 * 1000;
31
+ this.maxAttempts = Math.max(1, options.maxAttempts || 5);
32
+ this.logger = options.logger || console;
33
+
34
+ // Fallback default sender logs code to console if no mailer provided
35
+ this.sendOtp = options.sendOtp || (async (email, code) => {
36
+ this.logger.info?.(`[SHIELD:OTP] Verification code for ${email}: ${code}`);
37
+ });
38
+
39
+ // In-memory active challenge store: email -> { code, expiresAt, attempts }
40
+ this.challenges = new Map();
41
+ }
42
+
43
+ /**
44
+ * Generate secure 6-digit numeric OTP code
45
+ * @returns {string}
46
+ */
47
+ generateCode() {
48
+ return crypto.randomInt(100000, 1000000).toString();
49
+ }
50
+
51
+ /**
52
+ * Create an HMAC signed session token
53
+ * @param {string} email
54
+ * @returns {string}
55
+ */
56
+ createSessionToken(email) {
57
+ const expiresAt = Date.now() + this.sessionTtlMs;
58
+ const payload = `${email}:${expiresAt}`;
59
+ const signature = crypto.createHmac('sha256', this.secret).update(payload).digest('hex');
60
+ return Buffer.from(`${payload}:${signature}`).toString('base64url');
61
+ }
62
+
63
+ /**
64
+ * Verify session token using timing-safe comparison
65
+ * @param {string} token
66
+ * @returns {{ valid: boolean, email?: string }}
67
+ */
68
+ verifySessionToken(token) {
69
+ if (!token || typeof token !== 'string') return { valid: false };
70
+ try {
71
+ const raw = Buffer.from(token, 'base64url').toString('utf8');
72
+ const parts = raw.split(':');
73
+ if (parts.length < 3) return { valid: false };
74
+
75
+ const signature = parts.pop();
76
+ const expiresAt = parseInt(parts.pop(), 10);
77
+ const email = parts.join(':');
78
+
79
+ if (!expiresAt || expiresAt < Date.now()) {
80
+ return { valid: false };
81
+ }
82
+
83
+ const payload = `${email}:${expiresAt}`;
84
+ const expected = crypto.createHmac('sha256', this.secret).update(payload).digest('hex');
85
+
86
+ const sigBuf = Buffer.from(signature, 'hex');
87
+ const expBuf = Buffer.from(expected, 'hex');
88
+
89
+ if (sigBuf.length !== expBuf.length || !crypto.timingSafeEqual(sigBuf, expBuf)) {
90
+ return { valid: false };
91
+ }
92
+
93
+ return { valid: true, email };
94
+ } catch {
95
+ return { valid: false };
96
+ }
97
+ }
98
+
99
+ /**
100
+ * Parse cookies from request header
101
+ * @param {Object} req
102
+ * @returns {Record<string, string>}
103
+ */
104
+ parseCookies(req) {
105
+ const header = req.headers?.cookie;
106
+ if (!header) return {};
107
+ const cookies = {};
108
+ for (const pair of header.split(';')) {
109
+ const [k, ...v] = pair.trim().split('=');
110
+ if (k) cookies[k] = decodeURIComponent(v.join('='));
111
+ }
112
+ return cookies;
113
+ }
114
+
115
+ /**
116
+ * Render self-contained zero-dependency dark-mode HTML challenge page
117
+ */
118
+ renderChallengeHtml(req, errorMessage = '') {
119
+ const baseUrl = req.baseUrl || '';
120
+ return `<!DOCTYPE html>
121
+ <html lang="en">
122
+ <head>
123
+ <meta charset="UTF-8">
124
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
125
+ <title>Shield Admin Authentication</title>
126
+ <style>
127
+ :root {
128
+ --bg: #090d16;
129
+ --surface: #131b2e;
130
+ --border: #1f2d4d;
131
+ --primary: #3b82f6;
132
+ --primary-hover: #2563eb;
133
+ --text: #f8fafc;
134
+ --muted: #94a3b8;
135
+ --danger: #ef4444;
136
+ --success: #10b981;
137
+ }
138
+ * { box-sizing: border-box; margin: 0; padding: 0; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; }
139
+ body {
140
+ background-color: var(--bg);
141
+ color: var(--text);
142
+ display: flex;
143
+ align-items: center;
144
+ justify-content: center;
145
+ min-height: 100vh;
146
+ padding: 1.5rem;
147
+ }
148
+ .card {
149
+ background: var(--surface);
150
+ border: 1px solid var(--border);
151
+ border-radius: 12px;
152
+ padding: 2.5rem;
153
+ width: 100%;
154
+ max-width: 420px;
155
+ box-shadow: 0 20px 40px rgba(0,0,0,0.4);
156
+ }
157
+ .header { text-align: center; margin-bottom: 2rem; }
158
+ .logo { font-size: 2.5rem; margin-bottom: 0.5rem; }
159
+ h1 { font-size: 1.5rem; font-weight: 600; margin-bottom: 0.25rem; }
160
+ p.subtitle { color: var(--muted); font-size: 0.875rem; }
161
+ .alert {
162
+ background: rgba(239, 68, 68, 0.15);
163
+ border: 1px solid var(--danger);
164
+ color: #fca5a5;
165
+ padding: 0.75rem 1rem;
166
+ border-radius: 6px;
167
+ font-size: 0.875rem;
168
+ margin-bottom: 1.25rem;
169
+ }
170
+ .success-box {
171
+ background: rgba(16, 185, 129, 0.15);
172
+ border: 1px solid var(--success);
173
+ color: #6ee7b7;
174
+ padding: 0.75rem 1rem;
175
+ border-radius: 6px;
176
+ font-size: 0.875rem;
177
+ margin-bottom: 1.25rem;
178
+ }
179
+ .form-group { margin-bottom: 1.25rem; }
180
+ label { display: block; font-size: 0.875rem; font-weight: 500; margin-bottom: 0.5rem; color: var(--muted); }
181
+ input[type="text"] {
182
+ width: 100%;
183
+ background: #0b1120;
184
+ border: 1px solid var(--border);
185
+ color: var(--text);
186
+ padding: 0.75rem 1rem;
187
+ border-radius: 6px;
188
+ font-size: 1.125rem;
189
+ letter-spacing: 0.25em;
190
+ text-align: center;
191
+ outline: none;
192
+ transition: border-color 0.2s;
193
+ }
194
+ input[type="text"]:focus { border-color: var(--primary); }
195
+ button {
196
+ width: 100%;
197
+ background: var(--primary);
198
+ color: white;
199
+ border: none;
200
+ padding: 0.75rem;
201
+ border-radius: 6px;
202
+ font-size: 0.95rem;
203
+ font-weight: 600;
204
+ cursor: pointer;
205
+ transition: background 0.2s;
206
+ }
207
+ button:hover { background: var(--primary-hover); }
208
+ .actions { display: flex; justify-content: space-between; margin-top: 1rem; }
209
+ .actions button.secondary {
210
+ background: transparent;
211
+ border: 1px solid var(--border);
212
+ color: var(--muted);
213
+ font-weight: normal;
214
+ }
215
+ .actions button.secondary:hover { color: var(--text); border-color: var(--text); background: transparent; }
216
+ </style>
217
+ </head>
218
+ <body>
219
+ <div class="card">
220
+ <div class="header">
221
+ <div class="logo">🛡️</div>
222
+ <h1>Shield Security Gate</h1>
223
+ <p class="subtitle">Enter the 6-digit authentication code sent to administrator.</p>
224
+ </div>
225
+
226
+ ${errorMessage ? `<div class="alert">${errorMessage}</div>` : ''}
227
+
228
+ <form method="POST" action="${baseUrl}/__shield_verify">
229
+ <div class="form-group">
230
+ <label for="otp">One-Time Passcode</label>
231
+ <input id="otp" name="code" type="text" maxlength="6" pattern="[0-9]{6}" inputmode="numeric" placeholder="••••••" required autofocus autocomplete="one-time-code" />
232
+ </div>
233
+ <button type="submit">Verify & Access Console</button>
234
+ </form>
235
+
236
+ <form method="POST" action="${baseUrl}/__shield_request" style="margin-top: 1rem;">
237
+ <button type="submit" class="secondary">Request New Code</button>
238
+ </form>
239
+ </div>
240
+ </body>
241
+ </html>`;
242
+ }
243
+
244
+ /**
245
+ * Express middleware handler
246
+ */
247
+ middleware() {
248
+ return async (req, res, next) => {
249
+ const cookies = this.parseCookies(req);
250
+ const sessionToken = cookies[this.cookieName] || req.headers?.['x-shield-auth-token'];
251
+
252
+ // 1. Check existing verified session
253
+ if (sessionToken) {
254
+ const verification = this.verifySessionToken(sessionToken);
255
+ if (verification.valid) {
256
+ req.shieldAdmin = { email: verification.email };
257
+ return next();
258
+ }
259
+ }
260
+
261
+ const path = req.path || '';
262
+
263
+ // 2. Handle POST Request to send a new code
264
+ if (req.method === 'POST' && path.endsWith('/__shield_request')) {
265
+ const code = this.generateCode();
266
+ this.challenges.set(this.adminEmail, {
267
+ code,
268
+ expiresAt: Date.now() + this.codeTtlMs,
269
+ attempts: 0
270
+ });
271
+
272
+ try {
273
+ await this.sendOtp(this.adminEmail, code);
274
+ } catch (err) {
275
+ this.logger.error?.('[SHIELD:OTP] Error dispatching OTP email:', err.message);
276
+ }
277
+
278
+ if (req.accepts('html')) {
279
+ res.setHeader('Content-Type', 'text/html; charset=utf-8');
280
+ return res.send(this.renderChallengeHtml(req, ''));
281
+ }
282
+ return res.json({ success: true, message: 'Verification code generated' });
283
+ }
284
+
285
+ // 3. Handle POST Request to verify code
286
+ if (req.method === 'POST' && path.endsWith('/__shield_verify')) {
287
+ // Parse body if express.urlencoded/json not mounted
288
+ let code = req.body?.code;
289
+ if (!code && typeof req.body === 'string') {
290
+ const match = req.body.match(/code=([^&]+)/);
291
+ if (match) code = decodeURIComponent(match[1]);
292
+ }
293
+
294
+ const challenge = this.challenges.get(this.adminEmail);
295
+ if (!challenge || Date.now() > challenge.expiresAt) {
296
+ if (req.accepts('html')) {
297
+ res.setHeader('Content-Type', 'text/html; charset=utf-8');
298
+ return res.status(401).send(this.renderChallengeHtml(req, 'Code has expired. Please request a new code.'));
299
+ }
300
+ return res.status(401).json({ error: 'Code has expired', code: 'EXPIRED_CODE' });
301
+ }
302
+
303
+ challenge.attempts = (challenge.attempts || 0) + 1;
304
+ if (challenge.attempts > this.maxAttempts) {
305
+ this.challenges.delete(this.adminEmail);
306
+ if (req.accepts('html')) {
307
+ res.setHeader('Content-Type', 'text/html; charset=utf-8');
308
+ return res.status(403).send(this.renderChallengeHtml(req, 'Too many failed attempts. Locked out.'));
309
+ }
310
+ return res.status(403).json({ error: 'Maximum verification attempts exceeded', code: 'MAX_ATTEMPTS_EXCEEDED' });
311
+ }
312
+
313
+ // Verify code with timing-safe comparison
314
+ const userCodeBuf = Buffer.from(String(code || '').trim());
315
+ const expectedCodeBuf = Buffer.from(challenge.code);
316
+
317
+ const isMatch = userCodeBuf.length === expectedCodeBuf.length &&
318
+ crypto.timingSafeEqual(userCodeBuf, expectedCodeBuf);
319
+
320
+ if (isMatch) {
321
+ this.challenges.delete(this.adminEmail);
322
+ const token = this.createSessionToken(this.adminEmail);
323
+
324
+ // Set HttpOnly signed cookie
325
+ const isSecure = req.secure || req.headers['x-forwarded-proto'] === 'https';
326
+ const cookieFlags = [
327
+ `${this.cookieName}=${encodeURIComponent(token)}`,
328
+ 'Path=/',
329
+ 'HttpOnly',
330
+ 'SameSite=Lax',
331
+ `Max-Age=${Math.floor(this.sessionTtlMs / 1000)}`
332
+ ];
333
+ if (isSecure) cookieFlags.push('Secure');
334
+
335
+ res.setHeader('Set-Cookie', cookieFlags.join('; '));
336
+
337
+ if (req.accepts('html')) {
338
+ const targetUrl = req.baseUrl || '/';
339
+ return res.redirect(targetUrl || '/');
340
+ }
341
+ return res.json({ success: true, token });
342
+ }
343
+
344
+ if (req.accepts('html')) {
345
+ res.setHeader('Content-Type', 'text/html; charset=utf-8');
346
+ return res.status(401).send(this.renderChallengeHtml(req, 'Invalid verification code. Please try again.'));
347
+ }
348
+ return res.status(401).json({ error: 'Invalid verification code', code: 'INVALID_CODE' });
349
+ }
350
+
351
+ // 4. Default unauthenticated presentation
352
+ if (req.accepts('html')) {
353
+ res.setHeader('Content-Type', 'text/html; charset=utf-8');
354
+ return res.status(401).send(this.renderChallengeHtml(req, ''));
355
+ }
356
+
357
+ return res.status(401).json({
358
+ error: 'Shield OTP authentication required',
359
+ code: 'SHIELD_OTP_REQUIRED'
360
+ });
361
+ };
362
+ }
363
+ }
364
+
365
+ export function createOtpGate(options = {}) {
366
+ const gate = new OTPGate(options);
367
+ return gate.middleware();
368
+ }
369
+
370
+ export default createOtpGate;