-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauth.js
More file actions
162 lines (135 loc) · 5.3 KB
/
Copy pathauth.js
File metadata and controls
162 lines (135 loc) · 5.3 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
// Authentication URLs
const baseAuthUrl = "https://api.trakt.tv/";
const authUrl = baseAuthUrl + "/oauth/device/code";
const pollUrl = baseAuthUrl + "/oauth/device/token";
const refreshUrl = baseAuthUrl + "/oauth/token";
function getFreshAuth()
{
// Retrieve refreshable auth info from scratch
// Using interactive "device" auth
// Initial Request - get code for user to enter
var options = {
'method': 'post',
'Content-Type': 'application/json',
'payload': {
"client_id": config.clientId,
}
};
var response = UrlFetchApp.fetch(authUrl, options);
// Retrieve values from response
var initResp = JSON.parse(response.getContentText());
const deviceCode = initResp.device_code;
const expiry = initResp.expires_in;
const interval = initResp.interval;
// Show the user where to go & what to enter
Logger.log("Go to %s and enter: %s", initResp.verification_url,
initResp.user_code);
// Poll Request - check if the app is authorised yet
var queryTime = 0;
while (queryTime < expiry)
{
var payload = {
"code": deviceCode,
"client_id": config.clientId,
"client_secret": config.clientSecret,
};
options = {
'method': 'post',
'Content-Type': 'application/json',
'payload': payload,
'muteHttpExceptions': true
};
response = UrlFetchApp.fetch(pollUrl, options);
// Logger.log(response);
// If the response was a success, we're authorised
if (response.getResponseCode() == 200)
{
// Grab the values we're looking for and return them
var newTokens = JSON.parse(response.getContentText());
var authInfo = new Object();
authInfo.accessToken = newTokens.access_token;
authInfo.refreshToken = newTokens.refresh_token;
authInfo.expiry = newTokens.created_at + newTokens.expires_in;
return authInfo;
}
// If the response is anything other than 'pending', something's wrong
else if (response.getResponseCode() != 400)
{
throw "Something went wrong polling for authorisation.";
}
// Let them know we're still waiting
Logger.log("Polling countdown: %s / %s ...", queryTime.toString(),
expiry.toString());
// Sleep until we can make another request
Utilities.sleep(interval * 1000);
queryTime += interval;
}
// Polling time expired without success
// Can't do much without authentication
throw "Polling expired without authorisation.";
}
function refreshAuth(refreshToken)
{
// Refresh auth info with refresh token
// Request refreshed tokens
var payload = {
"refresh_token": refreshToken,
"client_id": config.clientId,
"client_secret": config.clientSecret,
"redirect_uri": "urn:ietf:wg:oauth:2.0:oob",
"grant_type": "refresh_token"
};
var options = {
'method': 'post',
'Content-Type': 'application/json',
'payload': payload
};
var response = UrlFetchApp.fetch(refreshUrl, options);
// Grab the values we're looking for and return them
var newTokens = JSON.parse(response.getContentText());
var authInfo = new Object();
authInfo.accessToken = newTokens.access_token;
authInfo.refreshToken = newTokens.refresh_token;
authInfo.expiry = newTokens.created_at + newTokens.expires_in;
return authInfo;
}
function retrieveAuth()
{
// Retrieve refreshable auth info from user properties store
var userProperties = PropertiesService.getUserProperties();
var authInfo = userProperties.getProperties();
// Check if auth info is there
if (!authInfo.hasOwnProperty("refreshToken") ||
!authInfo.hasOwnProperty("accessToken"))
{
// Fall back to getting fresh auth
Logger.log("No access/refresh token. Running first-run authentication.");
authInfo = getFreshAuth(config);
// Save the new auth info back to the user properties store
userProperties.setProperties(authInfo);
}
// Check if the auth token has expired yet
var now = Date.now() / 1000;
if (now > authInfo.expiry)
{
// Refresh the auth info
Logger.log("Access token expired. Refreshing authentication...");
authInfo = refreshAuth(authInfo.refreshToken);
// Save the new auth info back to the user properties store
userProperties.setProperties(authInfo);
}
// Return an object with the details we need for retrieving data
authInfo.clientId = config.clientId;
return authInfo;
}
function resetAuth()
{
// Wipe refreshable auth info from user properties store
var userProperties = PropertiesService.getUserProperties();
userProperties.deleteProperty("refreshToken").deleteProperty("accessToken");
// Get fresh auth
Logger.log("Access/refresh token deleted. Running first-run authentication again.");
var authInfo = getFreshAuth(config);
// Save the new auth info back to the user properties store
userProperties.setProperties(authInfo);
}