@@ -144,6 +144,12 @@ export class Angular2TokenService implements CanActivate {
144144 this . _tryLoadAuthData ( ) ;
145145 }
146146
147+ /**
148+ *
149+ * Actions
150+ *
151+ */
152+
147153 // Register request
148154 registerAccount ( registerData : RegisterData ) : Observable < Response > {
149155
@@ -160,12 +166,12 @@ export class Angular2TokenService implements CanActivate {
160166 confirm_success_url : this . _options . registerAccountCallback
161167 } ) ;
162168
163- return this . post ( this . _constructUserPath ( ) + this . _options . registerAccountPath , body ) ;
169+ return this . post ( this . _getUserPath ( ) + this . _options . registerAccountPath , body ) ;
164170 }
165171
166172 // Delete Account
167173 deleteAccount ( ) : Observable < Response > {
168- return this . delete ( this . _constructUserPath ( ) + this . _options . deleteAccountPath ) ;
174+ return this . delete ( this . _getUserPath ( ) + this . _options . deleteAccountPath ) ;
169175 }
170176
171177 // Sign in request and set storage
@@ -181,7 +187,7 @@ export class Angular2TokenService implements CanActivate {
181187 password : signInData . password
182188 } ) ;
183189
184- let observ = this . post ( this . _constructUserPath ( ) + this . _options . signInPath , body ) ;
190+ let observ = this . post ( this . _getUserPath ( ) + this . _options . signInPath , body ) ;
185191
186192 observ . subscribe ( res => this . _currentUserData = res . json ( ) . data , _error => null ) ;
187193
@@ -193,7 +199,7 @@ export class Angular2TokenService implements CanActivate {
193199 let oAuthPath : string = this . _getOAuthPath ( oAuthType ) ;
194200 let callbackUrl : string = `${ window . location . origin } /${ this . _options . oAuthCallbackPath } ` ;
195201 let oAuthWindowType : string = this . _options . oAuthWindowType ;
196- let authUrl : string = this . _buildOAuthUrl ( oAuthPath , callbackUrl , oAuthWindowType ) ;
202+ let authUrl : string = this . _getOAuthUrl ( oAuthPath , callbackUrl , oAuthWindowType ) ;
197203
198204 if ( oAuthWindowType == 'newWindow' ) {
199205 let oAuthWindowOptions = this . _options . oAuthWindowOptions ;
@@ -224,7 +230,7 @@ export class Angular2TokenService implements CanActivate {
224230
225231 // Sign out request and delete storage
226232 signOut ( ) : Observable < Response > {
227- let observ = this . delete ( this . _constructUserPath ( ) + this . _options . signOutPath ) ;
233+ let observ = this . delete ( this . _getUserPath ( ) + this . _options . signOutPath ) ;
228234
229235 localStorage . removeItem ( 'accessToken' ) ;
230236 localStorage . removeItem ( 'client' ) ;
@@ -241,7 +247,7 @@ export class Angular2TokenService implements CanActivate {
241247
242248 // Validate token request
243249 validateToken ( ) : Observable < Response > {
244- let observ = this . get ( this . _constructUserPath ( ) + this . _options . validateTokenPath ) ;
250+ let observ = this . get ( this . _getUserPath ( ) + this . _options . validateTokenPath ) ;
245251
246252 observ . subscribe (
247253 res => this . _currentUserData = res . json ( ) . data ,
@@ -280,7 +286,7 @@ export class Angular2TokenService implements CanActivate {
280286 }
281287
282288 let body = JSON . stringify ( args ) ;
283- return this . put ( this . _constructUserPath ( ) + this . _options . updatePasswordPath , body ) ;
289+ return this . put ( this . _getUserPath ( ) + this . _options . updatePasswordPath , body ) ;
284290 }
285291
286292 // Reset password request
@@ -296,43 +302,48 @@ export class Angular2TokenService implements CanActivate {
296302 redirect_url : this . _options . resetPasswordCallback
297303 } ) ;
298304
299- return this . post ( this . _constructUserPath ( ) + this . _options . resetPasswordPath , body ) ;
305+ return this . post ( this . _getUserPath ( ) + this . _options . resetPasswordPath , body ) ;
300306 }
301307
302- // Standard HTTP requests
308+ /**
309+ *
310+ * HTTP Wrappers
311+ *
312+ */
313+
303314 get ( url : string , options ?: RequestOptionsArgs ) : Observable < Response > {
304315 return this . request ( this . _mergeRequestOptionsArgs ( {
305- url : this . _constructApiPath ( ) + url ,
316+ url : this . _getApiPath ( ) + url ,
306317 method : RequestMethod . Get
307318 } , options ) ) ;
308319 }
309320
310321 post ( url : string , body : any , options ?: RequestOptionsArgs ) : Observable < Response > {
311322 return this . request ( this . _mergeRequestOptionsArgs ( {
312- url : this . _constructApiPath ( ) + url ,
323+ url : this . _getApiPath ( ) + url ,
313324 method : RequestMethod . Post ,
314325 body : body
315326 } , options ) ) ;
316327 }
317328
318329 put ( url : string , body : any , options ?: RequestOptionsArgs ) : Observable < Response > {
319330 return this . request ( this . _mergeRequestOptionsArgs ( {
320- url : this . _constructApiPath ( ) + url ,
331+ url : this . _getApiPath ( ) + url ,
321332 method : RequestMethod . Put ,
322333 body : body
323334 } , options ) ) ;
324335 }
325336
326337 delete ( url : string , options ?: RequestOptionsArgs ) : Observable < Response > {
327338 return this . request ( this . _mergeRequestOptionsArgs ( {
328- url : this . _constructApiPath ( ) + url ,
339+ url : this . _getApiPath ( ) + url ,
329340 method : RequestMethod . Delete
330341 } , options ) ) ;
331342 }
332343
333344 patch ( url : string , body : any , options ?: RequestOptionsArgs ) : Observable < Response > {
334345 return this . request ( this . _mergeRequestOptionsArgs ( {
335- url : this . _constructApiPath ( ) + url ,
346+ url : this . _getApiPath ( ) + url ,
336347 method : RequestMethod . Patch ,
337348 body : body
338349 } , options ) ) ;
@@ -341,13 +352,13 @@ export class Angular2TokenService implements CanActivate {
341352 head ( path : string , options ?: RequestOptionsArgs ) : Observable < Response > {
342353 return this . request ( {
343354 method : RequestMethod . Head ,
344- url : this . _constructApiPath ( ) + path
355+ url : this . _getApiPath ( ) + path
345356 } ) ;
346357 }
347358
348359 options ( url : string , options ?: RequestOptionsArgs ) : Observable < Response > {
349360 return this . request ( this . _mergeRequestOptionsArgs ( {
350- url : this . _constructApiPath ( ) + url ,
361+ url : this . _getApiPath ( ) + url ,
351362 method : RequestMethod . Options
352363 } , options ) ) ;
353364 }
@@ -396,13 +407,35 @@ export class Angular2TokenService implements CanActivate {
396407 // Check if response is complete and newer, then update storage
397408 private _handleResponse ( response : Observable < Response > ) {
398409 response . subscribe ( res => {
399- this . _parseAuthHeadersFromResponse ( < any > res ) ;
410+ this . _getAuthHeadersFromResponse ( < any > res ) ;
400411 } , error => {
401- this . _parseAuthHeadersFromResponse ( < any > error ) ;
412+ this . _getAuthHeadersFromResponse ( < any > error ) ;
402413 } ) ;
403414 }
404415
405- private _parseAuthHeadersFromResponse ( data : any ) {
416+ /**
417+ *
418+ * Get Auth Data
419+ *
420+ */
421+
422+ // Try to load auth data
423+ private _tryLoadAuthData ( ) {
424+
425+ let userType = this . _getUserTypeByName ( localStorage . getItem ( 'userType' ) ) ;
426+
427+ if ( userType )
428+ this . _currentUserType = userType ;
429+
430+ this . _getAuthDataFromStorage ( ) ;
431+ this . _getAuthDataFromParams ( ) ;
432+
433+ if ( this . _currentAuthData != null )
434+ this . validateToken ( ) ;
435+ }
436+
437+ // Parse Auth data from response
438+ private _getAuthHeadersFromResponse ( data : any ) {
406439 let headers = data . headers ;
407440
408441 let authData : AuthData = {
@@ -416,6 +449,19 @@ export class Angular2TokenService implements CanActivate {
416449 this . _setAuthData ( authData ) ;
417450 }
418451
452+ // Parse Auth data from post message
453+ private _getAuthDataFromPostMessage ( data : any ) {
454+ let authData : AuthData = {
455+ accessToken : data [ 'auth_token' ] ,
456+ client : data [ 'client_id' ] ,
457+ expiry : data [ 'expiry' ] ,
458+ tokenType : 'Bearer' ,
459+ uid : data [ 'uid' ]
460+ } ;
461+
462+ this . _setAuthData ( authData ) ;
463+ }
464+
419465 // Try to get auth data from storage.
420466 private _getAuthDataFromStorage ( ) {
421467
@@ -427,7 +473,7 @@ export class Angular2TokenService implements CanActivate {
427473 uid : localStorage . getItem ( 'uid' )
428474 } ;
429475
430- if ( this . _checkIfComplete ( authData ) )
476+ if ( this . _checkAuthData ( authData ) )
431477 this . _currentAuthData = authData ;
432478 }
433479
@@ -443,27 +489,21 @@ export class Angular2TokenService implements CanActivate {
443489 uid : queryParams [ 'uid' ]
444490 } ;
445491
446- if ( this . _checkIfComplete ( authData ) )
492+ if ( this . _checkAuthData ( authData ) )
447493 this . _currentAuthData = authData ;
448494 } ) ;
449495 }
450496
451- private _getAuthDataFromPostMessage ( data : any ) {
452- let authData : AuthData = {
453- accessToken : data [ 'auth_token' ] ,
454- client : data [ 'client_id' ] ,
455- expiry : data [ 'expiry' ] ,
456- tokenType : 'Bearer' ,
457- uid : data [ 'uid' ]
458- } ;
459-
460- this . _setAuthData ( authData ) ;
461- }
497+ /**
498+ *
499+ * Set Auth Data
500+ *
501+ */
462502
463503 // Write auth data to storage
464504 private _setAuthData ( authData : AuthData ) {
465505
466- if ( this . _checkIfComplete ( authData ) && this . _checkIfNewer ( authData ) ) {
506+ if ( this . _checkAuthData ( authData ) ) {
467507
468508 this . _currentAuthData = authData ;
469509
@@ -479,61 +519,45 @@ export class Angular2TokenService implements CanActivate {
479519 }
480520 }
481521
482- // Check if auth data complete
483- private _checkIfComplete ( authData : AuthData ) : boolean {
522+ /**
523+ *
524+ * Validate Auth Data
525+ *
526+ */
527+
528+ // Check if auth data complete and if response token is newer
529+ private _checkAuthData ( authData : AuthData ) : boolean {
530+
484531 if (
485532 authData . accessToken != null &&
486533 authData . client != null &&
487534 authData . expiry != null &&
488535 authData . tokenType != null &&
489536 authData . uid != null
490537 ) {
491- return true ;
538+ if ( this . _currentAuthData != null )
539+ return authData . expiry >= this . _currentAuthData . expiry ;
540+ else
541+ return true ;
492542 } else {
493543 return false ;
494544 }
495545 }
496546
497- // Check if response token is newer
498- private _checkIfNewer ( authData : AuthData ) : boolean {
499- if ( this . _currentAuthData != null )
500- return authData . expiry >= this . _currentAuthData . expiry ;
501- else
502- return true ;
503- }
547+ /**
548+ *
549+ * Construct Paths / Urls
550+ *
551+ */
504552
505- // Try to load user config from storage
506- private _tryLoadAuthData ( ) {
507-
508- let userType = this . _getUserTypeByName ( localStorage . getItem ( 'userType' ) ) ;
509- if ( userType )
510- this . _currentUserType = userType ;
511-
512- this . _getAuthDataFromStorage ( ) ;
513- this . _getAuthDataFromParams ( ) ;
514-
515- if ( this . _currentAuthData != null )
516- this . validateToken ( ) ;
517- }
518-
519- // Match user config by user config name
520- private _getUserTypeByName ( name : string ) : UserType {
521- if ( name == null || this . _options . userTypes == null )
522- return null ;
523-
524- return this . _options . userTypes . find (
525- userType => userType . name === name
526- ) ;
527- }
528-
529- private _constructUserPath ( ) : string {
553+ private _getUserPath ( ) : string {
530554 if ( this . _currentUserType == null )
531555 return '' ;
532556 else
533557 return this . _currentUserType . path + '/' ;
534558 }
535559
536- private _constructApiPath ( ) : string {
560+ private _getApiPath ( ) : string {
537561 let constructedPath = '' ;
538562
539563 if ( this . _options . apiBase != null )
@@ -556,7 +580,7 @@ export class Angular2TokenService implements CanActivate {
556580 return oAuthPath ;
557581 }
558582
559- private _buildOAuthUrl ( oAuthPath : string , callbackUrl : string , windowType : string ) : string {
583+ private _getOAuthUrl ( oAuthPath : string , callbackUrl : string , windowType : string ) : string {
560584 let url : string ;
561585
562586 url = `${ this . _options . oAuthBase } /${ oAuthPath } ` ;
@@ -569,6 +593,12 @@ export class Angular2TokenService implements CanActivate {
569593 return url ;
570594 }
571595
596+ /**
597+ *
598+ * OAuth
599+ *
600+ */
601+
572602 private _requestCredentialsViaPostMessage ( authWindow : any ) : Observable < any > {
573603 let pollerObserv = Observable . interval ( 500 ) ;
574604
@@ -593,4 +623,20 @@ export class Angular2TokenService implements CanActivate {
593623 if ( data . message == 'deliverCredentials' || data . message == 'authFailure' )
594624 return data ;
595625 }
626+
627+ /**
628+ *
629+ * Utilities
630+ *
631+ */
632+
633+ // Match user config by user config name
634+ private _getUserTypeByName ( name : string ) : UserType {
635+ if ( name == null || this . _options . userTypes == null )
636+ return null ;
637+
638+ return this . _options . userTypes . find (
639+ userType => userType . name === name
640+ ) ;
641+ }
596642}
0 commit comments