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 *
@@ -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
0 commit comments