Skip to content

Commit 508f9b2

Browse files
committed
Merge branch 'release/21.2.2'
2 parents c3085f2 + bc3a962 commit 508f9b2

3 files changed

Lines changed: 45 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
We follow the CalVer (https://calver.org/) versioning scheme: YY.MINOR.MICRO.
44

5+
21.2.2 (04-29-2021)
6+
===================
7+
8+
Fix OAuth callback authorize redirect URL and consent approval callback URL
9+
510
21.2.1 (04-28-2021)
611
===================
712

src/main/java/org/apereo/cas/support/oauth/web/endpoints/OAuth20CallbackAuthorizeEndpointController.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
import javax.servlet.http.HttpServletRequest;
2727
import javax.servlet.http.HttpServletResponse;
2828

29+
import java.net.URI;
30+
import java.net.URISyntaxException;
31+
2932
/**
3033
* OAuth callback authorize controller based on the pac4j callback controller.
3134
*
@@ -58,16 +61,37 @@ public ModelAndView handleRequest(final HttpServletRequest request, final HttpSe
5861

5962
val callback = new OAuth20CallbackLogic();
6063
val context = new JEEContext(request, response, getOAuthConfigurationContext().getSessionStore());
64+
// Build the request URL with server prefix "https://accounts.osf.io" instead of calling `context.getFullRequestURL()` which
65+
// somehow uses "https://127.0.0.1". This is a work-around for a weird tomcat relative redirect issue on staging 1 and prod.
66+
val serverPrefix = getOAuthConfigurationContext().getCasProperties().getServer().getPrefix();
67+
val fullRequestUrl = serverPrefix + request.getRequestURI() + '?' + request.getQueryString();
68+
LOGGER.debug("[OAuth callbackAuthorize] full request URL in context [{}]", context.getFullRequestURL());
69+
LOGGER.debug("[OAuth callbackAuthorize] full request URL customized [{}]", fullRequestUrl);
6170
callback.perform(
6271
context,
6372
getOAuthConfigurationContext().getOauthConfig(), (object, ctx) -> Boolean.FALSE,
64-
context.getFullRequestURL(),
73+
fullRequestUrl,
6574
Boolean.TRUE,
6675
Boolean.FALSE,
6776
Boolean.FALSE,
6877
Authenticators.CAS_OAUTH_CLIENT
6978
);
7079
var url = callback.getRedirectUrl();
80+
if (!url.startsWith(serverPrefix)) {
81+
LOGGER.warn("[OAuth callbackAuthorize] redirect URL [{}] doesn't have the correct server prefix [{}]", url, serverPrefix);
82+
try {
83+
URI uri = new URI(url);
84+
url = serverPrefix + uri.getPath() + '?' + uri.getRawQuery();
85+
LOGGER.warn("[OAuth callbackAuthorize] use server prefix for redirect URL [{}]", url);
86+
} catch (URISyntaxException e) {
87+
modelContext.setErrorCode(OsfCasOAuth20Constants.INTERNAL_SERVER_ERROR);
88+
modelContext.setErrorMsg(e.getMessage());
89+
modelContext.setOsfUrl(getOsfUrl());
90+
return OsfCasOAuth20Utils.produceOAuth20ErrorView(modelContext);
91+
}
92+
} else {
93+
LOGGER.info("[OAuth callbackAuthorize] redirect URL [{}] uses the correct server prefix: [{}}]", url, serverPrefix);
94+
}
7195
if (StringUtils.isBlank(url)) {
7296
modelContext.setErrorCode(OsfCasOAuth20Constants.SESSION_STALE_MISMATCH);
7397
LOGGER.error(modelContext.getErrorMsg());
@@ -76,7 +100,7 @@ public ModelAndView handleRequest(final HttpServletRequest request, final HttpSe
76100
return OsfCasOAuth20Utils.produceOAuth20ErrorView(modelContext);
77101
}
78102
val manager = new ProfileManager<>(context, context.getSessionStore());
79-
LOGGER.trace("OAuth callback URL is [{}]", url);
103+
LOGGER.debug("OAuth callback URL is [{}]", url);
80104
return getOAuthConfigurationContext().getCallbackAuthorizeViewResolver().resolve(context, manager, url);
81105
}
82106

src/main/java/org/apereo/cas/support/oauth/web/views/OAuth20ConsentApprovalViewResolver.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.springframework.web.servlet.ModelAndView;
2222

23+
import java.net.URI;
2324
import java.net.URISyntaxException;
2425
import java.util.HashSet;
2526
import java.util.Map;
@@ -139,14 +140,24 @@ protected ModelAndView redirectToApproveView(final JEEContext context, final Osf
139140
*/
140141
protected String getCallbackUrl(final JEEContext context) throws URISyntaxException {
141142
val callbackUrl = context.getFullRequestURL();
143+
val serverPrefix = casProperties.getServer().getPrefix();
144+
if (!callbackUrl.startsWith(serverPrefix)) {
145+
LOGGER.warn("[OAuth ConsentApproval] callback URL [{}] doesn't have the correct server prefix [{}]", callbackUrl, serverPrefix);
146+
} else {
147+
LOGGER.info("[OAuth ConsentApproval] callback URL [{}] uses the correct server prefix [{}]", callbackUrl, serverPrefix);
148+
}
149+
val serverPrefixUrl = new URI(serverPrefix);
142150
val url = new URIBuilder(callbackUrl);
151+
url.setScheme(serverPrefixUrl.getScheme());
152+
url.setHost(serverPrefixUrl.getHost());
153+
url.setPort(-1);
143154
// APPROVAL_PROMPT can be set to any value. It does not have any effect since BYPASS_APPROVAL_PROMPT is present.
144155
// However, setting it to EMPTY is preferred so that it is different from its original values "auto" or "force".
145156
url.setParameter(OsfCasOAuth20Constants.APPROVAL_PROMPT, StringUtils.EMPTY);
146157
url.setParameter(OAuth20Constants.BYPASS_APPROVAL_PROMPT, Boolean.TRUE.toString());
147-
LOGGER.debug("Callback URL for approval submit action: [{}]", callbackUrl);
148-
return url.toString();
149-
158+
val approvalCallbackUrl = url.toString();
159+
LOGGER.info("[OAuth ConsentApproval] modified approval callback URL [{}]", approvalCallbackUrl);
160+
return approvalCallbackUrl;
150161
}
151162

152163
/**

0 commit comments

Comments
 (0)