diff --git a/src/main/java/org/sonarqube/auth/googleoauth/GoogleIdentityProvider.java b/src/main/java/org/sonarqube/auth/googleoauth/GoogleIdentityProvider.java index d5e5b23..4cad830 100644 --- a/src/main/java/org/sonarqube/auth/googleoauth/GoogleIdentityProvider.java +++ b/src/main/java/org/sonarqube/auth/googleoauth/GoogleIdentityProvider.java @@ -53,6 +53,9 @@ import javax.servlet.http.HttpServletRequest; import java.io.IOException; +import java.net.URL; +import java.net.URLDecoder; + import static java.lang.String.format; @ServerSide @@ -122,7 +125,25 @@ public void callback(CallbackContext context) { GsonUser gsonUser = requestUser(scribe, accessToken); String redirectTo; if (settings.oauthDomain()==null || (settings.oauthDomain()!=null && gsonUser.getEmail().endsWith("@"+settings.oauthDomain()))) { - redirectTo = settings.getSonarBaseURL(); + redirectTo = settings.getSonarBaseURL(); + String referer_url = request.getHeader("referer"); + try { + URL urlObj = new URL(referer_url); + String returnToValue = null; + for( String param : urlObj.getQuery().split("&")) { + if( param.startsWith("return_to=")){ + System.out.println("Return_to param : " + param); + System.out.println("Web context : " + settings.getWebContext()); + param = URLDecoder.decode(param,"UTF-8"); + returnToValue = param.split("=",2)[1].replace(settings.getWebContext(),""); + } + } + if(returnToValue != null){ + redirectTo = redirectTo.concat(returnToValue); + } + } catch(Exception e) { + LOGGER.trace("Exception while parsing return to URL"); + } UserIdentity userIdentity = userIdentityFactory.create(gsonUser); context.authenticate(userIdentity); } else { diff --git a/src/main/java/org/sonarqube/auth/googleoauth/GoogleSettings.java b/src/main/java/org/sonarqube/auth/googleoauth/GoogleSettings.java index b8a65e6..3434d53 100644 --- a/src/main/java/org/sonarqube/auth/googleoauth/GoogleSettings.java +++ b/src/main/java/org/sonarqube/auth/googleoauth/GoogleSettings.java @@ -69,6 +69,7 @@ public class GoogleSettings { public static final String LOGIN_STRATEGY_DEFAULT_VALUE = LOGIN_STRATEGY_UNIQUE; public static final String CATEGORY = "security"; public static final String SUBCATEGORY = "googleoauth"; + public static final String SONAR_CONTEXT = "sonar.web.context"; private final Settings settings; @@ -109,6 +110,10 @@ public String webURL() { } return url; } + + public String getWebContext() { + return settings.getString(SONAR_CONTEXT) == null ? "" : settings.getString(SONAR_CONTEXT); + } public String apiURL() { String url = settings.getString(API_URL);