13
13
root . angularOAuth2 = factory ( root . angular , root . queryString ) ;
14
14
}
15
15
} ) ( this , function ( angular , queryString ) {
16
- var ngModule = angular . module ( "angular-oauth2" , [ "ipCookie" ] ) . config ( oauthConfig ) . factory ( "oauthInterceptor" , oauthInterceptor ) . provider ( "OAuth" , OAuthProvider ) . provider ( "OAuthToken" , OAuthTokenProvider ) ;
17
- function oauthConfig ( $httpProvider ) {
18
- $httpProvider . interceptors . push ( "oauthInterceptor" ) ;
19
- }
20
- oauthConfig . $inject = [ "$httpProvider" ] ;
16
+ var ngModule = angular . module ( "angular-oauth2" , [ "ngStorage" , "ngCookies" ] ) . config ( oauthConfig ) . factory ( "oauthInterceptor" , oauthInterceptor ) . provider ( "OAuth" , OAuthProvider ) . factory ( "OAuthStorageService" , OAuthStorageService ) . provider ( "OAuthToken" , OAuthTokenProvider ) ;
21
17
function oauthInterceptor ( $q , $rootScope , OAuthToken ) {
22
18
return {
23
19
request : function ( config ) {
179
175
if ( instanceProps ) Object . defineProperties ( child . prototype , instanceProps ) ;
180
176
} ;
181
177
function OAuthTokenProvider ( ) {
182
- var storage ;
183
178
var config = {
184
179
name : "token" ,
185
180
storage : "cookies" ,
194
189
angular . extend ( config , params ) ;
195
190
return config ;
196
191
} ;
197
- this . $get = function ( ipCookie , $window ) {
192
+ this . $get = function ( OAuthStorageService ) {
198
193
var OAuthToken = function ( ) {
199
- function OAuthToken ( ) { }
194
+ function OAuthToken ( ) {
195
+ OAuthStorageService . configure ( config ) ;
196
+ }
200
197
_prototypeProperties ( OAuthToken , null , {
201
198
token : {
202
199
set : function ( data ) {
203
- return setToken ( data ) ;
200
+ return OAuthStorageService . setToken ( data ) ;
204
201
} ,
205
202
get : function ( ) {
206
- return getToken ( ) ;
203
+ return OAuthStorageService . getToken ( ) ;
207
204
} ,
208
205
enumerable : true ,
209
206
configurable : true
244
241
configurable : true
245
242
} ,
246
243
removeToken : {
247
- value : function ( _removeToken ) {
248
- var _removeTokenWrapper = function removeToken ( ) {
249
- return _removeToken . apply ( this , arguments ) ;
250
- } ;
251
- _removeTokenWrapper . toString = function ( ) {
252
- return _removeToken . toString ( ) ;
253
- } ;
254
- return _removeTokenWrapper ;
255
- } ( function ( ) {
256
- return removeToken ( ) ;
257
- } ) ,
244
+ value : function removeToken ( ) {
245
+ return OAuthStorageService . removeToken ( ) ;
246
+ } ,
258
247
writable : true ,
259
248
enumerable : true ,
260
249
configurable : true
261
250
}
262
251
} ) ;
263
252
return OAuthToken ;
264
253
} ( ) ;
265
- var setToken = function ( data ) {
266
- storage = config . storage . toLowerCase ( ) ;
267
- switch ( storage ) {
268
- case "cookies" :
269
- return ipCookie ( config . name , data , config . options ) ;
270
-
271
- case "localstorage" :
272
- return $window . localStorage . setItem ( config . name , angular . toJson ( data ) ) ;
273
-
274
- case "sessionstorage" :
275
- return $window . sessionStorage . setItem ( config . name , angular . toJson ( data ) ) ;
276
-
277
- default :
278
- return ipCookie ( config . name , data , config . options ) ;
279
- }
280
- } ;
281
- var getToken = function ( ) {
282
- storage = config . storage . toLowerCase ( ) ;
283
- switch ( storage ) {
284
- case "cookies" :
285
- return ipCookie ( config . name ) ;
286
-
287
- case "localstorage" :
288
- return angular . fromJson ( $window . localStorage . getItem ( config . name ) ) ;
289
-
290
- case "sessionstorage" :
291
- return angular . fromJson ( $window . sessionStorage . getItem ( config . name ) ) ;
292
-
293
- default :
294
- return ipCookie ( config . name ) ;
295
- }
296
- } ;
297
- var removeToken = function ( ) {
298
- storage = config . storage . toLowerCase ( ) ;
299
- switch ( storage ) {
300
- case "cookies" :
301
- return ipCookie . remove ( config . name , config . options ) ;
302
-
303
- case "localstorage" :
304
- return $window . localStorage . removeItem ( config . name ) ;
305
-
306
- case "sessionstorage" :
307
- return $window . sessionStorage . removeItem ( config . name ) ;
308
-
309
- default :
310
- return ipCookie . remove ( config . name , config . options ) ;
311
- }
312
- } ;
313
254
return new OAuthToken ( ) ;
314
255
} ;
315
- this . $get . $inject = [ "ipCookie" , "$window" ] ;
256
+ this . $get . $inject = [ "OAuthStorageService" ] ;
257
+ }
258
+ function oauthConfig ( $httpProvider ) {
259
+ $httpProvider . interceptors . push ( "oauthInterceptor" ) ;
260
+ }
261
+ oauthConfig . $inject = [ "$httpProvider" ] ;
262
+ var _prototypeProperties = function ( child , staticProps , instanceProps ) {
263
+ if ( staticProps ) Object . defineProperties ( child , staticProps ) ;
264
+ if ( instanceProps ) Object . defineProperties ( child . prototype , instanceProps ) ;
265
+ } ;
266
+ function oauthStorageService ( ) {
267
+ var config = { } ;
268
+ this . configure = function ( params ) {
269
+ angular . extend ( config , params ) ;
270
+ return config ;
271
+ } ;
272
+ this . $get = [ "$localStorage" , "$sessionStorage" , "$cookies" , "$log" , function ( $localStorage , $sessionStorage , $cookies , $log ) {
273
+ var storage ;
274
+ var ngStorage = config . storage . toLowerCase ( ) ;
275
+ if ( ngStorage === "localstorage" ) {
276
+ storage = $localStorage ;
277
+ } else if ( ngStorage === "sessionstorage" ) {
278
+ storage = $sessionStorage ;
279
+ } else if ( ngStorage === "cookies" ) {
280
+ storage = $cookies ;
281
+ } else {
282
+ storage = $cookies ;
283
+ $log . warn ( "Set storage to cookies, because storage type is unknown" ) ;
284
+ }
285
+ var BrowserStorage = function ( ) {
286
+ function BrowserStorage ( storage , name ) {
287
+ this . storage = storage ;
288
+ this . name = name ;
289
+ }
290
+ _prototypeProperties ( BrowserStorage , null , {
291
+ token : {
292
+ set : function ( data ) {
293
+ return this . storage . setItem ( this . name , angular . toJson ( data ) ) ;
294
+ } ,
295
+ get : function ( ) {
296
+ return angular . fromJson ( this . storage . getItem ( this . name ) ) ;
297
+ } ,
298
+ enumerable : true ,
299
+ configurable : true
300
+ } ,
301
+ deleteToken : {
302
+ value : function deleteToken ( ) {
303
+ this . storage . removeItem ( this . name ) ;
304
+ } ,
305
+ writable : true ,
306
+ enumerable : true ,
307
+ configurable : true
308
+ }
309
+ } ) ;
310
+ return BrowserStorage ;
311
+ } ( ) ;
312
+ var CookieStorage = function ( ) {
313
+ function CookieStorage ( $cookies , name , options ) {
314
+ this . $cookies = $cookies ;
315
+ this . name = name ;
316
+ this . options = options ;
317
+ }
318
+ _prototypeProperties ( CookieStorage , null , {
319
+ token : {
320
+ set : function ( value ) {
321
+ return this . $cookies . putObject ( this . name , value , this . options ) ;
322
+ } ,
323
+ get : function ( ) {
324
+ return this . $cookies . getObject ( this . name ) ;
325
+ } ,
326
+ enumerable : true ,
327
+ configurable : true
328
+ } ,
329
+ deleteToken : {
330
+ value : function deleteToken ( ) {
331
+ return this . $cookies . remove ( this . name , this . options ) ;
332
+ } ,
333
+ writable : true ,
334
+ enumerable : true ,
335
+ configurable : true
336
+ }
337
+ } ) ;
338
+ return CookieStorage ;
339
+ } ( ) ;
340
+ return ngStorage === "cookies" ? new CookieStorage ( storage , config . name , config . options ) : new BrowserStorage ( storage , config . name ) ;
341
+ } ] ;
316
342
}
317
343
return ngModule ;
318
344
} ) ;
0 commit comments