@@ -196,7 +196,8 @@ export class OAuthService extends AuthConfig implements OnDestroy {
196
196
}
197
197
198
198
protected refreshInternal ( params , noPrompt ) : Promise < TokenResponse | OAuthEvent > {
199
- if ( this . responseType === 'code' ) {
199
+
200
+ if ( ! this . silentRefreshRedirectUri && this . responseType === 'code' ) {
200
201
return this . refreshToken ( ) ;
201
202
} else {
202
203
return this . silentRefresh ( params , noPrompt ) ;
@@ -833,14 +834,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
833
834
this . tryLogin ( {
834
835
customHashFragment : message ,
835
836
preventClearHashAfterLogin : true ,
836
- onLoginError : err => {
837
- this . eventsSubject . next (
838
- new OAuthErrorEvent ( 'silent_refresh_error' , err )
839
- ) ;
840
- } ,
841
- onTokenReceived : ( ) => {
842
- this . eventsSubject . next ( new OAuthSuccessEvent ( 'silently_refreshed' ) ) ;
843
- }
837
+ customRedirectUri : this . silentRefreshRedirectUri || this . redirectUri
844
838
} ) . catch ( err => this . debug ( 'tryLogin during silent refresh failed' , err ) ) ;
845
839
} ;
846
840
@@ -900,7 +894,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
900
894
first ( )
901
895
) ;
902
896
const success = this . events . pipe (
903
- filter ( e => e . type === 'silently_refreshed ' ) ,
897
+ filter ( e => e . type === 'token_received ' ) ,
904
898
first ( )
905
899
) ;
906
900
const timeout = of (
@@ -909,22 +903,35 @@ export class OAuthService extends AuthConfig implements OnDestroy {
909
903
910
904
return race ( [ errors , success , timeout ] )
911
905
. pipe (
912
- tap ( e => {
913
- if ( e . type === 'silent_refresh_timeout' ) {
914
- this . eventsSubject . next ( e ) ;
915
- }
916
- } ) ,
917
906
map ( e => {
918
907
if ( e instanceof OAuthErrorEvent ) {
908
+ if ( e . type === 'silent_refresh_timeout' ) {
909
+ this . eventsSubject . next ( e ) ;
910
+ } else {
911
+ e = new OAuthErrorEvent ( 'silent_refresh_error' , e ) ;
912
+ this . eventsSubject . next ( e ) ;
913
+ }
919
914
throw e ;
915
+ } else if ( e . type === 'token_received' ) {
916
+ e = new OAuthSuccessEvent ( 'silently_refreshed' ) ;
917
+ this . eventsSubject . next ( e ) ;
920
918
}
921
919
return e ;
922
920
} )
923
921
)
924
922
. toPromise ( ) ;
925
923
}
926
924
925
+ /**
926
+ * This method exists for backwards compatibility.
927
+ * {@link OAuthService#initLoginFlowInPopup} handles both code
928
+ * and implicit flows.
929
+ */
927
930
public initImplicitFlowInPopup ( options ?: { height ?: number , width ?: number } ) {
931
+ return this . initLoginFlowInPopup ( options ) ;
932
+ }
933
+
934
+ public initLoginFlowInPopup ( options ?: { height ?: number , width ?: number } ) {
928
935
options = options || { } ;
929
936
return this . createLoginUrl ( null , null , this . silentRefreshRedirectUri , false , {
930
937
display : 'popup'
@@ -959,10 +966,12 @@ export class OAuthService extends AuthConfig implements OnDestroy {
959
966
960
967
const listener = ( e : MessageEvent ) => {
961
968
const message = this . processMessageEventMessage ( e ) ;
969
+
962
970
if ( message && message !== null ) {
963
971
this . tryLogin ( {
964
972
customHashFragment : message ,
965
973
preventClearHashAfterLogin : true ,
974
+ customRedirectUri : this . silentRefreshRedirectUri ,
966
975
} ) . then ( ( ) => {
967
976
cleanup ( ) ;
968
977
resolve ( ) ;
@@ -973,6 +982,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
973
982
} else {
974
983
console . log ( 'false event firing' ) ;
975
984
}
985
+
976
986
} ;
977
987
978
988
window . addEventListener ( 'message' , listener ) ;
@@ -1402,25 +1412,50 @@ export class OAuthService extends AuthConfig implements OnDestroy {
1402
1412
*/
1403
1413
public tryLogin ( options : LoginOptions = null ) : Promise < boolean > {
1404
1414
if ( this . config . responseType === 'code' ) {
1405
- return this . tryLoginCodeFlow ( ) . then ( _ => true ) ;
1406
- } else {
1415
+ return this . tryLoginCodeFlow ( options ) . then ( _ => true ) ;
1416
+ }
1417
+ else {
1407
1418
return this . tryLoginImplicitFlow ( options ) ;
1408
1419
}
1409
1420
}
1410
1421
1411
- public tryLoginCodeFlow ( ) : Promise < void > {
1422
+
1423
+
1424
+ private parseQueryString ( queryString : string ) : object {
1425
+ if ( ! queryString || queryString . length === 0 ) {
1426
+ return { } ;
1427
+ }
1428
+
1429
+ if ( queryString . charAt ( 0 ) === '?' ) {
1430
+ queryString = queryString . substr ( 1 ) ;
1431
+ }
1432
+
1433
+ return this . urlHelper . parseQueryString ( queryString ) ;
1434
+
1435
+
1436
+ }
1437
+
1438
+ public tryLoginCodeFlow ( options : LoginOptions = null ) : Promise < void > {
1439
+ options = options || { } ;
1440
+
1441
+ const querySource = options . customHashFragment ?
1442
+ options . customHashFragment . substring ( 1 ) :
1443
+ window . location . search ;
1444
+
1412
1445
const parts = this . getCodePartsFromUrl ( window . location . search ) ;
1413
1446
1414
1447
const code = parts [ 'code' ] ;
1415
1448
const state = parts [ 'state' ] ;
1416
1449
1417
- const href = location . href
1418
- . replace ( / [ & \? ] c o d e = [ ^ & \$ ] * / , '' )
1419
- . replace ( / [ & \? ] s c o p e = [ ^ & \$ ] * / , '' )
1420
- . replace ( / [ & \? ] s t a t e = [ ^ & \$ ] * / , '' )
1421
- . replace ( / [ & \? ] s e s s i o n _ s t a t e = [ ^ & \$ ] * / , '' ) ;
1450
+ if ( ! options . preventClearHashAfterLogin ) {
1451
+ const href = location . href
1452
+ . replace ( / [ & \? ] c o d e = [ ^ & \$ ] * / , '' )
1453
+ . replace ( / [ & \? ] s c o p e = [ ^ & \$ ] * / , '' )
1454
+ . replace ( / [ & \? ] s t a t e = [ ^ & \$ ] * / , '' )
1455
+ . replace ( / [ & \? ] s e s s i o n _ s t a t e = [ ^ & \$ ] * / , '' ) ;
1422
1456
1423
- history . replaceState ( null , window . name , href ) ;
1457
+ history . replaceState ( null , window . name , href ) ;
1458
+ }
1424
1459
1425
1460
let [ nonceInState , userState ] = this . parseState ( state ) ;
1426
1461
this . state = userState ;
@@ -1446,7 +1481,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
1446
1481
1447
1482
if ( code ) {
1448
1483
return new Promise ( ( resolve , reject ) => {
1449
- this . getTokenFromCode ( code ) . then ( result => {
1484
+ this . getTokenFromCode ( code , options ) . then ( result => {
1450
1485
resolve ( ) ;
1451
1486
} ) . catch ( err => {
1452
1487
reject ( err ) ;
@@ -1477,11 +1512,11 @@ export class OAuthService extends AuthConfig implements OnDestroy {
1477
1512
/**
1478
1513
* Get token using an intermediate code. Works for the Authorization Code flow.
1479
1514
*/
1480
- private getTokenFromCode ( code : string ) : Promise < TokenResponse > {
1515
+ private getTokenFromCode ( code : string , options : LoginOptions ) : Promise < object > {
1481
1516
let params = new HttpParams ( )
1482
1517
. set ( 'grant_type' , 'authorization_code' )
1483
1518
. set ( 'code' , code )
1484
- . set ( 'redirect_uri' , this . redirectUri ) ;
1519
+ . set ( 'redirect_uri' , options . customRedirectUri || this . redirectUri ) ;
1485
1520
1486
1521
if ( ! this . disablePKCE ) {
1487
1522
const pkciVerifier = this . _storage . getItem ( 'PKCI_verifier' ) ;
0 commit comments