-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlibrary.js
322 lines (267 loc) · 9.21 KB
/
library.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
'use strict';
const passport = module.parent.require('passport');
const nconf = module.parent.require('nconf');
const winston = module.parent.require('winston');
const db = require.main.require('./src/database');
const user = require.main.require('./src/user');
const plugins = require.main.require('./src/plugins');
const meta = require.main.require('./src/meta');
const groups = require.main.require('./src/groups');
const authenticationController = require.main.require('./src/controllers/authentication');
const routeHelpers = require.main.require('./src/routes/helpers');
const OAuth = module.exports;
OAuth.init = async (params) => {
const { router /* , middleware , controllers */ } = params;
const controllers = require('./lib/controllers');
routeHelpers.setupAdminPageRoute(router, '/admin/plugins/sso-oauth2-multiple', controllers.renderAdminPage);
};
OAuth.addRoutes = async ({ router, middleware }) => {
const controllers = require('./lib/controllers');
const middlewares = [
middleware.ensureLoggedIn,
middleware.admin.checkPrivileges,
];
routeHelpers.setupApiRoute(router, 'get', '/oauth2-multiple/discover', middlewares, controllers.getOpenIdMetadata);
routeHelpers.setupApiRoute(router, 'post', '/oauth2-multiple/strategies', middlewares, controllers.editStrategy);
routeHelpers.setupApiRoute(router, 'get', '/oauth2-multiple/strategies/:name', middlewares, controllers.getStrategy);
routeHelpers.setupApiRoute(router, 'delete', '/oauth2-multiple/strategies/:name', middlewares, controllers.deleteStrategy);
};
OAuth.addAdminNavigation = (header) => {
header.authentication.push({
route: '/plugins/sso-oauth2-multiple',
icon: 'fa-tint',
name: 'Multiple OAuth2',
});
return header;
};
OAuth.listStrategies = async (full) => {
const names = await db.getSortedSetMembers('oauth2-multiple:strategies');
names.sort();
return await getStrategies(names, full);
};
OAuth.getStrategy = async (name) => {
const strategies = await getStrategies([name], true);
return strategies.length ? strategies[0] : null;
};
async function getStrategies(names, full) {
const strategies = await db.getObjects(names.map(name => `oauth2-multiple:strategies:${name}`), full ? undefined : ['enabled']);
strategies.forEach((strategy, idx) => {
strategy.name = names[idx];
strategy.enabled = strategy.enabled === 'true' || strategy.enabled === true;
strategy.callbackUrl = `${nconf.get('url')}/auth/${names[idx]}/callback`;
});
return strategies;
}
OAuth.loadStrategies = async (strategies) => {
const passportOAuth = require('passport-oauth').OAuth2Strategy;
let configured = await OAuth.listStrategies(true);
configured = configured.filter(obj => obj.enabled);
const configs = configured.map(({
name,
authUrl: authorizationURL,
tokenUrl: tokenURL,
id: clientID,
secret: clientSecret,
callbackUrl: callbackURL,
}) => new passportOAuth({
authorizationURL,
tokenURL,
clientID,
clientSecret,
callbackURL,
passReqToCallback: true,
}, async (req, token, secret, profile, done) => {
const { id, displayName, email, email_verified } = profile;
if (![id, displayName, email].every(Boolean)) {
return done(new Error('insufficient-scope'));
}
try {
const user = await OAuth.login({
name,
oAuthid: id,
handle: displayName,
email,
email_verified,
});
winston.verbose(`[plugin/sso-oauth2-multiple] Successful login to uid ${user.uid} via ${name} (remote id ${id})`);
await authenticationController.onSuccessfulLogin(req, user.uid);
await OAuth.assignGroups({ provider: name, user, profile });
await OAuth.updateProfile(user.uid, profile);
done(null, user);
plugins.hooks.fire('action:oauth2.login', { name, user, profile });
} catch (err) {
done(err);
}
}));
configs.forEach((strategy, idx) => {
strategy.userProfile = OAuth.getUserProfile.bind(strategy, configured[idx].name, configured[idx].userRoute);
passport.use(configured[idx].name, strategy);
});
strategies.push(...configured.map(({ name, scope, loginLabel, registerLabel, faIcon }) => ({
name,
url: `/auth/${name}`,
callbackURL: `/auth/${name}/callback`,
icon: faIcon || 'fa-right-to-bracket',
icons: {
normal: `fa ${faIcon || 'fa-right-to-bracket'}`,
square: `fa ${faIcon || 'fa-right-to-bracket'}`,
},
labels: {
login: loginLabel || 'Log In',
register: registerLabel || 'Register',
},
color: '#666',
scope: scope || 'openid email profile',
})));
return strategies;
};
OAuth.getUserProfile = function (name, userRoute, accessToken, done) {
// If your OAuth provider requires the access token to be sent in the query parameters
// instead of the request headers, comment out the next line:
this._oauth2._useAuthorizationHeaderForGET = true;
this._oauth2.get(userRoute, accessToken, async (err, body/* , res */) => {
if (err) {
return done(err);
}
try {
const json = JSON.parse(body);
const profile = await OAuth.parseUserReturn(name, json);
profile.provider = name;
done(null, profile);
} catch (e) {
done(e);
}
});
};
OAuth.parseUserReturn = async (provider, profile) => {
const {
id, sub,
name, nickname, preferred_username,
given_name, middle_name, family_name,
picture, roles, email, email_verified,
} = profile;
const { usernameViaEmail, forceUsernameViaEmail, idKey } = await OAuth.getStrategy(provider);
const displayName = nickname || preferred_username || name;
const combinedFullName = [given_name, middle_name, family_name].filter(Boolean).join(' ');
const fullname = name || combinedFullName;
const normalized = {
provider,
id: profile[idKey] || id || sub,
displayName,
fullname,
picture,
roles,
email,
email_verified,
};
if (forceUsernameViaEmail || (!normalized.displayName && email && usernameViaEmail === 'on')) {
normalized.displayName = email.split('@')[0];
}
return normalized;
};
OAuth.getAssociations = async () => {
let { roles, groups } = await meta.settings.get('sso-oauth2-multiple');
if (!roles || !groups) {
return [];
}
if (!Array.isArray(groups)) {
groups = groups.split(',');
}
if (!Array.isArray(roles)) {
roles = roles.split(',');
}
return roles.map((role, idx) => ({
role,
group: groups[idx],
}));
};
OAuth.login = async (payload) => {
let uid = await OAuth.getUidByOAuthid(payload.name, payload.oAuthid);
if (uid !== null) {
// Existing User
return ({ uid });
}
const { trustEmailVerified } = await OAuth.getStrategy(payload.name);
const { email } = payload;
const email_verified =
parseInt(trustEmailVerified, 10) &&
(payload.email_verified || payload.email_verified === true);
// Check for user via email fallback
if (email && email_verified) {
uid = await user.getUidByEmail(payload.email);
}
if (!uid) {
// New user
uid = await user.create({
username: payload.handle,
});
// Automatically confirm user email
if (email) {
await user.setUserField(uid, 'email', email);
if (email_verified) {
await user.email.confirmByUid(uid);
}
}
}
// Save provider-specific information to the user
await user.setUserField(uid, `${payload.name}Id`, payload.oAuthid);
await db.setObjectField(`${payload.name}Id:uid`, payload.oAuthid, uid);
return { uid };
};
OAuth.assignGroups = async ({ user, profile }) => {
if (!profile.roles || !Array.isArray(profile.roles)) {
return;
}
const { uid } = user;
const associations = await OAuth.getAssociations();
const { toJoin, toLeave } = associations.reduce((memo, { role, group }) => {
if (profile.roles.includes(role)) {
memo.toJoin.push(group);
} else {
memo.toLeave.push(group);
}
return memo;
}, { toJoin: [], toLeave: [] });
if (toLeave.length) {
winston.verbose(`[plugins/sso-auth0] uid ${uid} now leaving ${toLeave.length} these user groups: ${toLeave.join(', ')}`);
}
await groups.leave(toLeave, uid);
await groups.join(toJoin, uid);
winston.verbose(`[plugins/sso-auth0] uid ${uid} now a part of ${toJoin.length} these user groups: ${toJoin.join(', ')}`);
};
OAuth.updateProfile = async (uid, profile) => {
const fields = ['fullname', 'picture'];
const strategy = await OAuth.getStrategy(profile.provider);
const allowList = [];
const payload = fields.reduce((memo, field) => {
const setting = `sync${field[0].toUpperCase()}${field.slice(1)}`;
if (strategy[setting] && parseInt(strategy[setting], 10)) {
memo[field] = profile[field];
if (field === 'picture') {
allowList.push('picture');
}
}
return memo;
}, {});
payload.uid = uid;
await user.updateProfile(uid, payload, allowList);
};
OAuth.getUidByOAuthid = async (name, oAuthid) => db.getObjectField(`${name}Id:uid`, oAuthid);
OAuth.deleteUserData = async (data) => {
const names = await db.getSortedSetMembers('oauth2-multiple:strategies');
const oAuthIds = await user.getUserFields(data.uid, names.map(name => `${name}Id`));
delete oAuthIds.uid;
const promises = [];
for (const [provider, id] of Object.entries(oAuthIds)) {
if (id) {
promises.push(db.deleteObjectField(`${provider}:uid`, id));
}
}
await Promise.all(promises);
};
// If this filter is not there, the deleteUserData function will fail when getting the oauthId for deletion.
OAuth.whitelistFields = async (params) => {
const names = await db.getSortedSetMembers('oauth2-multiple:strategies');
params.whitelist.push(...names.map(name => `${name}Id`));
return params;
};