-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlibrary.js
186 lines (157 loc) · 4.98 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
'use strict';
const Twitter = module.exports;
const passport = require.main.require('passport');
const passportTwitter = require('passport-twitter').Strategy;
const path = require.main.require('path');
const nconf = require.main.require('nconf');
const user = require.main.require('./src/user');
const meta = require.main.require('./src/meta');
const db = require.main.require('./src/database');
const constants = Object.freeze({
name: 'Twitter',
admin: {
route: '/plugins/sso-twitter',
icon: 'fa-twitter-square',
},
});
Twitter.init = async function (data) {
const hostHelpers = require.main.require('./src/routes/helpers');
hostHelpers.setupAdminPageRoute(data.router, '/admin/plugins/sso-twitter', (req, res) => {
res.render('admin/plugins/sso-twitter', {
title: constants.name,
});
});
hostHelpers.setupPageRoute(data.router, '/deauth/twitter', [data.middleware.requireUser], (req, res) => {
res.render('plugins/sso-twitter/deauth', {
service: 'Twitter',
});
});
data.router.get('/auth/twitter/callback', (req, res, next) => {
// passport-twitter checks that the oauth_token
// parameter is the same as the one it generated.
//
// Twitter does not support OAuth2, so the "state"
// query string argument is not present.
req.query.state = req.session.ssoState;
next();
});
data.router.post('/deauth/twitter', [data.middleware.requireUser, data.middleware.applyCSRF], async (req, res, next) => {
try {
await Twitter.deleteUserData(req.user.uid);
res.redirect(`${nconf.get('relative_path')}/me/edit`);
} catch (err) {
next(err);
}
});
};
Twitter.filterAuthInit = async function (strategies) {
const { key, secret } = await meta.settings.get('sso-twitter');
if (key && secret) {
passport.use(new passportTwitter({
consumerKey: key,
consumerSecret: secret,
callbackURL: `${nconf.get('url')}/auth/twitter/callback`,
passReqToCallback: true,
}, async (req, token, tokenSecret, profile, done) => {
try {
if (req.hasOwnProperty('user') && req.user.hasOwnProperty('uid') && req.user.uid > 0) {
// Save twitter-specific information to the user
await Promise.all([
user.setUserField(req.user.uid, 'twid', profile.id),
db.setObjectField('twid:uid', profile.id, req.user.uid),
]);
return done(null, req.user);
}
const userData = await Twitter.login(profile.id, profile.username, profile.photos);
done(null, userData);
} catch (err) {
done(err);
}
}));
strategies.push({
name: 'twitter',
url: '/auth/twitter',
callbackURL: '/auth/twitter/callback',
icon: constants.admin.icon,
icons: {
normal: 'fa-brands fa-twitter',
square: 'fa-brands fa-twitter-square',
},
labels: {
login: '[[social:sign-in-with-twitter]]',
register: '[[social:sign-up-with-twitter]]',
},
color: '#1DA1F2',
scope: '',
});
}
return strategies;
};
Twitter.filterAuthList = async function (data) {
const twitterId = await user.getUserField(data.uid, 'twid');
if (twitterId) {
data.associations.push({
associated: true,
url: `https://twitter.com/intent/user?user_id=${twitterId}`,
deauthUrl: `${nconf.get('url')}/deauth/twitter`,
name: constants.name,
icon: constants.admin.icon,
});
} else {
data.associations.push({
associated: false,
url: `${nconf.get('url')}/auth/twitter`,
name: constants.name,
icon: constants.admin.icon,
});
}
return data;
};
Twitter.addMenuItem = function (custom_header) {
custom_header.authentication.push({
route: constants.admin.route,
icon: constants.admin.icon,
name: constants.name,
});
return custom_header;
};
Twitter.deleteUserData = async function (data) {
const twid = await user.getUserField(data.uid, 'twid');
if (twid) {
await db.deleteObjectField('twid:uid', twid);
await db.deleteObjectField(`user:${data.uid}`, 'twid');
}
};
Twitter.filterUserWhitelistFields = function (data) {
data.whitelist.push('twid');
return data;
};
Twitter.login = async function (twid, handle, photos) {
const { disableRegistration } = await meta.settings.get('sso-twitter');
let uid = await Twitter.getUidByTwitterId(twid);
if (uid) { // Existing User
return { uid };
}
// Abort user creation if registration via SSO is restricted
if (disableRegistration === 'on') {
throw new Error('[[error:sso-registration-disabled, Twitter]]');
}
// New User
uid = await user.create({ username: handle });
const twitterData = { twid };
// Save their photo, if present
if (photos && photos.length > 0) {
const photoUrl = photos[0].value;
twitterData.uploadedpicture = `${path.dirname(photoUrl)}/${path.basename(photoUrl, path.extname(photoUrl)).slice(0, -6)}bigger${path.extname(photoUrl)}`;
twitterData.picture = twitterData.uploadedpicture;
}
// Save twitter-specific information to the user
await Promise.all([
user.setUserFields(uid, twitterData),
db.setObjectField('twid:uid', twid, uid),
]);
return { uid };
};
Twitter.getUidByTwitterId = async function (twid) {
return await db.getObjectField('twid:uid', twid);
};