forked from lynndylanhurley/ng-token-auth
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathng-token-auth.js
1159 lines (948 loc) · 43 KB
/
ng-token-auth.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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS205: Consider reworking code to avoid use of IIFEs
* DS207: Consider shorter variations of null checks
* Full docs: /~https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
if ((typeof module !== 'undefined') && (typeof exports !== 'undefined') && (module.exports === exports)) {
module.exports = 'ng-token-auth';
}
angular.module('ng-token-auth', ['ipCookie'])
.provider('$auth', function() {
const configs = {
default: {
apiUrl: '/api',
signOutUrl: '/auth/sign_out',
emailSignInPath: '/auth/sign_in',
emailRegistrationPath: '/auth',
accountUpdatePath: '/auth',
accountDeletePath: '/auth',
confirmationSuccessUrl() { return window.location.href; },
passwordResetPath: '/auth/password',
passwordUpdatePath: '/auth/password',
passwordResetSuccessUrl() { return window.location.href; },
tokenValidationPath: '/auth/validate_token',
proxyIf() { return false; },
proxyUrl: '/proxy',
validateOnPageLoad: true,
omniauthWindowType: 'sameWindow',
storage: 'cookies',
forceValidateToken: false,
tokenFormat: {
"access-token": "{{ token }}",
"token-type": "Bearer",
client: "{{ clientId }}",
expiry: "{{ expiry }}",
uid: "{{ uid }}"
},
cookieOps: {
path: "/",
expires: 9999,
expirationUnit: 'days',
secure: false
},
// popups are difficult to test. mock this method in testing.
createPopup(url) {
return window.open(url, '_blank', 'closebuttoncaption=Cancel');
},
parseExpiry(headers) {
// convert from ruby time (seconds) to js time (millis)
return (parseInt(headers['expiry'], 10) * 1000) || null;
},
handleLoginResponse(resp) { return resp.data; },
handleAccountUpdateResponse(resp) { return resp.data; },
handleTokenValidationResponse(resp) { return resp.data; },
authProviderPaths: {
github: '/auth/github',
facebook: '/auth/facebook',
google: '/auth/google_oauth2',
apple: '/auth/apple'
}
}
};
let defaultConfigName = "default";
return {
configure(params) {
// user is using multiple concurrent configs (>1 user types).
if (params instanceof Array && params.length) {
// extend each item in array from default settings
for (let i = 0; i < params.length; i++) {
// get the name of the config
const conf = params[i];
let label = null;
for (let k in conf) {
const v = conf[k];
label = k;
// set the first item in array as default config
if (i === 0) { defaultConfigName = label; }
}
// use copy preserve the original default settings object while
// extending each config object
const defaults = angular.copy(configs["default"]);
const fullConfig = {};
fullConfig[label] = angular.extend(defaults, conf[label]);
angular.extend(configs, fullConfig);
}
// remove existng default config
if (defaultConfigName !== "default") { delete configs["default"]; }
// user is extending the single default config
} else if (params instanceof Object) {
angular.extend(configs["default"], params);
// user is doing something wrong
} else {
throw "Invalid argument: ng-token-auth config should be an Array or Object.";
}
return configs;
},
$get: [
'$http',
'$q',
'$location',
'ipCookie',
'$window',
'$timeout',
'$rootScope',
'$interpolate',
'$interval',
($http, $q, $location, ipCookie, $window, $timeout, $rootScope, $interpolate, $interval) => {
return {
header: null,
dfd: null,
user: {},
mustResetPassword: false,
listener: null,
// called once at startup
initialize() {
this.initializeListeners();
this.cancelOmniauthInAppBrowserListeners = (function() {});
return this.addScopeMethods();
},
initializeListeners() {
//@listener = @handlePostMessage.bind(@)
this.listener = angular.bind(this, this.handlePostMessage);
if ($window.addEventListener) {
return $window.addEventListener("message", this.listener, false);
}
},
cancel(reason) {
// cancel any pending timers
if (this.requestCredentialsPollingTimer != null) {
$timeout.cancel(this.requestCredentialsPollingTimer);
}
// cancel inAppBrowser listeners if set
this.cancelOmniauthInAppBrowserListeners();
// reject any pending promises
if (this.dfd != null) {
this.rejectDfd(reason);
}
// nullify timer after reflow
return $timeout((() => { return this.requestCredentialsPollingTimer = null; }), 0);
},
// cancel any pending processes, clean up garbage
destroy() {
this.cancel();
if ($window.removeEventListener) {
return $window.removeEventListener("message", this.listener, false);
}
},
// handle the events broadcast from external auth tabs/popups
handlePostMessage(ev) {
if (ev.data.message === 'deliverCredentials') {
delete ev.data.message;
// check if a new user was registered
const oauthRegistration = ev.data.oauth_registration;
delete ev.data.oauth_registration;
this.handleValidAuth(ev.data, true);
$rootScope.$broadcast('auth:login-success', ev.data);
if (oauthRegistration) {
$rootScope.$broadcast('auth:oauth-registration', ev.data);
}
}
if (ev.data.message === 'authFailure') {
const error = {
reason: 'unauthorized',
errors: [ev.data.error]
};
this.cancel(error);
return $rootScope.$broadcast('auth:login-error', error);
}
},
// make all public API methods available to directives
addScopeMethods() {
// bind global user object to auth user
$rootScope.user = this.user;
// template access to authentication method
$rootScope.authenticate = angular.bind(this, this.authenticate);
// template access to view actions
$rootScope.signOut = angular.bind(this, this.signOut);
$rootScope.destroyAccount = angular.bind(this, this.destroyAccount);
$rootScope.submitRegistration = angular.bind(this, this.submitRegistration);
$rootScope.submitLogin = angular.bind(this, this.submitLogin);
$rootScope.requestPasswordReset = angular.bind(this, this.requestPasswordReset);
$rootScope.updatePassword = angular.bind(this, this.updatePassword);
$rootScope.updateAccount = angular.bind(this, this.updateAccount);
// check to see if user is returning user
if (this.getConfig().validateOnPageLoad) {
return this.validateUser({config: this.getSavedConfig()});
}
},
// register by email. server will send confirmation email
// containing a link to activate the account. the link will
// redirect to this site.
submitRegistration(params, opts) {
if (opts == null) { opts = {}; }
const successUrl = this.getResultOrValue(this.getConfig(opts.config).confirmationSuccessUrl);
angular.extend(params, {
confirm_success_url: successUrl,
config_name: this.getCurrentConfigName(opts.config)
});
const request = $http.post(this.apiUrl(opts.config) + this.getConfig(opts.config).emailRegistrationPath, params);
request
.then(resp => $rootScope.$broadcast('auth:registration-email-success', params)
, resp => $rootScope.$broadcast('auth:registration-email-error', resp.data));
return request;
},
// capture input from user, authenticate serverside
submitLogin(params, opts, httpopts) {
if (opts == null) { opts = {}; }
if (httpopts == null) { httpopts = {}; }
this.initDfd();
$http.post(this.apiUrl(opts.config) + this.getConfig(opts.config).emailSignInPath, params, httpopts)
.then(resp => {
this.setConfigName(opts.config);
const authData = this.getConfig(opts.config).handleLoginResponse(resp.data, this);
this.handleValidAuth(authData);
return $rootScope.$broadcast('auth:login-success', this.user);
}
, resp => {
this.rejectDfd({
reason: 'unauthorized',
errors: ['Invalid credentials']
});
return $rootScope.$broadcast('auth:login-error', resp.data);
});
return this.dfd.promise;
},
// check if user is authenticated
userIsAuthenticated() {
return this.retrieveData('auth_headers') && this.user.signedIn && !this.tokenHasExpired();
},
// request password reset from API
requestPasswordReset(params, opts) {
if (opts == null) { opts = {}; }
const successUrl = this.getResultOrValue(
this.getConfig(opts.config).passwordResetSuccessUrl
);
params.redirect_url = successUrl;
if (opts.config != null) { params.config_name = opts.config; }
const request = $http.post(this.apiUrl(opts.config) + this.getConfig(opts.config).passwordResetPath, params);
request
.then(resp => $rootScope.$broadcast('auth:password-reset-request-success', params)
, resp => $rootScope.$broadcast('auth:password-reset-request-error', resp.data));
return request;
},
// update user password
updatePassword(params) {
const request = $http.put(this.apiUrl() + this.getConfig().passwordUpdatePath, params);
request
.then(resp => {
$rootScope.$broadcast('auth:password-change-success', resp.data);
return this.mustResetPassword = false;
}
, resp => $rootScope.$broadcast('auth:password-change-error', resp.data));
return request;
},
// update user account info
updateAccount(params) {
const request = $http.put(this.apiUrl() + this.getConfig().accountUpdatePath, params);
request
.then(resp => {
const updateResponse = this.getConfig().handleAccountUpdateResponse(resp.data);
const curHeaders = this.retrieveData('auth_headers');
angular.extend(this.user, updateResponse);
// ensure any critical headers (uid + ?) that are returned in
// the update response are updated appropriately in storage
if (curHeaders) {
const newHeaders = {};
const object = this.getConfig().tokenFormat;
for (let key in object) {
const val = object[key];
if (curHeaders[key] && updateResponse[key]) {
newHeaders[key] = updateResponse[key];
}
}
this.setAuthHeaders(newHeaders);
}
return $rootScope.$broadcast('auth:account-update-success', resp.data);
}
, resp => $rootScope.$broadcast('auth:account-update-error', resp.data));
return request;
},
// permanently destroy a user's account.
destroyAccount(params) {
const request = $http.delete(this.apiUrl() + this.getConfig().accountUpdatePath, params);
request
.then(resp => {
this.invalidateTokens();
return $rootScope.$broadcast('auth:account-destroy-success', resp.data);
}
, resp => $rootScope.$broadcast('auth:account-destroy-error', resp.data));
return request;
},
// open external auth provider in separate window, send requests for
// credentials until api auth callback page responds.
authenticate(provider, opts) {
if (opts == null) { opts = {}; }
if (this.dfd == null) {
this.setConfigName(opts.config);
this.initDfd();
this.openAuthWindow(provider, opts);
}
return this.dfd.promise;
},
setConfigName(configName) {
if (configName == null) { configName = defaultConfigName; }
return this.persistData('currentConfigName', configName, configName);
},
// open external window to authentication provider
openAuthWindow(provider, opts) {
const {
omniauthWindowType
} = this.getConfig(opts.config);
const authUrl = this.buildAuthUrl(omniauthWindowType, provider, opts);
if (omniauthWindowType === 'newWindow') {
return this.requestCredentialsViaPostMessage(this.getConfig().createPopup(authUrl));
} else if (omniauthWindowType === 'inAppBrowser') {
return this.requestCredentialsViaExecuteScript(this.getConfig().createPopup(authUrl));
} else if (omniauthWindowType === 'sameWindow') {
return this.visitUrl(authUrl);
} else {
throw 'Unsupported omniauthWindowType "#{omniauthWindowType}"';
}
},
// testing actual redirects is difficult. stub this for testing
visitUrl(url) {
return $window.location.replace(url);
},
buildAuthUrl(omniauthWindowType, provider, opts) {
if (opts == null) { opts = {}; }
let authUrl = this.getConfig(opts.config).apiUrl;
authUrl += this.getConfig(opts.config).authProviderPaths[provider];
authUrl += '?auth_origin_url=' + encodeURIComponent(opts.auth_origin_url || $window.location.href);
const params = angular.extend({}, opts.params || {}, {
omniauth_window_type: omniauthWindowType
});
for (let key in params) {
const val = params[key];
authUrl += '&';
authUrl += encodeURIComponent(key);
authUrl += '=';
authUrl += encodeURIComponent(val);
}
return authUrl;
},
// ping auth window to see if user has completed registration.
// this method is recursively called until:
// 1. user completes authentication
// 2. user fails authentication
// 3. auth window is closed
requestCredentialsViaPostMessage(authWindow) {
// user has closed the external provider's auth window without
// completing login.
if (authWindow.closed) {
return this.handleAuthWindowClose(authWindow);
// still awaiting user input
} else {
authWindow.postMessage("requestCredentials", "*");
return this.requestCredentialsPollingTimer = $timeout((() => this.requestCredentialsViaPostMessage(authWindow)), 500);
}
},
// handle inAppBrowser's executeScript flow
// flow will complete if:
// 1. user completes authentication
// 2. user fails authentication
// 3. inAppBrowser auth window is closed
requestCredentialsViaExecuteScript(authWindow) {
this.cancelOmniauthInAppBrowserListeners();
const handleAuthWindowClose = this.handleAuthWindowClose.bind(this, authWindow);
const handleLoadStop = this.handleLoadStop.bind(this, authWindow);
const handlePostMessage = this.handlePostMessage.bind(this);
authWindow.addEventListener('loadstop', handleLoadStop);
authWindow.addEventListener('exit', handleAuthWindowClose);
authWindow.addEventListener('message', handlePostMessage);
return this.cancelOmniauthInAppBrowserListeners = function() {
authWindow.removeEventListener('loadstop', handleLoadStop);
authWindow.removeEventListener('exit', handleAuthWindowClose);
return authWindow.addEventListener('message', handlePostMessage);
};
},
// responds to inAppBrowser window loads
handleLoadStop(authWindow) {
const _this = this;
// favor InAppBrowser postMessage API if available, otherwise revert to returning directly via
// the executeScript API, which is known to have limitations on payload size
const remoteCode = `\
function performBestTransit() { \
var data = requestCredentials(); \
if (webkit && webkit.messageHandlers && webkit.messageHandlers.cordova_iab) { \
var dataWithDeliverMessage = Object.assign({}, data, { message: 'deliverCredentials' }); \
webkit.messageHandlers.cordova_iab.postMessage(JSON.stringify(dataWithDeliverMessage)); \
return 'postMessageSuccess'; \
} else { \
return data; \
} \
} \
performBestTransit();`;
return authWindow.executeScript({code: remoteCode }, function(response) {
const data = response[0];
if (data === 'postMessageSuccess') {
// the standard issue postHandler will take care of the rest
return authWindow.close();
} else if (data) {
const ev = new Event('message');
ev.data = data;
_this.cancelOmniauthInAppBrowserListeners();
$window.dispatchEvent(ev);
_this.initDfd();
return authWindow.close();
}
});
},
// responds to inAppBrowser window closes
handleAuthWindowClose(authWindow) {
this.cancel({
reason: 'unauthorized',
errors: ['User canceled login']
});
this.cancelOmniauthInAppBrowserListeners;
return $rootScope.$broadcast('auth:window-closed');
},
// this needs to happen after a reflow so that the promise
// can be rejected properly before it is destroyed.
resolveDfd() {
if (!this.dfd) { return; }
this.dfd.resolve(this.user);
return $timeout((() => {
this.dfd = null;
if (!$rootScope.$$phase) { return $rootScope.$digest(); }
}
), 0);
},
// generates query string based on simple or complex object graphs
buildQueryString(param, prefix) {
const str = [];
for (let k in param) {
const v = param[k];
k = prefix ? prefix + "[" + k + "]" : k;
const encoded = angular.isObject(v) ? this.buildQueryString(v, k) : (k) + "=" + encodeURIComponent(v);
str.push(encoded);
}
return str.join("&");
},
// parses raw URL for querystring parameters to account for issues
// with querystring / fragment ordering in angular < 1.4.x
parseLocation(location) {
const locationSubstring = location.substring(1);
const obj = {};
if (locationSubstring) {
const pairs = locationSubstring.split('&');
let pair = undefined;
let i = undefined;
for (i in pairs) {
i = i;
if ((pairs[i] === '') || (typeof pairs[i] === 'function')) {
continue;
}
pair = pairs[i].split('=');
obj[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
}
}
return obj;
},
// this is something that can be returned from 'resolve' methods
// of pages that have restricted access
validateUser(opts) {
if (opts == null) { opts = {}; }
let configName = opts.config;
if (this.dfd == null) {
this.initDfd();
// save trip to API if possible. assume that user is still signed
// in if auth headers are present and token has not expired.
if (this.userIsAuthenticated()) {
// user is still presumably logged in
this.resolveDfd();
} else {
// token querystring is present. user most likely just came from
// registration email link.
const search = $location.search();
// determine querystring params accounting for possible angular parsing issues
const location_parse = this.parseLocation(window.location.search);
const params = Object.keys(search).length===0 ? location_parse : search;
// auth_token matches what is sent with postMessage, but supporting token for
// backwards compatability
const token = params.auth_token || params.token;
if (token !== undefined) {
const clientId = params.client_id;
const {
uid
} = params;
const {
expiry
} = params;
configName = params.config;
// use the configuration that was used in creating
// the confirmation link
this.setConfigName(configName);
// check if redirected from password reset link
this.mustResetPassword = params.reset_password;
// check if redirected from email confirmation link
this.firstTimeLogin = params.account_confirmation_success;
// check if redirected from auth registration
this.oauthRegistration = params.oauth_registration;
// persist these values
this.setAuthHeaders(this.buildAuthHeaders({
token,
clientId,
uid,
expiry
}));
// build url base
let url = ($location.path() || '/');
// strip token-related qs from url to prevent re-use of these params
// on page refresh
['auth_token', 'token', 'client_id', 'uid', 'expiry', 'config', 'reset_password', 'account_confirmation_success', 'oauth_registration'].forEach(prop => delete params[prop]);
// append any remaining params, if any
if (Object.keys(params).length > 0) {
url += '?' + this.buildQueryString(params);
}
// redirect to target url
$location.url(url);
// token cookie is present. user is returning to the site, or
// has refreshed the page.
} else if (this.retrieveData('currentConfigName')) {
configName = this.retrieveData('currentConfigName');
}
// cookie might not be set, but forcing token validation has
// been enabled
if (this.getConfig().forceValidateToken) {
this.validateToken({config: configName});
} else if (!isEmpty(this.retrieveData('auth_headers'))) {
// if token has expired, do not verify token with API
if (this.tokenHasExpired()) {
$rootScope.$broadcast('auth:session-expired');
this.rejectDfd({
reason: 'unauthorized',
errors: ['Session expired.']
});
} else {
// token has been saved in session var, token has not
// expired. must be verified with API.
this.validateToken({config: configName});
}
// new user session. will redirect to login
} else {
this.rejectDfd({
reason: 'unauthorized',
errors: ['No credentials']
});
$rootScope.$broadcast('auth:invalid');
}
}
}
return this.dfd.promise;
},
// confirm that user's auth token is still valid.
validateToken(opts) {
if (opts == null) { opts = {}; }
if (!this.tokenHasExpired()) {
return $http.get(this.apiUrl(opts.config) + this.getConfig(opts.config).tokenValidationPath)
.then(resp => {
const authData = this.getConfig(opts.config).handleTokenValidationResponse(resp.data);
this.handleValidAuth(authData);
// broadcast event for first time login
if (this.firstTimeLogin) {
$rootScope.$broadcast('auth:email-confirmation-success', this.user);
}
if (this.oauthRegistration) {
$rootScope.$broadcast('auth:oauth-registration', this.user);
}
if (this.mustResetPassword) {
$rootScope.$broadcast('auth:password-reset-confirm-success', this.user);
}
return $rootScope.$broadcast('auth:validation-success', this.user);
}
, resp => {
// broadcast event for first time login failure
if (this.firstTimeLogin) {
$rootScope.$broadcast('auth:email-confirmation-error', resp.data);
}
if (this.mustResetPassword) {
$rootScope.$broadcast('auth:password-reset-confirm-error', resp.data);
}
$rootScope.$broadcast('auth:validation-error', resp.data);
// No data is no response, no response is no connection. Token cannot be destroyed if no connection
return this.rejectDfd({
reason: 'unauthorized',
errors: (resp.data != null) ? resp.data.errors : ['Unspecified error']
}
,
resp.status > 0
);
});
} else {
return this.rejectDfd({
reason: 'unauthorized',
errors: ['Expired credentials']
});
}
},
// ensure token has not expired
tokenHasExpired() {
const expiry = this.getExpiry();
const now = new Date().getTime();
return (expiry && (expiry < now));
},
// get expiry by method provided in config
getExpiry() {
return this.getConfig().parseExpiry(this.retrieveData('auth_headers') || {});
},
// this service attempts to cache auth tokens, but sometimes we
// will want to discard saved tokens. examples include:
// 1. login failure
// 2. token validation failure
// 3. user logs out
invalidateTokens() {
// cannot delete user object for scoping reasons. instead, delete
// all keys on object.
for (let key in this.user) { const val = this.user[key]; delete this.user[key]; }
// remove any assumptions about current configuration
this.deleteData('currentConfigName');
if (this.timer != null) { $interval.cancel(this.timer); }
// kill cookies, otherwise session will resume on page reload
// setting this value to null will force the validateToken method
// to re-validate credentials with api server when validate is called
return this.deleteData('auth_headers');
},
// destroy auth token on server, destroy user auth credentials
signOut() {
const request = $http.delete(this.apiUrl() + this.getConfig().signOutUrl);
request.then(resp => {
this.invalidateTokens();
return $rootScope.$broadcast('auth:logout-success');
}
, resp => {
this.invalidateTokens();
return $rootScope.$broadcast('auth:logout-error', resp.data);
});
return request;
},
// handle successful authentication
handleValidAuth(user, setHeader) {
// cancel any pending postMessage checks
if (setHeader == null) { setHeader = false; }
if (this.requestCredentialsPollingTimer != null) { $timeout.cancel(this.requestCredentialsPollingTimer); }
// cancel any inAppBrowser listeners
this.cancelOmniauthInAppBrowserListeners();
// must extend existing object for scoping reasons
angular.extend(this.user, user);
// add shortcut to determine user auth status
this.user.signedIn = true;
this.user.configName = this.getCurrentConfigName();
// postMessage will not contain header. must save headers manually.
if (setHeader) {
this.setAuthHeaders(this.buildAuthHeaders({
token: this.user.auth_token,
clientId: this.user.client_id,
uid: this.user.uid,
expiry: this.user.expiry
}));
}
// fulfill promise
return this.resolveDfd();
},
// configure auth token format.
buildAuthHeaders(ctx) {
const headers = {};
const object = this.getConfig().tokenFormat;
for (let key in object) {
const val = object[key];
headers[key] = $interpolate(val)(ctx);
}
return headers;
},
// abstract persistent data store
persistData(key, val, configName) {
if (this.getConfig(configName).storage instanceof Object) {
return this.getConfig(configName).storage.persistData(key, val, this.getConfig(configName));
} else {
switch (this.getConfig(configName).storage) {
case 'localStorage':
return $window.localStorage.setItem(key, JSON.stringify(val));
case 'sessionStorage':
return $window.sessionStorage.setItem(key, JSON.stringify(val));
default:
return ipCookie(key, val, this.getConfig().cookieOps);
}
}
},
// abstract persistent data retrieval
retrieveData(key) {
try {
if (this.getConfig().storage instanceof Object) {
return this.getConfig().storage.retrieveData(key);
} else {
switch (this.getConfig().storage) {
case 'localStorage':
return JSON.parse($window.localStorage.getItem(key));
case 'sessionStorage':
return JSON.parse($window.sessionStorage.getItem(key));
default: return ipCookie(key);
}
}
} catch (e) {
// gracefully handle if JSON parsing
if (e instanceof SyntaxError) {
return undefined;
} else {
throw e;
}
}
},
// abstract persistent data removal
deleteData(key) {
if (this.getConfig().storage instanceof Object) {
this.getConfig().storage.deleteData(key);
}
switch (this.getConfig().storage) {
case 'localStorage':
return $window.localStorage.removeItem(key);
case 'sessionStorage':
return $window.sessionStorage.removeItem(key);
default:
var cookieOps = {path: this.getConfig().cookieOps.path};
if (this.getConfig().cookieOps.domain !== undefined) {
cookieOps.domain = this.getConfig().cookieOps.domain;
}
return ipCookie.remove(key, cookieOps);
}
},
// persist authentication token, client id, uid
setAuthHeaders(h) {
const newHeaders = angular.extend((this.retrieveData('auth_headers') || {}), h);
const result = this.persistData('auth_headers', newHeaders);
const expiry = this.getExpiry();
const now = new Date().getTime();
if (expiry > now) {
if (this.timer != null) { $interval.cancel(this.timer); }
this.timer = $interval((() => {
return this.validateUser({config: this.getSavedConfig()});
}
), (parseInt((expiry - now))), 1);
}
return result;
},
initDfd() {
this.dfd = $q.defer();
return this.dfd.promise.then(angular.noop, angular.noop);
},
// failed login. invalidate auth header and reject promise.
// defered object must be destroyed after reflow.
rejectDfd(reason, invalidateTokens) {
if (invalidateTokens == null) { invalidateTokens = true; }
if (invalidateTokens === true) { this.invalidateTokens(); }
if (this.dfd != null) {
this.dfd.reject(reason);
// must nullify after reflow so promises can be rejected
return $timeout((() => { return this.dfd = null; }), 0);
}
},
// use proxy for IE
apiUrl(configName) {
if (this.getConfig(configName).proxyIf()) {
return this.getConfig(configName).proxyUrl;
} else {
return this.getConfig(configName).apiUrl;
}
},
getConfig(name) {
return configs[this.getCurrentConfigName(name)];
},
// if value is a method, call the method. otherwise return the
// argument itself
getResultOrValue(arg) {
if (typeof(arg) === 'function') {
return arg();
} else {
return arg;
}
},
// a config name will be return in the following order of precedence:
// 1. matches arg
// 2. saved from past authentication
// 3. first available config name
getCurrentConfigName(name) {
return name || this.getSavedConfig();
},
// can't rely on retrieveData because it will cause a recursive loop
// if config hasn't been initialized. instead find first available
// value of 'defaultConfigName'. searches the following places in
// this priority:
// 1. localStorage
// 2. sessionStorage
// 3. cookies
// 4. default (first available config)
getSavedConfig() {
let c = undefined;
const key = 'currentConfigName';
if (this.hasLocalStorage()) {
if (c == null) { c = JSON.parse($window.localStorage.getItem(key)); }
} else if (this.hasSessionStorage()) {
if (c == null) { c = JSON.parse($window.sessionStorage.getItem(key)); }
}
if (c == null) { c = ipCookie(key); }
return c || defaultConfigName;
},
hasSessionStorage() {
if ((this._hasSessionStorage == null)) {
this._hasSessionStorage = false;
// trying to call setItem will
// throw an error if sessionStorage is disabled
try {
$window.sessionStorage.setItem('ng-token-auth-test', 'ng-token-auth-test');
$window.sessionStorage.removeItem('ng-token-auth-test');
this._hasSessionStorage = true;
} catch (error) {}
}