Search in sources :

Example 6 with User

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

the class JsonMessageHandler1_0Test method shouldHandleEmptyResponseMessageForAuthenticateUser.

@Test
public void shouldHandleEmptyResponseMessageForAuthenticateUser() throws Exception {
    User user1 = messageHandler.responseMessageForAuthenticateUser("");
    assertThat(user1, is(nullValue()));
    User user2 = messageHandler.responseMessageForAuthenticateUser("{}");
    assertThat(user2, is(nullValue()));
}
Also used : User(com.thoughtworks.go.plugin.access.authentication.models.User) Test(org.junit.Test)

Example 7 with User

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

the class JsonMessageHandler1_0 method responseMessageForAuthenticateUser.

@Override
public User responseMessageForAuthenticateUser(String responseBody) {
    Map map;
    try {
        map = parseResponseToMap(responseBody);
    } catch (Exception e) {
        throw new RuntimeException("User should be returned as a map");
    }
    if (map == null || map.isEmpty()) {
        return null;
    }
    Map userMap;
    try {
        userMap = (Map) map.get("user");
    } catch (Exception e) {
        throw new RuntimeException("User should be returned as a map");
    }
    User user = toUser(userMap);
    return user;
}
Also used : User(com.thoughtworks.go.plugin.access.authentication.models.User) Map(java.util.Map) HashMap(java.util.HashMap)

Example 8 with User

use of com.thoughtworks.go.plugin.access.authentication.models.User 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 9 with User

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

the class PluginAuthenticationProviderTest method shouldAddUserIfDoesNotExistOnSuccessfulAuthenticationUsingTheAuthorizationPlugin.

@Test
public void shouldAddUserIfDoesNotExistOnSuccessfulAuthenticationUsingTheAuthorizationPlugin() {
    String pluginId = "plugin-id-1";
    securityConfig.securityAuthConfigs().add(new SecurityAuthConfig("github", pluginId));
    when(authenticationPluginRegistry.getPluginsThatSupportsPasswordBasedAuthentication()).thenReturn(new HashSet<>(Arrays.asList()));
    when(store.getPluginsThatSupportsPasswordBasedAuthentication()).thenReturn(new HashSet<>(Arrays.asList(pluginId)));
    AuthenticationResponse response = new AuthenticationResponse(new User("username", "display-name", "username@example.com"), Collections.emptyList());
    when(authorizationExtension.authenticateUser(pluginId, "username", "password", securityConfig.securityAuthConfigs().findByPluginId(pluginId), securityConfig.getPluginRoles(pluginId))).thenReturn(response);
    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) AuthenticationResponse(com.thoughtworks.go.plugin.access.authorization.models.AuthenticationResponse) Test(org.junit.Test)

Example 10 with User

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

the class JsonMessageHandler1_0 method responseMessageForSearchUser.

@Override
public List<User> responseMessageForSearchUser(String responseBody) {
    List<Map> list;
    try {
        list = parseResponseToList(responseBody);
    } catch (Exception e) {
        throw new RuntimeException("Search results should be returned as a list");
    }
    List<User> searchResults = new ArrayList<>();
    if (list == null || list.isEmpty()) {
        return searchResults;
    }
    for (Map userMap : list) {
        User user = toUser(userMap);
        searchResults.add(user);
    }
    return searchResults;
}
Also used : User(com.thoughtworks.go.plugin.access.authentication.models.User) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

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