@@ -827,14 +827,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
827
827
this . tryLogin ( {
828
828
customHashFragment : message ,
829
829
preventClearHashAfterLogin : true ,
830
- onLoginError : err => {
831
- this . eventsSubject . next (
832
- new OAuthErrorEvent ( 'silent_refresh_error' , err )
833
- ) ;
834
- } ,
835
- onTokenReceived : ( ) => {
836
- this . eventsSubject . next ( new OAuthSuccessEvent ( 'silently_refreshed' ) ) ;
837
- }
830
+ customRedirectUri : this . silentRefreshRedirectUri || this . redirectUri
838
831
} ) . catch ( err => this . debug ( 'tryLogin during silent refresh failed' , err ) ) ;
839
832
} ;
840
833
@@ -896,7 +889,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
896
889
first ( )
897
890
) ;
898
891
const success = this . events . pipe (
899
- filter ( e => e . type === 'silently_refreshed ' ) ,
892
+ filter ( e => e . type === 'token_received ' ) ,
900
893
first ( )
901
894
) ;
902
895
const timeout = of (
@@ -905,22 +898,35 @@ export class OAuthService extends AuthConfig implements OnDestroy {
905
898
906
899
return race ( [ errors , success , timeout ] )
907
900
. pipe (
908
- tap ( e => {
909
- if ( e . type === 'silent_refresh_timeout' ) {
910
- this . eventsSubject . next ( e ) ;
911
- }
912
- } ) ,
913
901
map ( e => {
914
902
if ( e instanceof OAuthErrorEvent ) {
903
+ if ( e . type === 'silent_refresh_timeout' ) {
904
+ this . eventsSubject . next ( e ) ;
905
+ } else {
906
+ e = new OAuthErrorEvent ( 'silent_refresh_error' , e ) ;
907
+ this . eventsSubject . next ( e ) ;
908
+ }
915
909
throw e ;
910
+ } else if ( e . type === 'token_received' ) {
911
+ e = new OAuthSuccessEvent ( 'silently_refreshed' ) ;
912
+ this . eventsSubject . next ( e ) ;
916
913
}
917
914
return e ;
918
915
} )
919
916
)
920
917
. toPromise ( ) ;
921
918
}
922
919
920
+ /**
921
+ * This method exists for backwards compatibility.
922
+ * {@link OAuthService#initLoginFlowInPopup} handles both code
923
+ * and implicit flows.
924
+ */
923
925
public initImplicitFlowInPopup ( options ?: { height ?: number , width ?: number } ) {
926
+ return this . initLoginFlowInPopup ( options ) ;
927
+ }
928
+
929
+ public initLoginFlowInPopup ( options ?: { height ?: number , width ?: number } ) {
924
930
options = options || { } ;
925
931
return this . createLoginUrl ( null , null , this . silentRefreshRedirectUri , false , {
926
932
display : 'popup'
@@ -940,6 +946,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
940
946
this . tryLogin ( {
941
947
customHashFragment : message ,
942
948
preventClearHashAfterLogin : true ,
949
+ customRedirectUri : this . silentRefreshRedirectUri
943
950
} ) . then ( ( ) => {
944
951
cleanup ( ) ;
945
952
resolve ( ) ;
@@ -1264,7 +1271,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
1264
1271
}
1265
1272
1266
1273
return url ;
1267
-
1274
+
1268
1275
}
1269
1276
1270
1277
initImplicitFlowInternal (
@@ -1373,7 +1380,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
1373
1380
*/
1374
1381
public tryLogin ( options : LoginOptions = null ) : Promise < boolean > {
1375
1382
if ( this . config . responseType === 'code' ) {
1376
- return this . tryLoginCodeFlow ( ) . then ( _ => true ) ;
1383
+ return this . tryLoginCodeFlow ( options ) . then ( _ => true ) ;
1377
1384
}
1378
1385
else {
1379
1386
return this . tryLoginImplicitFlow ( options ) ;
@@ -1395,20 +1402,27 @@ export class OAuthService extends AuthConfig implements OnDestroy {
1395
1402
1396
1403
}
1397
1404
1398
- public tryLoginCodeFlow ( ) : Promise < void > {
1405
+ public tryLoginCodeFlow ( options : LoginOptions = null ) : Promise < void > {
1406
+ options = options || { } ;
1399
1407
1400
- const parts = this . parseQueryString ( window . location . search )
1408
+ const querySource = options . customHashFragment ?
1409
+ options . customHashFragment . substring ( 1 ) :
1410
+ window . location . search ;
1411
+
1412
+ const parts = this . parseQueryString ( querySource )
1401
1413
1402
1414
const code = parts [ 'code' ] ;
1403
1415
const state = parts [ 'state' ] ;
1404
1416
1405
- const href = location . href
1406
- . replace ( / [ & \? ] c o d e = [ ^ & \$ ] * / , '' )
1407
- . replace ( / [ & \? ] s c o p e = [ ^ & \$ ] * / , '' )
1408
- . replace ( / [ & \? ] s t a t e = [ ^ & \$ ] * / , '' )
1409
- . replace ( / [ & \? ] s e s s i o n _ s t a t e = [ ^ & \$ ] * / , '' ) ;
1417
+ if ( ! options . preventClearHashAfterLogin ) {
1418
+ const href = location . href
1419
+ . replace ( / [ & \? ] c o d e = [ ^ & \$ ] * / , '' )
1420
+ . replace ( / [ & \? ] s c o p e = [ ^ & \$ ] * / , '' )
1421
+ . replace ( / [ & \? ] s t a t e = [ ^ & \$ ] * / , '' )
1422
+ . replace ( / [ & \? ] s e s s i o n _ s t a t e = [ ^ & \$ ] * / , '' ) ;
1410
1423
1411
- history . replaceState ( null , window . name , href ) ;
1424
+ history . replaceState ( null , window . name , href ) ;
1425
+ }
1412
1426
1413
1427
let [ nonceInState , userState ] = this . parseState ( state ) ;
1414
1428
this . state = userState ;
@@ -1434,7 +1448,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
1434
1448
1435
1449
if ( code ) {
1436
1450
return new Promise ( ( resolve , reject ) => {
1437
- this . getTokenFromCode ( code ) . then ( result => {
1451
+ this . getTokenFromCode ( code , options ) . then ( result => {
1438
1452
resolve ( ) ;
1439
1453
} ) . catch ( err => {
1440
1454
reject ( err ) ;
@@ -1448,11 +1462,11 @@ export class OAuthService extends AuthConfig implements OnDestroy {
1448
1462
/**
1449
1463
* Get token using an intermediate code. Works for the Authorization Code flow.
1450
1464
*/
1451
- private getTokenFromCode ( code : string ) : Promise < object > {
1465
+ private getTokenFromCode ( code : string , options : LoginOptions ) : Promise < object > {
1452
1466
let params = new HttpParams ( )
1453
1467
. set ( 'grant_type' , 'authorization_code' )
1454
1468
. set ( 'code' , code )
1455
- . set ( 'redirect_uri' , this . redirectUri ) ;
1469
+ . set ( 'redirect_uri' , options . customRedirectUri || this . redirectUri ) ;
1456
1470
1457
1471
if ( ! this . disablePKCE ) {
1458
1472
const pkciVerifier = this . _storage . getItem ( 'PKCI_verifier' ) ;
@@ -1503,32 +1517,32 @@ export class OAuthService extends AuthConfig implements OnDestroy {
1503
1517
( tokenResponse ) => {
1504
1518
this . debug ( 'refresh tokenResponse' , tokenResponse ) ;
1505
1519
this . storeAccessTokenResponse (
1506
- tokenResponse . access_token ,
1507
- tokenResponse . refresh_token ,
1520
+ tokenResponse . access_token ,
1521
+ tokenResponse . refresh_token ,
1508
1522
tokenResponse . expires_in ,
1509
1523
tokenResponse . scope ) ;
1510
1524
1511
1525
if ( this . oidc && tokenResponse . id_token ) {
1512
- this . processIdToken ( tokenResponse . id_token , tokenResponse . access_token ) .
1526
+ this . processIdToken ( tokenResponse . id_token , tokenResponse . access_token ) .
1513
1527
then ( result => {
1514
1528
this . storeIdToken ( result ) ;
1515
-
1529
+
1516
1530
this . eventsSubject . next ( new OAuthSuccessEvent ( 'token_received' ) ) ;
1517
1531
this . eventsSubject . next ( new OAuthSuccessEvent ( 'token_refreshed' ) ) ;
1518
-
1532
+
1519
1533
resolve ( tokenResponse ) ;
1520
1534
} )
1521
1535
. catch ( reason => {
1522
1536
this . eventsSubject . next ( new OAuthErrorEvent ( 'token_validation_error' , reason ) ) ;
1523
1537
console . error ( 'Error validating tokens' ) ;
1524
1538
console . error ( reason ) ;
1525
-
1539
+
1526
1540
reject ( reason ) ;
1527
1541
} ) ;
1528
1542
} else {
1529
1543
this . eventsSubject . next ( new OAuthSuccessEvent ( 'token_received' ) ) ;
1530
1544
this . eventsSubject . next ( new OAuthSuccessEvent ( 'token_refreshed' ) ) ;
1531
-
1545
+
1532
1546
resolve ( tokenResponse ) ;
1533
1547
}
1534
1548
} ,
@@ -1688,7 +1702,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
1688
1702
) : boolean {
1689
1703
const savedNonce = this . _storage . getItem ( 'nonce' ) ;
1690
1704
if ( savedNonce !== nonceInState ) {
1691
-
1705
+
1692
1706
const err = 'Validating access_token failed, wrong state/nonce.' ;
1693
1707
console . error ( err , savedNonce , nonceInState ) ;
1694
1708
return false ;
0 commit comments