Search in sources :

Example 11 with PreAuthenticatedAuthenticationToken

use of com.thoughtworks.go.server.security.tokens.PreAuthenticatedAuthenticationToken in project gocd by gocd.

the class PreAuthenticatedAuthenticationProviderTest method authenticate_shouldReturnAuthenticationTokenOnSuccessfulAuthorization.

@Test
public void authenticate_shouldReturnAuthenticationTokenOnSuccessfulAuthorization() {
    Map<String, String> credentials = Collections.singletonMap("access_token", "some_token");
    PreAuthenticatedAuthenticationToken authenticationToken = new PreAuthenticatedAuthenticationToken(null, credentials, pluginId);
    PreAuthenticatedAuthenticationToken authenticate = (PreAuthenticatedAuthenticationToken) authenticationProvider.authenticate(authenticationToken);
    assertThat(authenticate.getCredentials(), is(credentials));
    assertThat(authenticate.getPluginId(), is(pluginId));
    assertThat(authenticate.getAuthorities(), is(authorities));
    assertThat(authenticate.isAuthenticated(), is(true));
}
Also used : PreAuthenticatedAuthenticationToken(com.thoughtworks.go.server.security.tokens.PreAuthenticatedAuthenticationToken) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) Test(org.junit.Test)

Example 12 with PreAuthenticatedAuthenticationToken

use of com.thoughtworks.go.server.security.tokens.PreAuthenticatedAuthenticationToken in project gocd by gocd.

the class PreAuthenticatedAuthenticationProviderTest method authenticate_inCaseOfMultipleAuthConfigsOnSuccessfulAuthenticationShouldNotTryAuthenticatingUserUsingRemainingAuthConfig.

@Test
public void authenticate_inCaseOfMultipleAuthConfigsOnSuccessfulAuthenticationShouldNotTryAuthenticatingUserUsingRemainingAuthConfig() {
    Map<String, String> credentials = Collections.singletonMap("access_token", "some_token");
    SecurityAuthConfig githubPublic = new SecurityAuthConfig("github_public", pluginId);
    SecurityAuthConfig githubEnterprise = new SecurityAuthConfig("github_enterprise", pluginId);
    PluginRoleConfig adminRole = new PluginRoleConfig("admin", githubPublic.getId(), new ConfigurationProperty());
    PluginRoleConfig operatorRole = new PluginRoleConfig("operator", githubEnterprise.getId(), new ConfigurationProperty());
    securityConfig.securityAuthConfigs().clear();
    securityConfig.securityAuthConfigs().add(githubPublic);
    securityConfig.securityAuthConfigs().add(githubEnterprise);
    securityConfig.addRole(adminRole);
    securityConfig.addRole(operatorRole);
    PreAuthenticatedAuthenticationToken authenticationToken = new PreAuthenticatedAuthenticationToken(null, credentials, pluginId);
    when(authorizationExtension.authenticateUser(pluginId, credentials, Collections.singletonList(githubPublic), Collections.singletonList(adminRole))).thenReturn(new AuthenticationResponse(user, asList("admin")));
    PreAuthenticatedAuthenticationToken authenticate = (PreAuthenticatedAuthenticationToken) authenticationProvider.authenticate(authenticationToken);
    assertThat(authenticate.getCredentials(), is(credentials));
    assertThat(authenticate.getPluginId(), is(pluginId));
    assertThat(authenticate.getAuthorities(), is(authorities));
    assertThat(authenticate.isAuthenticated(), is(true));
    verify(authorizationExtension).authenticateUser(pluginId, credentials, Collections.singletonList(githubPublic), Collections.singletonList(adminRole));
    verify(authorizationExtension, never()).authenticateUser(pluginId, credentials, Collections.singletonList(githubEnterprise), Collections.singletonList(operatorRole));
}
Also used : ConfigurationProperty(com.thoughtworks.go.domain.config.ConfigurationProperty) SecurityAuthConfig(com.thoughtworks.go.config.SecurityAuthConfig) PreAuthenticatedAuthenticationToken(com.thoughtworks.go.server.security.tokens.PreAuthenticatedAuthenticationToken) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) PluginRoleConfig(com.thoughtworks.go.config.PluginRoleConfig) AuthenticationResponse(com.thoughtworks.go.plugin.access.authorization.models.AuthenticationResponse) Test(org.junit.Test)

Example 13 with PreAuthenticatedAuthenticationToken

use of com.thoughtworks.go.server.security.tokens.PreAuthenticatedAuthenticationToken in project gocd by gocd.

