forked from andreassolberg/jso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jso.js
592 lines (465 loc) · 13.5 KB
/
jso.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
(function(exp, $) {
var
config = {},
default_lifetime = 3600,
options = {
"debug": false
},
api_redirect,
Api_default_storage,
api_storage,
internalStates = [];
/*
* ------ SECTION: Utilities
*/
/*
* Returns a random string used for state
*/
var uuid = function() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
}
/**
* A log wrapper, that only logs if logging is turned on in the config
* @param {string} msg Log message
*/
var log = function(msg) {
if (!options.debug) return;
if (!console) return;
if (!console.log) return;
// console.log("LOG(), Arguments", arguments, msg)
if (arguments.length > 1) {
console.log(arguments);
} else {
console.log(msg);
}
}
/**
* Set the global options.
*/
var setOptions = function(opts) {
if (!opts) return;
for(var k in opts) {
if (opts.hasOwnProperty(k)) {
options[k] = opts[k];
}
}
log("Options is set to ", options);
}
/*
* Takes an URL as input and a params object.
* Each property in the params is added to the url as query string parameters
*/
var encodeURL = function(url, params) {
var res = url;
var k, i = 0;
var firstSeparator = (url.indexOf("?") === -1) ? '?' : '&';
for(k in params) {
res += (i++ === 0 ? firstSeparator : '&') + encodeURIComponent(k) + '=' + encodeURIComponent(params[k]);
}
return res;
}
/*
* Returns epoch, seconds since 1970.
* Used for calculation of expire times.
*/
var epoch = function() {
return Math.round(new Date().getTime()/1000.0);
}
var parseQueryString = function (qs) {
var e,
a = /\+/g, // Regex for replacing addition symbol with a space
r = /([^&;=]+)=?([^&;]*)/g,
d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
q = qs,
urlParams = {};
while (e = r.exec(q))
urlParams[d(e[1])] = d(e[2]);
return urlParams;
}
/*
* ------ / SECTION: Utilities
*/
/*
* Redirects the user to a specific URL
*/
api_redirect = function(url) {
window.location = url;
};
Api_default_storage = function() {
log("Constructor");
};
/**
saveState stores an object with an Identifier.
TODO: Ensure that both localstorage and JSON encoding has fallbacks for ancient browsers.
In the state object, we put the request object, plus these parameters:
* restoreHash
* providerID
* scopes
*/
Api_default_storage.prototype.saveState = function (state, obj) {
localStorage.setItem("state-" + state, JSON.stringify(obj));
}
/**
* getStage() returns the state object, but also removes it.
* @type {Object}
*/
Api_default_storage.prototype.getState = function(state) {
// log("getState (" + state+ ")");
var obj = JSON.parse(localStorage.getItem("state-" + state));
localStorage.removeItem("state-" + state)
return obj;
};
/*
* Checks if a token, has includes a specific scope.
* If token has no scope at all, false is returned.
*/
Api_default_storage.prototype.hasScope = function(token, scope) {
var i;
if (!token.scopes) return false;
for(i = 0; i < token.scopes.length; i++) {
if (token.scopes[i] === scope) return true;
}
return false;
};
/*
* Takes an array of tokens, and removes the ones that
* are expired, and the ones that do not meet a scopes requirement.
*/
Api_default_storage.prototype.filterTokens = function(tokens, scopes) {
var i, j,
result = [],
now = epoch(),
usethis;
if (!scopes) scopes = [];
for(i = 0; i < tokens.length; i++) {
usethis = true;
// Filter out expired tokens. Tokens that is expired in 1 second from now.
if (tokens[i].expires && tokens[i].expires < (now+1)) usethis = false;
// Filter out this token if not all scope requirements are met
for(j = 0; j < scopes.length; j++) {
if (!api_storage.hasScope(tokens[i], scopes[j])) usethis = false;
}
if (usethis) result.push(tokens[i]);
}
return result;
};
/*
* saveTokens() stores a list of tokens for a provider.
Usually the tokens stored are a plain Access token plus:
* expires : time that the token expires
* providerID: the provider of the access token?
* scopes: an array with the scopes (not string)
*/
Api_default_storage.prototype.saveTokens = function(provider, tokens) {
// log("Save Tokens (" + provider+ ")");
localStorage.setItem("tokens-" + provider, JSON.stringify(tokens));
};
Api_default_storage.prototype.getTokens = function(provider) {
// log("Get Tokens (" + provider+ ")");
var tokens = JSON.parse(localStorage.getItem("tokens-" + provider));
if (!tokens) tokens = [];
log("Token received", tokens)
return tokens;
};
Api_default_storage.prototype.wipeTokens = function(provider) {
localStorage.removeItem("tokens-" + provider);
};
/*
* Save a single token for a provider.
* This also cleans up expired tokens for the same provider.
*/
Api_default_storage.prototype.saveToken = function(provider, token) {
var tokens = this.getTokens(provider);
tokens = api_storage.filterTokens(tokens);
tokens.push(token);
this.saveTokens(provider, tokens);
};
/*
* Get a token if exists for a provider with a set of scopes.
* The scopes parameter is OPTIONAL.
*/
Api_default_storage.prototype.getToken = function(provider, scopes) {
var tokens = this.getTokens(provider);
tokens = api_storage.filterTokens(tokens, scopes);
if (tokens.length < 1) return null;
return tokens[0];
};
api_storage = new Api_default_storage();
/**
* Check if the hash contains an access token.
* And if it do, extract the state, compare with
* config, and store the access token for later use.
*
* The url parameter is optional. Used with phonegap and
* childbrowser when the jso context is not receiving the response,
* instead the response is received on a child browser.
*/
exp.jso_checkfortoken = function(providerID, url, callback) {
var
atoken,
h = window.location.hash,
now = epoch(),
state,
co;
log("jso_checkfortoken(" + providerID + ")");
// If a url is provided
if (url) {
// log('Hah, I got the url and it ' + url);
if(url.indexOf('#') === -1) return;
h = url.substring(url.indexOf('#'));
// log('Hah, I got the hash and it is ' + h);
}
/*
* Start with checking if there is a token in the hash
*/
if (h.length < 2) return;
if (h.indexOf("access_token") === -1) return;
h = h.substring(1);
atoken = parseQueryString(h);
if (atoken.state) {
state = api_storage.getState(atoken.state);
} else {
if (!providerID) {throw "Could not get [state] and no default providerid is provided.";}
state = {providerID: providerID};
}
if (!state) throw "Could not retrieve state";
if (!state.providerID) throw "Could not get providerid from state";
if (!config[state.providerID]) throw "Could not retrieve config for this provider.";
co = config[state.providerID];
/**
* If state was not provided, and default provider contains a scope parameter
* we assume this is the one requested...
*/
if (!atoken.state && co.scope) {
state.scopes = co.scope;
log("Setting state: ", state);
}
log("Checking atoken ", atoken, " and co ", co);
/*
* Decide when this token should expire.
* Priority fallback:
* 1. Access token expires_in
* 2. Life time in config (may be false = permanent...)
* 3. Specific permanent scope.
* 4. Default library lifetime:
*/
if (atoken["expires_in"]) {
atoken["expires"] = now + parseInt(atoken["expires_in"], 10);
} else if (co["default_lifetime"] === false) {
// Token is permanent.
} else if (co["default_lifetime"]) {
atoken["expires"] = now + co["default_lifetime"];
} else if (co["permanent_scope"]) {
if (!api_storage.hasScope(atoken, co["permanent_scope"])) {
atoken["expires"] = now + default_lifetime;
}
} else {
atoken["expires"] = now + default_lifetime;
}
/*
* Handle scopes for this token
*/
if (atoken["scope"]) {
atoken["scopes"] = atoken["scope"].split(" ");
} else if (state["scopes"]) {
atoken["scopes"] = state["scopes"];
}
api_storage.saveToken(state.providerID, atoken);
if (state.restoreHash) {
window.location.hash = state.restoreHash;
} else {
window.location.hash = '';
}
log(atoken);
if (internalStates[atoken.state] && typeof internalStates[atoken.state] === 'function') {
// log("InternalState is set, calling it now!");
internalStates[atoken.state]();
delete internalStates[atoken.state];
}
if (typeof callback === 'function') {
callback();
}
// log(atoken);
}
/*
* A config object contains:
*/
var jso_authrequest = function(providerid, scopes, callback) {
var
state,
request,
authurl,
co;
if (!config[providerid]) throw "Could not find configuration for provider " + providerid;
co = config[providerid];
log("About to send an authorization request to [" + providerid + "]. Config:")
log(co);
state = uuid();
request = {
"response_type": "token"
};
request.state = state;
if (callback && typeof callback === 'function') {
internalStates[state] = callback;
}
if (co["redirect_uri"]) {
request["redirect_uri"] = co["redirect_uri"];
}
if (co["client_id"]) {
request["client_id"] = co["client_id"];
}
if (scopes) {
request["scope"] = scopes.join(" ");
}
authurl = encodeURL(co.authorization, request);
// We'd like to cache the hash for not loosing Application state.
// With the implciit grant flow, the hash will be replaced with the access
// token when we return after authorization.
if (window.location.hash) {
request["restoreHash"] = window.location.hash;
}
request["providerID"] = providerid;
if (scopes) {
request["scopes"] = scopes;
}
log("Saving state [" + state+ "]");
log(JSON.parse(JSON.stringify(request)));
api_storage.saveState(state, request);
api_redirect(authurl);
};
exp.jso_ensureTokens = function (ensure) {
var providerid, scopes, token;
for(providerid in ensure) {
scopes = undefined;
if (ensure[providerid]) scopes = ensure[providerid];
token = api_storage.getToken(providerid, scopes);
log("Ensure token for provider [" + providerid + "] ");
log(token);
if (token === null) {
jso_authrequest(providerid, scopes);
}
}
return true;
}
exp.jso_findDefaultEntry = function(c) {
var
k,
i = 0;
if (!c) return;
log("c", c);
for(k in c) {
i++;
if (c[k].isDefault && c[k].isDefault === true) {
return k;
}
}
if (i === 1) return k;
};
exp.jso_configure = function(c, opts) {
config = c;
setOptions(opts);
try {
var def = exp.jso_findDefaultEntry(c);
log("jso_configure() about to check for token for this entry", def);
exp.jso_checkfortoken(def);
} catch(e) {
log("Error when retrieving token from hash: " + e);
window.location.hash = "";
}
}
exp.jso_dump = function() {
var key;
for(key in config) {
log("=====> Processing provider [" + key + "]");
log("=] Config");
log(config[key]);
log("=] Tokens")
log(api_storage.getTokens(key));
}
}
exp.jso_wipe = function() {
var key;
log("jso_wipe()");
for(key in config) {
log("Wipping tokens for " + key);
api_storage.wipeTokens(key);
}
}
exp.jso_getToken = function(providerid, scopes) {
var token = api_storage.getToken(providerid, scopes);
if (!token) return null;
if (!token["access_token"]) return null;
return token["access_token"];
}
exp.jso_registerRedirectHandler = function(callback) {
api_redirect = callback;
};
exp.jso_registerStorageHandler = function(object) {
api_storage = object;
};
/*
* From now on, we only perform tasks that require jQuery.
* Like adding the $.oajax function.
*/
if (typeof $ === 'undefined') return;
$.oajax = function(settings) {
var
allowia,
scopes,
token,
providerid,
co;
providerid = settings.jso_provider;
allowia = settings.jso_allowia || false;
scopes = settings.jso_scopes;
token = api_storage.getToken(providerid, scopes);
co = config[providerid];
// var successOverridden = settings.success;
// settings.success = function(response) {
// }
var errorOverridden = settings.error || null;
var performAjax = function() {
// log("Perform ajax!");
if (!token) throw "Could not perform AJAX call because no valid tokens was found.";
if (co["presenttoken"] && co["presenttoken"] === "qs") {
// settings.url += ((h.indexOf("?") === -1) ? '?' : '&') + "access_token=" + encodeURIComponent(token["access_token"]);
if (!settings.data) settings.data = {};
settings.data["access_token"] = token["access_token"];
} else {
if (!settings.headers) settings.headers = {};
settings.headers["Authorization"] = "Bearer " + token["access_token"];
}
$.ajax(settings);
};
settings.error = function(jqXHR, textStatus, errorThrown) {
log('error(jqXHR, textStatus, errorThrown)');
log(jqXHR);
log(textStatus);
log(errorThrown);
if (jqXHR.status === 401) {
log("Token expired. About to delete this token");
log(token);
api_storage.wipeTokens(providerid);
}
if (errorOverridden && typeof errorOverridden === 'function') {
errorOverridden(jqXHR, textStatus, errorThrown);
}
}
if (!token) {
if (allowia) {
log("Perform authrequest");
jso_authrequest(providerid, scopes, function() {
token = api_storage.getToken(providerid, scopes);
performAjax();
});
return;
} else {
throw "Could not perform AJAX call because no valid tokens was found.";
}
}
performAjax();
};
})(window, window.jQuery);