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));
}
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));
}
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));
}
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()));
}
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));
}
Aggregations