-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathexecute.js
290 lines (272 loc) · 11.8 KB
/
execute.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
const debug = true;
const hardcodedClientId = 'gp762nuuoqcoxypju8c569th9wz7q5';
const hardcodedRefreshToken = 'w7lwaxc73n49y6etcp2w3o4uipbvcudiqse6zngiybmkstawl9'; // this refresh token with no scopes maps to user 'username_userid_ext', a dedicated user made for use with this extension
var storage = chrome.storage.local;
// getUserId(string, function(string), function(string))
function getUserIdV2(username, callbackSuccess, callbackFailure) {
getCredentials(
function(credentials) {
dbg("getUserIdV2", "credentials acquired, getting users");
helixGetUsers(
credentials,
function(userid) {
// GetUsers success, update UI
dbg("getUserIdV2", "userid acquired, calling back to UI");
callbackSuccess(userid);
},
function(message) {
// GetUsers failed, update UI
dbg("getUserIdV2", "userid failed, calling back to UI");
callbackFailure(message);
},
username
);
},
function(message) {
// getting credentials failed, update UI
dbg("getUserIdV2", "credentials failed, calling back to UI");
callbackFailure(message);
}
);
}
// getUsername(string, function(string), function(string))
function getUsernameV2(userid, callbackSuccess, callbackFailure) {
getCredentials(
function(credentials) {
dbg("getUsernameV2", "credentials acquired, getting users");
helixGetUsers(
credentials,
function(username) {
// GetUsers success, update UI
dbg("getUsernameV2", "username acquired, calling back to UI");
callbackSuccess(username);
},
function(message) {
// GetUsers failed, update UI
dbg("getUsernameV2", "username failed, calling back to UI");
callbackFailure(message);
},
undefined,
userid
);
},
function(message) {
// getting credentials failed, update UI
dbg("getUsernameV2", "credentials failed, calling back to UI");
callbackFailure(message);
}
);
}
function helixGetUsers(credentials, callbackSuccess, callbackFailure, username = undefined, userid = undefined, isRetry = false) {
var getUsersUrl = 'https://api.twitch.tv/helix/users';
if(username == undefined) {
getUsersUrl += '?id=' + userid;
} else {
getUsersUrl += '?login=' + username;
}
dbg("helixGetUsers", getUsersUrl);
let req = new XMLHttpRequest();
req.onreadystatechange = function () {
if (req.readyState === 4 && req.status === 200) {
let json = JSON.parse(req.responseText);
if (json['data'].length > 0) {
if(username == undefined) {
// return the username
dbg("helixGetUsers", "get username success: " + json);
cacheCredentials(credentials);
callbackSuccess(json['data'][0]['login']);
} else {
// return the userid
dbg("helixGetUsers", "get user id success: " + json);
cacheCredentials(credentials);
callbackSuccess(json['data'][0]['id']);
}
} else {
dbg("helixGetUsers", "user not found");
cacheCredentials(credentials);
callbackFailure("user not found")
}
} else if (req.readyState === 4) {
dbg("helixGetUsers", "get users request failed: " + req.responseText);
if(isRetry) {
// do not retry request
callbackFailure("get users request failed");
} else {
// retry request with forced new credentials
getCredentials(
function(newCredentials) {
helixGetUsers(newCredentials, callbackSuccess, callbackFailure, username, userid, true);
},
function() {
callbackFailure("get users request failed [retry]");
}, true);
}
}
};
req.open("GET", getUsersUrl, true);
req.setRequestHeader("Authorization", "Bearer " + credentials['token']);
req.setRequestHeader("Client-Id", credentials['client_id']);
req.send(null);
}
function cacheCredentials(credentials) {
storage.set({'client_id': credentials['client_id']});
storage.set({'access_token': credentials['token']});
}
function getCredentials(callbackSuccess, callbackFailure, forceReauth = false) {
if(!forceReauth) {
dbg("getCredentials", "checking for cached credentials");
storage.get(["client_id", "access_token"], function(results) {
if(results !== undefined && results.access_token !== undefined && results.client_id !== undefined) {
dbg("getCredentials", "cached credentials discovered!");
if(debug) {
console.log(results);
}
callbackSuccess({client_id: results.client_id, token: results.access_token});
return;
} else {
dbg("getCredentials", "one or more cached values were undefined, forcing refresh");
getFreshCredentials(function(credentials) { callbackSuccess(credentials); }, function(message) { callbackFailure(message); });
}
});
} else {
dbg("getCredentials", "forcing reauth");
getFreshCredentials(function(credentials) { callbackSuccess(credentials); }, function(message) { callbackFailure(message); });
}
}
function getFreshCredentials(callbackSuccess, callbackFailure) {
storage.get('mode', function (result) {
var mode = "user";
if(result != undefined && result.mode != undefined) {
mode = result.mode;
}
dbg("getCredentials", "mode: " + mode);
switch(mode) {
case "app":
getCredentialsFromCustomApp(
function(credentials) {
// credentials call successful, return array(client_id, token)
dbg("getCredentials", "app success: " + credentials);
callbackSuccess(credentials);
},
function(message) {
// credentails call failed, return string("error message")
dbg("getCredentials", "app failure: " + message);
callbackFailure(message);
}
);
break;
case "user":
default:
getCredentialsFromUser(
function(credentials) {
// credentials call successful, return array(client_id, token)
dbg("getCredentials", "user success: " + credentials);
callbackSuccess(credentials);
},
function(message) {
// credentails call failed, return string("error message")
dbg("getCredentials", "user failure: " + message);
callbackFailure(message);
}
);
break;
}
});
}
function getCredentialsFromUser(callbackSuccess, callbackFailure) {
let req = new XMLHttpRequest();
req.onreadystatechange = function () {
if (req.readyState === 4 && req.status === 200) {
let json = JSON.parse(req.responseText);
if(json['success'] == true) {
// twitchtokengenerator's API indicates request success via 'success', not status codes
dbg("getCredentialsFromUser", "success: " + json);
callbackSuccess({client_id: hardcodedClientId, token: json['token']});
} else {
dbg("getCredentialsFromUser", "error getting access token: " + json["message"]);
callbackFailure("error getting access token: " + json['message']);
}
} else if (req.readyState === 4) {
// this would indicate a server side error on twitchtokengenerator's side
dbg("getCredentialsFromUser", "failed to fetch user credentials");
callbackFailure("failed to fetch user credentials");
}
};
var url = "https://twitchtokengenerator.com/api/refresh/" + hardcodedRefreshToken;
dbg("getCredentialsFromUser", "url: " + url);
req.open("GET", "https://twitchtokengenerator.com/api/refresh/" + hardcodedRefreshToken, true);
req.send(null);
}
function getCredentialsFromCustomApp(callbackSuccess, callbackFailure) {
getCustomAppStorage(
function(customapp_storage) {
var clientId = customapp_storage['client_id'];
var clientSecret = customapp_storage['client_secret'];
dbg("getCredentialsFromCustomApp", "clientId: " + clientId);
dbg("getCredentialsFromCustomApp", "clientSecret: " + clientSecret);
getCredentialsFromCustomAppCredentials(
clientId,
clientSecret,
function(token) {
dbg("getCredentialsFromCustomApp", "success");
var result = {client_id: clientId, token: token};
dbg("getCredentialsfromCustomApp", result);
callbackSuccess(result);
},
function(message) {
dbg("getCredentialsFromCustomApp", "failed: " + message);
callbackFailure(message);
}
);
},
function(message) {
dbg("getCredentialsFromCustomApp", "failed: " + message);
callbackFailure(message);
}
)
}
function getCredentialsFromCustomAppCredentials(clientId, clientSecret, callbackSuccess, callbackFailure) {
var url = 'https://id.twitch.tv/oauth2/token?client_id=' + clientId + '&client_secret=' + clientSecret + '&grant_type=client_credentials&scope=';
dbg("getCredentialsFromCustomAppCredentials", url);
dbg("getCredentialsFromCustomAppCredentials", "clientId: " + clientId);
let req = new XMLHttpRequest();
req.onreadystatechange = function () {
if (req.readyState === 4 && req.status === 200) {
dbg("getAccessFromCustomAppCredentials", "successful call");
let json = JSON.parse(req.responseText);
dbg("getAccessFromCustomAppCredentials", json);
callbackSuccess(json['access_token']);
} else if (req.readyState === 4) {
dbg("getAccessFromCustomAppCredentials", "app client credential flow failed");
callbackFailure("app client credential flow failed")
}
};
req.open("POST", url, true);
req.send(null);
}
function getCustomAppStorage(callbackSuccess, callbackFailure) {
storage.get('custom_clientid', function (storage_clientid) {
if (storage_clientid != undefined && storage_clientid.custom_clientid != undefined) {
storage.get('custom_clientsecret', function (storage_clientsecret) {
if (storage_clientsecret != undefined && storage_clientsecret.custom_clientsecret != undefined) {
dbg("getCustomAppStorage", "success");
// both async storage get calls succeeded, return array(client_id, client_secret)
var result = { client_id: storage_clientid.custom_clientid, client_secret: storage_clientsecret.custom_clientsecret };
dbg("getCustomAppStorage", result);
callbackSuccess(result);
} else {
dbg("getCustomAppStorage", "failed to get clientsecret from storage");
callbackFailure("failed to get clientsecret from storage");
}
});
} else {
dbg("getCustomAppStorage", "failed to get clientid from storage");
callbackFailure("failed to get clientid from storage");
}
});
}
function dbg(section, msg) {
if(debug) {
console.log("[" + section + "] " + msg);
}
}