2626import javax .servlet .http .HttpServletRequest ;
2727import 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 *
@@ -62,6 +65,8 @@ public ModelAndView handleRequest(final HttpServletRequest request, final HttpSe
6265 // somehow uses "https://127.0.0.1". This is a work-around for a weird tomcat relative redirect issue on staging 1 and prod.
6366 val serverPrefix = getOAuthConfigurationContext ().getCasProperties ().getServer ().getPrefix ();
6467 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 );
6570 callback .perform (
6671 context ,
6772 getOAuthConfigurationContext ().getOauthConfig (), (object , ctx ) -> Boolean .FALSE ,
@@ -72,6 +77,21 @@ public ModelAndView handleRequest(final HttpServletRequest request, final HttpSe
7277 Authenticators .CAS_OAUTH_CLIENT
7378 );
7479 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+ }
7595 if (StringUtils .isBlank (url )) {
7696 modelContext .setErrorCode (OsfCasOAuth20Constants .SESSION_STALE_MISMATCH );
7797 LOGGER .error (modelContext .getErrorMsg ());
0 commit comments