@flink-app/generic-auth-plugin 0.3.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.
- package/.flink/generatedHandlers.ts +1 -1
- package/.flink/generatedRepos.ts +1 -1
- package/.flink/schemas/schemas.ts +1 -1
- package/.flink/start.ts +1 -1
- package/dist/.flink/generatedHandlers.js +1 -1
- package/dist/.flink/generatedRepos.js +1 -1
- package/dist/.flink/start.js +1 -1
- package/dist/src/coreFunctions.js +2 -2
- package/package.json +2 -2
- package/src/coreFunctions.ts +245 -270
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Generated Fri
|
|
1
|
+
// Generated Fri Dec 03 2021 10:39:01 GMT+0100 (Central European Standard Time)
|
|
2
2
|
import { autoRegisteredHandlers, HttpMethod } from "@flink-app/flink";
|
|
3
3
|
import * as UserCreate_0 from "../src/handlers/UserCreate";
|
|
4
4
|
import * as UserLogin_0 from "../src/handlers/UserLogin";
|
package/.flink/generatedRepos.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Generated Fri
|
|
1
|
+
// Generated Fri Dec 03 2021 10:39:01 GMT+0100 (Central European Standard Time)
|
|
2
2
|
import { autoRegisteredRepos } from "@flink-app/flink";
|
|
3
3
|
export const repos = [];
|
|
4
4
|
autoRegisteredRepos.push(...repos);
|
|
@@ -27,7 +27,7 @@ import { PutManagementUserRolesByUseridRes } from "../../src/schemas/Management/
|
|
|
27
27
|
import { PutManagementUserUsernameByUseridReq } from "../../src/schemas/Management/PutUserUsernameByUseridReq";
|
|
28
28
|
import { PutManagementUserUsernameByUseridRes } from "../../src/schemas/Management/PutUserUsernameByUseridRes";
|
|
29
29
|
|
|
30
|
-
// Generated Fri
|
|
30
|
+
// Generated Fri Dec 03 2021 10:39:03 GMT+0100 (Central European Standard Time)
|
|
31
31
|
export interface UserCreate_13_ReqSchema extends UserCreateReq {}
|
|
32
32
|
|
|
33
33
|
export interface UserCreate_13_ResSchema extends UserCreateRes {}
|
package/.flink/start.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.handlers = void 0;
|
|
4
|
-
// Generated Fri
|
|
4
|
+
// Generated Fri Dec 03 2021 10:39:01 GMT+0100 (Central European Standard Time)
|
|
5
5
|
var flink_1 = require("@flink-app/flink");
|
|
6
6
|
exports.handlers = [];
|
|
7
7
|
flink_1.autoRegisteredHandlers.push.apply(flink_1.autoRegisteredHandlers, exports.handlers);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.repos = void 0;
|
|
4
|
-
// Generated Fri
|
|
4
|
+
// Generated Fri Dec 03 2021 10:39:01 GMT+0100 (Central European Standard Time)
|
|
5
5
|
var flink_1 = require("@flink-app/flink");
|
|
6
6
|
exports.repos = [];
|
|
7
7
|
flink_1.autoRegisteredRepos.push.apply(flink_1.autoRegisteredRepos, exports.repos);
|
package/dist/.flink/start.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
// Generated Fri
|
|
3
|
+
// Generated Fri Dec 03 2021 10:39:01 GMT+0100 (Central European Standard Time)
|
|
4
4
|
require("./generatedHandlers");
|
|
5
5
|
require("./generatedRepos");
|
|
6
6
|
require("../src/index");
|
|
@@ -87,7 +87,7 @@ function createUser(repo, auth, username, password, authentificationMethod, role
|
|
|
87
87
|
}];
|
|
88
88
|
}
|
|
89
89
|
userData = {
|
|
90
|
-
username: username,
|
|
90
|
+
username: username.toLowerCase(),
|
|
91
91
|
roles: roles,
|
|
92
92
|
profile: profile,
|
|
93
93
|
authentificationMethod: authentificationMethod,
|
|
@@ -281,7 +281,7 @@ function passwordResetComplete(repo, auth, jwtSecret, passwordResetToken, code,
|
|
|
281
281
|
payload = { type: "", username: "" };
|
|
282
282
|
try {
|
|
283
283
|
secret = jwtSecret + ":" + code;
|
|
284
|
-
payload =
|
|
284
|
+
payload = jsonwebtoken_1.default.verify(passwordResetToken, secret);
|
|
285
285
|
}
|
|
286
286
|
catch (ex) {
|
|
287
287
|
return [2 /*return*/, { status: "invalidCode" }];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flink-app/generic-auth-plugin",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "Flink plugin that provides a generic user authentification solution.",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "echo \"Error: no test specified\"",
|
|
@@ -29,5 +29,5 @@
|
|
|
29
29
|
"ts-node": "^9.1.1",
|
|
30
30
|
"typescript": "^4.2.4"
|
|
31
31
|
},
|
|
32
|
-
"gitHead": "
|
|
32
|
+
"gitHead": "95140d0eaa2c17a8f6214c44fe5dda057585af82"
|
|
33
33
|
}
|
package/src/coreFunctions.ts
CHANGED
|
@@ -11,314 +11,289 @@ import { UserPasswordResetCompleteRes } from "./schemas/UserPasswordResetComplet
|
|
|
11
11
|
|
|
12
12
|
import jsonwebtoken from "jsonwebtoken";
|
|
13
13
|
|
|
14
|
-
export function getJtwTokenPlugin(
|
|
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
|
-
passwordPolicy,
|
|
42
|
-
rolePermissions,
|
|
43
|
-
});
|
|
14
|
+
export function getJtwTokenPlugin(secret: string, rolePermissions?: { [role: string]: string[] }, passwordPolicy?: RegExp) {
|
|
15
|
+
if (passwordPolicy == undefined) {
|
|
16
|
+
passwordPolicy = /.{1,}$/;
|
|
17
|
+
}
|
|
18
|
+
if (rolePermissions == undefined) {
|
|
19
|
+
rolePermissions = {};
|
|
20
|
+
}
|
|
21
|
+
if (rolePermissions["user"] == null) {
|
|
22
|
+
rolePermissions["user"] = [];
|
|
23
|
+
}
|
|
24
|
+
if (!rolePermissions["user"].includes("authenticated")) rolePermissions["user"].push("authenticated");
|
|
25
|
+
|
|
26
|
+
return jwtAuthPlugin({
|
|
27
|
+
secret,
|
|
28
|
+
getUser: (tokenData: any) => {
|
|
29
|
+
return new Promise<FlinkAuthUser>((res) => {
|
|
30
|
+
res({
|
|
31
|
+
username: tokenData.username,
|
|
32
|
+
_id: tokenData._id,
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
},
|
|
36
|
+
passwordPolicy,
|
|
37
|
+
rolePermissions,
|
|
38
|
+
});
|
|
44
39
|
}
|
|
45
40
|
|
|
46
41
|
export async function createUser(
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
42
|
+
repo: FlinkRepo<any, User>,
|
|
43
|
+
auth: JwtAuthPlugin,
|
|
44
|
+
username: string,
|
|
45
|
+
password: string,
|
|
46
|
+
authentificationMethod: "password" | "sms",
|
|
47
|
+
roles: string[],
|
|
48
|
+
profile: UserProfile,
|
|
49
|
+
createPasswordHashAndSaltMethod?: {
|
|
50
|
+
(password: string): Promise<{ hash: string; salt: string } | null>;
|
|
51
|
+
}
|
|
57
52
|
): Promise<UserCreateRes> {
|
|
58
|
-
|
|
53
|
+
if (!roles.includes("user")) roles.push("user");
|
|
59
54
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}
|
|
66
|
-
let userData: Omit<User, "_id"> = {
|
|
67
|
-
username,
|
|
68
|
-
roles,
|
|
69
|
-
profile,
|
|
70
|
-
authentificationMethod,
|
|
71
|
-
pushNotificationTokens: [],
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
if (authentificationMethod == "password") {
|
|
75
|
-
let passwordAndSalt = null;
|
|
76
|
-
if (createPasswordHashAndSaltMethod != null) {
|
|
77
|
-
passwordAndSalt = await createPasswordHashAndSaltMethod(password);
|
|
78
|
-
} else {
|
|
79
|
-
passwordAndSalt = await auth.createPasswordHashAndSalt(password);
|
|
55
|
+
const existingUser = await repo.getOne({ username: username.toLowerCase() });
|
|
56
|
+
if (existingUser != null) {
|
|
57
|
+
return {
|
|
58
|
+
status: "userExists",
|
|
59
|
+
};
|
|
80
60
|
}
|
|
61
|
+
let userData: Omit<User, "_id"> = {
|
|
62
|
+
username: username.toLowerCase(),
|
|
63
|
+
roles,
|
|
64
|
+
profile,
|
|
65
|
+
authentificationMethod,
|
|
66
|
+
pushNotificationTokens: [],
|
|
67
|
+
};
|
|
81
68
|
|
|
82
|
-
if (
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
69
|
+
if (authentificationMethod == "password") {
|
|
70
|
+
let passwordAndSalt = null;
|
|
71
|
+
if (createPasswordHashAndSaltMethod != null) {
|
|
72
|
+
passwordAndSalt = await createPasswordHashAndSaltMethod(password);
|
|
73
|
+
} else {
|
|
74
|
+
passwordAndSalt = await auth.createPasswordHashAndSalt(password);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (passwordAndSalt == null) {
|
|
78
|
+
return {
|
|
79
|
+
status: "passwordError",
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
userData.password = passwordAndSalt.hash;
|
|
83
|
+
userData.salt = passwordAndSalt.salt;
|
|
86
84
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
user: {
|
|
101
|
-
_id: user._id,
|
|
102
|
-
token: token,
|
|
103
|
-
username: username.toLowerCase(),
|
|
104
|
-
},
|
|
105
|
-
};
|
|
85
|
+
|
|
86
|
+
const user = await repo.create(userData);
|
|
87
|
+
|
|
88
|
+
const token = await auth.createToken({ username: username.toLowerCase(), _id: user._id }, roles);
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
status: "success",
|
|
92
|
+
user: {
|
|
93
|
+
_id: user._id,
|
|
94
|
+
token: token,
|
|
95
|
+
username: username.toLowerCase(),
|
|
96
|
+
},
|
|
97
|
+
};
|
|
106
98
|
}
|
|
107
99
|
|
|
108
100
|
export async function loginUser(
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
101
|
+
repo: FlinkRepo<any, User>,
|
|
102
|
+
auth: JwtAuthPlugin,
|
|
103
|
+
username: string,
|
|
104
|
+
password: string | undefined,
|
|
105
|
+
validatePasswordMethod?: {
|
|
106
|
+
(password: string, hash: string, salt: string): Promise<boolean>;
|
|
107
|
+
}
|
|
116
108
|
): Promise<UserLoginRes> {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
let valid: boolean = false;
|
|
123
|
-
|
|
124
|
-
if (user.authentificationMethod == "password") {
|
|
125
|
-
if (password == null) password = "";
|
|
126
|
-
|
|
127
|
-
if (validatePasswordMethod != null) {
|
|
128
|
-
valid = await validatePasswordMethod(
|
|
129
|
-
password,
|
|
130
|
-
<string>user.password,
|
|
131
|
-
<string>user.salt
|
|
132
|
-
);
|
|
133
|
-
|
|
134
|
-
//If not valid, try to use default auth
|
|
135
|
-
if (!valid) {
|
|
136
|
-
try {
|
|
137
|
-
valid = await auth.validatePassword(
|
|
138
|
-
password,
|
|
139
|
-
<string>user.password,
|
|
140
|
-
<string>user.salt
|
|
141
|
-
);
|
|
142
|
-
} catch (ex) {}
|
|
143
|
-
}
|
|
144
|
-
} else {
|
|
145
|
-
valid = await auth.validatePassword(
|
|
146
|
-
password,
|
|
147
|
-
<string>user.password,
|
|
148
|
-
<string>user.salt
|
|
149
|
-
);
|
|
109
|
+
const user = await repo.getOne({ username: username.toLowerCase() });
|
|
110
|
+
if (user == null) {
|
|
111
|
+
return { status: "failed" };
|
|
150
112
|
}
|
|
151
|
-
}
|
|
152
|
-
if (user.authentificationMethod == "sms") {
|
|
153
|
-
log.error("SMS login is not yet impleted.");
|
|
154
|
-
return { status: "failed" };
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
if (valid) {
|
|
158
|
-
const token = await auth.createToken(
|
|
159
|
-
{ username: username.toLowerCase(), _id: user._id },
|
|
160
|
-
user.roles
|
|
161
|
-
);
|
|
162
113
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
114
|
+
let valid: boolean = false;
|
|
115
|
+
|
|
116
|
+
if (user.authentificationMethod == "password") {
|
|
117
|
+
if (password == null) password = "";
|
|
118
|
+
|
|
119
|
+
if (validatePasswordMethod != null) {
|
|
120
|
+
valid = await validatePasswordMethod(password, <string>user.password, <string>user.salt);
|
|
121
|
+
|
|
122
|
+
//If not valid, try to use default auth
|
|
123
|
+
if (!valid) {
|
|
124
|
+
try {
|
|
125
|
+
valid = await auth.validatePassword(password, <string>user.password, <string>user.salt);
|
|
126
|
+
} catch (ex) {}
|
|
127
|
+
}
|
|
128
|
+
} else {
|
|
129
|
+
valid = await auth.validatePassword(password, <string>user.password, <string>user.salt);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
if (user.authentificationMethod == "sms") {
|
|
133
|
+
log.error("SMS login is not yet impleted.");
|
|
134
|
+
return { status: "failed" };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (valid) {
|
|
138
|
+
const token = await auth.createToken({ username: username.toLowerCase(), _id: user._id }, user.roles);
|
|
139
|
+
|
|
140
|
+
return {
|
|
141
|
+
status: "success",
|
|
142
|
+
user: {
|
|
143
|
+
_id: user._id,
|
|
144
|
+
username: user.username,
|
|
145
|
+
token,
|
|
146
|
+
profile: user.profile,
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
} else {
|
|
150
|
+
return { status: "failed" };
|
|
151
|
+
}
|
|
175
152
|
}
|
|
176
153
|
|
|
177
154
|
export async function changePassword(
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
155
|
+
repo: FlinkRepo<any, User>,
|
|
156
|
+
auth: JwtAuthPlugin,
|
|
157
|
+
userId: string,
|
|
158
|
+
newPassword: string,
|
|
159
|
+
createPasswordHashAndSaltMethod?: {
|
|
160
|
+
(password: string): Promise<{ hash: string; salt: string } | null>;
|
|
161
|
+
}
|
|
185
162
|
): Promise<UserPasswordChangeRes> {
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
163
|
+
const user = await repo.getById(userId);
|
|
164
|
+
if (user == null) {
|
|
165
|
+
return { status: "failed" };
|
|
166
|
+
}
|
|
190
167
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
168
|
+
if (user.authentificationMethod != "password") {
|
|
169
|
+
return { status: "failed" };
|
|
170
|
+
}
|
|
194
171
|
|
|
195
|
-
|
|
172
|
+
let passwordAndSalt = null;
|
|
196
173
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
174
|
+
if (createPasswordHashAndSaltMethod == null) {
|
|
175
|
+
passwordAndSalt = await auth.createPasswordHashAndSalt(newPassword);
|
|
176
|
+
} else {
|
|
177
|
+
passwordAndSalt = await createPasswordHashAndSaltMethod(newPassword);
|
|
178
|
+
}
|
|
202
179
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
180
|
+
if (passwordAndSalt == null) {
|
|
181
|
+
return {
|
|
182
|
+
status: "passwordError",
|
|
183
|
+
};
|
|
184
|
+
}
|
|
208
185
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
186
|
+
await repo.updateOne(userId, {
|
|
187
|
+
password: passwordAndSalt.hash,
|
|
188
|
+
salt: passwordAndSalt.salt,
|
|
189
|
+
});
|
|
213
190
|
|
|
214
|
-
|
|
191
|
+
return { status: "success" };
|
|
215
192
|
}
|
|
216
193
|
|
|
217
194
|
export async function passwordResetStart(
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
195
|
+
repo: FlinkRepo<any, User>,
|
|
196
|
+
auth: JwtAuthPlugin,
|
|
197
|
+
jwtSecret: string,
|
|
198
|
+
username: string,
|
|
199
|
+
numberOfDigits?: number,
|
|
200
|
+
lifeTime?: string
|
|
224
201
|
): Promise<UserPasswordResetStartRes> {
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
const secret = jwtSecret + ":" + code;
|
|
244
|
-
|
|
245
|
-
const options: jsonwebtoken.SignOptions = {
|
|
246
|
-
expiresIn: lifeTime,
|
|
247
|
-
};
|
|
248
|
-
|
|
249
|
-
const token = jsonwebtoken.sign(payload, secret, options);
|
|
250
|
-
|
|
251
|
-
return {
|
|
252
|
-
status: "success",
|
|
253
|
-
passwordResetToken: token,
|
|
254
|
-
code,
|
|
255
|
-
profile: user.profile,
|
|
256
|
-
};
|
|
257
|
-
}
|
|
202
|
+
const user = await repo.getOne({ username: username.toLowerCase() });
|
|
203
|
+
if (user == null) {
|
|
204
|
+
return { status: "userNotFound" };
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (user.authentificationMethod != "password") {
|
|
208
|
+
return { status: "userNotFound" };
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (numberOfDigits == null) numberOfDigits = 6;
|
|
212
|
+
if (lifeTime == null) lifeTime = "1h";
|
|
213
|
+
|
|
214
|
+
const payload = {
|
|
215
|
+
type: "passwordReset",
|
|
216
|
+
username: username.toLocaleLowerCase(),
|
|
217
|
+
};
|
|
218
|
+
const code = generate(numberOfDigits);
|
|
258
219
|
|
|
259
|
-
export async function passwordResetComplete(
|
|
260
|
-
repo: FlinkRepo<any, User>,
|
|
261
|
-
auth: JwtAuthPlugin,
|
|
262
|
-
jwtSecret: string,
|
|
263
|
-
passwordResetToken: string,
|
|
264
|
-
code: string,
|
|
265
|
-
newPassword: string,
|
|
266
|
-
createPasswordHashAndSaltMethod?: {
|
|
267
|
-
(password: string): Promise<{ hash: string; salt: string } | null>;
|
|
268
|
-
}
|
|
269
|
-
): Promise<UserPasswordResetCompleteRes> {
|
|
270
|
-
let payload: { type: string; username: string } = { type: "", username: "" };
|
|
271
|
-
try {
|
|
272
220
|
const secret = jwtSecret + ":" + code;
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
const user = await repo.getOne({ username: payload.username });
|
|
281
|
-
if (user == null) {
|
|
282
|
-
return { status: "userNotFound" };
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
if (user.authentificationMethod != "password") {
|
|
286
|
-
return { status: "userNotFound" };
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
let passwordAndSalt = null;
|
|
290
|
-
|
|
291
|
-
if (createPasswordHashAndSaltMethod == null) {
|
|
292
|
-
passwordAndSalt = await auth.createPasswordHashAndSalt(newPassword);
|
|
293
|
-
} else {
|
|
294
|
-
passwordAndSalt = await createPasswordHashAndSaltMethod(newPassword);
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
if (passwordAndSalt == null) {
|
|
221
|
+
|
|
222
|
+
const options: jsonwebtoken.SignOptions = {
|
|
223
|
+
expiresIn: lifeTime,
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
const token = jsonwebtoken.sign(payload, secret, options);
|
|
227
|
+
|
|
298
228
|
return {
|
|
299
|
-
|
|
229
|
+
status: "success",
|
|
230
|
+
passwordResetToken: token,
|
|
231
|
+
code,
|
|
232
|
+
profile: user.profile,
|
|
300
233
|
};
|
|
301
|
-
|
|
234
|
+
}
|
|
302
235
|
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
236
|
+
export async function passwordResetComplete(
|
|
237
|
+
repo: FlinkRepo<any, User>,
|
|
238
|
+
auth: JwtAuthPlugin,
|
|
239
|
+
jwtSecret: string,
|
|
240
|
+
passwordResetToken: string,
|
|
241
|
+
code: string,
|
|
242
|
+
newPassword: string,
|
|
243
|
+
createPasswordHashAndSaltMethod?: {
|
|
244
|
+
(password: string): Promise<{ hash: string; salt: string } | null>;
|
|
245
|
+
}
|
|
246
|
+
): Promise<UserPasswordResetCompleteRes> {
|
|
247
|
+
let payload: { type: string; username: string } = { type: "", username: "" };
|
|
248
|
+
try {
|
|
249
|
+
const secret = jwtSecret + ":" + code;
|
|
250
|
+
payload = <{ type: string; username: string }>jsonwebtoken.verify(passwordResetToken, secret);
|
|
251
|
+
} catch (ex) {
|
|
252
|
+
return { status: "invalidCode" };
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const user = await repo.getOne({ username: payload.username });
|
|
256
|
+
if (user == null) {
|
|
257
|
+
return { status: "userNotFound" };
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
if (user.authentificationMethod != "password") {
|
|
261
|
+
return { status: "userNotFound" };
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
let passwordAndSalt = null;
|
|
307
265
|
|
|
308
|
-
|
|
266
|
+
if (createPasswordHashAndSaltMethod == null) {
|
|
267
|
+
passwordAndSalt = await auth.createPasswordHashAndSalt(newPassword);
|
|
268
|
+
} else {
|
|
269
|
+
passwordAndSalt = await createPasswordHashAndSaltMethod(newPassword);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
if (passwordAndSalt == null) {
|
|
273
|
+
return {
|
|
274
|
+
status: "passwordError",
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
await repo.updateOne(user._id, {
|
|
279
|
+
password: passwordAndSalt.hash,
|
|
280
|
+
salt: passwordAndSalt.salt,
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
return { status: "success" };
|
|
309
284
|
}
|
|
310
285
|
|
|
311
286
|
function generate(n: number): string {
|
|
312
|
-
|
|
313
|
-
|
|
287
|
+
var add = 1,
|
|
288
|
+
max = 12 - add;
|
|
314
289
|
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
290
|
+
if (n > max) {
|
|
291
|
+
return generate(max) + generate(n - max);
|
|
292
|
+
}
|
|
318
293
|
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
294
|
+
max = Math.pow(10, n + add);
|
|
295
|
+
var min = max / 10;
|
|
296
|
+
var number = Math.floor(Math.random() * (max - min + 1)) + min;
|
|
322
297
|
|
|
323
|
-
|
|
298
|
+
return ("" + number).substring(add);
|
|
324
299
|
}
|