public class InitFilter extends ServletFilter {
- private static final String INIT_CONTEXT = "/sessions/init";
+ private static final String INIT_CONTEXT = "/sessions/init/";
private final IdentityProviderRepository identityProviderRepository;
private final BaseContextFactory baseContextFactory;
@Override
public UrlPattern doGetPattern() {
- return UrlPattern.create(INIT_CONTEXT + "/*");
+ return UrlPattern.create(INIT_CONTEXT + "*");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
- String requestUri = httpRequest.getRequestURI();
- final String keyProvider = requestUri.replace(INIT_CONTEXT + "/", "");
+ String requestURI = httpRequest.getRequestURI();
+ String keyProvider = "";
try {
- if (isNullOrEmpty(keyProvider)) {
- throw new IllegalArgumentException("A valid identity provider key is required");
- }
-
+ keyProvider = extractKeyProvider(requestURI, INIT_CONTEXT);
IdentityProvider provider = identityProviderRepository.getEnabledByKey(keyProvider);
if (provider instanceof BaseIdentityProvider) {
BaseIdentityProvider baseIdentityProvider = (BaseIdentityProvider) provider;
}
}
+ public static String extractKeyProvider(String requestUri, String context) {
+ if (requestUri.contains(context)) {
+ String key = requestUri.replace(context, "");
+ if (!isNullOrEmpty(key)) {
+ return key;
+ }
+ }
+ throw new IllegalArgumentException("A valid identity provider key is required.");
+ }
+
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// Nothing to do
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import org.sonar.api.CoreProperties;
import org.sonar.api.server.authentication.IdentityProvider;
import org.sonar.api.server.authentication.OAuth2IdentityProvider;
import org.sonar.api.server.authentication.UnauthorizedException;
import org.sonar.api.web.ServletFilter;
+import static com.google.common.base.Strings.isNullOrEmpty;
import static java.lang.String.format;
import static org.sonar.server.authentication.AuthenticationError.handleError;
import static org.sonar.server.authentication.AuthenticationError.handleUnauthorizedError;
public class OAuth2CallbackFilter extends ServletFilter {
- public static final String CALLBACK_PATH = "/oauth2/callback";
+ public static final String CALLBACK_PATH = "/oauth2/callback/";
private final IdentityProviderRepository identityProviderRepository;
private final OAuth2ContextFactory oAuth2ContextFactory;
@Override
public UrlPattern doGetPattern() {
- return UrlPattern.create(CALLBACK_PATH + "/*");
+ return UrlPattern.create(CALLBACK_PATH + "*");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
String requestUri = httpRequest.getRequestURI();
- String keyProvider = requestUri.replace(CALLBACK_PATH + "/", "");
-
+ String keyProvider = "";
try {
+ keyProvider = extractKeyProvider(requestUri, CALLBACK_PATH);
IdentityProvider provider = identityProviderRepository.getEnabledByKey(keyProvider);
if (provider instanceof OAuth2IdentityProvider) {
OAuth2IdentityProvider oauthProvider = (OAuth2IdentityProvider) provider;
} catch (UnauthorizedException e) {
handleUnauthorizedError(e, (HttpServletResponse) response);
} catch (Exception e) {
- handleError(e, (HttpServletResponse) response, format("Fail to callback authentication with %s", keyProvider));
+ handleError(e, (HttpServletResponse) response,
+ keyProvider.isEmpty() ? "Fail to callback authentication" :
+ format("Fail to callback authentication with '%s'", keyProvider));
+ }
+ }
+
+ public static String extractKeyProvider(String requestUri, String context) {
+ if (requestUri.contains(context)) {
+ String key = requestUri.replace(context, "");
+ if (!isNullOrEmpty(key)) {
+ return key;
+ }
}
+ throw new IllegalArgumentException(String.format("A valid identity provider key is required. Please check that property '%s' is valid.", CoreProperties.SERVER_BASE_URL));
}
@Override
if (publicRootUrl.startsWith("http:") && !server.isDev()) {
throw MessageException.of(format("The server url should be configured in https, please update the property '%s'", SERVER_BASE_URL));
}
- return publicRootUrl + CALLBACK_PATH + "/" + identityProvider.getKey();
+ return publicRootUrl + CALLBACK_PATH + identityProvider.getKey();
}
@Override
}
@Test
- public void fail_if_identity_provider_class_is_unsuported() throws Exception {
+ public void fail_if_uri_doesnt_contains_callback() throws Exception {
+ when(request.getRequestURI()).thenReturn("/sessions/init");
+
+ underTest.doFilter(request, response, chain);
+
+ assertError("Fail to initialize authentication with provider ''");
+ }
+
+ @Test
+ public void fail_if_identity_provider_class_is_unsupported() throws Exception {
final String unsupportedKey = "unsupported";
when(request.getRequestURI()).thenReturn("/sessions/init/" + unsupportedKey);
identityProviderRepository.addIdentityProvider(new IdentityProvider() {
underTest.doFilter(request, response, chain);
- assertError("Fail to callback authentication with github");
+ assertError("Fail to callback authentication with 'github'");
}
@Test
verify(response).sendRedirect("/sessions/unauthorized?message=Email+john%40email.com+is+already+used");
}
+ @Test
+ public void fail_when_no_oauth2_provider_provided() throws Exception {
+ String providerKey = "openid";
+ when(request.getRequestURI()).thenReturn("/oauth2/callback");
+ identityProviderRepository.addIdentityProvider(new FakeBasicIdentityProvider(providerKey, true));
+
+ underTest.doFilter(request, response, chain);
+
+ assertError("Fail to callback authentication");
+ }
+
private void assertCallbackCalled(){
assertThat(logTester.logs(LoggerLevel.ERROR)).isEmpty();
assertThat(oAuth2IdentityProvider.isCallbackCalled()).isTrue();