beech-api 3.9.0-beta.9-rc → 3.9.75
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/LICENSE +21 -21
- package/README.md +1715 -1649
- package/index.js +2 -2
- package/package.json +92 -84
- package/packages/cli/beech +9 -10
- package/packages/cli/bin/beech-app.js +390 -390
- package/packages/cli/bin/beech-service.js +263 -170
- package/packages/cli/core/auth/Credentials.js +174 -174
- package/packages/cli/core/auth/Passport.js +664 -664
- package/packages/cli/core/auth/_Request.js +12 -12
- package/packages/cli/core/configure/_gitignore +16 -15
- package/packages/cli/core/configure/_sequelizerc +9 -9
- package/packages/cli/core/configure/app.config-basic.js +55 -55
- package/packages/cli/core/configure/app.config-sequelize.js +88 -88
- package/packages/cli/core/configure/beech.config.js +9 -9
- package/packages/cli/core/configure/global.config-basic.js +8 -8
- package/packages/cli/core/configure/global.config-sequelize.js +8 -8
- package/packages/cli/core/configure/jest.config.js +6 -6
- package/packages/cli/core/configure/jsconfig.json +8 -7
- package/packages/cli/core/configure/passport.config.js +97 -97
- package/packages/cli/core/databases/mysql.js +94 -95
- package/packages/cli/core/databases/sequelize.js +187 -188
- package/packages/cli/core/databases/test.js +250 -255
- package/packages/cli/core/file-walk/file-walk.js +35 -35
- package/packages/cli/core/generator/_endpoints +15 -15
- package/packages/cli/core/generator/_endpoints_basic +42 -42
- package/packages/cli/core/generator/_help +26 -18
- package/packages/cli/core/generator/_help_create +10 -10
- package/packages/cli/core/generator/_help_service +10 -10
- package/packages/cli/core/generator/_helpers +9 -9
- package/packages/cli/core/generator/_helpers_basic +22 -22
- package/packages/cli/core/generator/_models +6 -6
- package/packages/cli/core/generator/_models_basic +13 -13
- package/packages/cli/core/generator/_package +23 -19
- package/packages/cli/core/generator/_scheduler +32 -32
- package/packages/cli/core/generator/_spec +29 -29
- package/packages/cli/core/generator/index.js +1081 -992
- package/packages/cli/core/helpers/2fa.js +106 -106
- package/packages/cli/core/helpers/math.js +115 -115
- package/packages/cli/core/helpers/poolEntity.js +103 -103
- package/packages/cli/core/index.js +264 -266
- package/packages/cli/core/middleware/express/duplicateRequest.js +16 -16
- package/packages/cli/core/middleware/express/jwtCheckAllow.js +85 -85
- package/packages/cli/core/middleware/express/rateLimit.js +29 -29
- package/packages/cli/core/middleware/express/slowDown.js +2 -2
- package/packages/cli/core/middleware/index.js +6 -6
- package/packages/cli/core/middleware/origin/guard/advance.js +75 -75
- package/packages/cli/core/middleware/origin/whitelist/cors.js +94 -94
- package/packages/cli/core/services/http.express.js +481 -481
- package/packages/cli/core/test/check-node.js +21 -21
- package/packages/cli/core/test/utils.js +7 -7
- package/packages/cli/entry +10 -0
- package/packages/lib/index.js +6 -6
- package/packages/lib/src/endpoint.js +947 -885
- package/packages/lib/src/guard.js +60 -60
- package/packages/lib/src/salt.js +3 -3
- package/packages/lib/src/schema.js +96 -96
- package/packages/lib/src/specificExpress.js +7 -7
- package/packages/lib/src/user.js +271 -271
|
@@ -1,482 +1,482 @@
|
|
|
1
|
-
const package = require(appRoot + '/package.json');
|
|
2
|
-
const fs = require("fs");
|
|
3
|
-
const passport_config_file = "/passport.config.js";
|
|
4
|
-
const auth = require("../auth/Credentials");
|
|
5
|
-
const { TwoFactor } = require("../helpers/2fa");
|
|
6
|
-
const { Limiter, Duplicater } = require("../middleware");
|
|
7
|
-
|
|
8
|
-
module.exports = {
|
|
9
|
-
expressStart() {
|
|
10
|
-
return new Promise((resolve, reject) => {
|
|
11
|
-
try {
|
|
12
|
-
// Create express server
|
|
13
|
-
const ExpressServer = _app_.listen(_config_.main_config.app_port, async () => {
|
|
14
|
-
await console.log('[102m[90m Passed [0m[0m Service is started at:');
|
|
15
|
-
await console.log('- [91mLocal[0m: [36mhttp://' + _config_.main_config.app_host + ':' + ExpressServer.address().port + '[0m');
|
|
16
|
-
await console.log('- [91mNetwork[0m: [36m' + _config_.main_config.client_host + '[0m');
|
|
17
|
-
await new Promise((resolve) => resolve(this.authentication()));
|
|
18
|
-
await new Promise((resolve) => {
|
|
19
|
-
if(this.scheduler()) {
|
|
20
|
-
resolve(true);
|
|
21
|
-
}
|
|
22
|
-
});
|
|
23
|
-
await new Promise((resolve) => resolve(this.badRequest()));
|
|
24
|
-
await resolve(ExpressServer);
|
|
25
|
-
});
|
|
26
|
-
} catch (error) {
|
|
27
|
-
reject(error);
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
},
|
|
31
|
-
// Bad request
|
|
32
|
-
badRequest() {
|
|
33
|
-
return new Promise((resolve, reject) => {
|
|
34
|
-
try {
|
|
35
|
-
// base get request
|
|
36
|
-
endpoint.get('/', (req, res) => {
|
|
37
|
-
res.status(200).json({
|
|
38
|
-
code: 200,
|
|
39
|
-
status: "SUCCESS",
|
|
40
|
-
message: `Welcome to ${package.name} (version ${package.version})`
|
|
41
|
-
});
|
|
42
|
-
});
|
|
43
|
-
// request 404 not found
|
|
44
|
-
endpoint.use((req, res, next) => {
|
|
45
|
-
res.status(404).json({
|
|
46
|
-
code: 404,
|
|
47
|
-
status: "404_NOT_FOUND",
|
|
48
|
-
message: "The Endpoint not found!.",
|
|
49
|
-
});
|
|
50
|
-
next();
|
|
51
|
-
});
|
|
52
|
-
// resolve it.
|
|
53
|
-
resolve(true);
|
|
54
|
-
} catch (error) {
|
|
55
|
-
reject(error);
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
},
|
|
59
|
-
scheduler() {
|
|
60
|
-
return new Promise((resolve, reject) => {
|
|
61
|
-
try {
|
|
62
|
-
// check Scheduler file exists ?
|
|
63
|
-
if (_config_.scheduler) {
|
|
64
|
-
if (fs.existsSync(appRoot + "/src/Scheduler.js")) {
|
|
65
|
-
console.log("- [91mJob Skd[0m: [93mON[0m");
|
|
66
|
-
let skd = require(appRoot + "/src/Scheduler.js");
|
|
67
|
-
if(skd.init()) {
|
|
68
|
-
resolve(true);
|
|
69
|
-
}
|
|
70
|
-
} else {
|
|
71
|
-
console.log("- [91mJob Skd[0m: [90mOFF[0m");
|
|
72
|
-
resolve(true);
|
|
73
|
-
}
|
|
74
|
-
} else {
|
|
75
|
-
console.log("- [91mJob Skd[0m: [90mOFF[0m");
|
|
76
|
-
resolve(true);
|
|
77
|
-
}
|
|
78
|
-
} catch (error) {
|
|
79
|
-
reject(error);
|
|
80
|
-
}
|
|
81
|
-
});
|
|
82
|
-
},
|
|
83
|
-
// Authentication request
|
|
84
|
-
authentication() {
|
|
85
|
-
return new Promise((resolve, reject) => {
|
|
86
|
-
try {
|
|
87
|
-
var passport_config = null;
|
|
88
|
-
var jwt = null;
|
|
89
|
-
var passport = null;
|
|
90
|
-
var User = null;
|
|
91
|
-
var passport_config_file_exists = true;
|
|
92
|
-
var jwt_allow = false;
|
|
93
|
-
var jwt_db_allow = null;
|
|
94
|
-
// First promise
|
|
95
|
-
const checkPassport = new Promise((resolve, reject) => {
|
|
96
|
-
// require jwt & passport
|
|
97
|
-
if (fs.existsSync(appRoot + passport_config_file)) {
|
|
98
|
-
try {
|
|
99
|
-
// assign prepare data
|
|
100
|
-
passport_config = require(appRoot + passport_config_file);
|
|
101
|
-
global._passport_config_ = passport_config;
|
|
102
|
-
jwt = require('jsonwebtoken');
|
|
103
|
-
passport = require('passport');
|
|
104
|
-
User = require("../../../lib/src/user");
|
|
105
|
-
if (!passport_config.jwt_allow) {
|
|
106
|
-
// jwt is false
|
|
107
|
-
console.log("- [91mJWT[0m: [90mOFF[0m");
|
|
108
|
-
resolve(true);
|
|
109
|
-
} else {
|
|
110
|
-
/// jwt is true
|
|
111
|
-
jwt_allow = true;
|
|
112
|
-
console.log("- [91mJWT[0m: [93mON[0m");
|
|
113
|
-
// loop check db connect is true
|
|
114
|
-
fs.readFile("./app.config.js", "utf-8", (err, data) => {
|
|
115
|
-
if(err) {
|
|
116
|
-
throw err;
|
|
117
|
-
} else {
|
|
118
|
-
let mineConfDb = eval(data).database_config;
|
|
119
|
-
mineConfDb.filter((e) => {
|
|
120
|
-
if(e.name == passport_config.model.name) {
|
|
121
|
-
jwt_db_allow = e.is_connect;
|
|
122
|
-
resolve(jwt_db_allow);
|
|
123
|
-
}
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
} catch (error) {
|
|
129
|
-
reject(error);
|
|
130
|
-
}
|
|
131
|
-
} else {
|
|
132
|
-
passport_config_file_exists = false;
|
|
133
|
-
resolve(true);
|
|
134
|
-
}
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
// Second promise
|
|
138
|
-
const signJWT = new Promise((resolve, reject) => {
|
|
139
|
-
try {
|
|
140
|
-
checkPassport.then(passportChecked => {
|
|
141
|
-
if(passportChecked) {
|
|
142
|
-
if (passport_config_file_exists && jwt_allow && jwt_db_allow) {
|
|
143
|
-
// declare authentication endpoint name with publicPath
|
|
144
|
-
let auth_endpoint = (passport_config.auth_endpoint) ? (passport_config.auth_endpoint[ 0 ] === "/" ? passport_config.auth_endpoint : "/" + passport_config.auth_endpoint) : "/authentication";
|
|
145
|
-
// authentication endpoints
|
|
146
|
-
endpoint.post(auth_endpoint, Duplicater(), Limiter(), auth.credentialsGuard, (req, res, next) => {
|
|
147
|
-
passport.authenticate('local', { session: false }, (err, user, opt) => {
|
|
148
|
-
if (err) {
|
|
149
|
-
res.status(502).json({ code: 502, status: "BAD_GATEWAY", message: String(err) });
|
|
150
|
-
return;
|
|
151
|
-
}
|
|
152
|
-
if (user) {
|
|
153
|
-
const accessToken = jwt.sign(user, passport_config.secret, {
|
|
154
|
-
expiresIn: passport_config.token_expired
|
|
155
|
-
});
|
|
156
|
-
// check guard fields
|
|
157
|
-
if(passport_config.model.guard.guard_field.length) {
|
|
158
|
-
TwoFactor(user, req.body, passport_config.model.guard.guard_field, (err, twoFaUserRes) => {
|
|
159
|
-
if(err) {
|
|
160
|
-
res.status(twoFaUserRes.code).json(twoFaUserRes);
|
|
161
|
-
return;
|
|
162
|
-
} else {
|
|
163
|
-
if(twoFaUserRes.length) {
|
|
164
|
-
res.status(200).json({
|
|
165
|
-
code: 200,
|
|
166
|
-
status: "AUTHORIZED",
|
|
167
|
-
user: twoFaUserRes[0],
|
|
168
|
-
accessToken
|
|
169
|
-
});
|
|
170
|
-
} else {
|
|
171
|
-
res.status(401).json({ code: 401, status: "UNAUTHORIZED", message: "Unauthorized guard." });
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
});
|
|
175
|
-
} else {
|
|
176
|
-
res.status(200).json({
|
|
177
|
-
code: 200,
|
|
178
|
-
status: "AUTHORIZED",
|
|
179
|
-
user,
|
|
180
|
-
accessToken
|
|
181
|
-
});
|
|
182
|
-
}
|
|
183
|
-
} else if (opt) {
|
|
184
|
-
res.status(400).json({
|
|
185
|
-
code: 400,
|
|
186
|
-
status: 'BAD_REQUEST',
|
|
187
|
-
message: "Bad request.",
|
|
188
|
-
info: {
|
|
189
|
-
status: "BAD_ENTITY",
|
|
190
|
-
message: "Bad options Entity."
|
|
191
|
-
},
|
|
192
|
-
});
|
|
193
|
-
} else {
|
|
194
|
-
res.status(401).json({ code: 401, status: "UNAUTHORIZED", message: "Unauthorized user." });
|
|
195
|
-
}
|
|
196
|
-
})(req, res, next);
|
|
197
|
-
});
|
|
198
|
-
// refresh token endpoints
|
|
199
|
-
endpoint.post(auth_endpoint + '/refresh', Duplicater(), Limiter(), auth.credentials, (req, res) => {
|
|
200
|
-
const refreshToken = req.headers.authorization.split("Bearer ")[1] || null;
|
|
201
|
-
if (!refreshToken) {
|
|
202
|
-
return res.status(400).json({
|
|
203
|
-
code: 400,
|
|
204
|
-
status: "BAD_REQUEST",
|
|
205
|
-
message: "Bad request.",
|
|
206
|
-
info: {
|
|
207
|
-
status: "BAD_ENTITY",
|
|
208
|
-
message: "Refresh token is required.",
|
|
209
|
-
}
|
|
210
|
-
});
|
|
211
|
-
} else {
|
|
212
|
-
jwt.verify(refreshToken, passport_config.secret, (err, user) => {
|
|
213
|
-
if (err) {
|
|
214
|
-
return res.status(401).json({
|
|
215
|
-
code: 401,
|
|
216
|
-
status: "UNAUTHORIZED",
|
|
217
|
-
message: "Unauthorized user.",
|
|
218
|
-
info: {
|
|
219
|
-
status: "TOKEN_INVALID_ERR",
|
|
220
|
-
message: "Invalid refresh token.",
|
|
221
|
-
}
|
|
222
|
-
});
|
|
223
|
-
} else {
|
|
224
|
-
delete user.iat;
|
|
225
|
-
delete user.exp;
|
|
226
|
-
const newAccessToken = jwt.sign(user, passport_config.secret, {
|
|
227
|
-
expiresIn: passport_config.token_expired
|
|
228
|
-
});
|
|
229
|
-
res.status(200).json({
|
|
230
|
-
code: 200,
|
|
231
|
-
status: "TOKEN_REFRESHED",
|
|
232
|
-
user: user,
|
|
233
|
-
accessToken: newAccessToken,
|
|
234
|
-
});
|
|
235
|
-
}
|
|
236
|
-
});
|
|
237
|
-
}
|
|
238
|
-
});
|
|
239
|
-
// create auth data endpoints
|
|
240
|
-
endpoint.post(auth_endpoint + '/create', Duplicater(), Limiter(), auth.credentialsGuard, (req, res) => {
|
|
241
|
-
const promise = new Promise((resolve) => {
|
|
242
|
-
/**
|
|
243
|
-
*
|
|
244
|
-
* [Disabled IF Condition AUTO pass to ELSE] add 0 (zero) in IF condition for closed app_key in headers for create new auth.
|
|
245
|
-
*
|
|
246
|
-
*/
|
|
247
|
-
if (0 && passport_config.app_key_allow) {
|
|
248
|
-
if (req.headers.app_key) {
|
|
249
|
-
if (_config_.main_config.app_key == req.headers.app_key) {
|
|
250
|
-
resolve(true);
|
|
251
|
-
} else {
|
|
252
|
-
res.status(400).json({
|
|
253
|
-
code: 400,
|
|
254
|
-
status: 'BAD_REQUEST',
|
|
255
|
-
message: "Bad request.",
|
|
256
|
-
info: {
|
|
257
|
-
status: "BAD_VALUE",
|
|
258
|
-
message: "Bad with wrong key."
|
|
259
|
-
},
|
|
260
|
-
});
|
|
261
|
-
}
|
|
262
|
-
} else {
|
|
263
|
-
res.status(400).json({
|
|
264
|
-
code: 400,
|
|
265
|
-
status: 'BAD_REQUEST',
|
|
266
|
-
message: "Bad request.",
|
|
267
|
-
info: {
|
|
268
|
-
status: "BAD_ENTITY",
|
|
269
|
-
message: "Bad application key Entity."
|
|
270
|
-
},
|
|
271
|
-
});
|
|
272
|
-
}
|
|
273
|
-
} else {
|
|
274
|
-
resolve(true);
|
|
275
|
-
}
|
|
276
|
-
});
|
|
277
|
-
// store data
|
|
278
|
-
Promise.all([promise])
|
|
279
|
-
.then(() => {
|
|
280
|
-
User.Store(req.body, (err, result) => {
|
|
281
|
-
if (err) {
|
|
282
|
-
res.status(501).json({ code: 501, status: "CREATE_FAILED", error: err });
|
|
283
|
-
} else {
|
|
284
|
-
res.status(201).json({ code: 201, status: "CREATE_SUCCESS", result });
|
|
285
|
-
}
|
|
286
|
-
});
|
|
287
|
-
})
|
|
288
|
-
.catch(err => {
|
|
289
|
-
res.status(501).json({ code: 501, status: "NOT_IMPLIMENTED", error: err
|
|
290
|
-
});
|
|
291
|
-
});
|
|
292
|
-
});
|
|
293
|
-
// patch auth data endpoints
|
|
294
|
-
endpoint.patch(auth_endpoint + '/update/:id', Duplicater(), Limiter(), auth.credentials, (req, res) => {
|
|
295
|
-
const promise = new Promise((resolve) => {
|
|
296
|
-
if (passport_config.app_key_allow) {
|
|
297
|
-
if (req.headers.app_key) {
|
|
298
|
-
if (_config_.main_config.app_key == req.headers.app_key) {
|
|
299
|
-
resolve(true);
|
|
300
|
-
} else {
|
|
301
|
-
res.status(400).json({
|
|
302
|
-
code: 400,
|
|
303
|
-
status: 'BAD_REQUEST',
|
|
304
|
-
message: "Bad request.",
|
|
305
|
-
info: {
|
|
306
|
-
status: "BAD_VALUE",
|
|
307
|
-
message: "Bad with wrong key."
|
|
308
|
-
},
|
|
309
|
-
});
|
|
310
|
-
}
|
|
311
|
-
} else {
|
|
312
|
-
res.status(400).json({
|
|
313
|
-
code: 400,
|
|
314
|
-
status: 'BAD_REQUEST',
|
|
315
|
-
message: "Bad request.",
|
|
316
|
-
info: {
|
|
317
|
-
status: "BAD_ENTITY",
|
|
318
|
-
message: "Bad application key Entity."
|
|
319
|
-
},
|
|
320
|
-
});
|
|
321
|
-
}
|
|
322
|
-
} else {
|
|
323
|
-
resolve(true);
|
|
324
|
-
}
|
|
325
|
-
});
|
|
326
|
-
// update data
|
|
327
|
-
Promise.all([promise])
|
|
328
|
-
.then(() => {
|
|
329
|
-
// require some fields with body params
|
|
330
|
-
User.Update(req.body, req.params.id, (err, result) => {
|
|
331
|
-
if (err) {
|
|
332
|
-
res.status(501).json({ code: 501, status: "UPDATE_FAILED", error: err });
|
|
333
|
-
} else {
|
|
334
|
-
res.status(200).json({ code: 200, status: "UPDATE_SUCCESS", result });
|
|
335
|
-
}
|
|
336
|
-
});
|
|
337
|
-
})
|
|
338
|
-
.catch(err => {
|
|
339
|
-
res.status(501).json({ code: 501, status: "NOT_IMPLIMENTED", error: err
|
|
340
|
-
});
|
|
341
|
-
});
|
|
342
|
-
});
|
|
343
|
-
/**
|
|
344
|
-
* Google Strategy
|
|
345
|
-
*
|
|
346
|
-
*/
|
|
347
|
-
if (passport_config.strategy.google.allow) {
|
|
348
|
-
endpoint.get(auth_endpoint + '/google', Limiter(), passport.authenticate('google', {
|
|
349
|
-
scope: [
|
|
350
|
-
'https://www.googleapis.com/auth/userinfo.email',
|
|
351
|
-
'https://www.googleapis.com/auth/plus.login'
|
|
352
|
-
]
|
|
353
|
-
}));
|
|
354
|
-
// google auth callback
|
|
355
|
-
const googleCallback = (passport_config.strategy.google.callbackURL) ? (passport_config.strategy.google.callbackURL[ 0 ] === "/" ? passport_config.strategy.google.callbackURL : "/" + passport_config.strategy.google.callbackURL) : "/google/callback";
|
|
356
|
-
endpoint.get(auth_endpoint + googleCallback, Limiter(), passport.authenticate('google', { failureRedirect: passport_config.strategy.google.failureRedirect, failureMessage: true }), (req, res) => {
|
|
357
|
-
if (typeof req.user.user !== 'undefined') {
|
|
358
|
-
// declare user for sign JWT
|
|
359
|
-
let user = JSON.parse(JSON.stringify(req.user.user));
|
|
360
|
-
const accessToken = jwt.sign(user, passport_config.secret, {
|
|
361
|
-
expiresIn: passport_config.token_expired
|
|
362
|
-
});
|
|
363
|
-
// response JWT
|
|
364
|
-
res.status(200).json({
|
|
365
|
-
code: 200,
|
|
366
|
-
status: "AUTHORIZED",
|
|
367
|
-
user: req.user,
|
|
368
|
-
accessToken
|
|
369
|
-
});
|
|
370
|
-
} else {
|
|
371
|
-
let condUser = {};
|
|
372
|
-
condUser[ (passport_config.strategy.google.local_profile_fields.google_id) ? passport_config.strategy.google.local_profile_fields.google_id : "google_id" ] = req.user.google.id;
|
|
373
|
-
User.FindOne([], condUser, (err, result) => {
|
|
374
|
-
if (err) {
|
|
375
|
-
res.status(500).json({
|
|
376
|
-
code: 500,
|
|
377
|
-
status: "INTERNAL_SERVER_ERR",
|
|
378
|
-
error: err
|
|
379
|
-
});
|
|
380
|
-
} else {
|
|
381
|
-
let user = JSON.parse(JSON.stringify(result[ 0 ]));
|
|
382
|
-
const accessToken = jwt.sign(user, passport_config.secret, {
|
|
383
|
-
expiresIn: passport_config.token_expired
|
|
384
|
-
});
|
|
385
|
-
// response JWT
|
|
386
|
-
res.status(201).json({
|
|
387
|
-
code: 201,
|
|
388
|
-
status: "AUTHORIZED",
|
|
389
|
-
user: {
|
|
390
|
-
google: req.user.google,
|
|
391
|
-
user
|
|
392
|
-
},
|
|
393
|
-
accessToken
|
|
394
|
-
});
|
|
395
|
-
}
|
|
396
|
-
});
|
|
397
|
-
}
|
|
398
|
-
});
|
|
399
|
-
}
|
|
400
|
-
/**
|
|
401
|
-
* Facebook strategy
|
|
402
|
-
*
|
|
403
|
-
*/
|
|
404
|
-
if (passport_config.strategy.facebook.allow) {
|
|
405
|
-
endpoint.get(auth_endpoint + '/facebook', Limiter(), passport.authenticate('facebook', { scope: [ 'email', 'public_profile' ] }));
|
|
406
|
-
// facebook callback
|
|
407
|
-
const facebookCallback = (passport_config.strategy.facebook.callbackURL) ? (passport_config.strategy.facebook.callbackURL[ 0 ] === "/" ? passport_config.strategy.facebook.callbackURL : "/" + passport_config.strategy.facebook.callbackURL) : "/facebook/callback";
|
|
408
|
-
endpoint.get(auth_endpoint + facebookCallback, Limiter(), passport.authenticate('facebook', { failureRedirect: passport_config.strategy.facebook.failureRedirect, failureMessage: true }), (req, res) => {
|
|
409
|
-
if (typeof req.user.user !== 'undefined') {
|
|
410
|
-
// declare user for sign JWT
|
|
411
|
-
let user = JSON.parse(JSON.stringify(req.user.user));
|
|
412
|
-
const accessToken = jwt.sign(user, passport_config.secret, {
|
|
413
|
-
expiresIn: passport_config.token_expired
|
|
414
|
-
});
|
|
415
|
-
// response JWT
|
|
416
|
-
res.status(200).json({
|
|
417
|
-
code: 200,
|
|
418
|
-
status: "AUTHORIZED",
|
|
419
|
-
user: req.user,
|
|
420
|
-
accessToken
|
|
421
|
-
});
|
|
422
|
-
} else {
|
|
423
|
-
let condUser = {};
|
|
424
|
-
condUser[ (passport_config.strategy.facebook.local_profile_fields.facebook_id) ? passport_config.strategy.facebook.local_profile_fields.facebook_id : "facebook_id" ] = req.user.facebook.id;
|
|
425
|
-
User.FindOne([], condUser, (err, result) => {
|
|
426
|
-
if (err) {
|
|
427
|
-
res.status(500).json({
|
|
428
|
-
code: 500,
|
|
429
|
-
status: "INTERNAL_SERVER_ERR",
|
|
430
|
-
error: err
|
|
431
|
-
});
|
|
432
|
-
} else {
|
|
433
|
-
let user = JSON.parse(JSON.stringify(result[ 0 ]));
|
|
434
|
-
const accessToken = jwt.sign(user, passport_config.secret, {
|
|
435
|
-
expiresIn: passport_config.token_expired
|
|
436
|
-
});
|
|
437
|
-
// response JWT
|
|
438
|
-
res.status(201).json({
|
|
439
|
-
code: 201,
|
|
440
|
-
status: "AUTHORIZED",
|
|
441
|
-
user: {
|
|
442
|
-
facebook: req.user.facebook,
|
|
443
|
-
user
|
|
444
|
-
},
|
|
445
|
-
accessToken
|
|
446
|
-
});
|
|
447
|
-
}
|
|
448
|
-
});
|
|
449
|
-
}
|
|
450
|
-
});
|
|
451
|
-
}
|
|
452
|
-
// Perfectly and resolve it.
|
|
453
|
-
resolve(true);
|
|
454
|
-
} else {
|
|
455
|
-
// Perfectly and resolve it.
|
|
456
|
-
resolve(false);
|
|
457
|
-
}
|
|
458
|
-
} else {
|
|
459
|
-
// checkPassport is Catch.
|
|
460
|
-
resolve(false);
|
|
461
|
-
}
|
|
462
|
-
});
|
|
463
|
-
} catch (error) {
|
|
464
|
-
reject(error);
|
|
465
|
-
}
|
|
466
|
-
});
|
|
467
|
-
// Promise all
|
|
468
|
-
Promise.all([ signJWT ]).then(async (final) => {
|
|
469
|
-
// [signJWT=init auth endpoint]
|
|
470
|
-
if(final[0]) {
|
|
471
|
-
resolve(true);
|
|
472
|
-
} else {
|
|
473
|
-
// resolve false to JWT turn OFF
|
|
474
|
-
resolve(false);
|
|
475
|
-
}
|
|
476
|
-
}).catch((err) => reject(err));
|
|
477
|
-
} catch (error) {
|
|
478
|
-
reject(error);
|
|
479
|
-
}
|
|
480
|
-
});
|
|
481
|
-
}
|
|
1
|
+
const package = require(appRoot + '/package.json');
|
|
2
|
+
const fs = require("fs");
|
|
3
|
+
const passport_config_file = "/passport.config.js";
|
|
4
|
+
const auth = require("../auth/Credentials");
|
|
5
|
+
const { TwoFactor } = require("../helpers/2fa");
|
|
6
|
+
const { Limiter, Duplicater } = require("../middleware");
|
|
7
|
+
|
|
8
|
+
module.exports = {
|
|
9
|
+
expressStart() {
|
|
10
|
+
return new Promise((resolve, reject) => {
|
|
11
|
+
try {
|
|
12
|
+
// Create express server
|
|
13
|
+
const ExpressServer = _app_.listen(_config_.main_config.app_port, async () => {
|
|
14
|
+
await console.log('[102m[90m Passed [0m[0m Service is started at:');
|
|
15
|
+
await console.log('- [91mLocal[0m: [36mhttp://' + _config_.main_config.app_host + ':' + ExpressServer.address().port + '[0m');
|
|
16
|
+
await console.log('- [91mNetwork[0m: [36m' + _config_.main_config.client_host + '[0m');
|
|
17
|
+
await new Promise((resolve) => resolve(this.authentication()));
|
|
18
|
+
await new Promise((resolve) => {
|
|
19
|
+
if(this.scheduler()) {
|
|
20
|
+
resolve(true);
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
await new Promise((resolve) => resolve(this.badRequest()));
|
|
24
|
+
await resolve(ExpressServer);
|
|
25
|
+
});
|
|
26
|
+
} catch (error) {
|
|
27
|
+
reject(error);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
},
|
|
31
|
+
// Bad request
|
|
32
|
+
badRequest() {
|
|
33
|
+
return new Promise((resolve, reject) => {
|
|
34
|
+
try {
|
|
35
|
+
// base get request
|
|
36
|
+
endpoint.get('/', (req, res) => {
|
|
37
|
+
res.status(200).json({
|
|
38
|
+
code: 200,
|
|
39
|
+
status: "SUCCESS",
|
|
40
|
+
message: `Welcome to ${package.name} (version ${package.version})`
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
// request 404 not found
|
|
44
|
+
endpoint.use((req, res, next) => {
|
|
45
|
+
res.status(404).json({
|
|
46
|
+
code: 404,
|
|
47
|
+
status: "404_NOT_FOUND",
|
|
48
|
+
message: "The Endpoint not found!.",
|
|
49
|
+
});
|
|
50
|
+
next();
|
|
51
|
+
});
|
|
52
|
+
// resolve it.
|
|
53
|
+
resolve(true);
|
|
54
|
+
} catch (error) {
|
|
55
|
+
reject(error);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
},
|
|
59
|
+
scheduler() {
|
|
60
|
+
return new Promise((resolve, reject) => {
|
|
61
|
+
try {
|
|
62
|
+
// check Scheduler file exists ?
|
|
63
|
+
if (_config_.scheduler) {
|
|
64
|
+
if (fs.existsSync(appRoot + "/src/Scheduler.js")) {
|
|
65
|
+
console.log("- [91mJob Skd[0m: [93mON[0m");
|
|
66
|
+
let skd = require(appRoot + "/src/Scheduler.js");
|
|
67
|
+
if(skd.init()) {
|
|
68
|
+
resolve(true);
|
|
69
|
+
}
|
|
70
|
+
} else {
|
|
71
|
+
console.log("- [91mJob Skd[0m: [90mOFF[0m");
|
|
72
|
+
resolve(true);
|
|
73
|
+
}
|
|
74
|
+
} else {
|
|
75
|
+
console.log("- [91mJob Skd[0m: [90mOFF[0m");
|
|
76
|
+
resolve(true);
|
|
77
|
+
}
|
|
78
|
+
} catch (error) {
|
|
79
|
+
reject(error);
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
},
|
|
83
|
+
// Authentication request
|
|
84
|
+
authentication() {
|
|
85
|
+
return new Promise((resolve, reject) => {
|
|
86
|
+
try {
|
|
87
|
+
var passport_config = null;
|
|
88
|
+
var jwt = null;
|
|
89
|
+
var passport = null;
|
|
90
|
+
var User = null;
|
|
91
|
+
var passport_config_file_exists = true;
|
|
92
|
+
var jwt_allow = false;
|
|
93
|
+
var jwt_db_allow = null;
|
|
94
|
+
// First promise
|
|
95
|
+
const checkPassport = new Promise((resolve, reject) => {
|
|
96
|
+
// require jwt & passport
|
|
97
|
+
if (fs.existsSync(appRoot + passport_config_file)) {
|
|
98
|
+
try {
|
|
99
|
+
// assign prepare data
|
|
100
|
+
passport_config = require(appRoot + passport_config_file);
|
|
101
|
+
global._passport_config_ = passport_config;
|
|
102
|
+
jwt = require('jsonwebtoken');
|
|
103
|
+
passport = require('passport');
|
|
104
|
+
User = require("../../../lib/src/user");
|
|
105
|
+
if (!passport_config.jwt_allow) {
|
|
106
|
+
// jwt is false
|
|
107
|
+
console.log("- [91mJWT[0m: [90mOFF[0m");
|
|
108
|
+
resolve(true);
|
|
109
|
+
} else {
|
|
110
|
+
/// jwt is true
|
|
111
|
+
jwt_allow = true;
|
|
112
|
+
console.log("- [91mJWT[0m: [93mON[0m");
|
|
113
|
+
// loop check db connect is true
|
|
114
|
+
fs.readFile("./app.config.js", "utf-8", (err, data) => {
|
|
115
|
+
if(err) {
|
|
116
|
+
throw err;
|
|
117
|
+
} else {
|
|
118
|
+
let mineConfDb = eval(data).database_config;
|
|
119
|
+
mineConfDb.filter((e) => {
|
|
120
|
+
if(e.name == passport_config.model.name) {
|
|
121
|
+
jwt_db_allow = e.is_connect;
|
|
122
|
+
resolve(jwt_db_allow);
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
} catch (error) {
|
|
129
|
+
reject(error);
|
|
130
|
+
}
|
|
131
|
+
} else {
|
|
132
|
+
passport_config_file_exists = false;
|
|
133
|
+
resolve(true);
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
// Second promise
|
|
138
|
+
const signJWT = new Promise((resolve, reject) => {
|
|
139
|
+
try {
|
|
140
|
+
checkPassport.then(passportChecked => {
|
|
141
|
+
if(passportChecked) {
|
|
142
|
+
if (passport_config_file_exists && jwt_allow && jwt_db_allow) {
|
|
143
|
+
// declare authentication endpoint name with publicPath
|
|
144
|
+
let auth_endpoint = (passport_config.auth_endpoint) ? (passport_config.auth_endpoint[ 0 ] === "/" ? passport_config.auth_endpoint : "/" + passport_config.auth_endpoint) : "/authentication";
|
|
145
|
+
// authentication endpoints
|
|
146
|
+
endpoint.post(auth_endpoint, Duplicater(), Limiter(), auth.credentialsGuard, (req, res, next) => {
|
|
147
|
+
passport.authenticate('local', { session: false }, (err, user, opt) => {
|
|
148
|
+
if (err) {
|
|
149
|
+
res.status(502).json({ code: 502, status: "BAD_GATEWAY", message: String(err) });
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
if (user) {
|
|
153
|
+
const accessToken = jwt.sign(user, passport_config.secret, {
|
|
154
|
+
expiresIn: passport_config.token_expired
|
|
155
|
+
});
|
|
156
|
+
// check guard fields
|
|
157
|
+
if(passport_config.model.guard.guard_field.length) {
|
|
158
|
+
TwoFactor(user, req.body, passport_config.model.guard.guard_field, (err, twoFaUserRes) => {
|
|
159
|
+
if(err) {
|
|
160
|
+
res.status(twoFaUserRes.code).json(twoFaUserRes);
|
|
161
|
+
return;
|
|
162
|
+
} else {
|
|
163
|
+
if(twoFaUserRes.length) {
|
|
164
|
+
res.status(200).json({
|
|
165
|
+
code: 200,
|
|
166
|
+
status: "AUTHORIZED",
|
|
167
|
+
user: twoFaUserRes[0],
|
|
168
|
+
accessToken
|
|
169
|
+
});
|
|
170
|
+
} else {
|
|
171
|
+
res.status(401).json({ code: 401, status: "UNAUTHORIZED", message: "Unauthorized guard." });
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
} else {
|
|
176
|
+
res.status(200).json({
|
|
177
|
+
code: 200,
|
|
178
|
+
status: "AUTHORIZED",
|
|
179
|
+
user,
|
|
180
|
+
accessToken
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
} else if (opt) {
|
|
184
|
+
res.status(400).json({
|
|
185
|
+
code: 400,
|
|
186
|
+
status: 'BAD_REQUEST',
|
|
187
|
+
message: "Bad request.",
|
|
188
|
+
info: {
|
|
189
|
+
status: "BAD_ENTITY",
|
|
190
|
+
message: "Bad options Entity."
|
|
191
|
+
},
|
|
192
|
+
});
|
|
193
|
+
} else {
|
|
194
|
+
res.status(401).json({ code: 401, status: "UNAUTHORIZED", message: "Unauthorized user." });
|
|
195
|
+
}
|
|
196
|
+
})(req, res, next);
|
|
197
|
+
});
|
|
198
|
+
// refresh token endpoints
|
|
199
|
+
endpoint.post(auth_endpoint + '/refresh', Duplicater(), Limiter(), auth.credentials, (req, res) => {
|
|
200
|
+
const refreshToken = req.headers.authorization.split("Bearer ")[1] || null;
|
|
201
|
+
if (!refreshToken) {
|
|
202
|
+
return res.status(400).json({
|
|
203
|
+
code: 400,
|
|
204
|
+
status: "BAD_REQUEST",
|
|
205
|
+
message: "Bad request.",
|
|
206
|
+
info: {
|
|
207
|
+
status: "BAD_ENTITY",
|
|
208
|
+
message: "Refresh token is required.",
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
} else {
|
|
212
|
+
jwt.verify(refreshToken, passport_config.secret, (err, user) => {
|
|
213
|
+
if (err) {
|
|
214
|
+
return res.status(401).json({
|
|
215
|
+
code: 401,
|
|
216
|
+
status: "UNAUTHORIZED",
|
|
217
|
+
message: "Unauthorized user.",
|
|
218
|
+
info: {
|
|
219
|
+
status: "TOKEN_INVALID_ERR",
|
|
220
|
+
message: "Invalid refresh token.",
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
} else {
|
|
224
|
+
delete user.iat;
|
|
225
|
+
delete user.exp;
|
|
226
|
+
const newAccessToken = jwt.sign(user, passport_config.secret, {
|
|
227
|
+
expiresIn: passport_config.token_expired
|
|
228
|
+
});
|
|
229
|
+
res.status(200).json({
|
|
230
|
+
code: 200,
|
|
231
|
+
status: "TOKEN_REFRESHED",
|
|
232
|
+
user: user,
|
|
233
|
+
accessToken: newAccessToken,
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
// create auth data endpoints
|
|
240
|
+
endpoint.post(auth_endpoint + '/create', Duplicater(), Limiter(), auth.credentialsGuard, (req, res) => {
|
|
241
|
+
const promise = new Promise((resolve) => {
|
|
242
|
+
/**
|
|
243
|
+
*
|
|
244
|
+
* [Disabled IF Condition AUTO pass to ELSE] add 0 (zero) in IF condition for closed app_key in headers for create new auth.
|
|
245
|
+
*
|
|
246
|
+
*/
|
|
247
|
+
if (0 && passport_config.app_key_allow) {
|
|
248
|
+
if (req.headers.app_key) {
|
|
249
|
+
if (_config_.main_config.app_key == req.headers.app_key) {
|
|
250
|
+
resolve(true);
|
|
251
|
+
} else {
|
|
252
|
+
res.status(400).json({
|
|
253
|
+
code: 400,
|
|
254
|
+
status: 'BAD_REQUEST',
|
|
255
|
+
message: "Bad request.",
|
|
256
|
+
info: {
|
|
257
|
+
status: "BAD_VALUE",
|
|
258
|
+
message: "Bad with wrong key."
|
|
259
|
+
},
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
} else {
|
|
263
|
+
res.status(400).json({
|
|
264
|
+
code: 400,
|
|
265
|
+
status: 'BAD_REQUEST',
|
|
266
|
+
message: "Bad request.",
|
|
267
|
+
info: {
|
|
268
|
+
status: "BAD_ENTITY",
|
|
269
|
+
message: "Bad application key Entity."
|
|
270
|
+
},
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
} else {
|
|
274
|
+
resolve(true);
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
// store data
|
|
278
|
+
Promise.all([promise])
|
|
279
|
+
.then(() => {
|
|
280
|
+
User.Store(req.body, (err, result) => {
|
|
281
|
+
if (err) {
|
|
282
|
+
res.status(501).json({ code: 501, status: "CREATE_FAILED", error: err });
|
|
283
|
+
} else {
|
|
284
|
+
res.status(201).json({ code: 201, status: "CREATE_SUCCESS", result });
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
})
|
|
288
|
+
.catch(err => {
|
|
289
|
+
res.status(501).json({ code: 501, status: "NOT_IMPLIMENTED", error: err
|
|
290
|
+
});
|
|
291
|
+
});
|
|
292
|
+
});
|
|
293
|
+
// patch auth data endpoints
|
|
294
|
+
endpoint.patch(auth_endpoint + '/update/:id', Duplicater(), Limiter(), auth.credentials, (req, res) => {
|
|
295
|
+
const promise = new Promise((resolve) => {
|
|
296
|
+
if (passport_config.app_key_allow) {
|
|
297
|
+
if (req.headers.app_key) {
|
|
298
|
+
if (_config_.main_config.app_key == req.headers.app_key) {
|
|
299
|
+
resolve(true);
|
|
300
|
+
} else {
|
|
301
|
+
res.status(400).json({
|
|
302
|
+
code: 400,
|
|
303
|
+
status: 'BAD_REQUEST',
|
|
304
|
+
message: "Bad request.",
|
|
305
|
+
info: {
|
|
306
|
+
status: "BAD_VALUE",
|
|
307
|
+
message: "Bad with wrong key."
|
|
308
|
+
},
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
} else {
|
|
312
|
+
res.status(400).json({
|
|
313
|
+
code: 400,
|
|
314
|
+
status: 'BAD_REQUEST',
|
|
315
|
+
message: "Bad request.",
|
|
316
|
+
info: {
|
|
317
|
+
status: "BAD_ENTITY",
|
|
318
|
+
message: "Bad application key Entity."
|
|
319
|
+
},
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
} else {
|
|
323
|
+
resolve(true);
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
// update data
|
|
327
|
+
Promise.all([promise])
|
|
328
|
+
.then(() => {
|
|
329
|
+
// require some fields with body params
|
|
330
|
+
User.Update(req.body, req.params.id, (err, result) => {
|
|
331
|
+
if (err) {
|
|
332
|
+
res.status(501).json({ code: 501, status: "UPDATE_FAILED", error: err });
|
|
333
|
+
} else {
|
|
334
|
+
res.status(200).json({ code: 200, status: "UPDATE_SUCCESS", result });
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
})
|
|
338
|
+
.catch(err => {
|
|
339
|
+
res.status(501).json({ code: 501, status: "NOT_IMPLIMENTED", error: err
|
|
340
|
+
});
|
|
341
|
+
});
|
|
342
|
+
});
|
|
343
|
+
/**
|
|
344
|
+
* Google Strategy
|
|
345
|
+
*
|
|
346
|
+
*/
|
|
347
|
+
if (passport_config.strategy.google.allow) {
|
|
348
|
+
endpoint.get(auth_endpoint + '/google', Limiter(), passport.authenticate('google', {
|
|
349
|
+
scope: [
|
|
350
|
+
'https://www.googleapis.com/auth/userinfo.email',
|
|
351
|
+
'https://www.googleapis.com/auth/plus.login'
|
|
352
|
+
]
|
|
353
|
+
}));
|
|
354
|
+
// google auth callback
|
|
355
|
+
const googleCallback = (passport_config.strategy.google.callbackURL) ? (passport_config.strategy.google.callbackURL[ 0 ] === "/" ? passport_config.strategy.google.callbackURL : "/" + passport_config.strategy.google.callbackURL) : "/google/callback";
|
|
356
|
+
endpoint.get(auth_endpoint + googleCallback, Limiter(), passport.authenticate('google', { failureRedirect: passport_config.strategy.google.failureRedirect, failureMessage: true }), (req, res) => {
|
|
357
|
+
if (typeof req.user.user !== 'undefined') {
|
|
358
|
+
// declare user for sign JWT
|
|
359
|
+
let user = JSON.parse(JSON.stringify(req.user.user));
|
|
360
|
+
const accessToken = jwt.sign(user, passport_config.secret, {
|
|
361
|
+
expiresIn: passport_config.token_expired
|
|
362
|
+
});
|
|
363
|
+
// response JWT
|
|
364
|
+
res.status(200).json({
|
|
365
|
+
code: 200,
|
|
366
|
+
status: "AUTHORIZED",
|
|
367
|
+
user: req.user,
|
|
368
|
+
accessToken
|
|
369
|
+
});
|
|
370
|
+
} else {
|
|
371
|
+
let condUser = {};
|
|
372
|
+
condUser[ (passport_config.strategy.google.local_profile_fields.google_id) ? passport_config.strategy.google.local_profile_fields.google_id : "google_id" ] = req.user.google.id;
|
|
373
|
+
User.FindOne([], condUser, (err, result) => {
|
|
374
|
+
if (err) {
|
|
375
|
+
res.status(500).json({
|
|
376
|
+
code: 500,
|
|
377
|
+
status: "INTERNAL_SERVER_ERR",
|
|
378
|
+
error: err
|
|
379
|
+
});
|
|
380
|
+
} else {
|
|
381
|
+
let user = JSON.parse(JSON.stringify(result[ 0 ]));
|
|
382
|
+
const accessToken = jwt.sign(user, passport_config.secret, {
|
|
383
|
+
expiresIn: passport_config.token_expired
|
|
384
|
+
});
|
|
385
|
+
// response JWT
|
|
386
|
+
res.status(201).json({
|
|
387
|
+
code: 201,
|
|
388
|
+
status: "AUTHORIZED",
|
|
389
|
+
user: {
|
|
390
|
+
google: req.user.google,
|
|
391
|
+
user
|
|
392
|
+
},
|
|
393
|
+
accessToken
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
/**
|
|
401
|
+
* Facebook strategy
|
|
402
|
+
*
|
|
403
|
+
*/
|
|
404
|
+
if (passport_config.strategy.facebook.allow) {
|
|
405
|
+
endpoint.get(auth_endpoint + '/facebook', Limiter(), passport.authenticate('facebook', { scope: [ 'email', 'public_profile' ] }));
|
|
406
|
+
// facebook callback
|
|
407
|
+
const facebookCallback = (passport_config.strategy.facebook.callbackURL) ? (passport_config.strategy.facebook.callbackURL[ 0 ] === "/" ? passport_config.strategy.facebook.callbackURL : "/" + passport_config.strategy.facebook.callbackURL) : "/facebook/callback";
|
|
408
|
+
endpoint.get(auth_endpoint + facebookCallback, Limiter(), passport.authenticate('facebook', { failureRedirect: passport_config.strategy.facebook.failureRedirect, failureMessage: true }), (req, res) => {
|
|
409
|
+
if (typeof req.user.user !== 'undefined') {
|
|
410
|
+
// declare user for sign JWT
|
|
411
|
+
let user = JSON.parse(JSON.stringify(req.user.user));
|
|
412
|
+
const accessToken = jwt.sign(user, passport_config.secret, {
|
|
413
|
+
expiresIn: passport_config.token_expired
|
|
414
|
+
});
|
|
415
|
+
// response JWT
|
|
416
|
+
res.status(200).json({
|
|
417
|
+
code: 200,
|
|
418
|
+
status: "AUTHORIZED",
|
|
419
|
+
user: req.user,
|
|
420
|
+
accessToken
|
|
421
|
+
});
|
|
422
|
+
} else {
|
|
423
|
+
let condUser = {};
|
|
424
|
+
condUser[ (passport_config.strategy.facebook.local_profile_fields.facebook_id) ? passport_config.strategy.facebook.local_profile_fields.facebook_id : "facebook_id" ] = req.user.facebook.id;
|
|
425
|
+
User.FindOne([], condUser, (err, result) => {
|
|
426
|
+
if (err) {
|
|
427
|
+
res.status(500).json({
|
|
428
|
+
code: 500,
|
|
429
|
+
status: "INTERNAL_SERVER_ERR",
|
|
430
|
+
error: err
|
|
431
|
+
});
|
|
432
|
+
} else {
|
|
433
|
+
let user = JSON.parse(JSON.stringify(result[ 0 ]));
|
|
434
|
+
const accessToken = jwt.sign(user, passport_config.secret, {
|
|
435
|
+
expiresIn: passport_config.token_expired
|
|
436
|
+
});
|
|
437
|
+
// response JWT
|
|
438
|
+
res.status(201).json({
|
|
439
|
+
code: 201,
|
|
440
|
+
status: "AUTHORIZED",
|
|
441
|
+
user: {
|
|
442
|
+
facebook: req.user.facebook,
|
|
443
|
+
user
|
|
444
|
+
},
|
|
445
|
+
accessToken
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
// Perfectly and resolve it.
|
|
453
|
+
resolve(true);
|
|
454
|
+
} else {
|
|
455
|
+
// Perfectly and resolve it.
|
|
456
|
+
resolve(false);
|
|
457
|
+
}
|
|
458
|
+
} else {
|
|
459
|
+
// checkPassport is Catch.
|
|
460
|
+
resolve(false);
|
|
461
|
+
}
|
|
462
|
+
});
|
|
463
|
+
} catch (error) {
|
|
464
|
+
reject(error);
|
|
465
|
+
}
|
|
466
|
+
});
|
|
467
|
+
// Promise all
|
|
468
|
+
Promise.all([ signJWT ]).then(async (final) => {
|
|
469
|
+
// [signJWT=init auth endpoint]
|
|
470
|
+
if(final[0]) {
|
|
471
|
+
resolve(true);
|
|
472
|
+
} else {
|
|
473
|
+
// resolve false to JWT turn OFF
|
|
474
|
+
resolve(false);
|
|
475
|
+
}
|
|
476
|
+
}).catch((err) => reject(err));
|
|
477
|
+
} catch (error) {
|
|
478
|
+
reject(error);
|
|
479
|
+
}
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
482
|
}
|