Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·264 lines (238 loc) · 7.86 KB
/
Copy pathindex.js
File metadata and controls
executable file
·264 lines (238 loc) · 7.86 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
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
/* Passport Middlewares */
const passport = require('passport');
const FacebookStrategy = require('passport-facebook').Strategy;
const TwitterStrategy = require('./passport-twitter/lib').Strategy;
const GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
const GitHubStrategy = require('passport-github').Strategy;
const LinkedInStrategy = require('passport-linkedin').Strategy;
const InstagramStrategy = require('passport-instagram').Strategy;
const AmazonStrategy = require('passport-amazon').Strategy;
const FoursquareStrategy = require('passport-foursquare').Strategy;
const ImgurStrategy = require('passport-imgur').Strategy;
const MeetupStrategy = require('passport-meetup').Strategy;
const AppleStrategy = require('passport-apple').Strategy;
const jwt = require('jsonwebtoken');
// const TumblrStrategy = require('passport-tumblr').Strategy;
// const VKontakteStrategy = require('passport-vkontakte').Strategy;
/* Misc */
const toolset = console;
var socialLoginClass = function (options) {
var scope = this;
this._basePath = options._basePath || "/";
this.disableSession = options.disableSession || false;
this.returnRaw = options.returnRaw || false;
this.app = options.app || {};
this.onAuth = options.onAuth || function () {
};
this.url = options.url || 'http://127.0.0.1';
this.logout = options.logout || {url: '/logout', after: '/'};
// Special Cases
// PassportJS doesn't have a standardized API, with its property names changing from Strategy to Strategy.
// Here we fix that, taking social-login's standardized API and turning it into what Passportjs expects.
this.specialCases = {
twitter: {
setup: {
userAuthorizationURL: "https://api.twitter.com/oauth/authorize",
},
varChanges: {
clientID: 'consumerKey',
clientSecret: 'consumerSecret'
}
},
apple: {
varChanges: {
clientSecret: "privateKeyString"
}
},
linkedin: {
varChanges: {
clientID: 'consumerKey',
clientSecret: 'consumerSecret'
}
},
google: {
varAdd: {
returnURL: function (settings) {
return scope.url + settings.url.callback;
},
realm: function (settings) {
return scope.url + '/';
},
}
},
meetup: {
varChanges: {
clientID: 'consumerKey',
clientSecret: 'consumerSecret'
}
},
// tumblr: {
// varChanges: {
// clientID: 'consumerKey',
// clientSecret: 'consumerSecret'
// }
// }
};
// The strategy aliases
this.map = {
facebook: FacebookStrategy,
// vkontakte: VKontakteStrategy,
twitter: TwitterStrategy,
instagram: InstagramStrategy,
linkedin: LinkedInStrategy,
github: GitHubStrategy,
google: GoogleStrategy,
amazon: AmazonStrategy,
foursquare: FoursquareStrategy,
imgur: ImgurStrategy,
meetup: MeetupStrategy,
apple: AppleStrategy,
// tumblr: TumblrStrategy
};
this.uniqueIds = {
facebook: 'id',
apple: 'id',
twitter: 'id',
instagram: 'id',
linkedin: 'id',
github: 'id',
google: 'id',
amazon: 'id',
foursquare: 'id',
imgur: 'id',
meetup: 'id',
// tumblr: 'name'
};
// The strategy names
// Some passport libs have more complex internal names than just the name of the service.
this.strategyNameMap = {};
};
socialLoginClass.prototype.use = function (settings) {
this.settings = settings;
this.init();
};
socialLoginClass.prototype.init = function () {
let scope = this;
// Setup PassportJS
this.app.use(scope._basePath, passport.initialize());
if (!scope.disableSession)
this.app.use(scope._basePath, passport.session());
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(function (user, done) {
done(null, user);
});
if (!scope.disableSession) {
this.app.get(this.logout.url, function (req, res) {
res.clearCookie('session_key');
req.logout();
res.redirect(scope.logout.after);
});
}
let type;
for (type in this.settings) {
this.setup(type, this.settings[type]);
}
};
socialLoginClass.prototype.setup = function (type, settings) {
//toolset.log("Setting up:", type);
let scope = this;
if (!this.map[type]) {
toolset.error("Error!", 'type "' + type + '" is not supported.');
return false;
}
// Passport setup
let passportSetup = {
...((!!settings.settings && !!settings.settings.strategy && typeof settings.settings.strategy === 'object') ? settings.settings.strategy : {}),
clientID: settings.settings.clientID,
clientSecret: settings.settings.clientSecret,
callbackURL: this.url + settings.url.callback,
passReqToCallback: true
};
// Update the variable names if needed, because Passport is unable to standardize things apparently.
if (this.specialCases[type] && this.specialCases[type].varChanges) {
var varname;
for (varname in this.specialCases[type].varChanges) {
(function (varname) {
// Save a copy
var buffer = passportSetup[varname];
// Create the new property
passportSetup[scope.specialCases[type].varChanges[varname]] = buffer;
/// Remove the original data
delete passportSetup[varname];
})(varname);
}
}
// Add new non-standard variables
if (this.specialCases[type] && this.specialCases[type].varAdd) {
var varname;
for (varname in this.specialCases[type].varAdd) {
(function (varname) {
passportSetup[varname] = scope.specialCases[type].varAdd[varname](settings);
})(varname);
}
}
// Extend the settings if needed
if (this.specialCases[type] && this.specialCases[type].setup) {
passportSetup = Object.assign({}, passportSetup, this.specialCases[type].setup);
}
// Execute the passport strategy
//passport.use(new (this.map[type])(passportSetup, settings.methods.auth));
passport.use(new (this.map[type])(passportSetup, function (req, accessToken, refreshToken, idToken, profile, done) {
if(type === 'apple') profile._json = jwt.decode(idToken);
scope.onAuth(req, type, scope.uniqueIds[type], accessToken, refreshToken, scope.returnRaw ? profile : scope.preparseProfileData(type, profile), done);
}));
let strategyName = type;
if (this.strategyNameMap[type]) {
strategyName = this.strategyNameMap[type]
}
// Setup the enty point (/auth/:service)
this.app.get(settings.url.auth, passport.authenticate(strategyName, settings.settings.authParameters || {}));
// Setup the callback url (/auth/:service/callback)
this.app.all(settings.url.callback, passport.authenticate(strategyName, {
successRedirect: settings.url.success,
failureRedirect: settings.url.fail,
failureFlash: true,
session: !scope.disableSession
}));
};
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
// The response is not uniform, making it harder to manage consistent data format accross all the services.
//
socialLoginClass.prototype.preparseProfileData = function (type, profile) {
let data = profile._json;
switch (type) {
default:
return data;
case "apple":
return {
id: data.sub,
first_name: capitalizeFirstLetter(String(data.email).split("@")[0]),
last_name: "#",
profile_url: "",
avatar: null,
email: data.email,
}
case "foursquare":
case "tumblr":
return data.response.user;
case "imgur":
case "instagram":
return data.data;
case "meetup":
return data.results[0];
case "google":
return {
id: data.sub,
first_name: (data.given_name) ? data.given_name : data.name || "Guest",
last_name: (data.family_name) ? data.family_name : "#",
profile_url: data.url,
avatar: (data.picture) ? data.picture : null,
email: data.email,
};
}
};
module.exports = socialLoginClass;