Search in sources :

Example 16 with AuthenticationResponse

use of com.thoughtworks.go.plugin.access.authorization.models.AuthenticationResponse in project gocd by gocd.

the class PreAuthenticatedAuthenticationProviderTest method authenticate_shouldEnsureUserDetailsInAuthTokenHasDisplayName.

@Test
public void authenticate_shouldEnsureUserDetailsInAuthTokenHasDisplayName() {
    Map<String, String> credentials = Collections.singletonMap("access_token", "some_token");
    PreAuthenticatedAuthenticationToken authenticationToken = new PreAuthenticatedAuthenticationToken(null, credentials, pluginId);
    AuthenticationResponse authenticationResponse = new AuthenticationResponse(new User("username", null, "email"), asList("admin"));
    when(authorizationExtension.authenticateUser(any(String.class), any(Map.class), any(List.class), any(List.class))).thenReturn(authenticationResponse);
    PreAuthenticatedAuthenticationToken authenticate = (PreAuthenticatedAuthenticationToken) authenticationProvider.authenticate(authenticationToken);
    GoUserPrinciple principal = (GoUserPrinciple) authenticate.getPrincipal();
    assertThat(principal.getDisplayName(), is(authenticationResponse.getUser().getUsername()));
}
Also used : User(com.thoughtworks.go.plugin.access.authorization.models.User) PreAuthenticatedAuthenticationToken(com.thoughtworks.go.server.security.tokens.PreAuthenticatedAuthenticationToken) Arrays.asList(java.util.Arrays.asList) List(java.util.List) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) GoUserPrinciple(com.thoughtworks.go.server.security.userdetail.GoUserPrinciple) AuthenticationResponse(com.thoughtworks.go.plugin.access.authorization.models.AuthenticationResponse) Map(java.util.Map) Test(org.junit.Test)

Example 17 with AuthenticationResponse

use of com.thoughtworks.go.plugin.access.authorization.models.AuthenticationResponse in project gocd by gocd.

the class PreAuthenticatedAuthenticationProvider method doAuthenticate.

private Authentication doAuthenticate(PreAuthenticatedAuthenticationToken preAuthToken) {
    String pluginId = preAuthToken.getPluginId();
    AuthenticationResponse response = null;
    try {
        response = authenticateUser(preAuthToken);
    } catch (Exception e) {
        handleUnSuccessfulAuthentication(preAuthToken);
    }
    if (!isAuthenticated(response)) {
        handleUnSuccessfulAuthentication(preAuthToken);
    }
    validateUser(response.getUser());
    assignRoles(pluginId, response.getUser().getUsername(), response.getRoles());
    UserDetails userDetails = getUserDetails(response.getUser());
    userService.addUserIfDoesNotExist(toDomainUser(response.getUser()));
    PreAuthenticatedAuthenticationToken result = new PreAuthenticatedAuthenticationToken(userDetails, preAuthToken.getCredentials(), pluginId, userDetails.getAuthorities());
    result.setAuthenticated(true);
    return result;
}
Also used : UserDetails(org.springframework.security.userdetails.UserDetails) PreAuthenticatedAuthenticationToken(com.thoughtworks.go.server.security.tokens.PreAuthenticatedAuthenticationToken) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) AuthenticationResponse(com.thoughtworks.go.plugin.access.authorization.models.AuthenticationResponse) BadCredentialsException(org.springframework.security.BadCredentialsException) AuthenticationException(org.springframework.security.AuthenticationException)

Example 18 with AuthenticationResponse

use of com.thoughtworks.go.plugin.access.authorization.models.AuthenticationResponse in project gocd by gocd.

the class AuthenticationResponseTest method shouldAbleToDeserializeJSON.

@Test
public void shouldAbleToDeserializeJSON() throws Exception {
    String json = "{\n" + "  \"user\": {\n" + "      \"username\":\"gocd\",\n" + "      \"display_name\": \"GoCD Admin\",\n" + "      \"email\": \"gocd@go.cd\"\n" + "  },\n" + "  \"roles\": [\"admin\",\"blackbird\"]\n" + "}";
    AuthenticationResponse authenticationResponse = AuthenticationResponse.fromJSON(json);
    assertThat(authenticationResponse.getUser(), is(new User("gocd", "GoCD Admin", "gocd@go.cd")));
    assertThat(authenticationResponse.getRoles(), hasSize(2));
    assertThat(authenticationResponse.getRoles(), containsInAnyOrder("admin", "blackbird"));
}
Also used : User(com.thoughtworks.go.plugin.access.authorization.models.User) AuthenticationResponse(com.thoughtworks.go.plugin.access.authorization.models.AuthenticationResponse) Test(org.junit.Test)

Aggregations

AuthenticationResponse (com.thoughtworks.go.plugin.access.authorization.models.AuthenticationResponse)18 Test (org.junit.Test)15 CaseInsensitiveString (com.thoughtworks.go.config.CaseInsensitiveString)14 SecurityAuthConfig (com.thoughtworks.go.config.SecurityAuthConfig)14 User (com.thoughtworks.go.plugin.access.authorization.models.User)13 UserDetails (org.springframework.security.userdetails.UserDetails)9 PluginRoleConfig (com.thoughtworks.go.config.PluginRoleConfig)5 GoUserPrinciple (com.thoughtworks.go.server.security.userdetail.GoUserPrinciple)5 UsernamePasswordAuthenticationToken (org.springframework.security.providers.UsernamePasswordAuthenticationToken)5 PreAuthenticatedAuthenticationToken (com.thoughtworks.go.server.security.tokens.PreAuthenticatedAuthenticationToken)4 Arrays.asList (java.util.Arrays.asList)3 List (java.util.List)3 Map (java.util.Map)3 ConfigurationProperty (com.thoughtworks.go.domain.config.ConfigurationProperty)2 User (com.thoughtworks.go.plugin.access.authentication.models.User)2 InOrder (org.mockito.InOrder)2 AuthenticationException (org.springframework.security.AuthenticationException)2 BadCredentialsException (org.springframework.security.BadCredentialsException)2 SecurityConfig (com.thoughtworks.go.config.SecurityConfig)1 AuthorizationExtension (com.thoughtworks.go.plugin.access.authorization.AuthorizationExtension)1