Search in sources :

Example 16 with GrantedAuthority

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

the class X509AuthoritiesPopulatorTest method shouldReturnUserDetailsWithCorrectAuthorityIfAgentCertificateHasOu.

@Test
public void shouldReturnUserDetailsWithCorrectAuthorityIfAgentCertificateHasOu() {
    X509Certificate agentCertificate = new X509CertificateGenerator().createCertificateWithDn("CN=hostname, OU=agent").getFirstCertificate();
    UserDetails userDetails = populator.getUserDetails(agentCertificate);
    GrantedAuthority[] actual = userDetails.getAuthorities();
    GrantedAuthority expected = new GrantedAuthorityImpl(ROLE_AGENT);
    assertThat(actual.length, is(1));
    assertThat(actual[0], is(expected));
}
Also used : UserDetails(org.springframework.security.userdetails.UserDetails) GrantedAuthorityImpl(org.springframework.security.GrantedAuthorityImpl) GrantedAuthority(org.springframework.security.GrantedAuthority) X509Certificate(java.security.cert.X509Certificate) X509CertificateGenerator(com.thoughtworks.go.security.X509CertificateGenerator) Test(org.junit.Test)

Example 17 with GrantedAuthority

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

the class AuthorityGranterTest method shouldGrantTemplateSupervisorRoleToTemplateAdmins.

@Test
public void shouldGrantTemplateSupervisorRoleToTemplateAdmins() throws Exception {
    String templateAdmin = "template-admin";
    when(securityService.isAuthorizedToViewAndEditTemplates(new Username(new CaseInsensitiveString(templateAdmin)))).thenReturn(true);
    GrantedAuthority[] authorities = authorityGranter.authorities(templateAdmin);
    assertThat(authorities, hasItemInArray(GoAuthority.ROLE_TEMPLATE_SUPERVISOR.asAuthority()));
    assertThat(authorities, not(hasItemInArray(GoAuthority.ROLE_GROUP_SUPERVISOR.asAuthority())));
    assertThat(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) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) Test(org.junit.Test)

Example 18 with GrantedAuthority

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

the class GoVelocityViewTest method shouldSetGroupAdministratorIfUserIsAPipelineGroupAdministrator.

@Test
public void shouldSetGroupAdministratorIfUserIsAPipelineGroupAdministrator() throws Exception {
    securityContext.setAuthentication(new TestingAuthenticationToken("jez", "badger", new GrantedAuthority[] { new GrantedAuthorityImpl(GoAuthority.ROLE_GROUP_SUPERVISOR.toString()) }));
    request.getSession().setAttribute(SPRING_SECURITY_CONTEXT_KEY, securityContext);
    view.exposeHelpers(velocityContext, request);
    assertThat(velocityContext.get(GoVelocityView.ADMINISTRATOR), is(nullValue()));
    assertThat(velocityContext.get(GoVelocityView.GROUP_ADMINISTRATOR), is(true));
}
Also used : GrantedAuthorityImpl(org.springframework.security.GrantedAuthorityImpl) GrantedAuthority(org.springframework.security.GrantedAuthority) TestingAuthenticationToken(org.springframework.security.providers.TestingAuthenticationToken) Test(org.junit.Test)

Example 19 with GrantedAuthority

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

the class GoVelocityViewTest method shouldSetAdministratorIfUserIsAdministrator.

@Test
public void shouldSetAdministratorIfUserIsAdministrator() throws Exception {
    securityContext.setAuthentication(new TestingAuthenticationToken("jez", "badger", new GrantedAuthority[] { new GrantedAuthorityImpl(GoAuthority.ROLE_SUPERVISOR.toString()) }));
    request.getSession().setAttribute(SPRING_SECURITY_CONTEXT_KEY, securityContext);
    view.exposeHelpers(velocityContext, request);
    assertThat(velocityContext.get(GoVelocityView.ADMINISTRATOR), is(true));
}
Also used : GrantedAuthorityImpl(org.springframework.security.GrantedAuthorityImpl) GrantedAuthority(org.springframework.security.GrantedAuthority) TestingAuthenticationToken(org.springframework.security.providers.TestingAuthenticationToken) Test(org.junit.Test)

Example 20 with GrantedAuthority

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

the class UserHelperTest method stubSecurityContextForRole.

public static void stubSecurityContextForRole(String roleName) {
    GrantedAuthority agentAuth = mock(GrantedAuthority.class);
    when(agentAuth.getAuthority()).thenReturn(roleName);
    GrantedAuthority[] grantedAuthorities = roleName == null ? new GrantedAuthority[] {} : new GrantedAuthority[] { agentAuth };
    stubSecurityContextForGrantedAuthorities(grantedAuthorities);
}
Also used : GrantedAuthority(org.springframework.security.GrantedAuthority)

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