@volcanicminds/typeorm 2.1.0 → 2.2.0
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/dist/lib/entities/user.d.ts +4 -0
- package/dist/lib/entities/user.d.ts.map +1 -1
- package/dist/lib/entities/user.js.map +1 -1
- package/dist/lib/loader/tokenManager.d.ts.map +1 -1
- package/dist/lib/loader/tokenManager.js +8 -7
- package/dist/lib/loader/tokenManager.js.map +1 -1
- package/dist/lib/loader/userManager.d.ts.map +1 -1
- package/dist/lib/loader/userManager.js +23 -22
- package/dist/lib/loader/userManager.js.map +1 -1
- package/dist/lib/util/crypto.d.ts +3 -0
- package/dist/lib/util/crypto.d.ts.map +1 -0
- package/dist/lib/util/crypto.js +30 -0
- package/dist/lib/util/crypto.js.map +1 -0
- package/dist/lib/util/error.d.ts +5 -0
- package/dist/lib/util/error.d.ts.map +1 -0
- package/dist/lib/util/error.js +8 -0
- package/dist/lib/util/error.js.map +1 -0
- package/lib/entities/user.ts +7 -0
- package/lib/loader/tokenManager.ts +8 -7
- package/lib/loader/userManager.ts +23 -22
- package/lib/util/crypto.ts +30 -0
- package/lib/util/error.ts +7 -0
- package/package.json +1 -1
|
@@ -17,6 +17,10 @@ export declare abstract class User extends BaseEntity {
|
|
|
17
17
|
abstract createdAt: Date;
|
|
18
18
|
abstract updatedAt: Date;
|
|
19
19
|
abstract deletedAt: Date;
|
|
20
|
+
abstract mfaEnabled: boolean;
|
|
21
|
+
abstract mfaSecret: string;
|
|
22
|
+
abstract mfaType: string;
|
|
23
|
+
abstract mfaRecoveryCodes: string[];
|
|
20
24
|
abstract setId(id: any): any;
|
|
21
25
|
abstract getId(): any;
|
|
22
26
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"user.d.ts","sourceRoot":"","sources":["../../../lib/entities/user.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AAEpC,8BAAsB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,SAAS,EAAE,OAAO,CAAA;IAC3B,QAAQ,CAAC,WAAW,EAAE,IAAI,CAAA;IAC1B,QAAQ,CAAC,iBAAiB,EAAE,IAAI,CAAA;IAChC,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAA;IACzB,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAA;IAC9B,QAAQ,CAAC,SAAS,EAAE,IAAI,CAAA;IACxB,QAAQ,CAAC,kBAAkB,EAAE,MAAM,CAAA;IACnC,QAAQ,CAAC,iBAAiB,EAAE,MAAM,CAAA;IAClC,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,CAAA;IACxB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;IACxB,QAAQ,CAAC,SAAS,EAAE,IAAI,CAAA;IACxB,QAAQ,CAAC,SAAS,EAAE,IAAI,CAAA;IACxB,QAAQ,CAAC,SAAS,EAAE,IAAI,CAAA;IAGxB,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAA;IAC5B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAA;IAC1B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;IACxB,QAAQ,CAAC,gBAAgB,EAAE,MAAM,EAAE,CAAA;IAEnC,QAAQ,CAAC,KAAK,CAAC,EAAE,EAAE,GAAG;IACtB,QAAQ,CAAC,KAAK,IAAI,GAAG;CACtB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"user.js","sourceRoot":"","sources":["../../../lib/entities/user.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AAEpC,MAAM,OAAgB,IAAK,SAAQ,UAAU;
|
|
1
|
+
{"version":3,"file":"user.js","sourceRoot":"","sources":["../../../lib/entities/user.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AAEpC,MAAM,OAAgB,IAAK,SAAQ,UAAU;CA4B5C"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tokenManager.d.ts","sourceRoot":"","sources":["../../../lib/loader/tokenManager.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"tokenManager.d.ts","sourceRoot":"","sources":["../../../lib/loader/tokenManager.ts"],"names":[],"mappings":"AAIA,wBAAgB,aAAa,YAE5B;AAED,wBAAsB,YAAY,CAAC,IAAI,EAAE,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,oBAElE;AAED,wBAAsB,WAAW,CAAC,IAAI,EAAE,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,gBA4BjE;AAED,wBAAsB,eAAe,CAAC,EAAE,EAAE,MAAM,gBAoB/C;AAED,wBAAsB,eAAe,CAAC,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,gBAclF;AAED,wBAAsB,iBAAiB,CAAC,EAAE,EAAE,MAAM,gBASjD;AAED,wBAAsB,yBAAyB,CAAC,UAAU,EAAE,MAAM,gBASjE;AAED,wBAAsB,cAAc,CAAC,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,gBAE9D;AAED,wBAAsB,gBAAgB,CAAC,EAAE,EAAE,MAAM,gBAEhD;AAED,wBAAsB,UAAU,CAAC,IAAI,EAAE,GAAG,gBAEzC;AAED,wBAAsB,SAAS,CAAC,IAAI,EAAE,GAAG;;;;;;;;;GAExC;AAED,wBAAsB,eAAe,CAAC,EAAE,EAAE,MAAM,gBAS/C"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as Crypto from 'crypto';
|
|
2
|
+
import { ServiceError } from '../util/error.js';
|
|
2
3
|
import { executeCountQuery, executeFindQuery } from '../query.js';
|
|
3
4
|
export function isImplemented() {
|
|
4
5
|
return true;
|
|
@@ -9,7 +10,7 @@ export async function isValidToken(data) {
|
|
|
9
10
|
export async function createToken(data) {
|
|
10
11
|
const { name, description } = data;
|
|
11
12
|
if (!name) {
|
|
12
|
-
|
|
13
|
+
throw new ServiceError('Invalid parameters', 400);
|
|
13
14
|
}
|
|
14
15
|
try {
|
|
15
16
|
let externalId, token;
|
|
@@ -33,7 +34,7 @@ export async function createToken(data) {
|
|
|
33
34
|
}
|
|
34
35
|
export async function resetExternalId(id) {
|
|
35
36
|
if (!id) {
|
|
36
|
-
|
|
37
|
+
throw new ServiceError('Invalid parameters', 400);
|
|
37
38
|
}
|
|
38
39
|
try {
|
|
39
40
|
let externalId, token;
|
|
@@ -52,12 +53,12 @@ export async function resetExternalId(id) {
|
|
|
52
53
|
}
|
|
53
54
|
export async function updateTokenById(id, token) {
|
|
54
55
|
if (!id || !token) {
|
|
55
|
-
|
|
56
|
+
throw new ServiceError('Invalid parameters', 400);
|
|
56
57
|
}
|
|
57
58
|
try {
|
|
58
59
|
const tokenEx = await global.repository.tokens.findOneById(id);
|
|
59
60
|
if (!tokenEx) {
|
|
60
|
-
|
|
61
|
+
throw new ServiceError('Token not found', 404);
|
|
61
62
|
}
|
|
62
63
|
const merged = global.repository.tokens.merge(tokenEx, token);
|
|
63
64
|
return await global.entity.Token.save(merged);
|
|
@@ -68,7 +69,7 @@ export async function updateTokenById(id, token) {
|
|
|
68
69
|
}
|
|
69
70
|
export async function retrieveTokenById(id) {
|
|
70
71
|
if (!id) {
|
|
71
|
-
|
|
72
|
+
throw new ServiceError('Invalid parameters', 400);
|
|
72
73
|
}
|
|
73
74
|
try {
|
|
74
75
|
return await global.repository.tokens.findOneById(id);
|
|
@@ -79,7 +80,7 @@ export async function retrieveTokenById(id) {
|
|
|
79
80
|
}
|
|
80
81
|
export async function retrieveTokenByExternalId(externalId) {
|
|
81
82
|
if (!externalId) {
|
|
82
|
-
|
|
83
|
+
throw new ServiceError('Invalid parameters', 400);
|
|
83
84
|
}
|
|
84
85
|
try {
|
|
85
86
|
return await global.repository.tokens.findOneBy({ externalId: externalId });
|
|
@@ -102,7 +103,7 @@ export async function findQuery(data) {
|
|
|
102
103
|
}
|
|
103
104
|
export async function removeTokenById(id) {
|
|
104
105
|
if (!id) {
|
|
105
|
-
|
|
106
|
+
throw new ServiceError('Invalid parameters', 400);
|
|
106
107
|
}
|
|
107
108
|
try {
|
|
108
109
|
return await global.repository.tokens.delete(id);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tokenManager.js","sourceRoot":"","sources":["../../../lib/loader/tokenManager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAA;AAChC,OAAO,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;AAEjE,MAAM,UAAU,aAAa;IAC3B,OAAO,IAAI,CAAA;AACb,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,YAAY,CAAC,IAAgC;IACjE,OAAO,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,UAAU,IAAI,CAAC,CAAC,IAAI,CAAC,IAAI,CAAA;AAChF,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,IAAgC;IAChE,MAAM,EAAE,IAAI,EAAE,WAAW,EAAE,GAAG,IAAI,CAAA;IAElC,IAAI,CAAC,IAAI,EAAE,CAAC;QACV,
|
|
1
|
+
{"version":3,"file":"tokenManager.js","sourceRoot":"","sources":["../../../lib/loader/tokenManager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAA;AAChC,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAA;AAC/C,OAAO,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;AAEjE,MAAM,UAAU,aAAa;IAC3B,OAAO,IAAI,CAAA;AACb,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,YAAY,CAAC,IAAgC;IACjE,OAAO,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC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S;IACxC,OAAO,MAAM,iBAAiB,CAAC,MAAM,CAAC,UAAU,CAAC,MAAM,EAAE,IAAI,EAAE,EAAE,CAAC,CAAA;AACpE,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,SAAS,CAAC,IAAS;IACvC,OAAO,MAAM,gBAAgB,CAAC,MAAM,CAAC,UAAU,CAAC,MAAM,EAAE,EAAE,EAAE,IAAI,CAAC,CAAA;AACnE,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,eAAe,CAAC,EAAU;IAC9C,IAAI,CAAC,EAAE,EAAE,CAAC;QACR,MAAM,IAAI,YAAY,CAAC,oBAAoB,EAAE,GAAG,CAAC,CAAA;IACnD,CAAC;IACD,IAAI,CAAC;QACH,OAAO,MAAM,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,CAAA;IAClD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,KAAK,CAAA;IACb,CAAC;AACH,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"userManager.d.ts","sourceRoot":"","sources":["../../../lib/loader/userManager.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"userManager.d.ts","sourceRoot":"","sources":["../../../lib/loader/userManager.ts"],"names":[],"mappings":"AAKA,wBAAgB,aAAa,YAE5B;AAED,wBAAsB,WAAW,CAAC,IAAI,EAAE,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,oBAEhE;AAED,wBAAsB,UAAU,CAAC,IAAI,EAAE,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,gBAsC/D;AAED,wBAAsB,UAAU,CAAC,EAAE,EAAE,MAAM,gBAe1C;AAED,wBAAsB,eAAe,CAAC,EAAE,EAAE,MAAM,gBAoB/C;AAED,wBAAsB,cAAc,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,gBAc/E;AAED,wBAAsB,gBAAgB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3
4
|
import { executeCountQuery, executeFindQuery } from '../query.js';
|
|
4
5
|
export function isImplemented() {
|
|
5
6
|
return true;
|
|
@@ -10,7 +11,7 @@ export async function isValidUser(data) {
|
|
|
10
11
|
export async function createUser(data) {
|
|
11
12
|
const { username, email, password } = data;
|
|
12
13
|
if (!email || !password) {
|
|
13
|
-
throw new
|
|
14
|
+
throw new ServiceError('Invalid parameters', 400);
|
|
14
15
|
}
|
|
15
16
|
const salt = await bcrypt.genSalt(12);
|
|
16
17
|
const hashedPassword = await bcrypt.hash(password, salt);
|
|
@@ -36,19 +37,19 @@ export async function createUser(data) {
|
|
|
36
37
|
}
|
|
37
38
|
catch (error) {
|
|
38
39
|
if (error?.code == 23505) {
|
|
39
|
-
throw new
|
|
40
|
+
throw new ServiceError('Email or username already registered', 409);
|
|
40
41
|
}
|
|
41
42
|
throw error;
|
|
42
43
|
}
|
|
43
44
|
}
|
|
44
45
|
export async function deleteUser(id) {
|
|
45
46
|
if (!id) {
|
|
46
|
-
throw new
|
|
47
|
+
throw new ServiceError('Invalid parameters', 400);
|
|
47
48
|
}
|
|
48
49
|
try {
|
|
49
50
|
const userEx = await retrieveUserById(id);
|
|
50
51
|
if (!userEx) {
|
|
51
|
-
throw new
|
|
52
|
+
throw new ServiceError('User not found', 404);
|
|
52
53
|
}
|
|
53
54
|
return global.entity.User.delete(id);
|
|
54
55
|
}
|
|
@@ -58,7 +59,7 @@ export async function deleteUser(id) {
|
|
|
58
59
|
}
|
|
59
60
|
export async function resetExternalId(id) {
|
|
60
61
|
if (!id) {
|
|
61
|
-
throw new
|
|
62
|
+
throw new ServiceError('Invalid parameters', 400);
|
|
62
63
|
}
|
|
63
64
|
try {
|
|
64
65
|
let externalId, user;
|
|
@@ -70,19 +71,19 @@ export async function resetExternalId(id) {
|
|
|
70
71
|
}
|
|
71
72
|
catch (error) {
|
|
72
73
|
if (error?.code == 23505) {
|
|
73
|
-
throw new
|
|
74
|
+
throw new ServiceError('External ID not changed', 409);
|
|
74
75
|
}
|
|
75
76
|
throw error;
|
|
76
77
|
}
|
|
77
78
|
}
|
|
78
79
|
export async function updateUserById(id, user) {
|
|
79
80
|
if (!id || !user) {
|
|
80
|
-
throw new
|
|
81
|
+
throw new ServiceError('Invalid parameters', 400);
|
|
81
82
|
}
|
|
82
83
|
try {
|
|
83
84
|
const userEx = await retrieveUserById(id);
|
|
84
85
|
if (!userEx) {
|
|
85
|
-
throw new
|
|
86
|
+
throw new ServiceError('User not found', 404);
|
|
86
87
|
}
|
|
87
88
|
const merged = global.repository.users.merge(userEx, user);
|
|
88
89
|
return await global.entity.User.save(merged);
|
|
@@ -93,7 +94,7 @@ export async function updateUserById(id, user) {
|
|
|
93
94
|
}
|
|
94
95
|
export async function retrieveUserById(id) {
|
|
95
96
|
if (!id) {
|
|
96
|
-
throw new
|
|
97
|
+
throw new ServiceError('Invalid parameters', 400);
|
|
97
98
|
}
|
|
98
99
|
try {
|
|
99
100
|
return await global.repository.users.findOneById(id);
|
|
@@ -104,7 +105,7 @@ export async function retrieveUserById(id) {
|
|
|
104
105
|
}
|
|
105
106
|
export async function retrieveUserByEmail(email) {
|
|
106
107
|
if (!email) {
|
|
107
|
-
throw new
|
|
108
|
+
throw new ServiceError('Invalid parameters', 400);
|
|
108
109
|
}
|
|
109
110
|
try {
|
|
110
111
|
return await global.repository.users.findOneBy({ email: email });
|
|
@@ -115,7 +116,7 @@ export async function retrieveUserByEmail(email) {
|
|
|
115
116
|
}
|
|
116
117
|
export async function retrieveUserByUsername(username) {
|
|
117
118
|
if (!username) {
|
|
118
|
-
throw new
|
|
119
|
+
throw new ServiceError('Invalid parameters', 400);
|
|
119
120
|
}
|
|
120
121
|
try {
|
|
121
122
|
return await global.repository.users.findOneBy({ username });
|
|
@@ -126,7 +127,7 @@ export async function retrieveUserByUsername(username) {
|
|
|
126
127
|
}
|
|
127
128
|
export async function retrieveUserByConfirmationToken(code) {
|
|
128
129
|
if (!code) {
|
|
129
|
-
throw new
|
|
130
|
+
throw new ServiceError('Invalid parameters', 400);
|
|
130
131
|
}
|
|
131
132
|
try {
|
|
132
133
|
return await global.repository.users.findOneBy({ confirmationToken: code });
|
|
@@ -137,7 +138,7 @@ export async function retrieveUserByConfirmationToken(code) {
|
|
|
137
138
|
}
|
|
138
139
|
export async function retrieveUserByResetPasswordToken(code) {
|
|
139
140
|
if (!code) {
|
|
140
|
-
throw new
|
|
141
|
+
throw new ServiceError('Invalid parameters', 400);
|
|
141
142
|
}
|
|
142
143
|
try {
|
|
143
144
|
return await global.repository.users.findOneBy({ resetPasswordToken: code });
|
|
@@ -148,7 +149,7 @@ export async function retrieveUserByResetPasswordToken(code) {
|
|
|
148
149
|
}
|
|
149
150
|
export async function retrieveUserByExternalId(externalId) {
|
|
150
151
|
if (!externalId) {
|
|
151
|
-
throw new
|
|
152
|
+
throw new ServiceError('Invalid parameters', 400);
|
|
152
153
|
}
|
|
153
154
|
try {
|
|
154
155
|
return await global.repository.users.findOne({
|
|
@@ -162,12 +163,12 @@ export async function retrieveUserByExternalId(externalId) {
|
|
|
162
163
|
}
|
|
163
164
|
export async function retrieveUserByPassword(email, password) {
|
|
164
165
|
if (!email || !password) {
|
|
165
|
-
throw new
|
|
166
|
+
throw new ServiceError('Invalid parameters', 400);
|
|
166
167
|
}
|
|
167
168
|
try {
|
|
168
169
|
const user = await global.repository.users.findOneBy({ email: email });
|
|
169
170
|
if (!user) {
|
|
170
|
-
throw new Error('
|
|
171
|
+
throw new Error('Wrong credentials');
|
|
171
172
|
}
|
|
172
173
|
const match = await bcrypt.compare(password, user.password);
|
|
173
174
|
return match ? user : null;
|
|
@@ -178,7 +179,7 @@ export async function retrieveUserByPassword(email, password) {
|
|
|
178
179
|
}
|
|
179
180
|
export async function changePassword(email, password, oldPassword) {
|
|
180
181
|
if (!email || !password || !oldPassword) {
|
|
181
|
-
throw new
|
|
182
|
+
throw new ServiceError('Invalid parameters', 400);
|
|
182
183
|
}
|
|
183
184
|
try {
|
|
184
185
|
const user = await global.repository.users.findOneBy({ email: email });
|
|
@@ -188,7 +189,7 @@ export async function changePassword(email, password, oldPassword) {
|
|
|
188
189
|
const hashedPassword = await bcrypt.hash(password, salt);
|
|
189
190
|
return await global.entity.User.save({ ...user, passwordChangedAt: new Date(), password: hashedPassword });
|
|
190
191
|
}
|
|
191
|
-
|
|
192
|
+
throw new ServiceError('Password not changed', 400);
|
|
192
193
|
}
|
|
193
194
|
catch (error) {
|
|
194
195
|
throw error;
|
|
@@ -196,7 +197,7 @@ export async function changePassword(email, password, oldPassword) {
|
|
|
196
197
|
}
|
|
197
198
|
export async function forgotPassword(email) {
|
|
198
199
|
if (!email) {
|
|
199
|
-
throw new
|
|
200
|
+
throw new ServiceError('Invalid parameters', 400);
|
|
200
201
|
}
|
|
201
202
|
try {
|
|
202
203
|
const user = await global.repository.users.findOneBy({ email: email });
|
|
@@ -206,7 +207,7 @@ export async function forgotPassword(email) {
|
|
|
206
207
|
resetPasswordToken: Crypto.randomBytes(64).toString('hex')
|
|
207
208
|
});
|
|
208
209
|
}
|
|
209
|
-
|
|
210
|
+
throw new ServiceError('Password not changed', 400);
|
|
210
211
|
}
|
|
211
212
|
catch (error) {
|
|
212
213
|
throw error;
|
|
@@ -214,7 +215,7 @@ export async function forgotPassword(email) {
|
|
|
214
215
|
}
|
|
215
216
|
export async function resetPassword(user, password) {
|
|
216
217
|
if (!user || !password) {
|
|
217
|
-
throw new
|
|
218
|
+
throw new ServiceError('Invalid parameters', 400);
|
|
218
219
|
}
|
|
219
220
|
try {
|
|
220
221
|
const salt = await bcrypt.genSalt(12);
|
|
@@ -234,7 +235,7 @@ export async function resetPassword(user, password) {
|
|
|
234
235
|
}
|
|
235
236
|
export async function userConfirmation(user) {
|
|
236
237
|
if (!user) {
|
|
237
|
-
throw new
|
|
238
|
+
throw new ServiceError('Invalid parameters', 400);
|
|
238
239
|
}
|
|
239
240
|
try {
|
|
240
241
|
return await global.entity.User.save({ ...user, confirmed: true, confirmedAt: new Date(), confirmationToken: null });
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"userManager.js","sourceRoot":"","sources":["../../../lib/loader/userManager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAA;AAChC,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAA;AAChC,OAAO,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;AAEjE,MAAM,UAAU,aAAa;IAC3B,OAAO,IAAI,CAAA;AACb,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,IAA+B;IAC/D,OAAO,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,UAAU,IAAI,CAAC,CAAC,IAAI,CAAC,KAAK,IAAI,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAA;AACpG,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,UAAU,CAAC,IAA+B;IAC9D,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,QAAQ,EAAE,GAAG,IAAI,CAAA;IAE1C,IAAI,CAAC,KAAK,IAAI,CAAC,QAAQ,EAAE,CAAC;QACxB,MAAM,IAAI,
|
|
1
|
+
{"version":3,"file":"userManager.js","sourceRoot":"","sources":["../../../lib/loader/userManager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAA;AAChC,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAA;AAChC,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAA;AAC/C,OAAO,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;AAEjE,MAAM,UAAU,aAAa;IAC3B,OAAO,IAAI,CAAA;AACb,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,IAA+B;IAC/D,OAAO,CAAC,CAAC,IAAI,IAAI,CA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AE,6BAA6B,EAAE,CAAC,CAAA;IAChH,OAAO,eAAe,CAAC,EAAE,CAAC,CAAA;AAC5B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../../../lib/util/crypto.ts"],"names":[],"mappings":"AAUA,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAO5C;AAED,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAU5C"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import * as crypto from 'crypto';
|
|
2
|
+
const ALGORITHM = 'aes-256-cbc';
|
|
3
|
+
const SECRET_KEY = process.env.MFA_DB_SECRET || process.env.JWT_SECRET || 'default_secret_must_be_changed_32b';
|
|
4
|
+
const IV_LENGTH = 16;
|
|
5
|
+
function getKey() {
|
|
6
|
+
return crypto.createHash('sha256').update(String(SECRET_KEY)).digest('base64').substr(0, 32);
|
|
7
|
+
}
|
|
8
|
+
export function encrypt(text) {
|
|
9
|
+
if (!text)
|
|
10
|
+
return text;
|
|
11
|
+
const iv = crypto.randomBytes(IV_LENGTH);
|
|
12
|
+
const cipher = crypto.createCipheriv(ALGORITHM, Buffer.from(getKey()), iv);
|
|
13
|
+
let encrypted = cipher.update(text);
|
|
14
|
+
encrypted = Buffer.concat([encrypted, cipher.final()]);
|
|
15
|
+
return iv.toString('hex') + ':' + encrypted.toString('hex');
|
|
16
|
+
}
|
|
17
|
+
export function decrypt(text) {
|
|
18
|
+
if (!text)
|
|
19
|
+
return text;
|
|
20
|
+
const textParts = text.split(':');
|
|
21
|
+
if (textParts.length < 2)
|
|
22
|
+
return text;
|
|
23
|
+
const iv = Buffer.from(textParts.shift(), 'hex');
|
|
24
|
+
const encryptedText = Buffer.from(textParts.join(':'), 'hex');
|
|
25
|
+
const decipher = crypto.createDecipheriv(ALGORITHM, Buffer.from(getKey()), iv);
|
|
26
|
+
let decrypted = decipher.update(encryptedText);
|
|
27
|
+
decrypted = Buffer.concat([decrypted, decipher.final()]);
|
|
28
|
+
return decrypted.toString();
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=crypto.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crypto.js","sourceRoot":"","sources":["../../../lib/util/crypto.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAA;AAEhC,MAAM,SAAS,GAAG,aAAa,CAAA;AAC/B,MAAM,UAAU,GAAG,OAAO,CAAC,GAAG,CAAC,aAAa,IAAI,OAAO,CAAC,GAAG,CAAC,UAAU,IAAI,oCAAoC,CAAA;AAC9G,MAAM,SAAS,GAAG,EAAE,CAAA;AAEpB,SAAS,MAAM;IACb,OAAO,MAAM,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,CAAC,EAAE,EAAE,CAAC,CAAA;AAC9F,CAAC;AAED,MAAM,UAAU,OAAO,CAAC,IAAY;IAClC,IAAI,CAAC,IAAI;QAAE,OAAO,IAAI,CAAA;IACtB,MAAM,EAAE,GAAG,MAAM,CAAC,WAAW,CAAC,SAAS,CAAC,CAAA;IACxC,MAAM,MAAM,GAAG,MAAM,CAAC,cAAc,CAAC,SAAS,EAAE,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC,CAAA;IAC1E,IAAI,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;IACnC,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,SAAS,EAAE,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAA;IACtD,OAAO,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,GAAG,GAAG,GAAG,SAAS,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAA;AAC7D,CAAC;AAED,MAAM,UAAU,OAAO,CAAC,IAAY;IAClC,IAAI,CAAC,IAAI;QAAE,OAAO,IAAI,CAAA;IACtB,MAAM,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IACjC,IAAI,SAAS,CAAC,MAAM,GAAG,CAAC;QAAE,OAAO,IAAI,CAAA;IACrC,MAAM,EAAE,GAAG,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,EAAG,EAAE,KAAK,CAAC,CAAA;IACjD,MAAM,aAAa,GAAG,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC,CAAA;IAC7D,MAAM,QAAQ,GAAG,MAAM,CAAC,gBAAgB,CAAC,SAAS,EAAE,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC,CAAA;IAC9E,IAAI,SAAS,GAAG,QAAQ,CAAC,MAAM,CAAC,aAAa,CAAC,CAAA;IAC9C,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,SAAS,EAAE,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC,CAAA;IACxD,OAAO,SAAS,CAAC,QAAQ,EAAE,CAAA;AAC7B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../../../lib/util/error.ts"],"names":[],"mappings":"AAAA,qBAAa,YAAa,SAAQ,KAAK;IACrC,UAAU,EAAE,MAAM,CAAA;gBACN,OAAO,EAAE,MAAM,EAAE,UAAU,GAAE,MAAY;CAItD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error.js","sourceRoot":"","sources":["../../../lib/util/error.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,YAAa,SAAQ,KAAK;IACrC,UAAU,CAAQ;IAClB,YAAY,OAAe,EAAE,aAAqB,GAAG;QACnD,KAAK,CAAC,OAAO,CAAC,CAAA;QACd,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;IAC9B,CAAC;CACF"}
|
package/lib/entities/user.ts
CHANGED
|
@@ -19,6 +19,13 @@ export abstract class User extends BaseEntity {
|
|
|
19
19
|
abstract createdAt: Date
|
|
20
20
|
abstract updatedAt: Date
|
|
21
21
|
abstract deletedAt: Date
|
|
22
|
+
|
|
23
|
+
// MFA Fields
|
|
24
|
+
abstract mfaEnabled: boolean
|
|
25
|
+
abstract mfaSecret: string
|
|
26
|
+
abstract mfaType: string
|
|
27
|
+
abstract mfaRecoveryCodes: string[]
|
|
28
|
+
|
|
22
29
|
abstract setId(id: any)
|
|
23
30
|
abstract getId(): any
|
|
24
31
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as Crypto from 'crypto'
|
|
2
|
+
import { ServiceError } from '../util/error.js'
|
|
2
3
|
import { executeCountQuery, executeFindQuery } from '../query.js'
|
|
3
4
|
|
|
4
5
|
export function isImplemented() {
|
|
@@ -13,7 +14,7 @@ export async function createToken(data: typeof global.entity.Token) {
|
|
|
13
14
|
const { name, description } = data
|
|
14
15
|
|
|
15
16
|
if (!name) {
|
|
16
|
-
|
|
17
|
+
throw new ServiceError('Invalid parameters', 400)
|
|
17
18
|
}
|
|
18
19
|
|
|
19
20
|
try {
|
|
@@ -41,7 +42,7 @@ export async function createToken(data: typeof global.entity.Token) {
|
|
|
41
42
|
|
|
42
43
|
export async function resetExternalId(id: string) {
|
|
43
44
|
if (!id) {
|
|
44
|
-
|
|
45
|
+
throw new ServiceError('Invalid parameters', 400)
|
|
45
46
|
}
|
|
46
47
|
|
|
47
48
|
try {
|
|
@@ -63,12 +64,12 @@ export async function resetExternalId(id: string) {
|
|
|
63
64
|
|
|
64
65
|
export async function updateTokenById(id: string, token: typeof global.entity.Token) {
|
|
65
66
|
if (!id || !token) {
|
|
66
|
-
|
|
67
|
+
throw new ServiceError('Invalid parameters', 400)
|
|
67
68
|
}
|
|
68
69
|
try {
|
|
69
70
|
const tokenEx = await global.repository.tokens.findOneById(id)
|
|
70
71
|
if (!tokenEx) {
|
|
71
|
-
|
|
72
|
+
throw new ServiceError('Token not found', 404)
|
|
72
73
|
}
|
|
73
74
|
const merged = global.repository.tokens.merge(tokenEx, token)
|
|
74
75
|
return await global.entity.Token.save(merged)
|
|
@@ -79,7 +80,7 @@ export async function updateTokenById(id: string, token: typeof global.entity.To
|
|
|
79
80
|
|
|
80
81
|
export async function retrieveTokenById(id: string) {
|
|
81
82
|
if (!id) {
|
|
82
|
-
|
|
83
|
+
throw new ServiceError('Invalid parameters', 400)
|
|
83
84
|
}
|
|
84
85
|
try {
|
|
85
86
|
return await global.repository.tokens.findOneById(id)
|
|
@@ -90,7 +91,7 @@ export async function retrieveTokenById(id: string) {
|
|
|
90
91
|
|
|
91
92
|
export async function retrieveTokenByExternalId(externalId: string) {
|
|
92
93
|
if (!externalId) {
|
|
93
|
-
|
|
94
|
+
throw new ServiceError('Invalid parameters', 400)
|
|
94
95
|
}
|
|
95
96
|
try {
|
|
96
97
|
return await global.repository.tokens.findOneBy({ externalId: externalId })
|
|
@@ -117,7 +118,7 @@ export async function findQuery(data: any) {
|
|
|
117
118
|
|
|
118
119
|
export async function removeTokenById(id: string) {
|
|
119
120
|
if (!id) {
|
|
120
|
-
|
|
121
|
+
throw new ServiceError('Invalid parameters', 400)
|
|
121
122
|
}
|
|
122
123
|
try {
|
|
123
124
|
return await global.repository.tokens.delete(id)
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as bcrypt from 'bcrypt'
|
|
2
2
|
import * as Crypto from 'crypto'
|
|
3
|
+
import { ServiceError } from '../util/error.js'
|
|
3
4
|
import { executeCountQuery, executeFindQuery } from '../query.js'
|
|
4
5
|
|
|
5
6
|
export function isImplemented() {
|
|
@@ -14,7 +15,7 @@ export async function createUser(data: typeof global.entity.User) {
|
|
|
14
15
|
const { username, email, password } = data
|
|
15
16
|
|
|
16
17
|
if (!email || !password) {
|
|
17
|
-
throw new
|
|
18
|
+
throw new ServiceError('Invalid parameters', 400)
|
|
18
19
|
}
|
|
19
20
|
|
|
20
21
|
const salt = await bcrypt.genSalt(12)
|
|
@@ -44,7 +45,7 @@ export async function createUser(data: typeof global.entity.User) {
|
|
|
44
45
|
return await global.entity.User.save(user)
|
|
45
46
|
} catch (error) {
|
|
46
47
|
if (error?.code == 23505) {
|
|
47
|
-
throw new
|
|
48
|
+
throw new ServiceError('Email or username already registered', 409)
|
|
48
49
|
}
|
|
49
50
|
throw error
|
|
50
51
|
}
|
|
@@ -52,13 +53,13 @@ export async function createUser(data: typeof global.entity.User) {
|
|
|
52
53
|
|
|
53
54
|
export async function deleteUser(id: string) {
|
|
54
55
|
if (!id) {
|
|
55
|
-
throw new
|
|
56
|
+
throw new ServiceError('Invalid parameters', 400)
|
|
56
57
|
}
|
|
57
58
|
|
|
58
59
|
try {
|
|
59
60
|
const userEx = await retrieveUserById(id)
|
|
60
61
|
if (!userEx) {
|
|
61
|
-
throw new
|
|
62
|
+
throw new ServiceError('User not found', 404)
|
|
62
63
|
}
|
|
63
64
|
|
|
64
65
|
return global.entity.User.delete(id)
|
|
@@ -69,7 +70,7 @@ export async function deleteUser(id: string) {
|
|
|
69
70
|
|
|
70
71
|
export async function resetExternalId(id: string) {
|
|
71
72
|
if (!id) {
|
|
72
|
-
throw new
|
|
73
|
+
throw new ServiceError('Invalid parameters', 400)
|
|
73
74
|
}
|
|
74
75
|
|
|
75
76
|
try {
|
|
@@ -83,7 +84,7 @@ export async function resetExternalId(id: string) {
|
|
|
83
84
|
return await updateUserById(id, { externalId: externalId })
|
|
84
85
|
} catch (error) {
|
|
85
86
|
if (error?.code == 23505) {
|
|
86
|
-
throw new
|
|
87
|
+
throw new ServiceError('External ID not changed', 409)
|
|
87
88
|
}
|
|
88
89
|
throw error
|
|
89
90
|
}
|
|
@@ -91,12 +92,12 @@ export async function resetExternalId(id: string) {
|
|
|
91
92
|
|
|
92
93
|
export async function updateUserById(id: string, user: typeof global.entity.User) {
|
|
93
94
|
if (!id || !user) {
|
|
94
|
-
throw new
|
|
95
|
+
throw new ServiceError('Invalid parameters', 400)
|
|
95
96
|
}
|
|
96
97
|
try {
|
|
97
98
|
const userEx = await retrieveUserById(id)
|
|
98
99
|
if (!userEx) {
|
|
99
|
-
throw new
|
|
100
|
+
throw new ServiceError('User not found', 404)
|
|
100
101
|
}
|
|
101
102
|
const merged = global.repository.users.merge(userEx, user)
|
|
102
103
|
return await global.entity.User.save(merged)
|
|
@@ -107,7 +108,7 @@ export async function updateUserById(id: string, user: typeof global.entity.User
|
|
|
107
108
|
|
|
108
109
|
export async function retrieveUserById(id: string) {
|
|
109
110
|
if (!id) {
|
|
110
|
-
throw new
|
|
111
|
+
throw new ServiceError('Invalid parameters', 400)
|
|
111
112
|
}
|
|
112
113
|
try {
|
|
113
114
|
return await global.repository.users.findOneById(id)
|
|
@@ -118,7 +119,7 @@ export async function retrieveUserById(id: string) {
|
|
|
118
119
|
|
|
119
120
|
export async function retrieveUserByEmail(email: string) {
|
|
120
121
|
if (!email) {
|
|
121
|
-
throw new
|
|
122
|
+
throw new ServiceError('Invalid parameters', 400)
|
|
122
123
|
}
|
|
123
124
|
try {
|
|
124
125
|
return await global.repository.users.findOneBy({ email: email })
|
|
@@ -129,7 +130,7 @@ export async function retrieveUserByEmail(email: string) {
|
|
|
129
130
|
|
|
130
131
|
export async function retrieveUserByUsername(username: string) {
|
|
131
132
|
if (!username) {
|
|
132
|
-
throw new
|
|
133
|
+
throw new ServiceError('Invalid parameters', 400)
|
|
133
134
|
}
|
|
134
135
|
try {
|
|
135
136
|
return await global.repository.users.findOneBy({ username })
|
|
@@ -140,7 +141,7 @@ export async function retrieveUserByUsername(username: string) {
|
|
|
140
141
|
|
|
141
142
|
export async function retrieveUserByConfirmationToken(code: string) {
|
|
142
143
|
if (!code) {
|
|
143
|
-
throw new
|
|
144
|
+
throw new ServiceError('Invalid parameters', 400)
|
|
144
145
|
}
|
|
145
146
|
try {
|
|
146
147
|
return await global.repository.users.findOneBy({ confirmationToken: code })
|
|
@@ -151,7 +152,7 @@ export async function retrieveUserByConfirmationToken(code: string) {
|
|
|
151
152
|
|
|
152
153
|
export async function retrieveUserByResetPasswordToken(code: string) {
|
|
153
154
|
if (!code) {
|
|
154
|
-
throw new
|
|
155
|
+
throw new ServiceError('Invalid parameters', 400)
|
|
155
156
|
}
|
|
156
157
|
try {
|
|
157
158
|
return await global.repository.users.findOneBy({ resetPasswordToken: code })
|
|
@@ -162,7 +163,7 @@ export async function retrieveUserByResetPasswordToken(code: string) {
|
|
|
162
163
|
|
|
163
164
|
export async function retrieveUserByExternalId(externalId: string) {
|
|
164
165
|
if (!externalId) {
|
|
165
|
-
throw new
|
|
166
|
+
throw new ServiceError('Invalid parameters', 400)
|
|
166
167
|
}
|
|
167
168
|
try {
|
|
168
169
|
return await global.repository.users.findOne({
|
|
@@ -176,12 +177,12 @@ export async function retrieveUserByExternalId(externalId: string) {
|
|
|
176
177
|
|
|
177
178
|
export async function retrieveUserByPassword(email: string, password: string) {
|
|
178
179
|
if (!email || !password) {
|
|
179
|
-
throw new
|
|
180
|
+
throw new ServiceError('Invalid parameters', 400)
|
|
180
181
|
}
|
|
181
182
|
try {
|
|
182
183
|
const user = await global.repository.users.findOneBy({ email: email })
|
|
183
184
|
if (!user) {
|
|
184
|
-
throw new Error('
|
|
185
|
+
throw new Error('Wrong credentials')
|
|
185
186
|
}
|
|
186
187
|
const match = await bcrypt.compare(password, user.password)
|
|
187
188
|
return match ? user : null
|
|
@@ -192,7 +193,7 @@ export async function retrieveUserByPassword(email: string, password: string) {
|
|
|
192
193
|
|
|
193
194
|
export async function changePassword(email: string, password: string, oldPassword: string) {
|
|
194
195
|
if (!email || !password || !oldPassword) {
|
|
195
|
-
throw new
|
|
196
|
+
throw new ServiceError('Invalid parameters', 400)
|
|
196
197
|
}
|
|
197
198
|
try {
|
|
198
199
|
const user = await global.repository.users.findOneBy({ email: email })
|
|
@@ -202,7 +203,7 @@ export async function changePassword(email: string, password: string, oldPasswor
|
|
|
202
203
|
const hashedPassword = await bcrypt.hash(password, salt)
|
|
203
204
|
return await global.entity.User.save({ ...user, passwordChangedAt: new Date(), password: hashedPassword })
|
|
204
205
|
}
|
|
205
|
-
|
|
206
|
+
throw new ServiceError('Password not changed', 400)
|
|
206
207
|
} catch (error) {
|
|
207
208
|
throw error
|
|
208
209
|
}
|
|
@@ -210,7 +211,7 @@ export async function changePassword(email: string, password: string, oldPasswor
|
|
|
210
211
|
|
|
211
212
|
export async function forgotPassword(email: string) {
|
|
212
213
|
if (!email) {
|
|
213
|
-
throw new
|
|
214
|
+
throw new ServiceError('Invalid parameters', 400)
|
|
214
215
|
}
|
|
215
216
|
try {
|
|
216
217
|
const user = await global.repository.users.findOneBy({ email: email })
|
|
@@ -221,7 +222,7 @@ export async function forgotPassword(email: string) {
|
|
|
221
222
|
resetPasswordToken: Crypto.randomBytes(64).toString('hex')
|
|
222
223
|
})
|
|
223
224
|
}
|
|
224
|
-
|
|
225
|
+
throw new ServiceError('Password not changed', 400)
|
|
225
226
|
} catch (error) {
|
|
226
227
|
throw error
|
|
227
228
|
}
|
|
@@ -229,7 +230,7 @@ export async function forgotPassword(email: string) {
|
|
|
229
230
|
|
|
230
231
|
export async function resetPassword(user: typeof global.entity.User, password: string) {
|
|
231
232
|
if (!user || !password) {
|
|
232
|
-
throw new
|
|
233
|
+
throw new ServiceError('Invalid parameters', 400)
|
|
233
234
|
}
|
|
234
235
|
try {
|
|
235
236
|
const salt = await bcrypt.genSalt(12)
|
|
@@ -249,7 +250,7 @@ export async function resetPassword(user: typeof global.entity.User, password: s
|
|
|
249
250
|
|
|
250
251
|
export async function userConfirmation(user: typeof global.entity.User) {
|
|
251
252
|
if (!user) {
|
|
252
|
-
throw new
|
|
253
|
+
throw new ServiceError('Invalid parameters', 400)
|
|
253
254
|
}
|
|
254
255
|
try {
|
|
255
256
|
return await global.entity.User.save({ ...user, confirmed: true, confirmedAt: new Date(), confirmationToken: null })
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import * as crypto from 'crypto'
|
|
2
|
+
|
|
3
|
+
const ALGORITHM = 'aes-256-cbc'
|
|
4
|
+
const SECRET_KEY = process.env.MFA_DB_SECRET || process.env.JWT_SECRET || 'default_secret_must_be_changed_32b'
|
|
5
|
+
const IV_LENGTH = 16
|
|
6
|
+
|
|
7
|
+
function getKey() {
|
|
8
|
+
return crypto.createHash('sha256').update(String(SECRET_KEY)).digest('base64').substr(0, 32)
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function encrypt(text: string): string {
|
|
12
|
+
if (!text) return text
|
|
13
|
+
const iv = crypto.randomBytes(IV_LENGTH)
|
|
14
|
+
const cipher = crypto.createCipheriv(ALGORITHM, Buffer.from(getKey()), iv)
|
|
15
|
+
let encrypted = cipher.update(text)
|
|
16
|
+
encrypted = Buffer.concat([encrypted, cipher.final()])
|
|
17
|
+
return iv.toString('hex') + ':' + encrypted.toString('hex')
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function decrypt(text: string): string {
|
|
21
|
+
if (!text) return text
|
|
22
|
+
const textParts = text.split(':')
|
|
23
|
+
if (textParts.length < 2) return text // Return as is if not encrypted properly
|
|
24
|
+
const iv = Buffer.from(textParts.shift()!, 'hex')
|
|
25
|
+
const encryptedText = Buffer.from(textParts.join(':'), 'hex')
|
|
26
|
+
const decipher = crypto.createDecipheriv(ALGORITHM, Buffer.from(getKey()), iv)
|
|
27
|
+
let decrypted = decipher.update(encryptedText)
|
|
28
|
+
decrypted = Buffer.concat([decrypted, decipher.final()])
|
|
29
|
+
return decrypted.toString()
|
|
30
|
+
}
|