1- import { Http , URLSearchParams , Headers } from '@angular/http' ;
21import { Injectable , Optional } from '@angular/core' ;
2+ import { HttpClient , HttpHeaders } from '@angular/common/http' ;
33import { Observable } from 'rxjs/Observable' ;
44import { Subject } from 'rxjs/Subject' ;
5+ import { Subscription } from 'rxjs/Subscription' ;
6+
57import { ValidationHandler , ValidationParams } from './token-validation/validation-handler' ;
68import { UrlHelperService } from './url-helper.service' ;
7- import { Subscription } from 'rxjs/Subscription' ;
89import { OAuthEvent , OAuthInfoEvent , OAuthErrorEvent , OAuthSuccessEvent } from './events' ;
9- import { OAuthStorage , LoginOptions , ParsedIdToken } from './types' ;
10+ import { OAuthStorage , LoginOptions , ParsedIdToken , OidcDiscoveryDoc , TokenResponse , UserInfo } from './types' ;
1011import { b64DecodeUnicode } from './base64-helper' ;
1112import { AuthConfig } from './auth.config' ;
1213
@@ -65,7 +66,7 @@ export class OAuthService
6566 private silentRefreshSubject : string ;
6667
6768 constructor (
68- private http : Http ,
69+ private http : HttpClient ,
6970 @Optional ( ) storage : OAuthStorage ,
7071 @Optional ( ) tokenValidationHandler : ValidationHandler ,
7172 @Optional ( ) private config : AuthConfig ,
@@ -92,7 +93,6 @@ export class OAuthService
9293
9394 this . setupRefreshTimer ( ) ;
9495
95-
9696 }
9797
9898 /**
@@ -140,14 +140,13 @@ export class OAuthService
140140 * @param params Additional parameter to pass
141141 */
142142 public setupAutomaticSilentRefresh ( params : object = { } ) {
143- this
144- . events
145- . filter ( e => e . type === 'token_expires' )
146- . subscribe ( e => {
147- this . silentRefresh ( params ) . catch ( _ => {
148- this . debug ( 'automatic silent refresh did not work' ) ;
149- } )
150- } ) ;
143+ this . events
144+ . filter ( e => e . type === 'token_expires' )
145+ . subscribe ( e => {
146+ this . silentRefresh ( params ) . catch ( _ => {
147+ this . debug ( 'automatic silent refresh did not work' ) ;
148+ } ) ;
149+ } ) ;
151150
152151 this . restartRefreshTimerIfStillLoggedIn ( ) ;
153152 }
@@ -306,7 +305,7 @@ export class OAuthService
306305 fullUrl = this . issuer || '' ;
307306 if ( ! fullUrl . endsWith ( '/' ) ) {
308307 fullUrl += '/' ;
309- }
308+ }
310309 fullUrl += '.well-known/openid-configuration' ;
311310 }
312311
@@ -315,7 +314,7 @@ export class OAuthService
315314 return ;
316315 }
317316
318- this . http . get ( fullUrl ) . map ( r => r . json ( ) ) . subscribe (
317+ this . http . get < OidcDiscoveryDoc > ( fullUrl ) . subscribe (
319318 ( doc ) => {
320319
321320 if ( ! this . validateDiscoveryDocument ( doc ) ) {
@@ -368,7 +367,7 @@ export class OAuthService
368367 private loadJwks ( ) : Promise < object > {
369368 return new Promise < object > ( ( resolve , reject ) => {
370369 if ( this . jwksUri ) {
371- this . http . get ( this . jwksUri ) . map ( r => r . json ( ) ) . subscribe (
370+ this . http . get ( this . jwksUri ) . subscribe (
372371 jwks => {
373372 this . jwks = jwks ;
374373 this . eventsSubject . next ( new OAuthSuccessEvent ( 'discovery_document_loaded' ) ) ;
@@ -388,55 +387,55 @@ export class OAuthService
388387
389388 }
390389
391- private validateDiscoveryDocument ( doc : object ) : boolean {
390+ private validateDiscoveryDocument ( doc : OidcDiscoveryDoc ) : boolean {
392391
393392 let errors : string [ ] ;
394393
395- if ( doc [ ' issuer' ] !== this . issuer ) {
394+ if ( doc . issuer !== this . issuer ) {
396395 console . error (
397396 'invalid issuer in discovery document' ,
398397 'expected: ' + this . issuer ,
399- 'current: ' + doc [ ' issuer' ]
398+ 'current: ' + doc . issuer
400399 ) ;
401400 return false ;
402401 }
403402
404- errors = this . validateUrlFromDiscoveryDocument ( doc [ ' authorization_endpoint' ] ) ;
403+ errors = this . validateUrlFromDiscoveryDocument ( doc . authorization_endpoint ) ;
405404 if ( errors . length > 0 ) {
406405 console . error ( 'error validating authorization_endpoint in discovery document' , errors ) ;
407406 return false ;
408407 }
409408
410- errors = this . validateUrlFromDiscoveryDocument ( doc [ ' end_session_endpoint' ] ) ;
409+ errors = this . validateUrlFromDiscoveryDocument ( doc . end_session_endpoint ) ;
411410 if ( errors . length > 0 ) {
412411 console . error ( 'error validating end_session_endpoint in discovery document' , errors ) ;
413412 return false ;
414413 }
415414
416- errors = this . validateUrlFromDiscoveryDocument ( doc [ ' token_endpoint' ] ) ;
415+ errors = this . validateUrlFromDiscoveryDocument ( doc . token_endpoint ) ;
417416 if ( errors . length > 0 ) {
418417 console . error ( 'error validating token_endpoint in discovery document' , errors ) ;
419418 }
420419
421- errors = this . validateUrlFromDiscoveryDocument ( doc [ ' userinfo_endpoint' ] ) ;
420+ errors = this . validateUrlFromDiscoveryDocument ( doc . userinfo_endpoint ) ;
422421 if ( errors . length > 0 ) {
423422 console . error ( 'error validating userinfo_endpoint in discovery document' , errors ) ;
424423 return false ;
425424 }
426425
427- errors = this . validateUrlFromDiscoveryDocument ( doc [ ' jwks_uri' ] ) ;
426+ errors = this . validateUrlFromDiscoveryDocument ( doc . jwks_uri ) ;
428427 if ( errors . length > 0 ) {
429428 console . error ( 'error validating jwks_uri in discovery document' , errors ) ;
430429 return false ;
431430 }
432431
433- if ( this . sessionChecksEnabled && ! doc [ ' check_session_iframe' ] ) {
432+ if ( this . sessionChecksEnabled && ! doc . check_session_iframe ) {
434433 console . warn (
435434 'sessionChecksEnabled is activated but discovery document'
436435 + ' does not contain a check_session_iframe field' ) ;
437436 }
438437
439- this . sessionChecksEnabled = doc [ ' check_session_iframe' ] ;
438+ this . sessionChecksEnabled = ! ! doc . check_session_iframe ;
440439
441440 return true ;
442441 }
@@ -458,7 +457,7 @@ export class OAuthService
458457 public fetchTokenUsingPasswordFlowAndLoadUserProfile (
459458 userName : string ,
460459 password : string ,
461- headers : Headers = new Headers ( ) ) : Promise < object > {
460+ headers : HttpHeaders = new HttpHeaders ( ) ) : Promise < object > {
462461 return this
463462 . fetchTokenUsingPasswordFlow ( userName , password , headers )
464463 . then ( ( ) => this . loadUserProfile ( ) ) ;
@@ -481,17 +480,17 @@ export class OAuthService
481480
482481 return new Promise ( ( resolve , reject ) => {
483482
484- let headers = new Headers ( ) ;
485- headers . set ( 'Authorization' , 'Bearer ' + this . getAccessToken ( ) ) ;
483+ const headers = new HttpHeaders ( )
484+ . set ( 'Authorization' , 'Bearer ' + this . getAccessToken ( ) ) ;
486485
487- this . http . get ( this . userinfoEndpoint , { headers } ) . map ( r => r . json ( ) ) . subscribe (
488- ( doc ) => {
489- this . debug ( 'userinfo received' , doc ) ;
486+ this . http . get < UserInfo > ( this . userinfoEndpoint , { headers } ) . subscribe (
487+ ( info ) => {
488+ this . debug ( 'userinfo received' , info ) ;
490489
491490 let existingClaims = this . getIdentityClaims ( ) || { } ;
492-
491+
493492 if ( ! this . skipSubjectCheck ) {
494- if ( this . oidc && ( ! existingClaims [ 'sub' ] || doc . sub !== existingClaims [ 'sub' ] ) ) {
493+ if ( this . oidc && ( ! existingClaims [ 'sub' ] || info . sub !== existingClaims [ 'sub' ] ) ) {
495494 let err = 'if property oidc is true, the received user-id (sub) has to be the user-id '
496495 + 'of the user that has logged in with oidc.\n'
497496 + 'if you are not using oidc but just oauth2 password flow set oidc to false' ;
@@ -501,11 +500,11 @@ export class OAuthService
501500 }
502501 }
503502
504- doc = Object . assign ( { } , existingClaims , doc ) ;
503+ info = Object . assign ( { } , existingClaims , info ) ;
505504
506- this . _storage . setItem ( 'id_token_claims_obj' , JSON . stringify ( doc ) ) ;
505+ this . _storage . setItem ( 'id_token_claims_obj' , JSON . stringify ( info ) ) ;
507506 this . eventsSubject . next ( new OAuthSuccessEvent ( 'user_profile_loaded' ) ) ;
508- resolve ( doc ) ;
507+ resolve ( info ) ;
509508 } ,
510509 ( err ) => {
511510 console . error ( 'error loading user info' , err ) ;
@@ -522,7 +521,7 @@ export class OAuthService
522521 * @param password
523522 * @param headers Optional additional http-headers.
524523 */
525- public fetchTokenUsingPasswordFlow ( userName : string , password : string , headers : Headers = new Headers ( ) ) : Promise < object > {
524+ public fetchTokenUsingPasswordFlow ( userName : string , password : string , headers : HttpHeaders = new HttpHeaders ( ) ) : Promise < object > {
526525
527526 if ( ! this . validateUrlForHttps ( this . tokenEndpoint ) ) {
528527 throw new Error ( 'tokenEndpoint must use Http. Also check property requireHttps.' ) ;
@@ -544,7 +543,7 @@ export class OAuthService
544543
545544 let params = search . toString ( ) ;
546545
547- this . http . post ( this . tokenEndpoint , params , { headers } ) . map ( r => r . json ( ) ) . subscribe (
546+ this . http . post < TokenResponse > ( this . tokenEndpoint , params , { headers } ) . subscribe (
548547 ( tokenResponse ) => {
549548 this . debug ( 'tokenResponse' , tokenResponse ) ;
550549 this . storeAccessTokenResponse ( tokenResponse . access_token , tokenResponse . refresh_token , tokenResponse . expires_in ) ;
@@ -586,12 +585,12 @@ export class OAuthService
586585 search . set ( 'client_secret' , this . dummyClientSecret ) ;
587586 }
588587
589- let headers = new Headers ( ) ;
590- headers . set ( 'Content-Type' , 'application/x-www-form-urlencoded' ) ;
588+ const headers = new HttpHeaders ( )
589+ . set ( 'Content-Type' , 'application/x-www-form-urlencoded' ) ;
591590
592591 let params = search . toString ( ) ;
593592
594- this . http . post ( this . tokenEndpoint , params , { headers } ) . map ( r => r . json ( ) ) . subscribe (
593+ this . http . post < TokenResponse > ( this . tokenEndpoint , params , { headers } ) . subscribe (
595594 ( tokenResponse ) => {
596595 this . debug ( 'refresh tokenResponse' , tokenResponse ) ;
597596 this . storeAccessTokenResponse ( tokenResponse . access_token , tokenResponse . refresh_token , tokenResponse . expires_in ) ;
@@ -785,8 +784,7 @@ export class OAuthService
785784 }
786785
787786 private waitForSilentRefreshAfterSessionChange ( ) {
788- this
789- . events
787+ this . events
790788 . filter ( ( e : OAuthEvent ) =>
791789 e . type === 'silently_refreshed'
792790 || e . type === 'silent_refresh_timeout'
@@ -1396,7 +1394,7 @@ export class OAuthService
13961394 this . _storage . removeItem ( 'id_token_expires_at' ) ;
13971395 this . _storage . removeItem ( 'id_token_stored_at' ) ;
13981396 this . _storage . removeItem ( 'access_token_stored_at' ) ;
1399-
1397+
14001398 this . silentRefreshSubject = null ;
14011399
14021400 this . eventsSubject . next ( new OAuthInfoEvent ( 'logout' ) ) ;
@@ -1408,7 +1406,7 @@ export class OAuthService
14081406 let logoutUrl : string ;
14091407
14101408 if ( ! this . validateUrlForHttps ( this . logoutUrl ) ) throw new Error ( 'logoutUrl must use Http. Also check property requireHttps.' ) ;
1411-
1409+
14121410 // For backward compatibility
14131411 if ( this . logoutUrl . indexOf ( '{{' ) > - 1 ) {
14141412 logoutUrl = this . logoutUrl . replace ( / \{ \{ i d _ t o k e n \} \} / , id_token ) ;
0 commit comments