use of org.springframework.security.providers.UsernamePasswordAuthenticationToken in project gocd by gocd.
the class SecurityContextHelper method setCurrentUserWithAuthorities.
public static void setCurrentUserWithAuthorities(String username, final GrantedAuthority[] authorities) {
SecurityContextImpl context = new SecurityContextImpl();
context.setAuthentication(new UsernamePasswordAuthenticationToken(new User(username, "", true, authorities), null, authorities));
SecurityContextHolder.setContext(context);
}
use of org.springframework.security.providers.UsernamePasswordAuthenticationToken in project gocd by gocd.
the class GoFileConfigDataSourceTest method shouldUse_UserFromSession_asConfigModifyingUserWhenNoneGiven.
@Test
public void shouldUse_UserFromSession_asConfigModifyingUserWhenNoneGiven() throws GitAPIException, IOException {
SecurityContext context = SecurityContextHolder.getContext();
context.setAuthentication(new UsernamePasswordAuthenticationToken(new User("loser_boozer", "pass", true, true, true, true, new GrantedAuthority[] {}), null));
goConfigDao.updateMailHost(getMailHost("mailhost.local"));
CruiseConfig cruiseConfig = goConfigDao.load();
GoConfigRevision revision = configRepository.getRevision(cruiseConfig.getMd5());
assertThat(revision.getUsername(), is("loser_boozer"));
}
use of org.springframework.security.providers.UsernamePasswordAuthenticationToken in project gocd by gocd.
the class LdapAuthenticationTest method commonLdapUserShouldOnlyHaveAuthorityOfUserAndNotAdmin.
@Test
public void commonLdapUserShouldOnlyHaveAuthorityOfUserAndNotAdmin() throws Exception {
ldapServer.addUser(employeesOrgUnit, "foleys", "some-password", "Shilpa Foley", "foleys@somecompany.com");
configFileHelper.initializeConfigFile();
configFileHelper.addLdapSecurityWithAdmin(LDAP_URL, MANAGER_DN, MANAGER_PASSWORD, SEARCH_BASE, SEARCH_FILTER, "another_admin");
Authentication authentication = new UsernamePasswordAuthenticationToken("foleys", "some-password");
Authentication result = ldapAuthenticationProvider.authenticate(authentication);
assertThat(result.isAuthenticated(), is(true));
GrantedAuthority[] authorities = result.getAuthorities();
assertThat("foleys should have only user authority. Found: " + ArrayUtils.toString(authorities), authorities.length, is(1));
assertThat(authorities[0].getAuthority(), is("ROLE_USER"));
}
use of org.springframework.security.providers.UsernamePasswordAuthenticationToken in project gocd by gocd.
the class LdapAuthenticationTest method assertAuthenticationOfValidAdminUser.
private void assertAuthenticationOfValidAdminUser(String userName, String password) {
Authentication authentication = new UsernamePasswordAuthenticationToken(userName, password);
Authentication result = ldapAuthenticationProvider.authenticate(authentication);
assertThat(result.isAuthenticated(), is(true));
assertThat(userName + " should have " + ROLE_SUPERVISOR + " authority", result.getAuthorities(), // by default, every user is administrator
hasItemInArray(ROLE_SUPERVISOR.asAuthority()));
}
use of org.springframework.security.providers.UsernamePasswordAuthenticationToken in project gocd by gocd.
the class PluginAuthenticationProviderTest method reuthenticationUsingAuthorizationPlugins_shouldFallbackOnUserNameInAbsenceOfLoginNameInGoUserPrinciple.
@Test
public void reuthenticationUsingAuthorizationPlugins_shouldFallbackOnUserNameInAbsenceOfLoginNameInGoUserPrinciple() throws Exception {
String pluginId1 = "cd.go.ldap";
securityConfig.securityAuthConfigs().add(new SecurityAuthConfig("ldap", "cd.go.ldap"));
addPluginSupportingPasswordBasedAuthentication(pluginId1);
when(authorizationExtension.authenticateUser(pluginId1, "username", "password", securityConfig.securityAuthConfigs().findByPluginId(pluginId1), securityConfig.getPluginRoles(pluginId1))).thenReturn(new AuthenticationResponse(new User("username", "bob", "bob@example.com"), Arrays.asList("blackbird", "admins")));
GoUserPrinciple principal = new GoUserPrinciple("username", "Display", "password", true, true, true, true, new GrantedAuthority[] {}, null);
UserDetails userDetails = provider.retrieveUser("username", new UsernamePasswordAuthenticationToken(principal, "password"));
assertNotNull(userDetails);
verify(pluginRoleService).updatePluginRoles("cd.go.ldap", "username", CaseInsensitiveString.caseInsensitiveStrings(Arrays.asList("blackbird", "admins")));
}
Aggregations