Skip to content

Commit 63bd4f3

Browse files
author
Paulo Pozeti
committed
Removing additionalState from oauth state property
It now saves the additionalStage into localStorage and after authentication it reads the localStorage and sets the state variable.
1 parent e0c0e51 commit 63bd4f3

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

projects/lib/src/oauth-service.ts

+21-20
Original file line numberDiff line numberDiff line change
@@ -843,9 +843,9 @@ export class OAuthService extends AuthConfig {
843843
this.setupSilentRefreshEventListener();
844844

845845
const redirectUri = this.silentRefreshRedirectUri || this.redirectUri;
846-
this.createLoginUrl(null, null, redirectUri, noPrompt, params).then(url => {
846+
this.createLoginUrl(null, redirectUri, noPrompt, params).then(url => {
847847
iframe.setAttribute('src', url);
848-
848+
849849
if (!this.silentRefreshShowIFrame) {
850850
iframe.style['display'] = 'none';
851851
}
@@ -1054,7 +1054,6 @@ export class OAuthService extends AuthConfig {
10541054
}
10551055

10561056
private createLoginUrl(
1057-
state = '',
10581057
loginHint = '',
10591058
customRedirectUri = '',
10601059
noPrompt = false,
@@ -1071,12 +1070,6 @@ export class OAuthService extends AuthConfig {
10711070
}
10721071

10731072
return this.createAndSaveNonce().then((nonce: any) => {
1074-
if (state) {
1075-
state = nonce + this.config.nonceStateSeparator + state;
1076-
} else {
1077-
state = nonce;
1078-
}
1079-
10801073
if (!this.requestAccessToken && !this.oidc) {
10811074
throw new Error(
10821075
'Either requestAccessToken or oidc or both must be true'
@@ -1107,7 +1100,7 @@ export class OAuthService extends AuthConfig {
11071100
'&client_id=' +
11081101
encodeURIComponent(that.clientId) +
11091102
'&state=' +
1110-
encodeURIComponent(state) +
1103+
encodeURIComponent(nonce) +
11111104
'&redirect_uri=' +
11121105
encodeURIComponent(redirectUri) +
11131106
'&scope=' +
@@ -1170,7 +1163,11 @@ export class OAuthService extends AuthConfig {
11701163
addParams = params;
11711164
}
11721165

1173-
this.createLoginUrl(additionalState, loginHint, null, false, addParams)
1166+
if (additionalState) {
1167+
this.storeAdditionalState(additionalState);
1168+
}
1169+
1170+
this.createLoginUrl(loginHint, null, false, addParams)
11741171
.then(function (url) {
11751172
location.href = url;
11761173
})
@@ -1181,6 +1178,10 @@ export class OAuthService extends AuthConfig {
11811178
});
11821179
}
11831180

1181+
private storeAdditionalState(additionalState: string) {
1182+
localStorage.setItem('additionalState', additionalState);
1183+
}
1184+
11841185
/**
11851186
* Starts the implicit flow and redirects to user to
11861187
* the auth servers login url.
@@ -1261,16 +1262,9 @@ export class OAuthService extends AuthConfig {
12611262
this.debug('parsed url', parts);
12621263

12631264
const state = parts['state'];
1264-
let nonceInState = state;
1265-
1266-
if (state) {
1267-
const idx = state.indexOf(this.config.nonceStateSeparator);
1265+
const nonceInState = state;
12681266

1269-
if (idx > -1) {
1270-
nonceInState = state.substr(0, idx);
1271-
this.state = state.substr(idx + this.config.nonceStateSeparator.length);
1272-
}
1273-
}
1267+
this.updateStateWithStoredAdditionalStateIfExists();
12741268

12751269
if (parts['error']) {
12761270
this.debug('error trying to login');
@@ -1372,6 +1366,13 @@ export class OAuthService extends AuthConfig {
13721366
});
13731367
}
13741368

1369+
private updateStateWithStoredAdditionalStateIfExists() {
1370+
const additionalState = localStorage.getItem('additionalState');
1371+
if (additionalState) {
1372+
this.state = additionalState;
1373+
}
1374+
}
1375+
13751376
private validateNonceForAccessToken(
13761377
accessToken: string,
13771378
nonceInState: string

0 commit comments

Comments
 (0)