Search in sources :

Example 1 with UnauthorizedException

use of org.sonar.server.exceptions.UnauthorizedException in project sonarqube by SonarSource.

the class LoginAction method doFilter.

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    HttpServletResponse response = (HttpServletResponse) servletResponse;
    if (!request.getMethod().equals(POST.name())) {
        response.setStatus(HTTP_BAD_REQUEST);
        return;
    }
    String login = request.getParameter("login");
    String password = request.getParameter("password");
    try {
        UserDto userDto = authenticate(request, login, password);
        jwtHttpHandler.generateToken(userDto, request, response);
        threadLocalUserSession.set(userSessionFactory.create(userDto));
    } catch (AuthenticationException e) {
        authenticationEvent.loginFailure(request, e);
        response.setStatus(HTTP_UNAUTHORIZED);
    } catch (UnauthorizedException e) {
        response.setStatus(e.httpCode());
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) AuthenticationException(org.sonar.server.authentication.event.AuthenticationException) UserDto(org.sonar.db.user.UserDto) UnauthorizedException(org.sonar.server.exceptions.UnauthorizedException) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Example 2 with UnauthorizedException

use of org.sonar.server.exceptions.UnauthorizedException in project sonarqube by SonarSource.

the class LoginActionTest method return_authorized_code_when_unauthorized_exception_is_thrown.

@Test
public void return_authorized_code_when_unauthorized_exception_is_thrown() throws Exception {
    doThrow(new UnauthorizedException("error !")).when(credentialsAuthenticator).authenticate(LOGIN, PASSWORD, request, FORM);
    executeRequest(LOGIN, PASSWORD);
    verify(response).setStatus(401);
    assertThat(threadLocalUserSession.hasSession()).isFalse();
    verifyZeroInteractions(authenticationEvent);
}
Also used : UnauthorizedException(org.sonar.server.exceptions.UnauthorizedException) Test(org.junit.Test)

Aggregations

UnauthorizedException (org.sonar.server.exceptions.UnauthorizedException)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Test (org.junit.Test)1 UserDto (org.sonar.db.user.UserDto)1 AuthenticationException (org.sonar.server.authentication.event.AuthenticationException)1