Search in sources :

Example 16 with Authentication

use of org.springframework.security.Authentication 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 17 with Authentication

use of org.springframework.security.Authentication 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 18 with Authentication

use of org.springframework.security.Authentication in project gocd by gocd.

the class ReAuthenticationFilterTest method setupAuthentication.

private Authentication setupAuthentication() {
    GrantedAuthority[] authorities = {};
    Authentication authentication = new TestingAuthenticationToken(new User("user", "password", true, true, true, true, authorities), null, authorities);
    SecurityContextHolder.getContext().setAuthentication(authentication);
    authentication.setAuthenticated(true);
    return authentication;
}
Also used : User(org.springframework.security.userdetails.User) Authentication(org.springframework.security.Authentication) GrantedAuthority(org.springframework.security.GrantedAuthority) TestingAuthenticationToken(org.springframework.security.providers.TestingAuthenticationToken)

Example 19 with Authentication

use of org.springframework.security.Authentication in project gocd by gocd.

the class UserEnabledCheckFilterTest method shouldNotSetUserIdInSessionIfAlreadySet.

@Test
public void shouldNotSetUserIdInSessionIfAlreadySet() throws IOException, ServletException {
    SecurityContextHelper.setCurrentUser("winner");
    Long userId = 1L;
    User user = getUser("winner", userId);
    Authentication actual = SecurityContextHolder.getContext().getAuthentication();
    when(session.getAttribute(USERID_ATTR)).thenReturn(userId);
    when(userService.load(userId)).thenReturn(user);
    filter.doFilterHttp(req, res, chain);
    assertThat(SecurityContextHolder.getContext().getAuthentication(), is(actual));
    verify(session, never()).setAttribute(USERID_ATTR, userId);
    verify(chain).doFilter(req, res);
}
Also used : User(com.thoughtworks.go.domain.User) NullUser(com.thoughtworks.go.domain.NullUser) Authentication(org.springframework.security.Authentication)

Example 20 with Authentication

use of org.springframework.security.Authentication in project gocd by gocd.

the class GoAuthenticationProviderTest method shouldNotFailWhenUnderlyingProviderDoesNotAuthenticate.

@Test
public void shouldNotFailWhenUnderlyingProviderDoesNotAuthenticate() throws Exception {
    when(underlyingProvider.authenticate(auth)).thenReturn(null);
    Authentication authentication = enforcementProvider.authenticate(auth);
    assertThat(authentication, is(nullValue()));
}
Also used : Authentication(org.springframework.security.Authentication) Test(org.junit.Test)

Aggregations

Authentication (org.springframework.security.Authentication)31 Test (org.junit.Test)16 UsernamePasswordAuthenticationToken (org.springframework.security.providers.UsernamePasswordAuthenticationToken)5 NullUser (com.thoughtworks.go.domain.NullUser)4 GrantedAuthority (org.springframework.security.GrantedAuthority)4 TestingAuthenticationToken (org.springframework.security.providers.TestingAuthenticationToken)4 User (com.thoughtworks.go.domain.User)3 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)3 PreAuthenticatedAuthenticationToken (com.thoughtworks.go.server.security.tokens.PreAuthenticatedAuthenticationToken)2 GoUserPrinciple (com.thoughtworks.go.server.security.userdetail.GoUserPrinciple)2 Expectations (org.jmock.Expectations)2 AuthenticationException (org.springframework.security.AuthenticationException)2 User (org.springframework.security.userdetails.User)2 SecurityAuthConfig (com.thoughtworks.go.config.SecurityAuthConfig)1 User (com.thoughtworks.go.plugin.access.authentication.models.User)1 DefaultGoApiResponse (com.thoughtworks.go.plugin.api.response.DefaultGoApiResponse)1 Username (com.thoughtworks.go.server.domain.Username)1 HttpLocalizedOperationResult (com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1