forked from Lichthagel/meteor-accounts-discord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
discord_server.js
169 lines (145 loc) · 4.29 KB
/
discord_server.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
// Adapted from accounts-google package:
// https://github.com/meteor/meteor/blob/bc0a25156b2d717785fea3b8149024ecfce19e39/packages/google-oauth/google_server.js
Discord = {};
var hasOwn = Object.prototype.hasOwnProperty;
var userAgent = 'Meteor';
if (Meteor.release)
userAgent += '/' + Meteor.release;
// https://discordapp.com/developers/docs/resources/user
Discord.whitelistedFields = [
'id',
'username',
'discriminator',
'avatar',
'bot',
'mfa_enabled',
'verified',
'email',
];
function getServiceDataFromTokens(tokens) {
var accessToken = tokens.accessToken;
var token_type = tokens.token_type;
var scope = tokens.scope;
var user = getUser(accessToken);
var serviceData = {
accessToken: accessToken,
token_type: token_type,
scope: scope
};
if (hasOwn.call(tokens, "expiresIn")) {
serviceData.expiresAt =
Date.now() + 1000 * parseInt(tokens.expiresIn, 10);
}
var fields = Object.create(null);
Discord.whitelistedFields.forEach(function (name) {
if (hasOwn.call(user, name)) {
fields[name] = user[name];
}
});
Object.assign(serviceData, fields);
// only set the token in serviceData if it's there. this ensures
// that we don't lose old ones (since we only get this on the first
// log in attempt)
if (tokens.refreshToken) {
serviceData.refreshToken = tokens.refreshToken;
}
return {
serviceData: serviceData,
options: {
profile: {
name: user.name
}
}
};
}
Accounts.registerLoginHandler(function (request) {
console.log('discord registerLoginHandler');
console.log(request);
if (request.discordSignIn !== true) {
return;
}
const tokens = {
accessToken: request.accessToken,
refreshToken: request.refreshToken,
};
if (request.serverAuthCode) {
Object.assign(tokens, getTokens({
code: request.serverAuthCode
}));
}
const result = getServiceDataFromTokens(tokens);
return Accounts.updateOrCreateUserFromExternalService("discord", {
id: request.userId,
accessToken: request.accessToken,
email: request.email,
picture: request.imageUrl,
...result.serviceData,
}, result.options);
});
// returns an object containing:
// - accessToken
// - token_type: (typically 'bearer')
// - scope: a space delimited list of scopes granted
// - expiresIn: lifetime of token in seconds
// - refreshToken, if this is the first authorization request
var getTokens = function (query) {
var config = ServiceConfiguration.configurations.findOne({service: 'discord'});
if (!config)
throw new ServiceConfiguration.ConfigError();
var response;
try {
response = HTTP.post(
'https://discordapp.com/api/oauth2/token', {
headers: {
Accept: 'application/json',
'User-Agent': userAgent
},
params: {
grant_type: 'authorization_code',
code: query.code,
client_id: config.clientId,
client_secret: OAuth.openSecret(config.secret),
redirect_uri: OAuth._redirectUri('discord', config),
state: query.state,
}
});
} catch (err) {
throw Object.assign(
new Error("Failed to complete OAuth handshake with Discord. " + err.message),
{ response: err.response }
);
}
if (response.data.error) { // if the http response was a json object with an error attribute
throw new Error("Failed to complete OAuth handshake with Discord. " + response.data.error);
} else {
return {
accessToken: response.data.access_token,
token_type: response.data.token_type,
refreshToken: response.data.refresh_token,
expiresIn: response.data.expires_in,
scope: response.data.scope,
};
}
};
function getServiceData(query) {
return getServiceDataFromTokens(getTokens(query));
}
OAuth.registerService('discord', 2, null, getServiceData);
var getUser = function (accessToken) {
try {
return HTTP.get(
'https://discordapp.com/api/users/@me', {
headers: {
'User-Agent': userAgent,
'Authorization': 'Bearer ' + accessToken
}
}).data;
} catch (err) {
throw Object.assign(
new Error('Failed to fetch user from Discord. ' + err.message),
{ response: err.response });
}
};
Discord.retrieveCredential = function(credentialToken, credentialSecret) {
return OAuth.retrieveCredential(credentialToken, credentialSecret);
};