free-be-account 0.0.8 → 0.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -152,6 +152,38 @@ const verify_api_permission = async (app, mdl, user, api_path) => {
152
152
  return true; // TODO: secure enough??
153
153
  }
154
154
 
155
+
156
+ async function clear_cache_token_by_user_id (app, id) {
157
+ if (!id) return;
158
+
159
+ const cacheKeys = await app.cache.keys();
160
+ if (cacheKeys && cacheKeys.length) {
161
+ for (let i = 0; i < cacheKeys.length; i += 1) {
162
+ const k = cacheKeys[i];
163
+
164
+ let value = await app.cache.get(k);
165
+ if (value && value.userId && value.userId === id)
166
+ await app.cache.del(k);
167
+ }
168
+ }
169
+ }
170
+
171
+ async function generate_new_access_token_pwd (app, userId, oldToken, keepToken = '', isWx = false) {
172
+ let uuid = keepToken || uuidv1();
173
+
174
+ // remove the old one from cache
175
+ app.cache.del(oldToken);
176
+ // cache.del(oldToken);
177
+ await clear_cache_token_by_user_id(app, userId);
178
+
179
+ // add the new one to the cache
180
+
181
+ app.cache.put(uuid, { userId: userId, type: isWx ? 'wx' : 'pwd' }, app.config['cacheTimeout']);
182
+ // cache.put(uuid, { userId: userId, type: 'pwd' }, app.config['cacheTimeout']);
183
+
184
+ return uuid;
185
+ }
186
+
155
187
  module.exports = (app) => ({
156
188
  sms: sms(app),
157
189
  AccountAuditStatus,
@@ -545,6 +577,8 @@ module.exports = (app) => ({
545
577
  ks.forEach(k => app.cache.del(k))
546
578
  });
547
579
  },
580
+ clear_cache_token_by_user_id,
581
+ generate_new_access_token_pwd,
548
582
  hooks: {
549
583
  onBegin: (app) => {
550
584
  app.use(passport.initialize());
@@ -833,7 +867,7 @@ module.exports = (app) => ({
833
867
  app.models['account'].create({
834
868
  Enabled: true,
835
869
  Deleted: false,
836
- Permission: app.config.passport.accountDefaultPermissions || {},
870
+ Permission: app.config.account.accountDefaultPermissions || {},
837
871
  Profile: profile,
838
872
  }).then((nuser) => {
839
873
  if (nuser) {
@@ -985,37 +1019,6 @@ module.exports = (app) => ({
985
1019
  return next();
986
1020
  });
987
1021
 
988
- async function clear_cache_token_by_user_id (id) {
989
- if (!id) return;
990
-
991
- const cacheKeys = await app.cache.keys();
992
- if (cacheKeys && cacheKeys.length) {
993
- for (let i = 0; i < cacheKeys.length; i += 1) {
994
- const k = cacheKeys[i];
995
-
996
- let value = await app.cache.get(k);
997
- if (value && value.userId && value.userId === id)
998
- await app.cache.del(k);
999
- }
1000
- }
1001
- }
1002
-
1003
- async function generate_new_access_token_pwd (userId, oldToken, keepToken = '', isWx = false) {
1004
- let uuid = keepToken || uuidv1();
1005
-
1006
- // remove the old one from cache
1007
- app.cache.del(oldToken);
1008
- // cache.del(oldToken);
1009
- await clear_cache_token_by_user_id(userId);
1010
-
1011
- // add the new one to the cache
1012
-
1013
- app.cache.put(uuid, { userId: userId, type: isWx ? 'wx' : 'pwd' }, app.config['cacheTimeout']);
1014
- // cache.put(uuid, { userId: userId, type: 'pwd' }, app.config['cacheTimeout']);
1015
-
1016
- return uuid;
1017
- }
1018
-
1019
1022
  // login with the specified strategy
1020
1023
  app.post(`${app.config['baseUrl'] || ''}/login`,
1021
1024
  passport.authenticate(m.config['strategy'] || 'local', { session: false }),
@@ -1044,10 +1047,10 @@ module.exports = (app) => ({
1044
1047
  (req.user && req.user.PhoneNumber && m.config['keepTokenAccounts'].indexOf(req.user.PhoneNumber) >= 0)) {
1045
1048
  // keep token
1046
1049
  const kt = await app.cache.get(`_keep_token_${req.user.id}`);
1047
- token = await generate_new_access_token_pwd(req.user.id, access_token, kt, req.user.isWx);
1050
+ token = await generate_new_access_token_pwd(app, req.user.id, access_token, kt, req.user.isWx);
1048
1051
  app.cache.set(`_keep_token_${req.user.id}`, token);
1049
1052
  } else {
1050
- token = await generate_new_access_token_pwd(req.user.id, access_token, null, req.user.isWx);
1053
+ token = await generate_new_access_token_pwd(app, req.user.id, access_token, null, req.user.isWx);
1051
1054
  }
1052
1055
 
1053
1056
  res.cookie('token', token, { maxAge: app.config['cookieTimeout'] });
@@ -1132,7 +1135,7 @@ module.exports = (app) => ({
1132
1135
  return next('route');
1133
1136
  }
1134
1137
 
1135
- const result = await res.Module('sms').sendRandom(phone, undefined, true, req.body.smsTemp || 'register');
1138
+ const result = await m.sms.sendRandom(phone, undefined, true, req.body.smsTemp || 'register');
1136
1139
 
1137
1140
  if (!result) {
1138
1141
  res.makeError(500, 'Failed to send sms!', m);
@@ -1147,7 +1150,6 @@ module.exports = (app) => ({
1147
1150
  return next();
1148
1151
  })
1149
1152
 
1150
-
1151
1153
  // verfiy the sms code
1152
1154
  app.post(`${(app.config['baseUrl'] || '')}/register/verify`, async (req, res, next) => {
1153
1155
  if (!req.body.PhoneNumber || !req.body.code) {
@@ -1155,7 +1157,7 @@ module.exports = (app) => ({
1155
1157
  return next('route');
1156
1158
  }
1157
1159
  const phone = crypto.encoder.desDecode(req.body.PhoneNumber, m.config.desKey);
1158
- const result = await res.Module('sms').verify(phone, req.body.code);
1160
+ const result = await m.sms.verify(phone, req.body.code);
1159
1161
  // app.logger.debug(cache.exportJson());
1160
1162
 
1161
1163
  if (!result) {
@@ -1167,7 +1169,6 @@ module.exports = (app) => ({
1167
1169
  return next();
1168
1170
  })
1169
1171
 
1170
-
1171
1172
  // verify phone number (duplication) for register
1172
1173
  app.post(`${(app.config['baseUrl'] || '')}/register/verify/phone`, async (req, res, next) => {
1173
1174
  if (!req.body.PhoneNumber) {
@@ -1196,7 +1197,7 @@ module.exports = (app) => ({
1196
1197
  return next('route');
1197
1198
  }
1198
1199
 
1199
- const result = await res.Module('sms').verify(phone, req.body.code);
1200
+ const result = await m.sms.verify(phone, req.body.code);
1200
1201
 
1201
1202
  if (!result) {
1202
1203
  res.makeError(403, 'Code verification failed!', m);
@@ -1269,7 +1270,7 @@ module.exports = (app) => ({
1269
1270
  const phone = crypto.encoder.desDecode(req.body.PhoneNumber, m.config.desKey);
1270
1271
  const password = crypto.encoder.desDecode(req.body.Password, m.config.desKey);
1271
1272
 
1272
- const result = await res.Module('sms').verify(phone, req.body.code);
1273
+ const result = await m.sms.verify(phone, req.body.code);
1273
1274
 
1274
1275
  if (!result) {
1275
1276
  res.makeError(403, 'Code verification failed!', m);
@@ -1371,7 +1372,7 @@ module.exports = (app) => ({
1371
1372
  // if (!clearPermission(perms)) {
1372
1373
  // perms = {}
1373
1374
  // }
1374
- const perm = '*';
1375
+ const perms = '*';
1375
1376
 
1376
1377
  await m.models.account.create({
1377
1378
  Saved: true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "free-be-account",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
4
4
  "main": "index.js",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
@@ -79,11 +79,11 @@ router.post('/submit',
79
79
  res.locals.body.Profile = {...user.Profile, ...req.body.Profile};
80
80
  }
81
81
 
82
- res.locals.body.Status = res.app.modules.passport.AccountAuditStatus.Auditing;
82
+ res.locals.body.Status = res.app.modules.account.AccountAuditStatus.Auditing;
83
83
 
84
84
  // set to default permission
85
- const p = res.app.modules.passport.config.accountDefaultPermissions;
86
- res.app.modules.passport.utils.clearPermission(p);
85
+ const p = res.app.modules.account.config.accountDefaultPermissions;
86
+ res.app.modules.account.utils.clearPermission(p);
87
87
  res.locals.body.Permission = p;
88
88
 
89
89
  res.locals.filters = { id: req.user.id };
@@ -24,9 +24,9 @@ router.put('/',
24
24
 
25
25
  // update phone number
26
26
  res.locals.body = {};
27
- res.locals.body.PhoneNumber = res.app.modules.passport.utils.crypto.encoder.desDecode(req.body.phone, res.app.modules.passport.config.desKey);
27
+ res.locals.body.PhoneNumber = res.app.modules.account.utils.crypto.encoder.desDecode(req.body.phone, res.app.modules.account.config.desKey);
28
28
 
29
- const oResult = await res.Module('sms').verify(ophone, req.body.ocode);
29
+ const oResult = await router.mdl.sms.verify(ophone, req.body.ocode);
30
30
  if (!oResult) {
31
31
  res.makeError(400, 'Verification code for the old phone is incorrect!', router.mdl);
32
32
  await res.app.cache.del(ophone);
@@ -34,7 +34,7 @@ router.put('/',
34
34
  return next('route');
35
35
  }
36
36
 
37
- const result = await res.Module('account').sms.verify(res.locals.body.PhoneNumber, req.body.code);
37
+ const result = await router.mdl.sms.verify(res.locals.body.PhoneNumber, req.body.code);
38
38
  if (!result) {
39
39
  res.makeError(405, 'Verification code for the new phone is incorrect!', router.mdl);
40
40
  await res.app.cache.del(ophone);
@@ -16,7 +16,7 @@ router.put('/',
16
16
  return next('route');
17
17
  }
18
18
 
19
- const result = await res.Module('sms').verify(phone, req.body.code);
19
+ const result = await router.mdl.sms.verify(phone, req.body.code);
20
20
  // app.logger.debug(cache.exportJson());
21
21
 
22
22
  if (!result) {
@@ -32,7 +32,7 @@ router.put('/',
32
32
  const password = res.app.modules.account.utils.crypto.encoder.desDecode(req.body.Password, res.app.modules.account.config.desKey);
33
33
 
34
34
  res.locals.body = {};
35
- res.locals.body.Password = res.app.modules.passport.utils.encryptPwd(password, res.app.modules.passport.config.pwdEncryptMethod || 'md5');
35
+ res.locals.body.Password = res.app.modules.account.utils.encryptPwd(password, res.app.modules.account.config.pwdEncryptMethod || 'md5');
36
36
 
37
37
  res.locals.filters = { id: req.user.id };
38
38
  res.locals.fields = [
@@ -79,7 +79,6 @@ router.post('/',
79
79
  // also same Org (but should check whether we have Org module??)
80
80
  if (req.user.Org) req.body.Org = req.user.Org;
81
81
 
82
- // TODO: should not set status here as we don't have this field yet (which was added in passport)
83
82
  req.body.Status = '1';
84
83
 
85
84
  // TODO: check permission, should not be bigger than the main account