String getContextPath() { return server.getContextPath(); } }
public MacroInterpreter(Server server) { this.macros = ImmutableList.of( new RuleMacro(server.getContextPath()) ); }
@Override public void redirectToRequestedPage() { try { Optional<String> redirectTo = oAuthParameters.getReturnTo(request); oAuthParameters.delete(request, response); getResponse().sendRedirect(redirectTo.orElse(server.getContextPath() + "/")); } catch (IOException e) { throw new IllegalStateException("Fail to redirect to requested page", e); } }
/** * @return the {@link IdentityProvider} for the key extracted in the request if is exists, or {@code null}, in which * case the request is fully handled and caller should not handle it */ @CheckForNull IdentityProvider resolveProviderOrHandleResponse(HttpServletRequest request, HttpServletResponse response, String path) { String requestUri = request.getRequestURI(); String providerKey = extractKeyProvider(requestUri, server.getContextPath() + path); if (providerKey == null) { handleError(response, "No provider key found in URI"); return null; } try { return identityProviderRepository.getEnabledByKey(providerKey); } catch (Exception e) { handleError(e, response, format("Failed to retrieve IdentityProvider for key '%s'", providerKey)); return null; } }
@Before public void setUp() throws Exception { when(server.getContextPath()).thenReturn(""); }
@Before public void setUp() throws Exception { when(server.getContextPath()).thenReturn(""); when(identityProvider.getName()).thenReturn(PROVIDER_NAME); }
@Before public void setUp() throws Exception { when(oAuth2ContextFactory.newCallback(request, response, oAuth2IdentityProvider)).thenReturn(mock(OAuth2IdentityProvider.CallbackContext.class)); when(server.getContextPath()).thenReturn(""); }
@Before public void setUp() { Server server = mock(Server.class); when(server.getContextPath()).thenReturn(path); interpreter = new MacroInterpreter(server); }
@Before public void setUp() throws Exception { when(oAuth2ContextFactory.newContext(request, response, oAuth2IdentityProvider)).thenReturn(oauth2Context); when(baseContextFactory.newContext(request, response, baseIdentityProvider)).thenReturn(baseContext); when(server.getContextPath()).thenReturn(""); }
@Test public void init_authentication_parameter_on_auth2_identity_provider() { when(server.getContextPath()).thenReturn("/sonarqube"); when(request.getRequestURI()).thenReturn("/sonarqube/sessions/init/" + OAUTH2_PROVIDER_KEY); identityProviderRepository.addIdentityProvider(oAuth2IdentityProvider); underTest.doFilter(request, response, chain); verify(auth2AuthenticationParameters).init(eq(request), eq(response)); }
@Test public void do_filter_with_context() { when(server.getContextPath()).thenReturn("/sonarqube"); when(request.getRequestURI()).thenReturn("/sonarqube/sessions/init/" + OAUTH2_PROVIDER_KEY); identityProviderRepository.addIdentityProvider(oAuth2IdentityProvider); underTest.doFilter(request, response, chain); assertOAuth2InitCalled(); verifyZeroInteractions(authenticationEvent); }
@Test public void redirect_to_home_with_context() throws Exception { when(server.getContextPath()).thenReturn("/sonarqube"); when(oAuthParameters.getReturnTo(request)).thenReturn(Optional.empty()); OAuth2IdentityProvider.CallbackContext callback = newCallbackContext(); callback.redirectToRequestedPage(); verify(response).sendRedirect("/sonarqube/"); }
@Test public void redirect_to_home() throws Exception { when(server.getContextPath()).thenReturn(""); when(oAuthParameters.getReturnTo(request)).thenReturn(Optional.empty()); OAuth2IdentityProvider.CallbackContext callback = newCallbackContext(); callback.redirectToRequestedPage(); verify(response).sendRedirect("/"); }
@Test public void redirect_to_requested_page_does_not_need_context() throws Exception { when(oAuthParameters.getReturnTo(request)).thenReturn(Optional.of("/sonarqube/settings")); when(server.getContextPath()).thenReturn("/other"); OAuth2IdentityProvider.CallbackContext callback = newCallbackContext(); callback.redirectToRequestedPage(); verify(response).sendRedirect("/sonarqube/settings"); }
@Test public void redirect_to_requested_page() throws Exception { when(oAuthParameters.getReturnTo(request)).thenReturn(Optional.of("/settings")); when(server.getContextPath()).thenReturn(""); OAuth2IdentityProvider.CallbackContext callback = newCallbackContext(); callback.redirectToRequestedPage(); verify(response).sendRedirect("/settings"); }
@Test public void delete_oauth2_parameters_during_redirection() { when(oAuthParameters.getReturnTo(request)).thenReturn(Optional.of("/settings")); when(server.getContextPath()).thenReturn(""); OAuth2IdentityProvider.CallbackContext callback = newCallbackContext(); callback.redirectToRequestedPage(); verify(oAuthParameters).delete(eq(request), eq(response)); }
@Test public void redirect_with_context_path_when_failing_because_of_UnauthorizedExceptionException() throws Exception { when(server.getContextPath()).thenReturn("/sonarqube"); FailWithUnauthorizedExceptionIdProvider identityProvider = new FailWithUnauthorizedExceptionIdProvider(); when(request.getRequestURI()).thenReturn("/sonarqube/oauth2/callback/" + identityProvider.getKey()); identityProviderRepository.addIdentityProvider(identityProvider); underTest.doFilter(request, response, chain); verify(response).sendRedirect("/sonarqube/sessions/unauthorized?message=Email+john%40email.com+is+already+used"); verify(oAuthRedirection).delete(eq(request), eq(response)); }
@Test public void redirect_with_context_path_when_failing_because_of_UnauthorizedException() throws Exception { when(server.getContextPath()).thenReturn("/sonarqube"); IdentityProvider identityProvider = new FailWithUnauthorizedExceptionIdProvider("failing"); when(request.getRequestURI()).thenReturn("/sonarqube/sessions/init/" + identityProvider.getKey()); identityProviderRepository.addIdentityProvider(identityProvider); underTest.doFilter(request, response, chain); verify(response).sendRedirect("/sonarqube/sessions/unauthorized?message=Email+john%40email.com+is+already+used"); verifyDeleteAuthCookie(); }
@Test public void do_filter_with_context() { when(server.getContextPath()).thenReturn("/sonarqube"); when(request.getRequestURI()).thenReturn("/sonarqube/oauth2/callback/" + OAUTH2_PROVIDER_KEY); identityProviderRepository.addIdentityProvider(oAuth2IdentityProvider); underTest.doFilter(request, response, chain); assertCallbackCalled(oAuth2IdentityProvider); verify(authenticationEvent).loginSuccess(request, LOGIN, Source.oauth2(oAuth2IdentityProvider)); }
@Test public void do_filter_with_context_no_log_if_provider_did_not_call_authenticate_on_context() { when(server.getContextPath()).thenReturn("/sonarqube"); when(request.getRequestURI()).thenReturn("/sonarqube/oauth2/callback/" + OAUTH2_PROVIDER_KEY); FakeOAuth2IdentityProvider identityProvider = new FakeOAuth2IdentityProvider(OAUTH2_PROVIDER_KEY, true); identityProviderRepository.addIdentityProvider(identityProvider); underTest.doFilter(request, response, chain); assertCallbackCalled(identityProvider); verify(authenticationEvent).loginFailure(eq(request), authenticationExceptionCaptor.capture()); AuthenticationException authenticationException = authenticationExceptionCaptor.getValue(); assertThat(authenticationException).hasMessage("Plugin did not call authenticate"); assertThat(authenticationException.getSource()).isEqualTo(Source.oauth2(identityProvider)); assertThat(authenticationException.getLogin()).isNull(); assertThat(authenticationException.getPublicMessage()).isNull(); }