Search in sources :

Example 1 with Authentication

use of org.springframework.security.Authentication in project gocd by gocd.

the class AuthenticationRequestProcessor method process.

@Override
public GoApiResponse process(GoPluginDescriptor pluginDescriptor, GoApiRequest goPluginApiRequest) {
    try {
        String version = goPluginApiRequest.apiVersion();
        if (!goSupportedVersions.contains(version)) {
            throw new RuntimeException(String.format("Unsupported '%s' API version: %s. Supported versions: %s", AUTHENTICATE_USER_REQUEST, version, goSupportedVersions));
        }
        User user = messageHandlerMap.get(version).responseMessageForAuthenticateUser(goPluginApiRequest.requestBody());
        if (user == null) {
            throw new RuntimeException(String.format("Could not parse User details. Request Body: %s", goPluginApiRequest.requestBody()));
        }
        GoUserPrinciple goUserPrincipal = getGoUserPrincipal(user);
        Authentication authentication = getAuthenticationToken(goUserPrincipal);
        userService.addUserIfDoesNotExist(UserHelper.getUser(authentication));
        getSecurityContext().setAuthentication(authentication);
        return new DefaultGoApiResponse(200);
    } catch (Exception e) {
        LOGGER.error("Error occurred while authenticating user", e);
    }
    return new DefaultGoApiResponse(500);
}
Also used : DefaultGoApiResponse(com.thoughtworks.go.plugin.api.response.DefaultGoApiResponse) User(com.thoughtworks.go.plugin.access.authentication.models.User) Authentication(org.springframework.security.Authentication) GoUserPrinciple(com.thoughtworks.go.server.security.userdetail.GoUserPrinciple)

Example 2 with Authentication

use of org.springframework.security.Authentication in project gocd by gocd.

the class LdapAuthenticationTest method assertFailedAuthentication.

private void assertFailedAuthentication(String userName, String password) {
    Authentication authentication = new UsernamePasswordAuthenticationToken(userName, password);
    try {
        ldapAuthenticationProvider.authenticate(authentication);
        fail("Expected authentication to fail for user: " + userName);
    } catch (BadCredentialsException e) {
    }
}
Also used : Authentication(org.springframework.security.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.providers.UsernamePasswordAuthenticationToken) BadCredentialsException(org.springframework.security.BadCredentialsException)

Example 3 with Authentication

use of org.springframework.security.Authentication in project gocd by gocd.

the class UserEnabledCheckFilterTest method shouldNotSetUserIdInSessionIfUserServiceReturnANullUser.

@Test
public void shouldNotSetUserIdInSessionIfUserServiceReturnANullUser() throws IOException, ServletException {
    String userName = "none";
    SecurityContextHelper.setCurrentUser(userName);
    Authentication actual = SecurityContextHolder.getContext().getAuthentication();
    when(session.getAttribute(USERID_ATTR)).thenReturn(null);
    NullUser nullUser = new NullUser();
    when(userService.findUserByName(userName)).thenReturn(nullUser);
    filter.doFilterHttp(req, res, chain);
    assertThat(SecurityContextHolder.getContext().getAuthentication(), is(actual));
    verify(session, never()).setAttribute(eq(USERID_ATTR), Matchers.<Object>any());
    verify(chain).doFilter(req, res);
}
Also used : Authentication(org.springframework.security.Authentication) NullUser(com.thoughtworks.go.domain.NullUser)

Example 4 with Authentication

use of org.springframework.security.Authentication in project gocd by gocd.

the class GoAuthenticationProviderTest method shouldEnforceLicenseLimit.

@Test
public void shouldEnforceLicenseLimit() throws Exception {
    Authentication authentication = enforcementProvider.authenticate(auth);
    assertThat(authentication, is(resultantAuthorization));
    verify(userService).addUserIfDoesNotExist(UserHelper.getUser(resultantAuthorization));
}
Also used : Authentication(org.springframework.security.Authentication) Test(org.junit.Test)

Example 5 with Authentication

use of org.springframework.security.Authentication in project gocd by gocd.

the class PreAuthenticatedAuthenticationProviderTest method authenticate_shouldSupportAuthenticationForPreAuthenticatedAuthenticationTokenOnly.

@Test
public void authenticate_shouldSupportAuthenticationForPreAuthenticatedAuthenticationTokenOnly() {
    Authentication authenticate = authenticationProvider.authenticate(new UsernamePasswordAuthenticationToken("p", "c"));
    assertNull(authenticate);
    verifyZeroInteractions(authorizationExtension);
}
Also used : Authentication(org.springframework.security.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.providers.UsernamePasswordAuthenticationToken) Test(org.junit.Test)

Aggregations

Authentication (org.springframework.security.Authentication)31 Test (org.junit.Test)16 UsernamePasswordAuthenticationToken (org.springframework.security.providers.UsernamePasswordAuthenticationToken)5 NullUser (com.thoughtworks.go.domain.NullUser)4 GrantedAuthority (org.springframework.security.GrantedAuthority)4 TestingAuthenticationToken (org.springframework.security.providers.TestingAuthenticationToken)4 User (com.thoughtworks.go.domain.User)3 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)3 PreAuthenticatedAuthenticationToken (com.thoughtworks.go.server.security.tokens.PreAuthenticatedAuthenticationToken)2 GoUserPrinciple (com.thoughtworks.go.server.security.userdetail.GoUserPrinciple)2 Expectations (org.jmock.Expectations)2 AuthenticationException (org.springframework.security.AuthenticationException)2 User (org.springframework.security.userdetails.User)2 SecurityAuthConfig (com.thoughtworks.go.config.SecurityAuthConfig)1 User (com.thoughtworks.go.plugin.access.authentication.models.User)1 DefaultGoApiResponse (com.thoughtworks.go.plugin.api.response.DefaultGoApiResponse)1 Username (com.thoughtworks.go.server.domain.Username)1 HttpLocalizedOperationResult (com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1