|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | 3 | (function() { |
| 4 | + var tokenStorage = { |
| 5 | + get: function($window) { return $window.sessionStorage.getItem('token') }, |
| 6 | + set: function(token, $window) { $window.sessionStorage.setItem('token', token); }, |
| 7 | + clear: function($window) { $window.sessionStorage.removeItem('token'); } |
| 8 | + }; |
| 9 | + |
4 | 10 | function expired(token) { |
5 | 11 | return (token && token.expires_at && new Date(token.expires_at) < new Date()); |
6 | 12 | }; |
7 | 13 | function getSessionToken($window) { |
8 | | - var tokenString = $window.sessionStorage.getItem('token'); |
| 14 | + var tokenString = tokenStorage.get($window); |
9 | 15 | var token = null; |
10 | 16 | if (tokenString && tokenString !== "null" ) { |
11 | 17 | token = JSON.parse(tokenString); |
|
32 | 38 | var token = getTokenFromHashParams(hash); |
33 | 39 | if (token !== null) { |
34 | 40 | setExpiresAt(token); |
35 | | - $window.sessionStorage.setItem('token', JSON.stringify(token)); |
| 41 | + tokenStorage.set(JSON.stringify(token), $window) |
36 | 42 | } |
37 | 43 | return token; |
38 | 44 | } |
|
126 | 132 | return service.token; |
127 | 133 | }; |
128 | 134 | service.destroy = function() { |
| 135 | + tokenStorage.clear($window) |
129 | 136 | $window.sessionStorage.setItem('token', null); |
130 | 137 | service.token = null; |
131 | 138 | }; |
|
308 | 315 | restrict: 'E', |
309 | 316 | replace: true, |
310 | 317 | scope: { |
311 | | - authorizationUrl: '@', // authorization server url |
312 | | - clientId: '@', // client ID |
313 | | - redirectUrl: '@', // uri th auth server should redirect to (cannot contain #) |
314 | | - responseType: '@', // defaults to token |
315 | | - scope: '@', // scopes required (not the Angular scope - the auth server scopes) |
316 | | - state: '@', // state to use for CSRF protection |
317 | | - template: '@', // path to a replace template for the button, defaults to the one supplied by bower |
318 | | - buttonClass: '@', // the class to use for the sign in / out button - defaults to btn btn-primary |
319 | | - signInText: '@', // text for the sign in button |
320 | | - signOutText: '@', // text for the sign out button |
321 | | - signOutUrl: '@', // url on the authorization server for logging out. Local token is deleted even if no URL is given but that will leave user logged in against STS |
322 | | - signOutAppendToken: '@', // defaults to 'false', set to 'true' to append the token to the sign out url |
323 | | - signOutRedirectUrl: '@', // url to redirect to after sign out on the STS has completed |
324 | | - silentTokenRedirectUrl: '@', // url to use for silently renewing access tokens, default behaviour is not to do |
325 | | - nonce: '@?', // nonce value, optional. If unspecified or an empty string and autoGenerateNonce is true then a nonce will be auto-generated |
326 | | - autoGenerateNonce: '=?' // Should a nonce be autogenerated if not supplied. Optional and defaults to true. |
| 318 | + authorizationUrl: '@', // authorization server url |
| 319 | + clientId: '@', // client ID |
| 320 | + redirectUrl: '@', // uri th auth server should redirect to (cannot contain #) |
| 321 | + responseType: '@', // defaults to token |
| 322 | + scope: '@', // scopes required (not the Angular scope - the auth server scopes) |
| 323 | + state: '@', // state to use for CSRF protection |
| 324 | + template: '@', // path to a replace template for the button, defaults to the one supplied by bower |
| 325 | + buttonClass: '@', // the class to use for the sign in / out button - defaults to btn btn-primary |
| 326 | + signInText: '@', // text for the sign in button |
| 327 | + signOutText: '@', // text for the sign out button |
| 328 | + signOutUrl: '@', // url on the authorization server for logging out. Local token is deleted even if no URL is given but that will leave user logged in against STS |
| 329 | + signOutAppendToken: '@', // defaults to 'false', set to 'true' to append the token to the sign out url |
| 330 | + signOutRedirectUrl: '@', // url to redirect to after sign out on the STS has completed |
| 331 | + silentTokenRedirectUrl: '@', // url to use for silently renewing access tokens, default behaviour is not to do |
| 332 | + nonce: '@?', // nonce value, optional. If unspecified or an empty string and autoGenerateNonce is true then a nonce will be auto-generated |
| 333 | + autoGenerateNonce: '=?', // Should a nonce be autogenerated if not supplied. Optional and defaults to true. |
| 334 | + tokenStorageHandler: '=' |
327 | 335 | } |
328 | 336 | }; |
329 | 337 |
|
|
353 | 361 |
|
354 | 362 |
|
355 | 363 | function init() { |
| 364 | + if (scope.tokenStorageHandler) { |
| 365 | + tokenStorage = scope.tokenStorageHandler |
| 366 | + } |
356 | 367 | scope.buttonClass = scope.buttonClass || 'btn btn-primary'; |
357 | 368 | scope.signInText = scope.signInText || 'Sign In'; |
358 | 369 | scope.signOutText = scope.signOutText || 'Sign Out'; |
|
0 commit comments