Search in sources :

Example 11 with GrantedAuthority

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

the class AuthorityGranterTest method shouldGrantGroupSupervisorRoleToPipelineGroupAdmins.

@Test
public void shouldGrantGroupSupervisorRoleToPipelineGroupAdmins() {
    when(securityService.isUserGroupAdmin(new Username(new CaseInsensitiveString("group-admin")))).thenReturn(true);
    GrantedAuthority[] authorities = authorityGranter.authorities("group-admin");
    assertThat("Should not have " + GoAuthority.ROLE_SUPERVISOR + " authority", authorities, not(hasItemInArray(GoAuthority.ROLE_SUPERVISOR.asAuthority())));
    assertThat("Should have " + GoAuthority.ROLE_GROUP_SUPERVISOR + " authority", authorities, hasItemInArray(GoAuthority.ROLE_GROUP_SUPERVISOR.asAuthority()));
    assertThat("Should have " + GoAuthority.ROLE_USER + " authority", authorities, hasItemInArray(GoAuthority.ROLE_USER.asAuthority()));
}
Also used : Username(com.thoughtworks.go.server.domain.Username) GrantedAuthority(org.springframework.security.GrantedAuthority) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) Test(org.junit.Test)

Example 12 with GrantedAuthority

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

the class RemoveAdminPermissionFilterIntegrationTest method setupAuthentication.

private Authentication setupAuthentication() {
    GrantedAuthority[] authorities = {};
    Authentication authentication = new TestingAuthenticationToken(new User("loser", "secret", 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 13 with GrantedAuthority

use of org.springframework.security.GrantedAuthority 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 14 with GrantedAuthority

use of org.springframework.security.GrantedAuthority 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 15 with GrantedAuthority

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

the class OauthAuthenticationFilterTest method shouldAuthenticateToken.

@Test
public void shouldAuthenticateToken() throws IOException, ServletException {
    when(req.getHeader(OauthAuthenticationFilter.AUTHORIZATION)).thenReturn("Token token=\"valid-token\"");
    OauthAuthenticationToken authenticatedToken = new OauthAuthenticationToken(new User("user-name", "valid-token", true, true, true, true, new GrantedAuthority[] { GoAuthority.ROLE_SUPERVISOR.asAuthority() }));
    when(authenticationManager.authenticate(new OauthAuthenticationToken("valid-token"))).thenReturn(authenticatedToken);
    filter.doFilterHttp(req, res, chain);
    verify(securityContext).setAuthentication(authenticatedToken);
    verify(chain).doFilter(req, res);
//assertThat(logFixture.contains(Level.DEBUG, "Oauth authorization header: Token token=\"valid-token\""), is(true));//uncomment this to run it locally (this fails on build, we need to find out why). -Rajesh & JJ
}
Also used : User(org.springframework.security.userdetails.User) GrantedAuthority(org.springframework.security.GrantedAuthority) Test(org.junit.Test)

Aggregations

GrantedAuthority (org.springframework.security.GrantedAuthority)20 Test (org.junit.Test)17 TestingAuthenticationToken (org.springframework.security.providers.TestingAuthenticationToken)9 GrantedAuthorityImpl (org.springframework.security.GrantedAuthorityImpl)7 CaseInsensitiveString (com.thoughtworks.go.config.CaseInsensitiveString)6 Username (com.thoughtworks.go.server.domain.Username)6 User (org.springframework.security.userdetails.User)4 Authentication (org.springframework.security.Authentication)3 UsernamePasswordAuthenticationToken (org.springframework.security.providers.UsernamePasswordAuthenticationToken)2 UserDetails (org.springframework.security.userdetails.UserDetails)2 X509CertificateGenerator (com.thoughtworks.go.security.X509CertificateGenerator)1 OauthAuthenticationToken (com.thoughtworks.go.server.security.OauthAuthenticationToken)1 GoUserPrinciple (com.thoughtworks.go.server.security.userdetail.GoUserPrinciple)1 X509Certificate (java.security.cert.X509Certificate)1 AuthenticationProvider (org.springframework.security.providers.AuthenticationProvider)1