create-lupine 1.3.0 → 1.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.
- package/index.js +10 -7
- package/package.json +1 -1
- package/templates/common/.env.development +2 -0
- package/templates/common/_env +2 -1
- package/templates/lupine-template-fullstack-note/api/src/service/user-api.ts +2 -2
- package/templates/lupine-template-fullstack-starter/README.md +2 -1
- package/templates/lupine-template-fullstack-starter/api/resources/install.mysql.sql +2 -2
- package/templates/lupine-template-fullstack-starter/api/resources/install.sqlite.sql +2 -2
- package/templates/lupine-template-fullstack-starter/api/src/service/contact-api.ts +2 -2
- package/templates/lupine-template-fullstack-starter/api/src/service/user-api.ts +61 -41
package/index.js
CHANGED
|
@@ -313,13 +313,13 @@ async function addApp(name, template, versions) {
|
|
|
313
313
|
async function init() {
|
|
314
314
|
console.log('Fetching latest versions...');
|
|
315
315
|
const versions = {
|
|
316
|
-
'lupine.api': getLatestVersion('lupine.api', '^1.2.
|
|
317
|
-
'lupine.web': getLatestVersion('lupine.web', '^1.3.
|
|
318
|
-
'lupine.core': getLatestVersion('lupine.core', '^1.1.
|
|
319
|
-
'lupine.web.components': getLatestVersion('lupine.web.components', '^1.3.
|
|
320
|
-
'lupine.native.core': getLatestVersion('lupine.native.core', '^1.1.
|
|
316
|
+
'lupine.api': getLatestVersion('lupine.api', '^1.2.44'),
|
|
317
|
+
'lupine.web': getLatestVersion('lupine.web', '^1.3.1'),
|
|
318
|
+
'lupine.core': getLatestVersion('lupine.core', '^1.1.2'),
|
|
319
|
+
'lupine.web.components': getLatestVersion('lupine.web.components', '^1.3.2'),
|
|
320
|
+
'lupine.native.core': getLatestVersion('lupine.native.core', '^1.1.1'),
|
|
321
321
|
'lupine.native.components': getLatestVersion('lupine.native.components', '^1.1.1'),
|
|
322
|
-
'lupine.press': getLatestVersion('lupine.press', '^1.3.
|
|
322
|
+
'lupine.press': getLatestVersion('lupine.press', '^1.3.1'),
|
|
323
323
|
};
|
|
324
324
|
|
|
325
325
|
if (argv['update-version']) {
|
|
@@ -415,6 +415,8 @@ async function init() {
|
|
|
415
415
|
copyDir(commonDir, root);
|
|
416
416
|
fs.renameSync(path.join(root, '_gitignore'), path.join(root, '.gitignore'));
|
|
417
417
|
fs.renameSync(path.join(root, '_env'), path.join(root, '.env'));
|
|
418
|
+
// .env gets the keys and passwords of the project: only its owner reads it
|
|
419
|
+
fs.chmodSync(path.join(root, '.env'), 0o600);
|
|
418
420
|
|
|
419
421
|
const appName = path.basename(root);
|
|
420
422
|
const targetAppDir = scaffoldApp(root, appName, templateObj);
|
|
@@ -481,7 +483,8 @@ async function init() {
|
|
|
481
483
|
});
|
|
482
484
|
|
|
483
485
|
envContent = envContent.replace(/{APP-NAME}/g, appName);
|
|
484
|
-
fs.writeFileSync(path.join(root, '.env'), envContent);
|
|
486
|
+
fs.writeFileSync(path.join(root, '.env'), envContent, { mode: 0o600 });
|
|
487
|
+
fs.chmodSync(path.join(root, '.env'), 0o600);
|
|
485
488
|
}
|
|
486
489
|
|
|
487
490
|
console.log(`\nDone. Now run:\n`);
|
package/package.json
CHANGED
package/templates/common/_env
CHANGED
|
@@ -13,7 +13,8 @@ HTTPS_PORT=11083
|
|
|
13
13
|
#'127.0.0.1' or for all ips: "::"
|
|
14
14
|
BIND_IP=0.0.0.0
|
|
15
15
|
|
|
16
|
-
NODE_TLS_REJECT_UNAUTHORIZED=0
|
|
16
|
+
# NODE_TLS_REJECT_UNAUTHORIZED=0 (certificates not checked): in .env.development only, never here (.env.production imports
|
|
17
|
+
# this file, and lupine-build --deploy refuses to send the key without a checked certificate)
|
|
17
18
|
|
|
18
19
|
LOG_FOLDER=./dist/log
|
|
19
20
|
LOG_FILENAME=log-%index%.log
|
|
@@ -129,7 +129,7 @@ export const register = async (req: ServerRequest, res: ServerResponse) => {
|
|
|
129
129
|
const d = await db();
|
|
130
130
|
const taken = () => fail(req, res, 409, 'username_taken', 'This user name is taken.');
|
|
131
131
|
if ((await d.select('SELECT id FROM $__account WHERE username = ?', [username])).length) return taken();
|
|
132
|
-
const password = CryptoUtils.
|
|
132
|
+
const password = await CryptoUtils.hashPasswordAsync(data.password);
|
|
133
133
|
let id: number;
|
|
134
134
|
try {
|
|
135
135
|
id = insertedId(await d.execute('INSERT INTO $__account (username, password, created_at) VALUES (?, ?, ?)', [username, password, now()]));
|
|
@@ -160,7 +160,7 @@ export const login = async (req: ServerRequest, res: ServerResponse) => {
|
|
|
160
160
|
if (minutes) return fail(req, res, 429, 'too_many_attempts', `Too many failed sign-ins. Try again in ${minutes} minutes.`, { minutes });
|
|
161
161
|
const d = await db();
|
|
162
162
|
const [a] = await d.select<Account>('SELECT id, username, password FROM $__account WHERE username = ?', [username]);
|
|
163
|
-
const valid = CryptoUtils.
|
|
163
|
+
const valid = await CryptoUtils.verifyPasswordAsync(password, a ? String(a.password) : NOBODY);
|
|
164
164
|
if (!a || !valid) {
|
|
165
165
|
await recordFailure(username);
|
|
166
166
|
return failed();
|
|
@@ -59,7 +59,8 @@ In the browser the app always calls the site it comes from, and `WEB.API_BASE_UR
|
|
|
59
59
|
- In the browser the API sets the httpOnly cookie `_token` when you sign in, so the scripts of the page never hold the token.
|
|
60
60
|
- A native app has no cookie store. It keeps the token from the sign-in answer in `storage` and sends it as `Cookie: _token=...`. The API puts the token in the answer only for requests without `Origin`, which browsers always send with a POST, so a script in a page can't get it from there either. Signing out removes it and calls `/api/logout`.
|
|
61
61
|
- The admin pages are web only. On a phone "Admin pages" opens the site in the browser, where you sign in again.
|
|
62
|
-
-
|
|
62
|
+
- A session lasts `ADMIN_SESSION_EXPIRE_DAYS` of `.env` (90 days without it): the token carries its sign-in time and the API checks it, so a copied token ends too. A new password ends all sessions of before.
|
|
63
|
+
- Writing routes are POST only and check the session. Uploads check the type and the size. A code by e-mail can be asked for once a minute and is good for 30 minutes (wrong tries don't make it last longer); the answers to asking for a code and to setting a new password don't tell whether the address has an account. The contact form takes five messages an hour from an address (per worker process, so limit it in front of the server too).
|
|
63
64
|
- Requests from native apps have no `Origin` header, so the CSRF check of the API lets them through. It still checks browsers.
|
|
64
65
|
|
|
65
66
|
## Dropped from the web-only version
|
|
@@ -46,7 +46,7 @@ CREATE TABLE IF NOT EXISTS $__s_user (
|
|
|
46
46
|
nickname VARCHAR(200) NOT NULL DEFAULT '',
|
|
47
47
|
username VARCHAR(150) NOT NULL DEFAULT '',
|
|
48
48
|
email VARCHAR(100) NOT NULL DEFAULT '',
|
|
49
|
-
password VARCHAR(
|
|
49
|
+
password VARCHAR(255) NOT NULL DEFAULT '',
|
|
50
50
|
usertype VARCHAR(25) NOT NULL DEFAULT '',
|
|
51
51
|
block CHAR(1) NOT NULL DEFAULT '0',
|
|
52
52
|
sendemail CHAR(1) DEFAULT '0',
|
|
@@ -57,7 +57,7 @@ CREATE TABLE IF NOT EXISTS $__s_user (
|
|
|
57
57
|
registerdate VARCHAR(20) NOT NULL DEFAULT '',
|
|
58
58
|
lastvisitdate VARCHAR(20) NOT NULL DEFAULT '',
|
|
59
59
|
activated CHAR(1) NOT NULL DEFAULT '0',
|
|
60
|
-
activation VARCHAR(
|
|
60
|
+
activation VARCHAR(40) NOT NULL DEFAULT '',
|
|
61
61
|
points INTEGER NOT NULL DEFAULT 0,
|
|
62
62
|
pwtips VARCHAR(100) DEFAULT '',
|
|
63
63
|
pwretry INTEGER NOT NULL DEFAULT 0,
|
|
@@ -32,7 +32,7 @@ CREATE TABLE $__s_user (
|
|
|
32
32
|
nickname VARCHAR(200) NOT NULL DEFAULT '',
|
|
33
33
|
username VARCHAR(150) NOT NULL DEFAULT '',
|
|
34
34
|
email VARCHAR(100) NOT NULL DEFAULT '',
|
|
35
|
-
password VARCHAR(
|
|
35
|
+
password VARCHAR(255) NOT NULL DEFAULT '',
|
|
36
36
|
usertype VARCHAR(25) NOT NULL DEFAULT '',
|
|
37
37
|
block CHAR(1) NOT NULL DEFAULT '0',
|
|
38
38
|
sendemail CHAR(1) DEFAULT '0',
|
|
@@ -43,7 +43,7 @@ CREATE TABLE $__s_user (
|
|
|
43
43
|
registerdate VARCHAR(20) NOT NULL DEFAULT '',
|
|
44
44
|
lastvisitdate VARCHAR(20) NOT NULL DEFAULT '',
|
|
45
45
|
activated CHAR(1) NOT NULL DEFAULT '0',
|
|
46
|
-
activation VARCHAR(
|
|
46
|
+
activation VARCHAR(40) NOT NULL DEFAULT '',
|
|
47
47
|
points INTEGER NOT NULL DEFAULT '0',
|
|
48
48
|
pwtips VARCHAR(100) DEFAULT '',
|
|
49
49
|
pwretry INTEGER NOT NULL DEFAULT '0',
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ServerResponse } from 'http';
|
|
2
|
-
import { Logger, ServerRequest, ApiHelper } from 'lupine.api';
|
|
2
|
+
import { Logger, ServerRequest, ApiHelper, getClientIp } from 'lupine.api';
|
|
3
3
|
import { sendSiteEmail } from './send-email';
|
|
4
4
|
|
|
5
5
|
const logger = new Logger('qa');
|
|
@@ -11,7 +11,7 @@ const MAX_PER_HOUR = 5;
|
|
|
11
11
|
const oneLine = (s: unknown, max: number) => String(s).replace(/[\r\n]+/g, ' ').trim().slice(0, max);
|
|
12
12
|
|
|
13
13
|
export const addContact = async (req: ServerRequest, res: ServerResponse) => {
|
|
14
|
-
const ip =
|
|
14
|
+
const ip = getClientIp(req);
|
|
15
15
|
const now = Date.now();
|
|
16
16
|
const recent = (sent.get(ip) || []).filter((t) => now - t < 3600 * 1000);
|
|
17
17
|
if (recent.length >= MAX_PER_HOUR) {
|
|
@@ -19,11 +19,30 @@ import {
|
|
|
19
19
|
DEV_ADMIN_USER_KEY_NAME,
|
|
20
20
|
DEV_ADMIN_PASS_KEY_NAME,
|
|
21
21
|
DEV_ADMIN_SESSION_INVALID_BEFORE_KEY_NAME,
|
|
22
|
+
loginGuard,
|
|
23
|
+
getClientIp,
|
|
22
24
|
} from 'lupine.api';
|
|
23
25
|
import { sendEmail, sendSiteEmail } from './send-email';
|
|
24
26
|
|
|
25
27
|
const PW_RETRY_MAX = 5;
|
|
26
28
|
const PW_RETRY_RESET_MINUTES = 15;
|
|
29
|
+
|
|
30
|
+
// A code by email (to sign in or set a new password) is good for CODE_MINUTES from when it was sent. The time is kept
|
|
31
|
+
// with it, as "code.time" in activation: lastvisitdate moves with every wrong try, so it can't tell the age of a code
|
|
32
|
+
const CODE_MINUTES = 30;
|
|
33
|
+
const codeIssuedAt = (stored: unknown): number => Number(String(stored || '').split('.')[1]) || 0;
|
|
34
|
+
const codeMatches = (stored: unknown, provided: unknown): boolean => {
|
|
35
|
+
const code = String(stored || '').split('.')[0];
|
|
36
|
+
const issued = codeIssuedAt(stored);
|
|
37
|
+
return (
|
|
38
|
+
code.length >= 6 &&
|
|
39
|
+
issued > 0 &&
|
|
40
|
+
Date.now() - issued <= CODE_MINUTES * 60 * 1000 &&
|
|
41
|
+
CryptoUtils.timingSafeEqual(code, String(provided || ''))
|
|
42
|
+
);
|
|
43
|
+
};
|
|
44
|
+
// a user's session: the cookie and the login time in the token (which the server checks) end after these days
|
|
45
|
+
const userSessionDays = () => getSessionExpireDays(ADMIN_SESSION_EXPIRE_DAYS_KEY_NAME, 90);
|
|
27
46
|
const logger = new Logger('user-api');
|
|
28
47
|
|
|
29
48
|
export type LoginJsonProps = {
|
|
@@ -122,7 +141,7 @@ export const appAdminHookCheckLogin: AppAdminHookCheckLoginProps = async (
|
|
|
122
141
|
const adminPass = process.env['ADMIN_PASS'] || '';
|
|
123
142
|
const pwCheck =
|
|
124
143
|
username && password && password.length <= 256 && adminUser && adminPass
|
|
125
|
-
? CryptoUtils.
|
|
144
|
+
? await CryptoUtils.verifyPasswordLayeredAsync(password, adminPass)
|
|
126
145
|
: { valid: false as const };
|
|
127
146
|
if (
|
|
128
147
|
adminUser &&
|
|
@@ -154,12 +173,12 @@ const computeUserSessionHash = (username: string, storedPassword: string): strin
|
|
|
154
173
|
return CryptoUtils.hash(`${username}:${storedPassword}`);
|
|
155
174
|
};
|
|
156
175
|
|
|
157
|
-
const verifyUserPassword = (plainPassword: string, storedPassword: string): boolean => {
|
|
176
|
+
const verifyUserPassword = async (plainPassword: string, storedPassword: string): Promise<boolean> => {
|
|
158
177
|
if (!plainPassword || plainPassword.length > 256 || !storedPassword) {
|
|
159
178
|
return false;
|
|
160
179
|
}
|
|
161
180
|
if (storedPassword.startsWith('scrypt$')) {
|
|
162
|
-
return CryptoUtils.
|
|
181
|
+
return await CryptoUtils.verifyPasswordAsync(plainPassword, storedPassword);
|
|
163
182
|
}
|
|
164
183
|
return CryptoUtils.timingSafeEqual(CryptoUtils.hash(plainPassword), storedPassword);
|
|
165
184
|
};
|
|
@@ -229,6 +248,14 @@ export const getUserFromCookie = async (
|
|
|
229
248
|
}
|
|
230
249
|
}
|
|
231
250
|
|
|
251
|
+
// a user's token carries its login time: an old (or copied) token ends like the cookie does
|
|
252
|
+
if (isSessionExpired(json.l, userSessionDays())) {
|
|
253
|
+
if (sendResponseWhenError) {
|
|
254
|
+
ApiHelper.sendJson(req, res, { status: 'error', message: langHelper.getLang('shared:not_logged_in') });
|
|
255
|
+
}
|
|
256
|
+
return false;
|
|
257
|
+
}
|
|
258
|
+
|
|
232
259
|
// verify whether the user is blocked
|
|
233
260
|
const db = apiCache.getDb();
|
|
234
261
|
const record = await db.selectObject('$__s_user', ['*'], {
|
|
@@ -409,19 +436,25 @@ export const userLogin = async (req: ServerRequest, res: ServerResponse) => {
|
|
|
409
436
|
return true;
|
|
410
437
|
}
|
|
411
438
|
|
|
439
|
+
// ADMIN_USER of .env: failed tries are counted per client address (loginGuard), and the password is only checked
|
|
440
|
+
// (scrypt) for this user name
|
|
412
441
|
const adminUser = process.env['ADMIN_USER'] || '';
|
|
413
442
|
const adminPass = process.env['ADMIN_PASS'] || '';
|
|
443
|
+
const isAdminName = !!adminUser && !!adminPass && typeof data.u === 'string' && adminApiHelper.timingSafeEqual(data.u, adminUser);
|
|
444
|
+
if (isAdminName && (await loginGuard.isLocked(req))) return loginGuard.sendLocked(req, res);
|
|
414
445
|
const adminPwCheck =
|
|
415
|
-
typeof data.
|
|
416
|
-
|
|
417
|
-
data.p.length <= 256 &&
|
|
418
|
-
adminUser &&
|
|
419
|
-
adminPass
|
|
420
|
-
? CryptoUtils.verifyPasswordLayered(data.p, adminPass)
|
|
446
|
+
isAdminName && typeof data.p === 'string' && data.p.length <= 256
|
|
447
|
+
? await CryptoUtils.verifyPasswordLayeredAsync(data.p, adminPass)
|
|
421
448
|
: { valid: false as const };
|
|
422
|
-
if (
|
|
449
|
+
if (isAdminName && (!adminPwCheck.valid || !adminPwCheck.keyHex)) {
|
|
450
|
+
await loginGuard.fail(req);
|
|
451
|
+
ApiHelper.sendJson(req, res, { status: 'error', message: langHelper.getLang('shared:login_failed') });
|
|
452
|
+
return true;
|
|
453
|
+
}
|
|
454
|
+
if (isAdminName && adminPwCheck.keyHex) {
|
|
455
|
+
loginGuard.clear(req);
|
|
423
456
|
const loginJson: LoginJsonProps = {
|
|
424
|
-
ip: req
|
|
457
|
+
ip: getClientIp(req),
|
|
425
458
|
id: 0,
|
|
426
459
|
u: data.u as string,
|
|
427
460
|
t: 'admin',
|
|
@@ -521,9 +554,7 @@ export const userLogin = async (req: ServerRequest, res: ServerResponse) => {
|
|
|
521
554
|
if (await checkRetryLockout()) {
|
|
522
555
|
return true;
|
|
523
556
|
}
|
|
524
|
-
|
|
525
|
-
const storedCode = String(result[0].activation || '');
|
|
526
|
-
if (storedCode.length < 6 || !CryptoUtils.timingSafeEqual(storedCode, providedCode)) {
|
|
557
|
+
if (!codeMatches(result[0].activation, data.c)) {
|
|
527
558
|
await recordUserRetry();
|
|
528
559
|
const response = {
|
|
529
560
|
status: 'error',
|
|
@@ -537,11 +568,7 @@ export const userLogin = async (req: ServerRequest, res: ServerResponse) => {
|
|
|
537
568
|
if (await checkRetryLockout()) {
|
|
538
569
|
return true;
|
|
539
570
|
}
|
|
540
|
-
|
|
541
|
-
const storedCode = String(result[0].activation || '');
|
|
542
|
-
const codeIssuedAt = DateUtils.fromJSONString(result[0].lastvisitdate);
|
|
543
|
-
const isCodeExpired = !codeIssuedAt || Date.now() - codeIssuedAt.getTime() > 30 * 60 * 1000;
|
|
544
|
-
if (storedCode.length < 6 || isCodeExpired || !CryptoUtils.timingSafeEqual(storedCode, providedCode)) {
|
|
571
|
+
if (!codeMatches(result[0].activation, data.c)) {
|
|
545
572
|
await recordUserRetry();
|
|
546
573
|
const response = {
|
|
547
574
|
status: 'error',
|
|
@@ -558,7 +585,7 @@ export const userLogin = async (req: ServerRequest, res: ServerResponse) => {
|
|
|
558
585
|
return true;
|
|
559
586
|
}
|
|
560
587
|
|
|
561
|
-
if (!loginOk && !verifyUserPassword(String(data.p || ''), String(result[0].password || ''))) {
|
|
588
|
+
if (!loginOk && !(await verifyUserPassword(String(data.p || ''), String(result[0].password || '')))) {
|
|
562
589
|
await recordUserRetry();
|
|
563
590
|
const response = {
|
|
564
591
|
status: 'error',
|
|
@@ -576,18 +603,19 @@ export const userLogin = async (req: ServerRequest, res: ServerResponse) => {
|
|
|
576
603
|
lastvisitdate: DateUtils.toJSONString(new Date()),
|
|
577
604
|
};
|
|
578
605
|
if (!loginOk && typeof data.p === 'string' && activePasswordHash && !activePasswordHash.startsWith('scrypt$')) {
|
|
579
|
-
activePasswordHash = CryptoUtils.
|
|
606
|
+
activePasswordHash = await CryptoUtils.hashPasswordAsync(data.p);
|
|
580
607
|
updateLoginObj.password = activePasswordHash;
|
|
581
608
|
}
|
|
582
609
|
|
|
583
610
|
await db.updateObject('$__s_user', updateLoginObj, { username: data.u as string });
|
|
584
611
|
const nameAndPassHash = CryptoUtils.sha256(`${result[0].username}:${activePasswordHash}`);
|
|
585
612
|
const loginJson: LoginJsonProps = {
|
|
586
|
-
ip: req
|
|
613
|
+
ip: getClientIp(req),
|
|
587
614
|
id: result[0].id,
|
|
588
615
|
u: result[0].username,
|
|
589
616
|
t: result[0].usertype,
|
|
590
617
|
h: nameAndPassHash,
|
|
618
|
+
l: Date.now(),
|
|
591
619
|
};
|
|
592
620
|
|
|
593
621
|
const token = JSON.stringify(loginJson);
|
|
@@ -604,7 +632,7 @@ export const userLogin = async (req: ServerRequest, res: ServerResponse) => {
|
|
|
604
632
|
},
|
|
605
633
|
};
|
|
606
634
|
req.locals.setCookie('_token', tokenCookie, {
|
|
607
|
-
expireDays:
|
|
635
|
+
expireDays: userSessionDays(),
|
|
608
636
|
path: '/',
|
|
609
637
|
httpOnly: true,
|
|
610
638
|
secure: isHttpsRequest(req),
|
|
@@ -702,7 +730,7 @@ export const userReg = async (req: ServerRequest, res: ServerResponse) => {
|
|
|
702
730
|
nickname: data.preferredName as string,
|
|
703
731
|
username: data.u as string,
|
|
704
732
|
email: data.u as string,
|
|
705
|
-
password: CryptoUtils.
|
|
733
|
+
password: await CryptoUtils.hashPasswordAsync(data.p),
|
|
706
734
|
gender: data.sex as string,
|
|
707
735
|
usertype: 'user',
|
|
708
736
|
registerdate: lastvisitdate,
|
|
@@ -779,7 +807,7 @@ export const userResetCode = async (req: ServerRequest, res: ServerResponse) =>
|
|
|
779
807
|
}
|
|
780
808
|
|
|
781
809
|
const db = apiCache.getDb();
|
|
782
|
-
const result = await db.selectObject('$__s_user', ['id', 'activation'
|
|
810
|
+
const result = await db.selectObject('$__s_user', ['id', 'activation'], {
|
|
783
811
|
username: userEmail,
|
|
784
812
|
});
|
|
785
813
|
// the same answer whether the address is known or not: nobody learns who has an account here
|
|
@@ -789,15 +817,13 @@ export const userResetCode = async (req: ServerRequest, res: ServerResponse) =>
|
|
|
789
817
|
return true;
|
|
790
818
|
}
|
|
791
819
|
// one code a minute: the form can't be used to flood a mailbox
|
|
792
|
-
|
|
793
|
-
if (issued && Date.now() - issued.getTime() < 60 * 1000) {
|
|
820
|
+
if (Date.now() - codeIssuedAt(result[0].activation) < 60 * 1000) {
|
|
794
821
|
answer();
|
|
795
822
|
return true;
|
|
796
823
|
}
|
|
797
824
|
|
|
798
|
-
const lastvisitdate = DateUtils.toJSONString(new Date());
|
|
799
825
|
const random = CryptoUtils.randomNumberString(8);
|
|
800
|
-
await db.updateObject('$__s_user', { activation: random
|
|
826
|
+
await db.updateObject('$__s_user', { activation: `${random}.${Date.now()}` }, { username: userEmail });
|
|
801
827
|
|
|
802
828
|
const siteUrl = (await apiStorage.getWeb('siteUrl')) || 'https://your-site.com';
|
|
803
829
|
const emailTitle = 'Authentication code';
|
|
@@ -829,12 +855,9 @@ export const userResetPw = async (req: ServerRequest, res: ServerResponse) => {
|
|
|
829
855
|
const result = await db.selectObject('$__s_user', ['id', 'activation', 'pwretry', 'lastvisitdate'], {
|
|
830
856
|
username: userEmail,
|
|
831
857
|
});
|
|
858
|
+
// the answer of a wrong code: nobody learns here who has an account
|
|
832
859
|
if (!result || result.length !== 1) {
|
|
833
|
-
|
|
834
|
-
status: 'error',
|
|
835
|
-
message: langHelper.getLang('shared:user_not_found'),
|
|
836
|
-
};
|
|
837
|
-
ApiHelper.sendJson(req, res, response);
|
|
860
|
+
ApiHelper.sendJson(req, res, { status: 'error', message: langHelper.getLang('shared:wrong_activation') });
|
|
838
861
|
return true;
|
|
839
862
|
}
|
|
840
863
|
|
|
@@ -852,10 +875,7 @@ export const userResetPw = async (req: ServerRequest, res: ServerResponse) => {
|
|
|
852
875
|
}
|
|
853
876
|
}
|
|
854
877
|
|
|
855
|
-
|
|
856
|
-
const codeIssuedAt = DateUtils.fromJSONString(result[0].lastvisitdate);
|
|
857
|
-
const isCodeExpired = !codeIssuedAt || Date.now() - codeIssuedAt.getTime() > 30 * 60 * 1000;
|
|
858
|
-
if (storedCode.length < 6 || isCodeExpired || !CryptoUtils.timingSafeEqual(storedCode, String(activation))) {
|
|
878
|
+
if (!codeMatches(result[0].activation, activation)) {
|
|
859
879
|
await db.updateObject(
|
|
860
880
|
'$__s_user',
|
|
861
881
|
{ pwretry: (Number(result[0].pwretry) || 0) + 1, lastvisitdate: DateUtils.toJSONString(new Date()) },
|
|
@@ -870,7 +890,7 @@ export const userResetPw = async (req: ServerRequest, res: ServerResponse) => {
|
|
|
870
890
|
}
|
|
871
891
|
await db.updateObject(
|
|
872
892
|
'$__s_user',
|
|
873
|
-
{ activation: '', pwretry: 0, password: CryptoUtils.
|
|
893
|
+
{ activation: '', activated: '1', pwretry: 0, password: await CryptoUtils.hashPasswordAsync(pass) },
|
|
874
894
|
{ username: userEmail }
|
|
875
895
|
);
|
|
876
896
|
const response = {
|
|
@@ -908,7 +928,7 @@ export const updateUser = async (req: ServerRequest, res: ServerResponse) => {
|
|
|
908
928
|
|
|
909
929
|
const updateObj: any = { usertype: usertype };
|
|
910
930
|
if (password) {
|
|
911
|
-
updateObj['password'] = CryptoUtils.
|
|
931
|
+
updateObj['password'] = await CryptoUtils.hashPasswordAsync(password);
|
|
912
932
|
}
|
|
913
933
|
// clear activation, only admin can update
|
|
914
934
|
updateObj['activation'] = '';
|
|
@@ -967,7 +987,7 @@ export const updateProfile = async (req: ServerRequest, res: ServerResponse) =>
|
|
|
967
987
|
ApiHelper.sendJson(req, res, response);
|
|
968
988
|
return true;
|
|
969
989
|
}
|
|
970
|
-
updateObj['password'] = CryptoUtils.
|
|
990
|
+
updateObj['password'] = await CryptoUtils.hashPasswordAsync(password);
|
|
971
991
|
}
|
|
972
992
|
const result = await db.updateObject('$__s_user', updateObj, {
|
|
973
993
|
id: loginJson.id,
|