forked from purewave1989/nodebb-plugin-sso-github
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathlibrary.js
More file actions
228 lines (199 loc) · 6.1 KB
/
Copy pathlibrary.js
File metadata and controls
228 lines (199 loc) · 6.1 KB
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
'use strict';
const User = nodebb.require('./src/user');
const db = nodebb.require('./src/database');
const meta = nodebb.require('./src/meta');
const nconf = nodebb.require('nconf');
const passport = nodebb.require('passport');
const GithubStrategy = require('passport-github2').Strategy;
const constants = Object.freeze({
name: 'GitHub',
admin: {
icon: 'fa-github',
route: '/plugins/sso-github',
},
});
const GitHub = module.exports;
GitHub.init = async function (data) {
const hostHelpers = nodebb.require('./src/routes/helpers');
hostHelpers.setupAdminPageRoute(data.router, '/admin/plugins/sso-github', function (req, res) {
res.render('admin/plugins/sso-github', {
title: constants.name,
callbackURL: nconf.get('url') + '/auth/github/callback',
});
});
hostHelpers.setupPageRoute(data.router, '/deauth/github', [data.middleware.requireUser], function (req, res) {
res.render('plugins/sso-github/deauth', {
service: constants.name,
});
});
data.router.post('/deauth/github', [data.middleware.requireUser, data.middleware.applyCSRF], hostHelpers.tryRoute(async function (req, res) {
await GitHub.deleteUserData({
uid: req.user.uid,
});
res.redirect(nconf.get('relative_path') + '/me/edit');
}));
};
GitHub.getStrategy = async function (strategies) {
const settings = await meta.settings.get('sso-github');
GitHub.settings = settings;
if (settings.id && settings.secret) {
passport.use(new GithubStrategy({
clientID: settings.id,
clientSecret: settings.secret,
callbackURL: nconf.get('url') + '/auth/github/callback',
passReqToCallback: true,
scope: ['user:email'], // fetches non-public emails as well
}, async function (req, token, tokenSecret, profile, done) {
try {
if (req.hasOwnProperty('user') && req.user.hasOwnProperty('uid') && req.user.uid > 0) {
// Save GitHub -specific information to the user
await User.setUserField(req.user.uid, 'githubid', profile.id);
await db.setObjectField('githubid:uid', profile.id, req.user.uid);
return done(null, req.user);
}
const email = Array.isArray(profile.emails) && profile.emails.length ? profile.emails[0].value : '';
const pictureUrl = Array.isArray(profile.photos) && profile.photos.length ? profile.photos[0].value : '';
const { queued, uid, message } = await GitHub.login(
req, profile.id, profile.displayName, profile.username, email, pictureUrl
);
if (queued) {
return done(null, false, { message });
}
done(null, { uid });
} catch (err) {
done(err);
}
}));
strategies.push({
name: 'github',
url: '/auth/github',
callbackURL: '/auth/github/callback',
icon: constants.admin.icon,
icons: {
normal: 'fa-brands fa-github',
square: 'fa-brands fa-github-square',
},
labels: {
login: '[[social:sign-in-with-github]]',
register: '[[social:sign-up-with-github]]',
},
color: '#25292f',
scope: 'user:email',
});
}
return strategies;
};
GitHub.appendUserHashWhitelist = function (data) {
data.whitelist.push('githubid');
return data;
};
GitHub.getAssociation = async function (data) {
const githubid = await User.getUserField(data.uid, 'githubid');
if (githubid) {
data.associations.push({
associated: true,
name: constants.name,
icon: constants.admin.icon,
deauthUrl: nconf.get('url') + '/deauth/github',
});
} else {
data.associations.push({
associated: false,
url: nconf.get('url') + '/auth/github',
name: constants.name,
icon: constants.admin.icon,
});
}
return data;
};
GitHub.login = async function (req, githubID, displayName, username, email, pictureUrl) {
if (!email) {
email = username + '@users.noreply.github.com';
}
let uid = await GitHub.getUidByGitHubID(githubID);
if (uid) {
return { uid };
}
uid = await User.getUidByEmail(email);
if (uid) { // Link github account to existing user with same email
await Promise.all([
User.setUserField(uid, 'githubid', githubID),
db.setObjectField('githubid:uid', githubID, uid),
]);
return { uid };
}
// Abort user creation if registration via SSO is restricted
if (GitHub.settings.disableRegistration === 'on') {
throw new Error('[[error:sso-registration-disabled, GitHub]]');
}
return await User.createOrQueue(req, {
githubid: githubID,
username: username,
email: email,
fullname: displayName,
picture: pictureUrl,
}, {
emailVerification: GitHub.settings.needToVerifyEmail === 'on' ? 'send' : 'verify',
});
};
GitHub.addToApprovalQueue = async (hookData) => {
await saveGitHubSpecificData(hookData.data, hookData.userData);
return hookData;
};
GitHub.filterUserCreate = async (hookData) => {
await saveGitHubSpecificData(hookData.user, hookData.data);
return hookData;
};
async function saveGitHubSpecificData(targetObj, sourceObj) {
const { githubid, picture } = sourceObj;
if (githubid) {
const uid = await GitHub.getUidByGitHubID(githubid);
if (uid) {
throw new Error('[[error:sso-account-exists, GitHub]]');
}
targetObj.githubid = githubid;
if (picture) {
targetObj.picture = picture;
targetObj.uploadedpicture = picture;
}
}
}
GitHub.actionUserCreate = async (hookData) => {
const { uid } = hookData.user;
const githubid = await User.getUserField(uid, 'githubid');
if (githubid) {
await db.setObjectField('githubid:uid', githubid, uid);
}
};
GitHub.filterUserGetRegistrationQueue = async (hookData) => {
const { users } = hookData;
users.forEach((user) => {
if (user?.githubid) {
user.sso = {
icon: 'fa-brands fa-github',
name: constants.name,
};
}
});
return hookData;
};
GitHub.getUidByGitHubID = async function (githubID) {
const uid = await db.getObjectField('githubid:uid', githubID);
return uid;
};
GitHub.addMenuItem = function (custom_header) {
custom_header.authentication.push({
route: constants.admin.route,
icon: constants.admin.icon,
name: constants.name,
});
return custom_header;
};
GitHub.deleteUserData = async function (data) {
const { uid } = data;
const githubid = await User.getUserField(uid, 'githubid');
if (githubid) {
await db.deleteObjectField('githubid:uid', githubid);
await db.deleteObjectField('user:' + uid, 'githubid');
}
};