Search in sources :

Example 1 with UsernamePassword

use of com.thoughtworks.go.server.newsecurity.models.UsernamePassword in project gocd by gocd.

the class AuthenticationController method performLogin.

@RequestMapping(value = "/auth/security_check", method = RequestMethod.POST)
public RedirectView performLogin(@RequestParam("j_username") String username, @RequestParam("j_password") String password, HttpServletRequest request) {
    if (securityIsDisabledOrAlreadyLoggedIn(request)) {
        return new RedirectView("/pipelines", true);
    }
    LOGGER.debug("Requesting authentication for form auth.");
    try {
        SavedRequest savedRequest = SessionUtils.savedRequest(request);
        final AuthenticationToken<UsernamePassword> authenticationToken = passwordBasedPluginAuthenticationProvider.authenticate(new UsernamePassword(username, password), null);
        if (authenticationToken == null) {
            return badAuthentication(request, BAD_CREDENTIALS_MSG);
        } else {
            SessionUtils.setAuthenticationTokenAfterRecreatingSession(authenticationToken, request);
        }
        String redirectUrl = savedRequest == null ? "/go/pipelines" : savedRequest.getRedirectUrl();
        return new RedirectView(redirectUrl, false);
    } catch (AuthenticationException e) {
        LOGGER.error("Failed to authenticate user: {} ", username, e);
        return badAuthentication(request, e.getMessage());
    } catch (Exception e) {
        return unknownAuthenticationError(request);
    }
}
Also used : AuthenticationException(org.springframework.security.core.AuthenticationException) RedirectView(org.springframework.web.servlet.view.RedirectView) AuthenticationException(org.springframework.security.core.AuthenticationException) SavedRequest(org.springframework.security.web.savedrequest.SavedRequest) UsernamePassword(com.thoughtworks.go.server.newsecurity.models.UsernamePassword) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with UsernamePassword

use of com.thoughtworks.go.server.newsecurity.models.UsernamePassword in project gocd by gocd.

the class AbstractBasicAuthenticationFilter method doFilterInternal.

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException {
    try {
        if (isPreviouslyAuthenticated(request)) {
            LOGGER.debug("Request is already authenticated.");
            filterChain.doFilter(request, response);
            return;
        }
        final UsernamePassword credential = BasicAuthHeaderExtractor.extractBasicAuthenticationCredentials(request.getHeader("Authorization"));
        if (credential != null) {
            LOGGER.debug("[Basic Authentication] Authorization header found for user '{}'", credential.getUsername());
        }
        if (securityService.isSecurityEnabled()) {
            LOGGER.debug("Security is enabled.");
            filterWhenSecurityEnabled(request, response, filterChain, credential);
        } else {
            LOGGER.debug("Security is disabled.");
            filterWhenSecurityDisabled(request, response, filterChain, credential);
        }
    } catch (AuthenticationException e) {
        onAuthenticationFailure(request, response, e.getMessage());
    }
}
Also used : AuthenticationException(org.springframework.security.core.AuthenticationException) UsernamePassword(com.thoughtworks.go.server.newsecurity.models.UsernamePassword)

Example 3 with UsernamePassword

use of com.thoughtworks.go.server.newsecurity.models.UsernamePassword in project gocd by gocd.

the class BasicAuthHeaderExtractor method extractBasicAuthenticationCredentials.

public static UsernamePassword extractBasicAuthenticationCredentials(String authorizationHeader) {
    if (isBlank(authorizationHeader)) {
        return null;
    }
    final Matcher matcher = BASIC_AUTH_EXTRACTOR_PATTERN.matcher(authorizationHeader);
    if (matcher.matches()) {
        final String encodedCredentials = matcher.group(1);
        final byte[] decode = Base64.getDecoder().decode(encodedCredentials);
        String decodedCredentials = new String(decode, StandardCharsets.UTF_8);
        final int indexOfSeparator = decodedCredentials.indexOf(':');
        if (indexOfSeparator == -1) {
            throw new BadCredentialsException("Invalid basic authentication credentials specified in request.");
        }
        final String username = decodedCredentials.substring(0, indexOfSeparator);
        final String password = decodedCredentials.substring(indexOfSeparator + 1);
        return new UsernamePassword(username, password);
    }
    return null;
}
Also used : Matcher(java.util.regex.Matcher) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) UsernamePassword(com.thoughtworks.go.server.newsecurity.models.UsernamePassword)

Example 4 with UsernamePassword

