-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
426 lines (393 loc) · 13.6 KB
/
index.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
const OAuth2Strategy = require("passport-oauth2"),
{ InternalOAuthError } = require("passport-oauth2"),
utils = require("passport-oauth2/lib/utils");
const url = require("url");
const API_BASE = "https://discord.com/api/";
/**
* `Strategy` constructor.
*
* The Discord authentication strategy authenticates requests by delegating to
* Discord using the OAuth 2.0 protocol.
*P
* Applications must supply a `verify` callback which accepts an `accessToken`,
* `refreshToken` and `profile`, and then calls the `done`
* callback supplying a `user`, which should be set to `false` if the
* credentials are not valid. If an exception occurred, `err` should be set.
*
* Options:
* - `clientID` your Discord application's Client ID
* - `clientSecret` your Discord application's Client Secret
* - `callbackURL` URL to which Discord will redirect the user after granting authorization
*
* Examples:
*
* passport.use(new Strategy({
* clientID: '123-456-789',
* clientSecret: 'shhh-its-a-secret',
* callbackURL: 'https://www.example.com/auth/discord/callback'
* },
* function(accessToken, refreshToken, profile, done) {
* User.findOrCreate({ discordId: profile.id }, function (err, user) {
* return done(err, user);
* });
* }
* ));
*
* @param {Object} options - Configuration options.
* @param {Function} verify - The verify callback function.
*/
class Strategy extends OAuth2Strategy {
constructor(options, verify) {
options = options || {};
options.authorizationURL =
options.authorizationURL || "https://discord.com/api/oauth2/authorize";
options.tokenURL =
options.tokenURL || "https://discord.com/api/oauth2/token";
options.scopeSeparator = options.scopeSeparator || " ";
options.scope = options.scope || ["identify", "email"];
if (!options.callbackURL) throw new Error("Missing callbackURL property");
if (!options.clientID) throw new Error("Missing clientID property");
if (!options.clientSecret) throw new Error("Missing clientSecret property");
super(options, verify); // Call the parent constructor
this.name = "discord";
this._oauth2.useAuthorizationHeaderforGET(true);
}
/**
* Retrieve user profile from Discord.
* @param {String} accessToken - The access token used to authenticate the request.
* @param {Function} done - The callback function to call with the result.
*/
userProfile(accessToken, done) {
this.getUser(accessToken, (err, result) => {
if (err) {
return done(err, null);
}
let profile = result;
//Allowed Sizes 16, 32, 64, 128, 256, 512, 1024, 2048, 4096
profile.avtarUrl = `https://cdn.discordapp.com/avatars/${profile.id}/${profile.avatar}`;
profile.connection = this.getConnection.bind(this, profile, accessToken);
profile.guilds = this.getGuilds.bind(this, profile, accessToken);
profile.clean = this.clean.bind(this, profile);
profile.guildJoiner = this.guildJoiner.bind(this, profile, accessToken);
profile.complexResolver = this._oauth2._request;
profile.resolver = (key, api, accessToken, done) => {
this.resolveApi(api, accessToken, (err, data) => {
if (err) {
return done(err);
}
profile[key] = data; // Store the resolved data in profile under the specified key
return done(null, profile);
});
};
return done(null, profile);
});
}
/**
* Retrieve user information from the Discord API.
* @param {String} accessToken - The access token used to authenticate the request.
* @param {Function} done - The callback function to call with the result.
*/
getUser(accessToken, done) {
return this.resolveApi("users/@me", accessToken, done);
}
/**
* Retrieve user connections from the Discord API.
* @param {Object} profile - The user profile object.
* @param {String} accessToken - The access token used to authenticate the request.
* @param {Function} done - The callback function to call with the result.
*/
getConnection(profile, accessToken, done) {
if (!this._scope || !this._scope.includes("connections"))
return done(new Error("Missing Scope, 'connections'"), null);
this.resolveApi(
"users/@me/connections",
accessToken,
(err, connections) => {
if (err) {
return done(err);
}
profile.connection = connections; // Store connections in profile
return done(null, profile);
}
);
}
getGuilds(profile, accessToken, done) {
if (!this._scope || !this._scope.includes("guilds"))
return done(new Error("Missing Scope, 'guilds'"));
this.resolveApi("users/@me/guilds", accessToken, (err, guild) => {
if (err) {
return done(err);
}
profile.guilds = guild; // Store connections in profile
return done(null, profile);
});
}
guildJoiner(profile, accessToken, botToken, serverId, nick, roles, done) {
/*
nick string value to set user's nickname to MANAGE_NICKNAMES
roles array of snowflakes array of role ids the member is assigned MANAGE_ROLES
mute boolean whether the user is muted in voice channels MUTE_MEMBERS
deaf boolean whether the user is deafened in voice channels DEAFEN_MEMBERS
*/
if (!this._scope || !this._scope.includes("guilds.join"))
return done(new Error("Missing Scope, 'guilds.join'"));
// can't use resolve api, as it is only support for get.
//gotta use inter _request api.
var _default = {
access_token: accessToken,
};
if (nick) _default.nick = nick;
if (roles) _default.roles = roles;
// Mute and deaf don't seem to have any practical use, so they have been skipped.
// If you want to use it, just copy this code, modify and pass to complexResolver.
return this._oauth2._request(
"PUT",
API_BASE + `guilds/${serverId}/members/${profile?.id}`,
{
Authorization: `Bot ${botToken}`,
"content-type": "application/json",
},
JSON.stringify(_default),
null,
(err, result, res) => {
if (err) done(err);
if (res.statusCode === 201 || res.statusCode === 204) return done();
}
);
}
/**
* Clean the user object by removing functions.
* @param {Object} profile - The user object to clean.
* @returns {Function} done - The cleaned user object.
*/
clean(profile, done) {
Object.keys(profile).forEach((key) => {
if (profile[key] instanceof Function) {
delete profile[key];
}
});
return done();
}
/**
* Resolve the API request and parse the result.
* @param {String} api - The API endpoint.
* @param {String} accessToken - The access token used to authenticate the request.
* @param {Function} cb - The callback function to call with the result.
*/
resolveApi(api, accessToken, cb) {
this._oauth2.get(API_BASE + api, accessToken, (err, result) => {
this._oauth2.get;
if (err) {
return cb(new InternalOAuthError("Failed to resolve API", err));
}
try {
const parsedData = JSON.parse(result);
return cb(null, parsedData);
} catch (e) {
return cb(new Error("Failed to parse the user profile."), null);
}
});
}
/**
* Authenticate the request.
* @param {Object} req - The request object.
* @param {Object} options - Authentication options.
*/
authenticate(req, options) {
options = options || {};
var self = this;
if (req.query && req.query.error) {
if (req.query.error == "access_denied") {
return this.fail({ message: req.query.error_description });
} else {
return this.error(
new AuthorizationError(
req.query.error_description,
req.query.error,
req.query.error_uri
)
);
}
}
var callbackURL = options.callbackURL || this._callbackURL;
if (callbackURL) {
var parsed = url.parse(callbackURL);
if (!parsed.protocol) {
// The callback URL is relative, resolve a fully qualified URL from the
// URL of the originating request.
callbackURL = url.resolve(
utils.originalURL(req, { proxy: this._trustProxy }),
callbackURL
);
}
}
var meta = {
authorizationURL: this._oauth2._authorizeUrl,
tokenURL: this._oauth2._accessTokenUrl,
clientID: this._oauth2._clientId,
callbackURL: callbackURL,
};
if ((req.query && req.query.code) || (req.body && req.body.code)) {
function loaded(err, ok, state) {
if (err) {
return self.error(err);
}
if (!ok) {
return self.fail(state, 403);
}
var code = (req.query && req.query.code) || (req.body && req.body.code);
var params = self.tokenParams(options);
params.grant_type = "authorization_code";
if (callbackURL) {
params.redirect_uri = callbackURL;
}
if (typeof ok == "string") {
// PKCE
params.code_verifier = ok;
}
self._oauth2.getOAuthAccessToken(
code,
params,
function (err, accessToken, refreshToken, params) {
if (err) {
return self.error(
self._createOAuthError("Failed to obtain access token", err)
);
}
if (!accessToken) {
return self.error(new Error("Failed to obtain access token"));
}
self._loadUserProfile(accessToken, function (err, profile) {
if (err) {
return self.error(err);
}
function verified(err, user, info) {
//if cleaner wasn't called this will make sure to clean, while storing in session
Object.keys(user).forEach((key) => {
if (user[key] instanceof Function) {
delete user[key];
}
});
if (err) {
return self.error(err);
}
if (!user) {
return self.fail(info);
}
info = info || {};
if (state) {
info.state = state;
}
self.success(user, info);
}
try {
if (self._passReqToCallback) {
var arity = self._verify.length;
if (arity == 6) {
self._verify(
req,
accessToken,
refreshToken,
params,
profile,
verified
);
} else {
// arity == 5
self._verify(
req,
accessToken,
refreshToken,
profile,
verified
);
}
} else {
var arity = self._verify.length;
if (arity == 5) {
self._verify(
accessToken,
refreshToken,
params,
profile,
verified
);
} else {
// arity == 4
self._verify(accessToken, refreshToken, profile, verified);
}
}
} catch (ex) {
return self.error(ex);
}
});
}
);
}
var state =
(req.query && req.query.state) || (req.body && req.body.state);
try {
var arity = this._stateStore.verify.length;
if (arity == 4) {
this._stateStore.verify(req, state, meta, loaded);
} else {
// arity == 3
this._stateStore.verify(req, state, loaded);
}
} catch (ex) {
return this.error(ex);
}
} else {
var params = this.authorizationParams(options);
params.response_type = "code";
if (callbackURL) {
params.redirect_uri = callbackURL;
}
var scope = options.scope || this._scope;
if (scope) {
if (Array.isArray(scope)) {
scope = scope.join(this._scopeSeparator);
}
params.scope = scope;
}
var verifier, challenge;
if (this._pkceMethod) {
verifier = base64url(crypto.pseudoRandomBytes(32));
switch (this._pkceMethod) {
case "plain":
challenge = verifier;
break;
case "S256":
challenge = base64url(
crypto.createHash("sha256").update(verifier).digest()
);
break;
default:
return this.error(
new Error(
"Unsupported code verifier transformation method: " +
this._pkceMethod
)
);
}
params.code_challenge = challenge;
params.code_challenge_method = this._pkceMethod;
if (this._stateStore) {
var state = utils.uid(24);
params.state = state;
this._stateStore.save(req, state, verifier, meta, function (err) {
if (err) {
return this.error(err);
}
this.redirect(self._oauth2.getAuthorizeUrl(params));
});
} else {
this.redirect(self._oauth2.getAuthorizeUrl(params));
}
} else {
this.redirect(self._oauth2.getAuthorizeUrl(params));
}
}
}
}
module.exports = Strategy;