Search in sources :

Example 11 with User

use of com.thoughtworks.go.plugin.access.authentication.models.User in project gocd by gocd.

the class PluginAuthenticationProvider method getUserDetailsFromAuthenticationPlugins.

private User getUserDetailsFromAuthenticationPlugins(String username, UsernamePasswordAuthenticationToken authentication) {
    Set<String> plugins = authenticationPluginRegistry.getPluginsThatSupportsPasswordBasedAuthentication();
    for (String pluginId : plugins) {
        String password = (String) authentication.getCredentials();
        User user = ensureDisplayNamePresent(authenticationExtension.authenticateUser(pluginId, username, password));
        if (user != null) {
            return user;
        }
    }
    return null;
}
Also used : User(com.thoughtworks.go.plugin.access.authentication.models.User) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString)

Example 12 with User

use of com.thoughtworks.go.plugin.access.authentication.models.User in project gocd by gocd.

the class PluginAuthenticationProviderTest method shouldAddUserIfDoesNotExistOnSuccessfulAuthenticationUsingTheAuthenticationPlugin.

@Test
public void shouldAddUserIfDoesNotExistOnSuccessfulAuthenticationUsingTheAuthenticationPlugin() {
    String pluginId = "plugin-id-1";
    securityConfig.securityAuthConfigs().add(new SecurityAuthConfig("github", pluginId));
    when(authenticationPluginRegistry.getPluginsThatSupportsPasswordBasedAuthentication()).thenReturn(new HashSet<>(Arrays.asList(pluginId)));
    when(authenticationExtension.authenticateUser(pluginId, "username", "password")).thenReturn(new User("username", "display-name", "username@example.com"));
    provider.retrieveUser("username", authenticationToken);
    verify(userService).addUserIfDoesNotExist(new com.thoughtworks.go.domain.User("username", "display-name", "username@example.com"));
}
Also used : SecurityAuthConfig(com.thoughtworks.go.config.SecurityAuthConfig) User(com.thoughtworks.go.plugin.access.authentication.models.User) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) Test(org.junit.Test)

Example 13 with User

use of com.thoughtworks.go.plugin.access.authentication.models.User in project gocd by gocd.

the class PluginAuthenticationProviderTest method shouldCreateGoUserPrincipalWhenAnAuthenticationPluginIsAbleToAuthenticateUser.

@Test
public void shouldCreateGoUserPrincipalWhenAnAuthenticationPluginIsAbleToAuthenticateUser() {
    String pluginId1 = "plugin-id-1";
    String pluginId2 = "plugin-id-2";
    when(authenticationPluginRegistry.getPluginsThatSupportsPasswordBasedAuthentication()).thenReturn(new HashSet<>(Arrays.asList(pluginId1, pluginId2)));
    when(authenticationExtension.authenticateUser(pluginId1, "username", "password")).thenReturn(null);
    when(authenticationExtension.authenticateUser(pluginId2, "username", "password")).thenReturn(new User("username", null, null));
    UserDetails userDetails = provider.retrieveUser("username", authenticationToken);
    assertThat(userDetails, is(instanceOf(GoUserPrinciple.class)));
    GoUserPrinciple goUserPrincipal = (GoUserPrinciple) userDetails;
    assertThat(goUserPrincipal.getUsername(), is("username"));
    assertThat(goUserPrincipal.getDisplayName(), is("username"));
    assertThat(goUserPrincipal.getAuthorities().length, is(1));
    assertThat(goUserPrincipal.getAuthorities()[0], is(userAuthority));
}
Also used : User(com.thoughtworks.go.plugin.access.authentication.models.User) UserDetails(org.springframework.security.userdetails.UserDetails) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) GoUserPrinciple(com.thoughtworks.go.server.security.userdetail.GoUserPrinciple) Test(org.junit.Test)

Example 14 with User

use of com.thoughtworks.go.plugin.access.authentication.models.User in project gocd by gocd.

the class PluginAuthenticationProviderTest method shouldCreateGoUserPrincipalWhenAnAuthorizationPluginIsAbleToAuthenticateUser.

@Test
public void shouldCreateGoUserPrincipalWhenAnAuthorizationPluginIsAbleToAuthenticateUser() {
    String pluginId1 = "plugin-id-1";
    String pluginId2 = "plugin-id-2";
    securityConfig.securityAuthConfigs().add(new SecurityAuthConfig("github", pluginId2));
    securityConfig.addRole(new PluginRoleConfig("admin", "github", ConfigurationPropertyMother.create("foo")));
    when(store.getPluginsThatSupportsPasswordBasedAuthentication()).thenReturn(new HashSet<>(Arrays.asList(pluginId1, pluginId2)));
    when(authorizationExtension.authenticateUser(pluginId1, "username", "password", securityConfig.securityAuthConfigs().findByPluginId(pluginId1), null)).thenReturn(NULL_AUTH_RESPONSE);
    AuthenticationResponse response = new AuthenticationResponse(new User("username", "display-name", "test@test.com"), Collections.emptyList());
    when(authorizationExtension.authenticateUser(pluginId2, "username", "password", securityConfig.securityAuthConfigs().findByPluginId(pluginId2), securityConfig.getPluginRoles(pluginId2))).thenReturn(response);
    UserDetails userDetails = provider.retrieveUser("username", authenticationToken);
    assertThat(userDetails, is(instanceOf(GoUserPrinciple.class)));
    GoUserPrinciple goUserPrincipal = (GoUserPrinciple) userDetails;
    assertThat(goUserPrincipal.getUsername(), is("username"));
    assertThat(goUserPrincipal.getDisplayName(), is("display-name"));
    assertThat(goUserPrincipal.getAuthorities().length, is(1));
    assertThat(goUserPrincipal.getAuthorities()[0], is(userAuthority));
}
Also used : SecurityAuthConfig(com.thoughtworks.go.config.SecurityAuthConfig) User(com.thoughtworks.go.plugin.access.authentication.models.User) UserDetails(org.springframework.security.userdetails.UserDetails) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) GoUserPrinciple(com.thoughtworks.go.server.security.userdetail.GoUserPrinciple) PluginRoleConfig(com.thoughtworks.go.config.PluginRoleConfig) AuthenticationResponse(com.thoughtworks.go.plugin.access.authorization.models.AuthenticationResponse) Test(org.junit.Test)

Aggregations

User (com.thoughtworks.go.plugin.access.authentication.models.User)14 Test (org.junit.Test)10 CaseInsensitiveString (com.thoughtworks.go.config.CaseInsensitiveString)5 GoUserPrinciple (com.thoughtworks.go.server.security.userdetail.GoUserPrinciple)4 SecurityAuthConfig (com.thoughtworks.go.config.SecurityAuthConfig)3 AuthenticationResponse (com.thoughtworks.go.plugin.access.authorization.models.AuthenticationResponse)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 UserDetails (org.springframework.security.userdetails.UserDetails)2 PluginRoleConfig (com.thoughtworks.go.config.PluginRoleConfig)1 DefaultGoApiResponse (com.thoughtworks.go.plugin.api.response.DefaultGoApiResponse)1 GoApiResponse (com.thoughtworks.go.plugin.api.response.GoApiResponse)1 ArrayList (java.util.ArrayList)1 Authentication (org.springframework.security.Authentication)1 PreAuthenticatedAuthenticationToken (org.springframework.security.providers.preauth.PreAuthenticatedAuthenticationToken)1