Search in sources :

Example 6 with GrantedAuthority

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

the class GoAuthenticationProviderFactoryTest method shouldCreateLicenseEnforcementProviderWithUserServicePassedIn.

@Test
public void shouldCreateLicenseEnforcementProviderWithUserServicePassedIn() throws Exception {
    GoAuthenticationProvider licenseEnforcementProvider = (GoAuthenticationProvider) factory.getObject();
    AuthenticationProvider underlyingProvider = mock(AuthenticationProvider.class);
    licenseEnforcementProvider.setProvider(underlyingProvider);
    UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken("foo", "bar");
    UsernamePasswordAuthenticationToken resultantAuthorization = new UsernamePasswordAuthenticationToken(new org.springframework.security.userdetails.User("foo-user", "pass", true, true, true, true, new GrantedAuthority[] { GoAuthority.ROLE_USER.asAuthority() }), "bar");
    when(underlyingProvider.authenticate(auth)).thenReturn(resultantAuthorization);
    licenseEnforcementProvider.authenticate(auth);
    verify(userService).addUserIfDoesNotExist(UserHelper.getUser(resultantAuthorization));
}
Also used : GrantedAuthority(org.springframework.security.GrantedAuthority) AuthenticationProvider(org.springframework.security.providers.AuthenticationProvider) UsernamePasswordAuthenticationToken(org.springframework.security.providers.UsernamePasswordAuthenticationToken) Test(org.junit.Test)

Example 7 with GrantedAuthority

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

the class OauthAuthenticationProviderTest method shouldReturnOAUTH_USERAsTheGrantedAuthority.

@Test
public void shouldReturnOAUTH_USERAsTheGrantedAuthority() {
    when(dataSource.findOauthTokenByAccessToken("token-string")).thenReturn(oauthTokenDto("user-id"));
    GrantedAuthority[] grantedAuthorities = { GoAuthority.ROLE_OAUTH_USER.asAuthority() };
    OauthAuthenticationToken authentication = provider.authenticate(new OauthAuthenticationToken("token-string"));
    assertThat(authentication.isAuthenticated(), is(true));
    UserDetails userDetails = authentication.getPrincipal();
    assertThat(userDetails.getUsername(), is("user-id"));
    assertThat(userDetails.getAuthorities(), is(grantedAuthorities));
    assertThat(authentication.getAuthorities(), is(grantedAuthorities));
}
Also used : UserDetails(org.springframework.security.userdetails.UserDetails) GrantedAuthority(org.springframework.security.GrantedAuthority) OauthAuthenticationToken(com.thoughtworks.go.server.security.OauthAuthenticationToken) Test(org.junit.Test)

Example 8 with GrantedAuthority

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

the class AuthorityGranterTest method shouldGrantTemplateViewUserRoleToTemplateViewUsers.

@Test
public void shouldGrantTemplateViewUserRoleToTemplateViewUsers() {
    String templateViewUser = "templateViewUser";
    when(securityService.isAuthorizedToViewAndEditTemplates(new Username(new CaseInsensitiveString(templateViewUser)))).thenReturn(false);
    when(securityService.isAuthorizedToViewTemplates(new Username(templateViewUser))).thenReturn(true);
    GrantedAuthority[] authorities = authorityGranter.authorities(templateViewUser);
    assertThat(authorities, hasItemInArray(GoAuthority.ROLE_TEMPLATE_VIEW_USER.asAuthority()));
    assertThat(authorities, not(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 9 with GrantedAuthority

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

the class AuthorityGranterTest method shouldGrantRoleUserToUsersWhoAreNotSpecial.

@Test
public void shouldGrantRoleUserToUsersWhoAreNotSpecial() {
    when(securityService.isUserAdmin(new Username(new CaseInsensitiveString("admin")))).thenReturn(false);
    when(securityService.isUserGroupAdmin(new Username(new CaseInsensitiveString("admin")))).thenReturn(false);
    GrantedAuthority[] authorities = authorityGranter.authorities("admin");
    assertThat("Should not have " + GoAuthority.ROLE_SUPERVISOR + " authority", authorities, not(hasItemInArray(GoAuthority.ROLE_SUPERVISOR.asAuthority())));
    assertThat("Should not have " + GoAuthority.ROLE_GROUP_SUPERVISOR + " authority", authorities, not(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 10 with GrantedAuthority

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

the class AuthorityGranterTest method shouldGrantSupervisorRoleToUsersWhoAreAdminsAndGroupAdmins.

@Test
public void shouldGrantSupervisorRoleToUsersWhoAreAdminsAndGroupAdmins() {
    when(securityService.isUserAdmin(new Username(new CaseInsensitiveString("admin")))).thenReturn(true);
    when(securityService.isUserGroupAdmin(new Username(new CaseInsensitiveString("admin")))).thenReturn(true);
    GrantedAuthority[] authorities = authorityGranter.authorities("admin");
    assertThat("Should have " + GoAuthority.ROLE_SUPERVISOR + " authority", authorities, 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)

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