This repository has been archived by the owner on Oct 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
service.js
executable file
·208 lines (190 loc) · 7.7 KB
/
service.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
const bcrypt = require('bcryptjs');
const errors = require('@feathersjs/errors');
const makeDebug = require('debug');
const merge = require('lodash.merge');
const { authenticate } = require('@feathersjs/authentication').hooks;
const checkUnique = require('./check-unique');
const identityChange = require('./identity-change');
const passwordChange = require('./password-change');
const resendVerifySignup = require('./resend-verify-signup');
const sanitizeUserForClient = require('./helpers/sanitize-user-for-client');
const sendResetPwd = require('./send-reset-pwd');
const { resetPwdWithLongToken, resetPwdWithShortToken } = require('./reset-password');
const { verifySignupWithLongToken, verifySignupWithShortToken } = require('./verify-signup');
const debug = makeDebug('authLocalMgnt:service');
const actionsNoAuth = [
'resendVerifySignup', 'verifySignupLong', 'verifySignupShort',
'sendResetPwd', 'resetPwdLong', 'resetPwdShort',
];
const optionsDefault = {
app: null, // Value set during configuration.
service: '/users', // Need exactly this for test suite. Overridden by config/default.json.
path: 'authManagement',
passwordField: 'password', // Overridden by config/default.json.
notifier: async () => () => {},
buildEmailLink,
longTokenLen: 15, // Token's length will be twice this by default.
shortTokenLen: 6,
shortTokenDigits: true,
resetDelay: 1000 * 60 * 60 * 2, // 2 hours
delay: 1000 * 60 * 60 * 24 * 5, // 5 days
identifyUserProps: ['email'],
actionsNoAuth: [
'resendVerifySignup', 'verifySignupLong', 'verifySignupShort',
'sendResetPwd', 'resetPwdLong', 'resetPwdShort',
],
ownAcctOnly: true,
sanitizeUserForClient,
bcryptCompare: bcrypt.compare,
catchErr: (err, options, data) => {
return Promise.reject(err); // support both async and Promise interfaces
},
customizeCalls: null, // Value set during configuration.
// Need option URL for notifier emails e.g. 'http://localhost:3030/' + type + '?token=' + hash ?????????????????????
// emailLink: (action, slug) => 'http://localhost:3030/' + type + '?token=' + slug, ????????????????????????????
};
/* Call options.customizeCalls using
const users = await options.customizeCalls.identityChange
.find(usersService, { query: identifyUser });
const user2 = await options.customizeCalls.identityChange
.patch(usersService, user1[usersServiceIdName], {
});
*/
function buildEmailLink(app) {
const isProd = process.env.NODE_ENV === 'production';
const port = (app.get('port') === '80' || isProd) ? '' : `:${app.get('port')}`;
const host = (app.get('host') === 'HOST')? 'localhost': app.get('host');
const protocol = (app.get('protocol') === 'PROTOCOL')? 'http': app.get('protocol');
const url = `${protocol}://${host}${port}/`;
const actionToSlug = {
aaa: 'aaa',
};
return (type, hash) => {
return `${url}${type}/${hash}`;
};
}
const optionsCustomizeCalls = {
checkUnique: {
find: async (usersService, params = {}) =>
await usersService.find(params),
},
identityChange: {
find: async (usersService, params = {}) =>
await usersService.find(params),
patch: async (usersService, id, data, params = {}) =>
await usersService.patch(id, data, params),
},
passwordChange: {
find: async (usersService, params = {}) =>
await usersService.find(params),
patch: async (usersService, id, data, params = {}) =>
await usersService.patch(id, data, params),
},
resendVerifySignup: {
find: async (usersService, params = {}) =>
await usersService.find(params),
patch: async (usersService, id, data, params = {}) =>
await usersService.patch(id, data, params),
},
resetPassword: {
resetTokenGet: async (usersService, id, params) =>
await usersService.get(id, params),
resetShortTokenFind: async (usersService, params = {}) =>
await usersService.find(params),
badTokenpatch: async (usersService, id, data, params = {}) =>
await usersService.patch(id, data, params),
patch: async (usersService, id, data, params = {}) =>
await usersService.patch(id, data, params),
},
sendResetPwd: {
find: async (usersService, params = {}) => await usersService.find(params),
patch: async (usersService, id, data, params = {}) =>
await usersService.patch(id, data, params),
},
verifySignup: {
find: async (usersService, params = {}) =>
await usersService.find(params),
patch: async (usersService, id, data, params = {}) =>
await usersService.patch(id, data, params),
},
};
module.exports = authenticationLocalManagement;
function authenticationLocalManagement(options1 = {}) {
debug('service being configured.');
return function (app) {
// Get defaults from config/default.json
const authOptions = app.get('authentication') || {};
optionsDefault.service = authOptions.service || optionsDefault.service;
optionsDefault.passwordField =
(authOptions.local || {}).passwordField || optionsDefault.passwordField;
let options = Object.assign({}, optionsDefault, options1, { app });
options.notifier = options.notifier(app);
options.customizeCalls = merge({}, optionsCustomizeCalls, options1.customizeCalls || {});
app.set('localManagement', options);
options.app.use(options.path, authLocalMgntMethods(options));
};
}
function authLocalMgntMethods(options) {
return {
async create (data) {
debug(`create called. action=${data.action}`);
try {
switch (data.action) {
case 'checkUnique':
return await checkUnique(
options, data.value, data.ownId || null, data.meta || {},
data.authUser, data.provider
);
case 'resendVerifySignup':
return await resendVerifySignup(
options, data.value, data.notifierOptions,
data.authUser, data.provider
);
case 'verifySignupLong':
return await verifySignupWithLongToken(
options, data.value,
data.authUser, data.provider
);
case 'verifySignupShort':
return await verifySignupWithShortToken(
options, data.value.token, data.value.user,
data.authUser, data.provider
);
case 'sendResetPwd':
return await sendResetPwd(
options, data.value, data.notifierOptions,
data.authUser, data.provider
);
case 'resetPwdLong':
return await resetPwdWithLongToken(
options, data.value.token, data.value.password,
data.authUser, data.provider
);
case 'resetPwdShort':
return await resetPwdWithShortToken(
options, data.value.token, data.value.user, data.value.password,
data.authUser, data.provider
);
case 'passwordChange':
return await passwordChange(
options, data.value.user, data.value.oldPassword, data.value.password,
data.authUser, data.provider
);
case 'identityChange':
return await identityChange(
options, data.value.user, data.value.password, data.value.changes,
data.authUser, data.provider
);
default:
return Promise.reject(
new errors.BadRequest(`Action '${data.action}' is invalid.`,
{ errors: { $className: 'badParams' } }
)
);
}
} catch (err) {
return options.catchErr(err, options, data);
}
}
}
}