Skip to content

Commit aaf534d

Browse files
committed
Fix callback url resolving logic for api based authn
1 parent ddd3275 commit aaf534d

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

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

+36-10
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,9 @@ protected String getAuthorizationServerEndpoint(Map<String, String> authenticato
280280
* configuration.
281281
* @return Callback URL configured in OIDC federated authenticator configuration. If it is empty returns
282282
* /commonauth endpoint URL path as the default value.
283+
* @deprecated use {@link #getCallbackUrl(Map, AuthenticationContext)}.
283284
*/
285+
@Deprecated
284286
protected String getCallbackUrl(Map<String, String> authenticatorProperties) {
285287

286288
String callbackUrl = authenticatorProperties.get(IdentityApplicationConstants.OAuth2.CALLBACK_URL);
@@ -295,6 +297,33 @@ protected String getCallbackUrl(Map<String, String> authenticatorProperties) {
295297
return callbackUrl;
296298
}
297299

300+
/**
301+
* Returns the callback URL of the IdP Hub.
302+
*
303+
* @param authenticatorProperties Authentication properties configured in OIDC federated authenticator
304+
* configuration.
305+
* @param context Authentication context.
306+
* @return If API based authn flow, returns the redirect URL from the authentication context. If not returns the
307+
* callback URL configured in OIDC federated authenticator configuration and if it is empty returns
308+
* /commonauth endpoint URL path as the default value.
309+
*/
310+
protected String getCallbackUrl(Map<String, String> authenticatorProperties, AuthenticationContext context) {
311+
312+
if (Boolean.parseBoolean((String) context.getProperty(IS_API_BASED))) {
313+
return (String) context.getProperty(REDIRECT_URL);
314+
}
315+
String callbackUrl = authenticatorProperties.get(IdentityApplicationConstants.OAuth2.CALLBACK_URL);
316+
if (StringUtils.isBlank(callbackUrl)) {
317+
try {
318+
callbackUrl = ServiceURLBuilder.create().addPath(FrameworkConstants.COMMONAUTH).build()
319+
.getAbsolutePublicURL();
320+
} catch (URLBuilderException e) {
321+
throw new RuntimeException("Error occurred while building URL in tenant qualified mode.", e);
322+
}
323+
}
324+
return callbackUrl;
325+
}
326+
298327
/**
299328
* Resolve the callback URL from the context properties to use in the API based authentication flow.
300329
*
@@ -379,6 +408,10 @@ protected String getAuthenticateUser(AuthenticationContext context, Map<String,
379408
return (String) oidcClaims.get(OIDCAuthenticatorConstants.Claim.SUB);
380409
}
381410

411+
/**
412+
* @deprecated use {@link #getCallbackUrl(Map, AuthenticationContext)} instead.
413+
*/
414+
@Deprecated
382415
protected String getCallBackURL(Map<String, String> authenticatorProperties) {
383416

384417
return getCallbackUrl(authenticatorProperties);
@@ -514,11 +547,8 @@ protected String prepareLoginPage(HttpServletRequest request, AuthenticationCont
514547
if (authenticatorProperties != null) {
515548
String clientId = authenticatorProperties.get(OIDCAuthenticatorConstants.CLIENT_ID);
516549
String authorizationEP = getOIDCAuthzEndpoint(authenticatorProperties);
517-
String callbackurl = getCallbackUrl(authenticatorProperties);
550+
String callbackurl = getCallbackUrl(authenticatorProperties, context);
518551

519-
if (Boolean.parseBoolean((String) context.getProperty(IS_API_BASED))) {
520-
callbackurl = resolveCallBackURLForAPIBasedAuthFlow(context);
521-
}
522552
String state = getStateParameter(request, context, authenticatorProperties);
523553
context.setProperty(OIDCAuthenticatorConstants.AUTHENTICATOR_NAME + STATE_PARAM_SUFFIX, state);
524554
String nonce = UUID.randomUUID().toString();
@@ -1321,7 +1351,7 @@ protected void initiateLogoutRequest(HttpServletRequest request, HttpServletResp
13211351
paramMap.put(OIDCAuthenticatorConstants.ID_TOKEN_HINT, idTokenHint);
13221352
}
13231353

1324-
String callback = getCallbackUrl(context.getAuthenticatorProperties());
1354+
String callback = getCallbackUrl(context.getAuthenticatorProperties(), context);
13251355
paramMap.put(OIDCAuthenticatorConstants.POST_LOGOUT_REDIRECT_URI, callback);
13261356

13271357
String sessionID = getStateParameter(request, context, context.getAuthenticatorProperties());
@@ -1493,11 +1523,7 @@ protected OAuthClientRequest getAccessTokenRequest(AuthenticationContext context
14931523

14941524
String callbackUrl = getCallbackUrlFromInitialRequestParamMap(context);
14951525
if (StringUtils.isBlank(callbackUrl)) {
1496-
if (Boolean.parseBoolean((String) context.getProperty(IS_API_BASED))) {
1497-
callbackUrl = resolveCallBackURLForAPIBasedAuthFlow(context);
1498-
} else {
1499-
callbackUrl = getCallbackUrl(authenticatorProperties);
1500-
}
1526+
callbackUrl = getCallbackUrl(authenticatorProperties, context);
15011527
}
15021528

15031529
boolean isHTTPBasicAuth = Boolean.parseBoolean(authenticatorProperties.get(OIDCAuthenticatorConstants

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ public void testGetAuthorizationServerEndpoint() throws IOException {
316316
@Test
317317
public void testGetCallbackUrl() throws IOException {
318318

319-
assertEquals(openIDConnectAuthenticator.getCallBackURL(authenticatorProperties),
319+
assertEquals(openIDConnectAuthenticator.getCallbackUrl(authenticatorProperties, mockAuthenticationContext),
320320
"http://localhost:8080/playground2/oauth2client",
321321
"Callback URL is not valid.");
322322
}
@@ -359,7 +359,7 @@ public void testRequiredIDToken() throws IOException {
359359
@Test
360360
public void testGetCallBackURL() throws IOException {
361361

362-
assertEquals(openIDConnectAuthenticator.getCallBackURL(authenticatorProperties),
362+
assertEquals(openIDConnectAuthenticator.getCallbackUrl(authenticatorProperties, mockAuthenticationContext),
363363
"http://localhost:8080/playground2/oauth2client",
364364
"Callback URL is not valid.");
365365
}

0 commit comments

Comments
 (0)