Skip to content

Commit e79c363

Browse files
Merge pull request #186 from ZiyamSanthosh/master-api-based-auth-authKey
Change authenticator property keys to be specific for different authenticators
2 parents 49bf38f + c16df49 commit e79c363

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

components/org.wso2.carbon.identity.application.authenticator.oidc/src/main/java/org/wso2/carbon/identity/application/authenticator/oidc/OpenIDConnectAuthenticator.java

+28-22
Original file line numberDiff line numberDiff line change
@@ -550,9 +550,9 @@ protected String prepareLoginPage(HttpServletRequest request, AuthenticationCont
550550
String callbackurl = getCallbackUrl(authenticatorProperties, context);
551551

552552
String state = getStateParameter(request, context, authenticatorProperties);
553-
context.setProperty(OIDCAuthenticatorConstants.AUTHENTICATOR_NAME + STATE_PARAM_SUFFIX, state);
553+
context.setProperty(getName() + STATE_PARAM_SUFFIX, state);
554554
String nonce = UUID.randomUUID().toString();
555-
context.setProperty(OIDC_FEDERATION_NONCE, nonce);
555+
context.setProperty(getName() + OIDC_FEDERATION_NONCE, nonce);
556556
boolean isPKCEEnabled = Boolean.parseBoolean(
557557
authenticatorProperties.get(OIDCAuthenticatorConstants.IS_PKCE_ENABLED));
558558

@@ -591,7 +591,7 @@ protected String prepareLoginPage(HttpServletRequest request, AuthenticationCont
591591

592592
String scope = paramValueMap.get(OAuthConstants.OAuth20Params.SCOPE);
593593
scope = getScope(scope, authenticatorProperties);
594-
context.setProperty(OIDCAuthenticatorConstants.AUTHENTICATOR_NAME + SCOPE_PARAM_SUFFIX, scope);
594+
context.setProperty(getName() + SCOPE_PARAM_SUFFIX, scope);
595595

596596
if (StringUtils.isNotBlank(queryString) && queryString.toLowerCase().contains("scope=") && queryString
597597
.toLowerCase().contains("redirect_uri=")) {
@@ -642,7 +642,7 @@ protected String prepareLoginPage(HttpServletRequest request, AuthenticationCont
642642
loginPage = loginPage + queryString;
643643
}
644644
}
645-
context.setProperty(OIDCAuthenticatorConstants.AUTHENTICATOR_NAME + REDIRECT_URL_SUFFIX, loginPage);
645+
context.setProperty(getName() + REDIRECT_URL_SUFFIX, loginPage);
646646
return loginPage;
647647
} else {
648648
if (LOG.isDebugEnabled()) {
@@ -668,6 +668,21 @@ protected String prepareLoginPage(HttpServletRequest request, AuthenticationCont
668668
}
669669
}
670670

671+
/**
672+
* This method can be used to add the authentication error message content into the context.
673+
*
674+
* @param errorMessage ErrorMessage object.
675+
* @param context AuthenticationContext.
676+
*/
677+
protected static void setAuthenticatorMessageToContext(ErrorMessages errorMessage,
678+
AuthenticationContext context) {
679+
680+
AuthenticatorMessage authenticatorMessage = new AuthenticatorMessage(FrameworkConstants.
681+
AuthenticatorMessageType.ERROR, errorMessage.
682+
getCode(), errorMessage.getMessage(), null);
683+
context.setProperty(AUTHENTICATOR_MESSAGE, authenticatorMessage);
684+
}
685+
671686
/**
672687
* This method is used to append the application side requested scopes after validating.
673688
* The application can request the scopes for federated token sharing either via adaptive scripts
@@ -925,15 +940,6 @@ private String getQueryParameter(AuthenticationContext context, String queryPara
925940
return null;
926941
}
927942

928-
private static void setAuthenticatorMessageToContext(ErrorMessages errorMessage,
929-
AuthenticationContext context) {
930-
931-
AuthenticatorMessage authenticatorMessage = new AuthenticatorMessage(FrameworkConstants.
932-
AuthenticatorMessageType.ERROR, errorMessage.
933-
getCode(), errorMessage.getMessage(), null);
934-
context.setProperty(AUTHENTICATOR_MESSAGE, authenticatorMessage);
935-
}
936-
937943
private String getStateParameter(HttpServletRequest request, AuthenticationContext context,
938944
Map<String, String> authenticatorProperties) {
939945

@@ -1047,12 +1053,13 @@ protected void processAuthenticationResponse(HttpServletRequest request, HttpSer
10471053
LOG.debug("Retrieved the User Information:" + jwtAttributeMap);
10481054
}
10491055

1050-
if (StringUtils.isNotBlank((String) context.getProperty(OIDC_FEDERATION_NONCE))) {
1056+
String nonceKey = getName() + OIDC_FEDERATION_NONCE;
1057+
if (StringUtils.isNotBlank((String) context.getProperty(nonceKey))) {
10511058
String nonce = (String) jwtAttributeMap.get(NONCE);
10521059
if (nonce == null) {
10531060
LOG.debug("OIDC provider does not support nonce claim in id_token.");
10541061
}
1055-
if (nonce != null && !nonce.equals(context.getProperty(OIDC_FEDERATION_NONCE))) {
1062+
if (nonce != null && !nonce.equals(context.getProperty(nonceKey))) {
10561063
setAuthenticatorMessageToContext(ErrorMessages.NONCE_MISMATCH, context);
10571064

10581065
throw new AuthenticationFailedException(ErrorMessages.NONCE_MISMATCH.getCode(),
@@ -1829,26 +1836,25 @@ private static AdditionalData getAdditionalData(
18291836
AuthenticationContext context, boolean isNativeSDKBasedFederationCall) {
18301837

18311838
AdditionalData additionalData = new AdditionalData();
1839+
String currentAuthenticator = StringUtils.isNotBlank(context.getCurrentAuthenticator()) ?
1840+
context.getCurrentAuthenticator() : OIDCAuthenticatorConstants.AUTHENTICATOR_NAME;
18321841

18331842
if (isNativeSDKBasedFederationCall) {
18341843
Map<String, String> additionalAuthenticationParams = new HashMap<>();
18351844

1836-
String nonce = (String) context.getProperty(OIDC_FEDERATION_NONCE);
1845+
String nonce = (String) context.getProperty(currentAuthenticator + OIDC_FEDERATION_NONCE);
18371846
if (StringUtils.isNotBlank(nonce)) {
18381847
additionalAuthenticationParams.put(NONCE, nonce);
18391848
}
18401849
additionalAuthenticationParams.put(OIDCAuthenticatorConstants.CLIENT_ID_PARAM,
18411850
context.getAuthenticatorProperties().get(OIDCAuthenticatorConstants.CLIENT_ID));
1842-
String scope = (String) context.getProperty(OIDCAuthenticatorConstants.AUTHENTICATOR_NAME +
1843-
SCOPE_PARAM_SUFFIX);
1851+
String scope = (String) context.getProperty(currentAuthenticator + SCOPE_PARAM_SUFFIX);
18441852
additionalAuthenticationParams.put(OIDCAuthenticatorConstants.SCOPE, scope);
18451853
additionalData.setAdditionalAuthenticationParams(additionalAuthenticationParams);
18461854
} else {
1847-
additionalData.setRedirectUrl((String) context.getProperty(OIDCAuthenticatorConstants.AUTHENTICATOR_NAME +
1848-
REDIRECT_URL_SUFFIX));
1855+
additionalData.setRedirectUrl((String) context.getProperty(currentAuthenticator + REDIRECT_URL_SUFFIX));
18491856
Map<String, String> additionalAuthenticationParams = new HashMap<>();
1850-
String state = (String) context.getProperty(OIDCAuthenticatorConstants.AUTHENTICATOR_NAME +
1851-
STATE_PARAM_SUFFIX);
1857+
String state = (String) context.getProperty(currentAuthenticator + STATE_PARAM_SUFFIX);
18521858
additionalAuthenticationParams.put(OIDCAuthenticatorConstants.OAUTH2_PARAM_STATE, state);
18531859
additionalData.setAdditionalAuthenticationParams(additionalAuthenticationParams);
18541860
}

components/org.wso2.carbon.identity.application.authenticator.oidc/src/test/java/org/wso2/carbon/identity/application/authenticator/oidc/OpenIDConnectAuthenticatorTest.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,8 @@ public void testFailProcessAuthenticationWhenNonceMisMatch() throws Exception {
629629
when(identityProvider.getIdpProperties()).thenReturn(identityProviderProperties);
630630
whenNew(OAuthClient.class).withAnyArguments().thenReturn(mockOAuthClient);
631631
when(mockOAuthClient.accessToken(any())).thenReturn(mockOAuthJSONAccessTokenResponse);
632-
when(mockAuthenticationContext.getProperty(OIDC_FEDERATION_NONCE)).thenReturn(invalidNonce);
632+
String nonceKey = openIDConnectAuthenticator.getName() + OIDC_FEDERATION_NONCE;
633+
when(mockAuthenticationContext.getProperty(nonceKey)).thenReturn(invalidNonce);
633634
when(mockOAuthJSONAccessTokenResponse.getParam(anyString())).thenReturn(idToken);
634635

635636
Assert.assertThrows(
@@ -1082,7 +1083,8 @@ public void testGetAuthInitiationDataForNativeSDKBasedFederation() {
10821083
when(externalIdPConfig.getIdentityProvider()).thenReturn(identityProvider);
10831084
when(identityProvider.getIdpProperties()).thenReturn(identityProviderProperties);
10841085
when(mockAuthenticationContext.getAuthenticationRequest()).thenReturn(mockAuthenticationRequest);
1085-
when(mockAuthenticationContext.getProperty(OIDC_FEDERATION_NONCE)).thenReturn(nonce);
1086+
String nonceKey = openIDConnectAuthenticator.getName() + OIDC_FEDERATION_NONCE;
1087+
when(mockAuthenticationContext.getProperty(nonceKey)).thenReturn(nonce);
10861088
when(mockAuthenticationContext.getAuthenticatorProperties()).thenReturn(authenticatorProperties);
10871089
authenticatorProperties.put(OIDCAuthenticatorConstants.CLIENT_ID, clientId);
10881090

0 commit comments

Comments
 (0)