Search in sources :

Example 1 with UsernamePasswordAuthenticationToken

use of org.springframework.security.providers.UsernamePasswordAuthenticationToken in project spring-security by spring-projects.

the class SecurityContextHolderMTTests method makeThread.

private Thread makeThread(final String threadIdentifier, final boolean topLevelThread, final boolean injectAuthIntoCurrentThread, final boolean expectAllThreadsToUseIdenticalAuthentication, final boolean expectChildrenToShareAuthenticationWithParent, final String expectedUsername) {
    final Random rnd = new Random();
    Thread t = new Thread(new Runnable() {

        public void run() {
            if (injectAuthIntoCurrentThread) {
                // Set authentication in this thread
                SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(expectedUsername, "pass"));
            //System.out.println(threadIdentifier + " - set to " + SecurityContextHolder.getContext().getAuthentication());
            } else {
            //System.out.println(threadIdentifier + " - not set (currently " + SecurityContextHolder.getContext().getAuthentication() + ")");
            }
            // Do some operations in current thread, checking authentication is as expected in the current thread (ie another thread doesn't change it)
            for (int i = 0; i < NUM_OPS; i++) {
                String currentUsername = (SecurityContextHolder.getContext().getAuthentication() == null) ? null : SecurityContextHolder.getContext().getAuthentication().getName();
                if ((i % 7) == 0) {
                    System.out.println(threadIdentifier + " at " + i + " username " + currentUsername);
                }
                try {
                    assertEquals("Failed on iteration " + i + "; Authentication was '" + currentUsername + "' but principal was expected to contain username '" + expectedUsername + "'", expectedUsername, currentUsername);
                } catch (ComparisonFailure err) {
                    errors++;
                    throw err;
                }
                try {
                    Thread.sleep(rnd.nextInt(250));
                } catch (InterruptedException ignore) {
                }
            }
            // Load some children threads, checking the authentication is as expected in the children (ie another thread doesn't change it)
            if (topLevelThread) {
                // Make four children, but we don't want the children to have any more children (so anti-nature, huh?)
                if (injectAuthIntoCurrentThread && expectChildrenToShareAuthenticationWithParent) {
                    loadStartAndWaitForThreads(false, threadIdentifier, 4, expectAllThreadsToUseIdenticalAuthentication, true);
                } else {
                    loadStartAndWaitForThreads(false, threadIdentifier, 4, expectAllThreadsToUseIdenticalAuthentication, false);
                }
            }
        }
    }, threadIdentifier);
    return t;
}
Also used : Random(java.util.Random) ComparisonFailure(junit.framework.ComparisonFailure) UsernamePasswordAuthenticationToken(org.springframework.security.providers.UsernamePasswordAuthenticationToken)

Example 2 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 3 with UsernamePasswordAuthenticationToken

use of org.springframework.security.providers.UsernamePasswordAuthenticationToken in project gocd by gocd.

the class GoConfigAdministrationControllerIntegrationTest method setCurrentUser.

private void setCurrentUser(String username) {
    SecurityContextImpl context = new SecurityContextImpl();
    context.setAuthentication(new UsernamePasswordAuthenticationToken(new User(username, "", true, new GrantedAuthority[] {}), null));
    SecurityContextHolder.setContext(context);
}
Also used : SecurityContextImpl(org.springframework.security.context.SecurityContextImpl) User(org.springframework.security.userdetails.User) UsernamePasswordAuthenticationToken(org.springframework.security.providers.UsernamePasswordAuthenticationToken)

Example 4 with UsernamePasswordAuthenticationToken

use of org.springframework.security.providers.UsernamePasswordAuthenticationToken in project gocd by gocd.

the class LdapAuthenticationTest method assertFailedAuthentication.

private void assertFailedAuthentication(String userName, String password) {
    Authentication authentication = new UsernamePasswordAuthenticationToken(userName, password);
    try {
        ldapAuthenticationProvider.authenticate(authentication);
        fail("Expected authentication to fail for user: " + userName);
    } catch (BadCredentialsException e) {
    }
}
Also used : Authentication(org.springframework.security.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.providers.UsernamePasswordAuthenticationToken) BadCredentialsException(org.springframework.security.BadCredentialsException)

Example 5 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)

Aggregations

UsernamePasswordAuthenticationToken (org.springframework.security.providers.UsernamePasswordAuthenticationToken)19 Test (org.junit.Test)11 User (org.springframework.security.userdetails.User)10 Authentication (org.springframework.security.Authentication)4 SecurityContext (org.springframework.security.context.SecurityContext)4 UserDetails (org.springframework.security.userdetails.UserDetails)4 AuthorityGranter (com.thoughtworks.go.server.security.AuthorityGranter)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 CaseInsensitiveString (com.thoughtworks.go.config.CaseInsensitiveString)1 SecurityAuthConfig (com.thoughtworks.go.config.SecurityAuthConfig)1 ConfigFileHasChangedException (com.thoughtworks.go.config.exceptions.ConfigFileHasChangedException)1 ConfigMergeException (com.thoughtworks.go.config.exceptions.ConfigMergeException)1 GoConfigInvalidException (com.thoughtworks.go.config.exceptions.GoConfigInvalidException)1 GitMaterialConfig (com.thoughtworks.go.config.materials.git.GitMaterialConfig)1 ConfigElementImplementationRegistry (com.thoughtworks.go.config.registry.ConfigElementImplementationRegistry)1 NoPluginsInstalled (com.thoughtworks.go.config.registry.NoPluginsInstalled)1