Search in sources :

Example 1 with GoUserPrinciple

use of com.thoughtworks.go.server.security.userdetail.GoUserPrinciple in project gocd by gocd.

the class UserHelperTest method shouldGetDisplayNameForAPasswordFileUser.

@Test
public void shouldGetDisplayNameForAPasswordFileUser() {
    GrantedAuthority[] authorities = { new GrantedAuthorityImpl("anything") };
    TestingAuthenticationToken authentication = new TestingAuthenticationToken(new GoUserPrinciple("user", "Full Name", "password", true, true, true, true, authorities), null, authorities);
    assertThat(UserHelper.getUserName(authentication), is(new Username(new CaseInsensitiveString("user"), "Full Name")));
}
Also used : GrantedAuthorityImpl(org.springframework.security.GrantedAuthorityImpl) Username(com.thoughtworks.go.server.domain.Username) GrantedAuthority(org.springframework.security.GrantedAuthority) GoUserPrinciple(com.thoughtworks.go.server.security.userdetail.GoUserPrinciple) TestingAuthenticationToken(org.springframework.security.providers.TestingAuthenticationToken) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) Test(org.junit.Test)

Example 2 with GoUserPrinciple

use of com.thoughtworks.go.server.security.userdetail.GoUserPrinciple 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 3 with GoUserPrinciple

use of com.thoughtworks.go.server.security.userdetail.GoUserPrinciple in project gocd by gocd.

the class PluginAuthenticationProvider method retrieveUser.

@Override
protected UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
    User user = getUserDetailsFromAuthorizationPlugins(username, authentication);
    if (user == null) {
        user = getUserDetailsFromAuthenticationPlugins(username, authentication);
    }
    if (user == null) {
        removeAnyAssociatedPluginRolesFor(username);
        throw new UsernameNotFoundException("Unable to authenticate user: " + username);
    }
    userService.addUserIfDoesNotExist(toDomainUser(user));
    GoUserPrinciple goUserPrinciple = getGoUserPrinciple(user);
    return goUserPrinciple;
}
Also used : UsernameNotFoundException(org.springframework.security.userdetails.UsernameNotFoundException) User(com.thoughtworks.go.plugin.access.authentication.models.User) GoUserPrinciple(com.thoughtworks.go.server.security.userdetail.GoUserPrinciple)

Example 4 with GoUserPrinciple

use of com.thoughtworks.go.server.security.userdetail.GoUserPrinciple in project gocd by gocd.

the class AuthenticationRequestProcessorTest method shouldAuthenticateUser.

@Test
public void shouldAuthenticateUser() {
    String responseBody = "expected-response-body";
    User user = new User("username", "display name", "test@test.com");
    when(jsonMessageHandler.responseMessageForAuthenticateUser(responseBody)).thenReturn(user);
    AuthenticationRequestProcessor processorSpy = spy(processor);
    doReturn(securityContext).when(processorSpy).getSecurityContext();
    GoApiResponse response = processorSpy.process(pluginDescriptor, getGoPluginApiRequest("1.0", responseBody));
    assertThat(response.responseCode(), is(200));
    verify(userService).addUserIfDoesNotExist(new com.thoughtworks.go.domain.User("username", "", ""));
    GoUserPrinciple goUserPrincipal = processorSpy.getGoUserPrincipal(user);
    assertThat(goUserPrincipal.getUsername(), is("username"));
    assertThat(goUserPrincipal.getDisplayName(), is("display name"));
    verifyGrantAuthorities(goUserPrincipal.getAuthorities());
    PreAuthenticatedAuthenticationToken authenticationToken = processorSpy.getAuthenticationToken(goUserPrincipal);
    assertThat(authenticationToken.getPrincipal(), is(goUserPrincipal));
    verifyGrantAuthorities(authenticationToken.getAuthorities());
    verify(securityContext).setAuthentication(authenticationToken);
}
Also used : GoApiResponse(com.thoughtworks.go.plugin.api.response.GoApiResponse) User(com.thoughtworks.go.plugin.access.authentication.models.User) PreAuthenticatedAuthenticationToken(org.springframework.security.providers.preauth.PreAuthenticatedAuthenticationToken) GoUserPrinciple(com.thoughtworks.go.server.security.userdetail.GoUserPrinciple) Test(org.junit.Test)

Example 5 with GoUserPrinciple

use of com.thoughtworks.go.server.security.userdetail.GoUserPrinciple in project gocd by gocd.

the class FileAuthenticationProviderTest method shouldReturnUserPrincipleWithTheRightDisplayName.

@Test
public void shouldReturnUserPrincipleWithTheRightDisplayName() throws Exception {
    setupFile(String.format("jez=%s\ncharan=%s\nbabe=%s", SHA1_BADGER, SHA1_BADGER, SHA1_BADGER));
    when(userService.findUserByName("jez")).thenReturn(new com.thoughtworks.go.domain.User("jez", "Jezz Humbles", "jez@humble.com"));
    when(userService.findUserByName("charan")).thenReturn(new com.thoughtworks.go.domain.User("charan", "", "ch@ar.an"));
    FileAuthenticationProvider provider = new FileAuthenticationProvider(goConfigService, new AuthorityGranter(securityService), userService, securityService);
    GoUserPrinciple details = (GoUserPrinciple) provider.retrieveUser("jez", null);
    assertThat(details.getUsername(), is("jez"));
    assertThat(details.getDisplayName(), is("Jezz Humbles"));
    details = (GoUserPrinciple) provider.retrieveUser("charan", null);
    assertThat(details.getUsername(), is("charan"));
    assertThat(details.getDisplayName(), is("charan"));
    details = (GoUserPrinciple) provider.retrieveUser("babe", null);
    assertThat(details.getUsername(), is("babe"));
    assertThat(details.getDisplayName(), is("babe"));
}
Also used : AuthorityGranter(com.thoughtworks.go.server.security.AuthorityGranter) GoUserPrinciple(com.thoughtworks.go.server.security.userdetail.GoUserPrinciple) Test(org.junit.Test)

Aggregations

GoUserPrinciple (com.thoughtworks.go.server.security.userdetail.GoUserPrinciple)8 Test (org.junit.Test)6 User (com.thoughtworks.go.plugin.access.authentication.models.User)5 CaseInsensitiveString (com.thoughtworks.go.config.CaseInsensitiveString)4 Username (com.thoughtworks.go.server.domain.Username)2 AuthorityGranter (com.thoughtworks.go.server.security.AuthorityGranter)2 UserDetails (org.springframework.security.userdetails.UserDetails)2 PluginRoleConfig (com.thoughtworks.go.config.PluginRoleConfig)1 SecurityAuthConfig (com.thoughtworks.go.config.SecurityAuthConfig)1 AuthenticationResponse (com.thoughtworks.go.plugin.access.authorization.models.AuthenticationResponse)1 DefaultGoApiResponse (com.thoughtworks.go.plugin.api.response.DefaultGoApiResponse)1 GoApiResponse (com.thoughtworks.go.plugin.api.response.GoApiResponse)1 Authentication (org.springframework.security.Authentication)1 GrantedAuthority (org.springframework.security.GrantedAuthority)1 GrantedAuthorityImpl (org.springframework.security.GrantedAuthorityImpl)1 TestingAuthenticationToken (org.springframework.security.providers.TestingAuthenticationToken)1 PreAuthenticatedAuthenticationToken (org.springframework.security.providers.preauth.PreAuthenticatedAuthenticationToken)1 UsernameNotFoundException (org.springframework.security.userdetails.UsernameNotFoundException)1