the class PreAuthenticatedAuthenticationProviderTest method authenticate_shouldReturnAuthTokenWithUserDetails.

@Test
public void authenticate_shouldReturnAuthTokenWithUserDetails() {
    Map<String, String> credentials = Collections.singletonMap("access_token", "some_token");
    PreAuthenticatedAuthenticationToken authenticationToken = new PreAuthenticatedAuthenticationToken(null, credentials, pluginId);
    PreAuthenticatedAuthenticationToken authenticate = (PreAuthenticatedAuthenticationToken) authenticationProvider.authenticate(authenticationToken);
    GoUserPrinciple principal = (GoUserPrinciple) authenticate.getPrincipal();
    assertThat(principal.getDisplayName(), is(user.getDisplayName()));
    assertThat(principal.getUsername(), is(user.getUsername()));
    assertThat(principal.getAuthorities(), is(authorities));
}
Also used : PreAuthenticatedAuthenticationToken(com.thoughtworks.go.server.security.tokens.PreAuthenticatedAuthenticationToken) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) GoUserPrinciple(com.thoughtworks.go.server.security.userdetail.GoUserPrinciple) Test(org.junit.Test)

Example 14 with PreAuthenticatedAuthenticationToken

use of com.thoughtworks.go.server.security.tokens.PreAuthenticatedAuthenticationToken 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 15 with PreAuthenticatedAuthenticationToken

use of com.thoughtworks.go.server.security.tokens.PreAuthenticatedAuthenticationToken in project gocd by gocd.

the class PreAuthenticatedRequestsProcessingFilterTest method shouldAuthenticateUsersWithCredentials.

@Test
public void shouldAuthenticateUsersWithCredentials() throws IOException, ServletException {
    PreAuthenticatedAuthenticationToken token = mock(PreAuthenticatedAuthenticationToken.class);
    HashMap<String, String[]> params = new HashMap<>();
    params.put("code", new String[] { "some_auth_code" });
    SecurityAuthConfig githubAuthConfig = new SecurityAuthConfig("github", "github.oauth");
    securityConfig.securityAuthConfigs().add(githubAuthConfig);
    when(request.getRequestURI()).thenReturn("/go/plugin/github.oauth/authenticate");
    when(request.getHeaderNames()).thenReturn(Collections.enumeration(Arrays.asList("Authorization")));
    when(request.getHeader("Authorization")).thenReturn("qwe123");
    when(request.getParameterMap()).thenReturn(params);
    when(authorizationExtension.fetchAccessToken("github.oauth", Collections.singletonMap("Authorization", "qwe123"), Collections.singletonMap("code", "some_auth_code"), Collections.singletonList(githubAuthConfig))).thenReturn(Collections.singletonMap("access_token", "token"));
    when(authenticationManager.authenticate(any(PreAuthenticatedAuthenticationToken.class))).thenReturn(token);
    filter.setDefaultTargetUrl("/");
    filter.doFilter(request, response, filterChain);
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    assertThat(authentication, is(token));
}
Also used : SecurityAuthConfig(com.thoughtworks.go.config.SecurityAuthConfig) HashMap(java.util.HashMap) Authentication(org.springframework.security.Authentication) PreAuthenticatedAuthenticationToken(com.thoughtworks.go.server.security.tokens.PreAuthenticatedAuthenticationToken) Test(org.junit.Test)

Aggregations

PreAuthenticatedAuthenticationToken (com.thoughtworks.go.server.security.tokens.PreAuthenticatedAuthenticationToken)16 Test (org.junit.Test)14 CaseInsensitiveString (com.thoughtworks.go.config.CaseInsensitiveString)11 AuthenticationResponse (com.thoughtworks.go.plugin.access.authorization.models.AuthenticationResponse)4 SecurityAuthConfig (com.thoughtworks.go.config.SecurityAuthConfig)3 User (com.thoughtworks.go.plugin.access.authorization.models.User)3 Arrays.asList (java.util.Arrays.asList)3 List (java.util.List)3 PluginRoleConfig (com.thoughtworks.go.config.PluginRoleConfig)2 ConfigurationProperty (com.thoughtworks.go.domain.config.ConfigurationProperty)2 GoUserPrinciple (com.thoughtworks.go.server.security.userdetail.GoUserPrinciple)2 Map (java.util.Map)2 InOrder (org.mockito.InOrder)2 Authentication (org.springframework.security.Authentication)2 HashMap (java.util.HashMap)1 AuthenticationException (org.springframework.security.AuthenticationException)1 BadCredentialsException (org.springframework.security.BadCredentialsException)1 UserDetails (org.springframework.security.userdetails.UserDetails)1