Search in sources :

Example 16 with UsernamePasswordAuthenticationToken

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);
}
Also used : SecurityContextImpl(org.springframework.security.context.SecurityContextImpl) User(org.springframework.security.userdetails.User) UsernamePasswordAuthenticationToken(org.springframework.security.providers.UsernamePasswordAuthenticationToken)

Example 17 with UsernamePasswordAuthenticationToken

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"));
}
Also used : User(org.springframework.security.userdetails.User) SecurityContext(org.springframework.security.context.SecurityContext) UsernamePasswordAuthenticationToken(org.springframework.security.providers.UsernamePasswordAuthenticationToken) GoConfigRevision(com.thoughtworks.go.domain.GoConfigRevision) Test(org.junit.Test)

Example 18 with UsernamePasswordAuthenticationToken

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"));
}
Also used : Authentication(org.springframework.security.Authentication) GrantedAuthority(org.springframework.security.GrantedAuthority) UsernamePasswordAuthenticationToken(org.springframework.security.providers.UsernamePasswordAuthenticationToken) Test(org.junit.Test)

Example 19 with UsernamePasswordAuthenticationToken

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()));
}
Also used : Authentication(org.springframework.security.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.providers.UsernamePasswordAuthenticationToken)

Example 20 with UsernamePasswordAuthenticationToken

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")));
}
Also used : SecurityAuthConfig(com.thoughtworks.go.config.SecurityAuthConfig) User(com.thoughtworks.go.plugin.access.authorization.models.User) UserDetails(org.springframework.security.userdetails.UserDetails) UsernamePasswordAuthenticationToken(org.springframework.security.providers.UsernamePasswordAuthenticationToken) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) GoUserPrinciple(com.thoughtworks.go.server.security.userdetail.GoUserPrinciple) AuthenticationResponse(com.thoughtworks.go.plugin.access.authorization.models.AuthenticationResponse) Test(org.junit.Test)

Aggregations

UsernamePasswordAuthenticationToken (org.springframework.security.providers.UsernamePasswordAuthenticationToken)28 Test (org.junit.Test)20 User (org.springframework.security.userdetails.User)10 UserDetails (org.springframework.security.userdetails.UserDetails)8 CaseInsensitiveString (com.thoughtworks.go.config.CaseInsensitiveString)5 SecurityAuthConfig (com.thoughtworks.go.config.SecurityAuthConfig)5 AuthenticationResponse (com.thoughtworks.go.plugin.access.authorization.models.AuthenticationResponse)5 User (com.thoughtworks.go.plugin.access.authorization.models.User)5 Authentication (org.springframework.security.Authentication)5 SecurityContext (org.springframework.security.context.SecurityContext)4 AuthorityGranter (com.thoughtworks.go.server.security.AuthorityGranter)2 GoUserPrinciple (com.thoughtworks.go.server.security.userdetail.GoUserPrinciple)2 IOException (java.io.IOException)2 Before (org.junit.Before)2 GrantedAuthority (org.springframework.security.GrantedAuthority)2 SecurityContextImpl (org.springframework.security.context.SecurityContextImpl)2 AuthenticationProvider (org.springframework.security.providers.AuthenticationProvider)2 ConfigFileHasChangedException (com.thoughtworks.go.config.exceptions.ConfigFileHasChangedException)1 ConfigMergeException (com.thoughtworks.go.config.exceptions.ConfigMergeException)1 GoConfigInvalidException (com.thoughtworks.go.config.exceptions.GoConfigInvalidException)1