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
/
plugins-default.js
executable file
·258 lines (234 loc) · 8.27 KB
/
plugins-default.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
const makeDebug = require('debug');
const checkUnique = require('./check-unique');
const identityChange = require('./identity-change');
const passwordChange = require('./password-change');
const resendVerifySignup = require('./resend-verify-signup');
const sendResetPwd = require('./send-reset-pwd');
const { resetPwdWithLongToken, resetPwdWithShortToken } = require('./reset-password');
const { verifySignupWithLongToken, verifySignupWithShortToken } = require('./verify-signup');
const debug = makeDebug('authLocalMgnt:plugins');
module.exports = [
// main service handlers
{
name: 'checkUnique',
desc: 'checkUnique - default plugin, authentication-local-management',
version: '1.0.0',
trigger: 'checkUnique',
run: async (accumulator, data, pluginsContext, pluginContext) => {
return await checkUnique(pluginsContext,
data.value, data.ownId || null, data.meta || {},
data.authUser, data.provider
);
},
},
{
name: 'identityChange',
desc: 'identityChange - default plugin, authentication-local-management',
version: '1.0.0',
trigger: 'identityChange',
run: async (accumulator, data, pluginsContext, pluginContext) => {
return await identityChange(pluginsContext,
data.value.user, data.value.password, data.value.changes, data.notifierOptions,
data.authUser, data.provider
);
},
},
{
name: 'passwordChange',
desc: 'passwordChange - default plugin, authentication-local-management',
version: '1.0.0',
trigger: 'passwordChange',
run: async (accumulator, data, pluginsContext, pluginContext) => {
return await passwordChange(pluginsContext,
data.value.user, data.value.oldPassword, data.value.password, data.notifierOptions,
data.authUser, data.provider
);
},
},
{
name: 'resendVerifySignup',
desc: 'resendVerifySignup - default plugin, authentication-local-management',
version: '1.0.0',
trigger: 'resendVerifySignup',
run: async (accumulator, data, pluginsContext, pluginContext) => {
return await resendVerifySignup(pluginsContext,
data.value, data.notifierOptions,
data.authUser, data.provider
);
},
},
{
name: 'resetPwdLong',
desc: 'resetPwdLong - default plugin, authentication-local-management',
version: '1.0.0',
trigger: 'resetPwdLong',
run: async (accumulator, data, pluginsContext, pluginContext) => {
return await resetPwdWithLongToken(pluginsContext,
data.value.token, data.value.password, data.notifierOptions,
data.authUser, data.provider
);
},
},
{
name: 'resetPwdShort',
desc: 'resetPwdShort - default plugin, authentication-local-management',
version: '1.0.0',
trigger: 'resetPwdShort',
run: async (accumulator, data, pluginsContext, pluginContext) => {
return await resetPwdWithShortToken(pluginsContext,
data.value.token, data.value.user, data.value.password, data.notifierOptions,
data.authUser, data.provider
);
},
},
{
name: 'sendResetPwd',
desc: 'sendResetPwd - default plugin, authentication-local-management',
version: '1.0.0',
trigger: 'sendResetPwd',
run: async (accumulator, data, pluginsContext, pluginContext) => {
return await sendResetPwd(pluginsContext,
data.value, data.notifierOptions,
data.authUser, data.provider
);
},
},
{
name: 'verifySignupLong',
desc: 'verifySignupLong - default plugin, authentication-local-management',
version: '1.0.0',
trigger: 'verifySignupLong',
run: async (accumulator, data, pluginsContext, pluginContext) => {
return await verifySignupWithLongToken(pluginsContext,
data.value, data.notifierOptions,
data.authUser, data.provider
);
},
},
{
name: 'verifySignupShort',
desc: 'verifySignupShort - default plugin, authentication-local-management',
version: '1.0.0',
trigger: 'verifySignupShort',
run: async (accumulator, data, pluginsContext, pluginContext) => {
return await verifySignupWithShortToken(pluginsContext,
data.value.token, data.value.user, data.notifierOptions,
data.authUser, data.provider
);
},
},
// checkUnique service calls
pluginFactory('checkUnique.find', 'find'),
// identityChange service calls
pluginFactory('identityChange.find', 'find'),
pluginFactory('identityChange.patch', 'patch'),
// passwordChange service calls
pluginFactory('passwordChange.find', 'find'),
pluginFactory('passwordChange.patch', 'patch'),
// resendVerifySignup service calls
pluginFactory('resendVerifySignup.find', 'find'),
pluginFactory('resendVerifySignup.patch', 'patch'),
// resetPassword service calls
pluginFactory('resetPassword.tokenGet', 'get'),
pluginFactory('resetPassword.shortTokenFind', 'find'),
pluginFactory('resetPassword.badTokenPatch', 'patch'),
pluginFactory('resetPassword.patch', 'patch'),
// sendResetPwd service calls
pluginFactory('sendResetPwd.find', 'find'),
pluginFactory('sendResetPwd.patch', 'patch'),
// verifySignup service calls
pluginFactory('verifySignup.find', 'find'),
pluginFactory('verifySignup.find', 'patch'),
// notifier
{
trigger: 'notifier',
run: async (accumulator, { type, sanitizedUser, notifierOptions }, { options }, pluginContext) => {
debug('notifier', type);
// console.log('a-l-m default notifier called', type, sanitizedUser, notifierOptions);
return sanitizedUser;
},
},
// utilities
{
trigger: 'sanitizeUserForNotifier',
run: async (accumulator, user, { options } , pluginContext) => {
const sanitizedUser = shallowCloneObject(user);
delete sanitizedUser[options.passwordField];
return sanitizedUser;
},
},
{
trigger: 'sanitizeUserForClient',
run: async (accumulator, user, { options } , pluginContext) => {
const sanitizedUser = shallowCloneObject(user);
delete sanitizedUser[options.passwordField];
delete sanitizedUser.verifyExpires;
delete sanitizedUser.verifyToken;
delete sanitizedUser.verifyShortToken;
delete sanitizedUser.verifyChanges;
delete sanitizedUser.resetExpires;
delete sanitizedUser.resetToken;
delete sanitizedUser.resetShortToken;
return sanitizedUser;
},
},
// catch error during processing
{
trigger: 'catchError',
run: async (accumulator, err, pluginsContext, pluginContext) =>
Promise.reject(err) // support both async and Promise interfaces
},
// buildEmailLink
{
trigger: 'buildEmailLink',
run: async (accumulator, actionToVerb, pluginsContext, pluginContext) => {
const app = pluginsContext.options.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') || 'http';
const url = `${protocol}://${host}${port}/`;
actionToVerb = {
sendVerifySignup: 'verify',
resendVerifySignup: 'verify',
sendResetPwd: 'reset',
};
return (type, hash) => {
return `${url}${actionToVerb[type] || type}/${hash}`;
};
},
},
];
function shallowCloneObject(obj) {
return Object.assign({}, obj);
}
function pluginFactory(trigger, type) {
let run;
switch (type) {
case 'find':
run = async (accumulator, { usersService, params}, pluginsContext, pluginContext) =>
await usersService.find(params);
break;
case 'get':
run = async (accumulator, { usersService, id, params}, pluginsContext, pluginContext) =>
await usersService.get(id, params);
break;
case 'patch':
run = async (accumulator, { usersService, id, data, params }, pluginsContext, pluginContext) =>
await usersService.patch(id, data, params);
break;
case 'no-op':
run = async (accumulator, data, pluginsContext, pluginContext) =>
accumulator || data;
break;
default:
throw new Error(`Invalid type ${type}. (plugins-default`);
}
return {
name: trigger,
desc: `${trigger} - default plugin, authentication-local-management`,
version: '1.0.0',
trigger: trigger,
run,
};
}