-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.mjs
More file actions
188 lines (173 loc) · 8.1 KB
/
Copy pathauth.mjs
File metadata and controls
188 lines (173 loc) · 8.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Auth primitives. ponytail: node:crypto only — no auth lib dependency.
// Passwords: scrypt. Tokens: HS256 JWT with a PINNED algorithm (no header-alg
// trust → immune to alg-confusion). This is a security boundary; do not simplify.
import { scryptSync, randomBytes, timingSafeEqual, createHmac, createHash } from 'node:crypto';
import { db } from './db.mjs';
/** Maximum lifetime for a general ScopeWeave session token, in seconds. */
const MAX_SESSION_TTL_SECONDS = 60 * 60 * 24 * 7;
/**
* Generate a one-time-visible ScopeWeave personal access token.
*
* Only the SHA-256 hash is suitable for persistence. The `full` value must be
* shown exactly once, while `prefix` is safe for later identification.
*
* @returns {{full:string,prefix:string,hash:string}} Token material and safe metadata.
*/
export function generateApiToken() {
const full = `swk_${randomBytes(24).toString('base64url')}`;
return { full, prefix: full.slice(0, 12), hash: createHash('sha256').update(full).digest('hex') };
}
/**
* Hash a personal access token for constant-shape database lookup.
*
* @param {unknown} full - Full token supplied by a client.
* @returns {string} Lowercase hexadecimal SHA-256 digest.
*/
export function hashApiToken(full) {
return createHash('sha256').update(String(full)).digest('hex');
}
// Fail closed: never mint or verify tokens with a missing/weak/placeholder secret.
// Require ≥32 non-whitespace characters so compose-unexpanded literals and short
// defaults cannot silently ship.
const SECRET = process.env.SCOPEWEAVE_JWT_SECRET;
if (
typeof SECRET !== 'string'
|| SECRET.replace(/\s/g, '').length < 32
|| SECRET.includes('${SCOPEWEAVE_JWT_SECRET')
) {
throw new Error('SCOPEWEAVE_JWT_SECRET must be set to at least 32 non-whitespace characters');
}
/**
* Hash a password with a fresh random salt using Node's scrypt implementation.
*
* Non-string values are normalized to an empty string so an untyped request
* cannot crash the process. API boundaries must still reject non-string inputs.
*
* @param {unknown} pw - Password value to hash.
* @returns {string} Persistable `salt:hash` representation.
*/
export function hashPassword(pw) {
const password = typeof pw === 'string' ? pw : '';
const salt = randomBytes(16).toString('hex');
const hash = scryptSync(password, salt, 64).toString('hex');
return `${salt}:${hash}`;
}
/**
* Verify a candidate password against a stored scrypt representation.
*
* Non-string candidates and malformed stored values fail closed. Equal-length
* digests are compared with `timingSafeEqual` to avoid content-dependent timing.
*
* @param {unknown} pw - Candidate password.
* @param {unknown} stored - Persisted `salt:hash` representation.
* @returns {boolean} Whether the candidate matches the stored password hash.
*/
export function verifyPassword(pw, stored) {
if (typeof pw !== 'string') return false;
const [salt, hash] = String(stored || '').split(':');
if (!salt || !hash) return false;
const test = scryptSync(pw, salt, 64);
const known = Buffer.from(hash, 'hex');
return test.length === known.length && timingSafeEqual(test, known);
}
/**
* Serialize a JSON value using the unpadded base64url form required by JWT.
*
* @param {unknown} value - JSON-serializable value.
* @returns {string} Base64url-encoded JSON.
*/
const b64urlJson = (value) => Buffer.from(JSON.stringify(value)).toString('base64url');
/**
* Determine whether a decoded JWT segment is a non-array JSON object.
*
* @param {unknown} value - Decoded JSON value.
* @returns {value is Record<string, unknown>} Whether the value is a claims object.
*/
function isClaimsObject(value) {
return value !== null && typeof value === 'object' && !Array.isArray(value);
}
/**
* Sign a ScopeWeave session JWT with pinned HS256 semantics.
*
* Session tokens are minted only for a positive safe-integer user subject and a
* non-negative safe-integer token version. The lifetime must be a positive safe
* integer no greater than seven days, so an internal caller cannot create an
* immortal, already-expired, excessively long-lived, or numerically imprecise
* general session token. Narrower credentials use the separate access-grant
* design tracked in issue #413 rather than extending this lifetime.
*
* @param {Record<string, unknown>} payload - Session claims to include.
* @param {number} [ttlSec=604800] - Token lifetime in seconds, at most seven days.
* @returns {string} Signed compact JWT.
* @throws {TypeError|RangeError} If the payload, subject, token version, or lifetime is invalid.
*/
export function signToken(payload, ttlSec = MAX_SESSION_TTL_SECONDS) {
if (!isClaimsObject(payload)) throw new TypeError('session claims must be an object');
if (!Number.isSafeInteger(payload.sub) || payload.sub < 1) {
throw new TypeError('session subject must be a positive safe integer');
}
if (!Number.isSafeInteger(payload.tv) || payload.tv < 0) {
throw new TypeError('session token version must be a non-negative safe integer');
}
if (!Number.isSafeInteger(ttlSec) || ttlSec < 1) {
throw new RangeError('session lifetime must be a positive safe integer');
}
if (ttlSec > MAX_SESSION_TTL_SECONDS) {
throw new RangeError(`session maximum lifetime is ${MAX_SESSION_TTL_SECONDS} seconds`);
}
const now = Math.floor(Date.now() / 1000);
const header = b64urlJson({ alg: 'HS256', typ: 'JWT' });
const body = b64urlJson({ ...payload, iat: now, exp: now + ttlSec });
const sig = createHmac('sha256', SECRET).update(`${header}.${body}`).digest('base64url');
return `${header}.${body}.${sig}`;
}
/**
* Verify a signed ScopeWeave session JWT and enforce database-backed revocation.
*
* The verifier recomputes an HS256 signature before parsing claims, then requires
* the signed header to declare the same pinned algorithm and JWT type. Session
* claims must contain a positive safe-integer subject, a future safe-integer
* expiry, and a non-negative safe-integer token version. The referenced user must
* exist and the token version must equal the current database value. Every
* session-JWT transport uses this function so `logout-all` cannot be bypassed by
* calendar, SSE, attachment-view, or bearer-token routes.
*
* @param {unknown} token - Compact JWT supplied by a client.
* @returns {Record<string, unknown>} Verified session claims.
* @throws {Error} If structure, signature, header, claims, expiry, user, or revocation checks fail.
*/
export function verifyToken(token) {
const parts = String(token || '').split('.');
if (parts.length !== 3) throw new Error('malformed token');
const [header, body, sig] = parts;
// Recompute HS256 first; do not parse or trust attacker-controlled claims
// before the compact representation has authenticated successfully.
const expected = createHmac('sha256', SECRET).update(`${header}.${body}`).digest('base64url');
const actualSignature = Buffer.from(sig);
const expectedSignature = Buffer.from(expected);
if (
actualSignature.length !== expectedSignature.length
|| !timingSafeEqual(actualSignature, expectedSignature)
) {
throw new Error('bad signature');
}
const headerClaims = JSON.parse(Buffer.from(header, 'base64url').toString());
if (!isClaimsObject(headerClaims)) throw new Error('invalid token header');
if (headerClaims.alg !== 'HS256') throw new Error('invalid token algorithm');
if (headerClaims.typ !== 'JWT') throw new Error('invalid token type');
const payload = JSON.parse(Buffer.from(body, 'base64url').toString());
if (!isClaimsObject(payload)) throw new Error('invalid session claims');
if (!Number.isSafeInteger(payload.sub) || payload.sub < 1) {
throw new Error('invalid session subject');
}
if (!Number.isSafeInteger(payload.exp) || payload.exp <= Math.floor(Date.now() / 1000)) {
throw new Error('expired or invalid session expiry');
}
if (!Number.isSafeInteger(payload.tv) || payload.tv < 0) {
throw new Error('invalid token version');
}
const user = db.prepare('SELECT token_version FROM users WHERE id = ?').get(payload.sub);
if (!user) throw new Error('unknown session subject');
if (payload.tv !== user.token_version) throw new Error('revoked session');
return payload;
}