-
Notifications
You must be signed in to change notification settings - Fork 6
/
oauth.js
328 lines (284 loc) · 10.3 KB
/
oauth.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
var JSONAPIClient = require('./json-api-client');
var Model = JSONAPIClient.Model;
var makeCredentialHTTPRequest = JSONAPIClient.makeCredentialHTTPRequest;
var ls = require('local-storage');
var config = require('./config');
var apiClient = require('./api-client');
var getCSRFToken = require('./csrf-token');
// We don't want to wait until the token is already expired before refreshing it.
var TOKEN_EXPIRATION_ALLOWANCE = 5 * 60 * 1000;
// Save local storage stuff as something totally obvious
var LOCAL_STORAGE_PREFIX = 'panoptesClientOAuth_';
// Specify whether to use local or session storage for session data.
var SESSION_STORAGE = window.localStorage;
// Create our model, then do stuff with it later
const authClient = new Model({
_bearerRefreshTimeout: NaN,
_clientAppId: ls.get(LOCAL_STORAGE_PREFIX + 'clientAppId'),
_currentSessionCheckPromise: null,
_currentUserPromise: null,
_tokenDetails: null,
_customRedirects: false,
checkBearerToken: function() {
var awaitBearerToken;
if (this._bearerTokenWillExpire()) {
awaitBearerToken = this._refreshBearerToken();
} else {
var tokenDetails = JSON.parse(SESSION_STORAGE.getItem(LOCAL_STORAGE_PREFIX + 'tokenDetails'));
awaitBearerToken = Promise.resolve(tokenDetails);
}
return awaitBearerToken;
},
checkCurrent: function() {
console.log('Checking current user');
// If we're checking for an existing session already, defer this until
// it's finished
var initialCheck = this._currentSessionCheckPromise || Promise.resolve();
if (!this._currentUserPromise) {
this.update({
_currentUserPromise: initialCheck
.then(this._getSession)
.catch(function() {
// Nobody's signed in. This isn't an error.
console.info('No current user');
return null;
}),
});
}
return this._currentUserPromise;
},
init: function (appID, options) {
return new Promise(function(resolve, reject) {
// Don't init if we're in an iFrame, as we could be the token refresh process
if (window.frameElement) {
return false;
}
console.log('Using OAuth (implicit grant) for login');
console.info('Setting app ID to', appID);
ls.get(LOCAL_STORAGE_PREFIX + 'clientAppId', appID);
this._clientAppId = appID;
var initializationOptions = options || {};
if (initializationOptions.customRedirects) this._customRedirects = initializationOptions.customRedirects;
// Handle new token details if we've completed a sign in
if (checkUrlForToken(window.location.hash)) {
console.log('Token found in URL');
var tokenDetails = tokenFromLocation(window.location);
this._handleNewBearerToken(tokenDetails);
if (!this._customRedirects) {
// And redirect to the desired page
var url = ls.get(LOCAL_STORAGE_PREFIX + 'redirectUri');
location.assign(url);
}
}
// If not, let's try and pick up an existing Panoptes session anyway
this.checkBearerToken()
.then(function (tokenDetails) {
this._handleNewBearerToken(tokenDetails);
resolve(tokenDetails);
}.bind(this))
.catch(function (error) {
// We probably haven't signed in before
console.info(error);
resolve(null);
});
}.bind(this));
},
signIn: function(redirectUri) {
console.log('Signing in with OAuth');
var originalArguments = arguments;
return this.checkCurrent()
.then(function (token) {
if (token) {
return this.signOut().then(function() {
return this.signIn.apply(this, originalArguments);
}.bind(this));
} else {
this._saveRedirectUri(redirectUri);
location.assign(this._createOAuthUrl(redirectUri));
}
}.bind(this));
},
async signOut() {
console.log('Signing out');
const user = await this.checkCurrent();
if (user) {
const token = await getCSRFToken(config.oauthHost);
const url = config.oauthHost + '/users/sign_out';
const tokenDetails = await this.checkBearerToken();
const deleteHeaders = {
...config.jsonHeaders,
['X-CSRF-Token']: token
};
if (tokenDetails && tokenDetails.access_token) {
deleteHeaders['Authorization'] = 'Bearer ' + tokenDetails.access_token
}
try {
makeCredentialHTTPRequest('DELETE', url, null, deleteHeaders)
this._deleteBearerToken();
this.update({
_currentUserPromise: Promise.resolve(null),
});
console.info('Signed out');
return null;
} catch (error) {
console.error('Failed to sign out');
return apiClient.handleError(error);
}
} else {
throw new Error('Failed to sign out; not signed in');
}
},
_createOAuthUrl: function(redirectUri) {
if (!this._clientAppId) {
throw new Error('Client app ID not set');
}
return [
config.oauthHost,
'/oauth/authorize',
'?response_type=token',
'&client_id=',
this._clientAppId,
'&redirect_uri=',
redirectUri
].join('');
},
_bearerTokenWillExpire: function() {
var tokenDetails = JSON.parse(SESSION_STORAGE.getItem(LOCAL_STORAGE_PREFIX + 'tokenDetails'));
if (tokenDetails) {
return Date.now() >= tokenDetails.expires_at - TOKEN_EXPIRATION_ALLOWANCE;
} else {
return false;
}
},
_getNewToken: function() {
var redirectUri = ls.get(LOCAL_STORAGE_PREFIX + 'redirectUri');
this.update({
_currentSessionCheckPromise: new Promise(function(resolve, reject) {
if (!redirectUri) {
reject(Error('No redirect URI found'));
}
// Create a new iFrame
var url = this._createOAuthUrl(redirectUri);
this._iframe = createIFrame(url);
// Try and get the token details from our iFrame. If it throws an error,
// it's because we're being redirected to the signin page (and therefore
// there is no session) - so we should also replace the security error
// with a more relevant one.
this._iframe.onload = function() {
try {
var iframeLocation = this._iframe.contentWindow.location;
if (checkUrlForToken(iframeLocation.hash)) {
console.info('Found existing Panoptes session');
var newTokenDetails = tokenFromLocation(iframeLocation);
resolve(newTokenDetails);
} else {
throw new TypeError('Valid OAuth details not found in URL');
}
} catch (error) {
if (error.name === 'SecurityError') {
console.warn('No existing Panoptes session found');
}
reject(error);
} finally {
this._iframe = destroyIFrame(this._iframe);
}
}.bind(this);
}.bind(this))
});
return this._currentSessionCheckPromise;
},
_deleteBearerToken: function() {
console.log('Deleting bearer token');
this._tokenDetails = null;
delete apiClient.headers.Authorization;
SESSION_STORAGE.removeItem(LOCAL_STORAGE_PREFIX + 'tokenDetails');
},
_getSession: function() {
console.log('Getting session');
return apiClient.get('/me')
.then(function(users) {
var user = users[0];
console.info('Got session', user.login, user.id);
return user;
})
.catch(function(error) {
console.error('Failed to get session');
throw error;
});
},
_handleExpiredToken: function() {
var tokenDetails = JSON.parse(SESSION_STORAGE.getItem(LOCAL_STORAGE_PREFIX + 'tokenDetails'));
var tokenHasExpired = false;
if (tokenDetails && tokenDetails.expires_at) {
tokenHasExpired = Date.now() > tokenDetails.expires_at;
}
if (tokenHasExpired) {
console.info('Panoptes session has expired');
this._deleteBearerToken();
}
},
_handleNewBearerToken: function(tokenDetails) {
if (tokenDetails && tokenDetails.access_token) {
console.log('Got new bearer token', tokenDetails.access_token.slice(-6));
this._tokenDetails = tokenDetails;
apiClient.headers.Authorization = 'Bearer ' + tokenDetails.access_token;
var refresh = this._refreshBearerToken.bind(this);
var timeToRefresh = (tokenDetails.expires_in * 1000) - TOKEN_EXPIRATION_ALLOWANCE;
this._bearerRefreshTimeout = setTimeout(refresh, timeToRefresh);
tokenDetails.expires_at = Date.now() + (tokenDetails.expires_in * 1000);
SESSION_STORAGE.setItem(LOCAL_STORAGE_PREFIX + 'tokenDetails', JSON.stringify(tokenDetails));
}
return tokenDetails;
},
_refreshBearerToken: function() {
return this._getNewToken()
.then(function(tokenDetails) {
return this._handleNewBearerToken(tokenDetails);
}.bind(this))
.catch(function (error) {
console.log(error);
this._handleExpiredToken();
return null;
}.bind(this));
},
_saveRedirectUri: function(redirectUri) {
console.info('Saving redirectUri:', redirectUri);
ls.set(LOCAL_STORAGE_PREFIX + 'redirectUri', redirectUri);
}
});
/*******************************************
Utility functions
*******************************************/
function checkUrlForToken(string) {
return string.indexOf('access_token') !== -1 &&
/token_type=(B|b)earer/.test(string);
}
function tokenFromLocation(loc) {
var params = loc.hash.slice(1).split('&');
var tokenDetails = {};
params.forEach(function(paramString) {
var param = paramString.split('=');
tokenDetails[param[0]] = param[1];
});
return tokenDetails;
}
function createIFrame(url) {
var iframe = document.createElement('iframe');
iframe.setAttribute('src', url);
iframe.setAttribute('style', 'display: none;');
document.body.appendChild(iframe);
return iframe;
}
function destroyIFrame(iframe) {
iframe.parentNode.removeChild(iframe);
return null;
}
module.exports = {
checkCurrent: authClient.checkCurrent.bind(authClient),
checkBearerToken: authClient.checkBearerToken.bind(authClient),
init: authClient.init.bind(authClient),
listen: authClient.listen.bind(authClient),
stopListening: authClient.stopListening.bind(authClient),
signIn: authClient.signIn.bind(authClient),
signOut: authClient.signOut.bind(authClient)
}