use of com.thoughtworks.go.server.newsecurity.models.UsernamePassword in project gocd by gocd.

the class DashBoardControllerTest method shouldResolveDashboardViewNonStandbyServer.

@Test
void shouldResolveDashboardViewNonStandbyServer() {
    when(authToken.isValid()).thenReturn(true);
    when(authToken.toUsernamePassword()).thenReturn(new UsernamePassword(USERNAME, PASSWORD));
    when(addOnConfiguration.isServerInStandby()).thenReturn(false);
    when(railsAssetsService.getAssetPath("application.css")).thenReturn("application.css");
    when(railsAssetsService.getAssetPath("patterns/application.css")).thenReturn("patterns/application.css");
    when(railsAssetsService.getAssetPath("application.js")).thenReturn("application.js");
    when(railsAssetsService.getAssetPath("cruise.ico")).thenReturn("cruise.ico");
    Map<String, String> expectedModelMap = new HashMap<>();
    expectedModelMap.put("REPLACED_BY_GO:application.css", "application.css");
    expectedModelMap.put("REPLACED_BY_GO:patterns/application.css", "patterns/application.css");
    expectedModelMap.put("REPLACED_BY_GO:application.js", "application.js");
    expectedModelMap.put("REPLACED_BY_GO:cruise.ico", "cruise.ico");
    String template = "<html></html>";
    when(viewResolver.resolveView("error", expectedModelMap)).thenReturn(template);
    HttpServletRequest request = HttpRequestBuilder.GET("").withBasicAuth(USERNAME, PASSWORD).build();
    String view = controller.dashboard(request, null);
    assertThat(view).isEqualTo(template);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HashMap(java.util.HashMap) UsernamePassword(com.thoughtworks.go.server.newsecurity.models.UsernamePassword) Test(org.junit.jupiter.api.Test)

Example 5 with UsernamePassword

use of com.thoughtworks.go.server.newsecurity.models.UsernamePassword in project gocd by gocd.

the class DashBoardControllerTest method shouldErrorWhenStandbyNotAddedAsOAuthClient.

@Test
void shouldErrorWhenStandbyNotAddedAsOAuthClient() {
    when(authToken.isValid()).thenReturn(true);
    when(authToken.toUsernamePassword()).thenReturn(new UsernamePassword(USERNAME, PASSWORD));
    when(authToken.forHttp()).thenReturn(CREDENTIALS);
    when(addOnConfiguration.isServerInStandby()).thenReturn(true);
    HttpServletRequest request = HttpRequestBuilder.GET("").withBasicAuth(USERNAME, PASSWORD).build();
    MockHttpServletResponse response = new MockHttpServletResponse();
    String dashboardData = controller.dashboardData(request, response);
    MockHttpServletResponseAssert.assertThat(response).hasStatus(200);
    JsonFluentAssert.assertThatJson(dashboardData).isEqualTo("{\"syncErrors\":[\"Unable to connect to primary, please check that the business-continuity-token file is identical on primary and secondary, and that this server can connect to the primary server.\"],\"setupStatus\":\"incomplete\", \"userName\": \"bob\"}");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletResponse(com.thoughtworks.go.http.mocks.MockHttpServletResponse) UsernamePassword(com.thoughtworks.go.server.newsecurity.models.UsernamePassword) Test(org.junit.jupiter.api.Test)

Aggregations

UsernamePassword (com.thoughtworks.go.server.newsecurity.models.UsernamePassword)14 Test (org.junit.jupiter.api.Test)9 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 HttpSession (javax.servlet.http.HttpSession)4 MockHttpServletResponse (com.thoughtworks.go.http.mocks.MockHttpServletResponse)3 HashMap (java.util.HashMap)2 AuthenticationException (org.springframework.security.core.AuthenticationException)2 GsonBuilder (com.google.gson.GsonBuilder)1 MockFilterChain (com.thoughtworks.go.http.mocks.MockFilterChain)1 MockHttpServletRequest (com.thoughtworks.go.http.mocks.MockHttpServletRequest)1 GoUserPrinciple (com.thoughtworks.go.server.security.userdetail.GoUserPrinciple)1 Date (java.util.Date)1 Matcher (java.util.regex.Matcher)1 FilterChain (javax.servlet.FilterChain)1 Servlet (javax.servlet.Servlet)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)1 SavedRequest (org.springframework.security.web.savedrequest.SavedRequest)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 OncePerRequestFilter (org.springframework.web.filter.OncePerRequestFilter)1