Search in sources :

Example 6 with GoUserPrinciple

use of com.thoughtworks.go.server.security.userdetail.GoUserPrinciple in project gocd by gocd.

the class UserHelperTest method shouldGetDisplayNameForAPasswordFileUser.

@Test
public void shouldGetDisplayNameForAPasswordFileUser() {
    GrantedAuthority[] authorities = { new GrantedAuthorityImpl("anything") };
    TestingAuthenticationToken authentication = new TestingAuthenticationToken(new GoUserPrinciple("user", "Full Name", "password", true, true, true, true, authorities), null, authorities);
    assertThat(UserHelper.getUserName(authentication), is(new Username(new CaseInsensitiveString("user"), "Full Name")));
}
Also used : GrantedAuthorityImpl(org.springframework.security.GrantedAuthorityImpl) Username(com.thoughtworks.go.server.domain.Username) GrantedAuthority(org.springframework.security.GrantedAuthority) GoUserPrinciple(com.thoughtworks.go.server.security.userdetail.GoUserPrinciple) TestingAuthenticationToken(org.springframework.security.providers.TestingAuthenticationToken) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) Test(org.junit.Test)

Example 7 with GoUserPrinciple

use of com.thoughtworks.go.server.security.userdetail.GoUserPrinciple in project gocd by gocd.

the class PluginAuthenticationProviderTest method shouldCreateGoUserPrincipalWhenAnAuthenticationPluginIsAbleToAuthenticateUser.

@Test
public void shouldCreateGoUserPrincipalWhenAnAuthenticationPluginIsAbleToAuthenticateUser() {
    String pluginId1 = "plugin-id-1";
    String pluginId2 = "plugin-id-2";
    when(authenticationPluginRegistry.getPluginsThatSupportsPasswordBasedAuthentication()).thenReturn(new HashSet<>(Arrays.asList(pluginId1, pluginId2)));
    when(authenticationExtension.authenticateUser(pluginId1, "username", "password")).thenReturn(null);
    when(authenticationExtension.authenticateUser(pluginId2, "username", "password")).thenReturn(new User("username", null, null));
    UserDetails userDetails = provider.retrieveUser("username", authenticationToken);
    assertThat(userDetails, is(instanceOf(GoUserPrinciple.class)));
    GoUserPrinciple goUserPrincipal = (GoUserPrinciple) userDetails;
    assertThat(goUserPrincipal.getUsername(), is("username"));
    assertThat(goUserPrincipal.getDisplayName(), is("username"));
    assertThat(goUserPrincipal.getAuthorities().length, is(1));
    assertThat(goUserPrincipal.getAuthorities()[0], is(userAuthority));
}
Also used : User(com.thoughtworks.go.plugin.access.authentication.models.User) UserDetails(org.springframework.security.userdetails.UserDetails) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) GoUserPrinciple(com.thoughtworks.go.server.security.userdetail.GoUserPrinciple) Test(org.junit.Test)

Example 8 with GoUserPrinciple

use of com.thoughtworks.go.server.security.userdetail.GoUserPrinciple in project gocd by gocd.

the class PluginAuthenticationProviderTest method shouldCreateGoUserPrincipalWhenAnAuthorizationPluginIsAbleToAuthenticateUser.

@Test
public void shouldCreateGoUserPrincipalWhenAnAuthorizationPluginIsAbleToAuthenticateUser() {
    String pluginId1 = "plugin-id-1";
    String pluginId2 = "plugin-id-2";
    securityConfig.securityAuthConfigs().add(new SecurityAuthConfig("github", pluginId2));
    securityConfig.addRole(new PluginRoleConfig("admin", "github", ConfigurationPropertyMother.create("foo")));
    when(store.getPluginsThatSupportsPasswordBasedAuthentication()).thenReturn(new HashSet<>(Arrays.asList(pluginId1, pluginId2)));
    when(authorizationExtension.authenticateUser(pluginId1, "username", "password", securityConfig.securityAuthConfigs().findByPluginId(pluginId1), null)).thenReturn(NULL_AUTH_RESPONSE);
    AuthenticationResponse response = new AuthenticationResponse(new User("username", "display-name", "test@test.com"), Collections.emptyList());
    when(authorizationExtension.authenticateUser(pluginId2, "username", "password", securityConfig.securityAuthConfigs().findByPluginId(pluginId2), securityConfig.getPluginRoles(pluginId2))).thenReturn(response);
    UserDetails userDetails = provider.retrieveUser("username", authenticationToken);
    assertThat(userDetails, is(instanceOf(GoUserPrinciple.class)));
    GoUserPrinciple goUserPrincipal = (GoUserPrinciple) userDetails;
    assertThat(goUserPrincipal.getUsername(), is("username"));
    assertThat(goUserPrincipal.getDisplayName(), is("display-name"));
    assertThat(goUserPrincipal.getAuthorities().length, is(1));
    assertThat(goUserPrincipal.getAuthorities()[0], is(userAuthority));
}
Also used : SecurityAuthConfig(com.thoughtworks.go.config.SecurityAuthConfig) User(com.thoughtworks.go.plugin.access.authentication.models.User) UserDetails(org.springframework.security.userdetails.UserDetails) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) GoUserPrinciple(com.thoughtworks.go.server.security.userdetail.GoUserPrinciple) PluginRoleConfig(com.thoughtworks.go.config.PluginRoleConfig) AuthenticationResponse(com.thoughtworks.go.plugin.access.authorization.models.AuthenticationResponse) Test(org.junit.Test)

Example 9 with GoUserPrinciple

use of com.thoughtworks.go.server.security.userdetail.GoUserPrinciple in project gocd by gocd.

the class FileAuthenticationProviderTest method shouldStripOutAuthoritiesThatIsSpecifiedInPasswordFile.

@Test
public void shouldStripOutAuthoritiesThatIsSpecifiedInPasswordFile() throws Exception {
    setupFile("jez=" + SHA1_BADGER + ",ROLE_OF_GOD");
    when(securityService.isUserAdmin(new Username(new CaseInsensitiveString("jez")))).thenReturn(true);
    when(userService.findUserByName("jez")).thenReturn(new com.thoughtworks.go.domain.User("jez", "Jezz Humbles", "jez@humble.com"));
    AuthorityGranter authorityGranter = new AuthorityGranter(securityService);
    FileAuthenticationProvider provider = new FileAuthenticationProvider(goConfigService, authorityGranter, userService, securityService);
    final GoUserPrinciple details = (GoUserPrinciple) provider.retrieveUser("jez", null);
    assertThat(details.getUsername(), is("jez"));
    assertThat(details.getDisplayName(), is("Jezz Humbles"));
    assertThat(details.getAuthorities().length, is(2));
    assertThat(details.getAuthorities()[0].getAuthority(), Is.is(GoAuthority.ROLE_SUPERVISOR.name()));
    assertThat(details.getAuthorities()[1].getAuthority(), is(GoAuthority.ROLE_USER.name()));
}
Also used : AuthorityGranter(com.thoughtworks.go.server.security.AuthorityGranter) Username(com.thoughtworks.go.server.domain.Username) GoUserPrinciple(com.thoughtworks.go.server.security.userdetail.GoUserPrinciple) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) Test(org.junit.Test)

Example 10 with GoUserPrinciple

use of com.thoughtworks.go.server.security.userdetail.GoUserPrinciple in project gocd by gocd.

the class PluginAuthenticationProviderTest method reuthenticationUsingAuthorizationPlugins_shouldFallbackOnUserNameInAbsenceOfLoginNameInGoUserPrinciple.

@Test
public void reuthenticationUsingAuthorizationPlugins_shouldFallbackOnUserNameInAbsenceOfLoginNameInGoUserPrinciple() throws Exception {
    String pluginId1 = "cd.go.ldap";
    securityConfig.securityAuthConfigs().add(new SecurityAuthConfig("ldap", "cd.go.ldap"));
    addPluginSupportingPasswordBasedAuthentication(pluginId1);
    when(authorizationExtension.authenticateUser(pluginId1, "username", "password", securityConfig.securityAuthConfigs().findByPluginId(pluginId1), securityConfig.getPluginRoles(pluginId1))).thenReturn(new AuthenticationResponse(new User("username", "bob", "bob@example.com"), Arrays.asList("blackbird", "admins")));
    GoUserPrinciple principal = new GoUserPrinciple("username", "Display", "password", true, true, true, true, new GrantedAuthority[] {}, null);
    UserDetails userDetails = provider.retrieveUser("username", new UsernamePasswordAuthenticationToken(principal, "password"));
    assertNotNull(userDetails);
    verify(pluginRoleService).updatePluginRoles("cd.go.ldap", "username", CaseInsensitiveString.caseInsensitiveStrings(Arrays.asList("blackbird", "admins")));
}
Also used : SecurityAuthConfig(com.thoughtworks.go.config.SecurityAuthConfig) User(com.thoughtworks.go.plugin.access.authorization.models.User) UserDetails(org.springframework.security.userdetails.UserDetails) UsernamePasswordAuthenticationToken(org.springframework.security.providers.UsernamePasswordAuthenticationToken) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) GoUserPrinciple(com.thoughtworks.go.server.security.userdetail.GoUserPrinciple) AuthenticationResponse(com.thoughtworks.go.plugin.access.authorization.models.AuthenticationResponse) Test(org.junit.Test)

Aggregations

GoUserPrinciple (com.thoughtworks.go.server.security.userdetail.GoUserPrinciple)15 Test (org.junit.Test)11 CaseInsensitiveString (com.thoughtworks.go.config.CaseInsensitiveString)10 AuthenticationResponse (com.thoughtworks.go.plugin.access.authorization.models.AuthenticationResponse)5 User (com.thoughtworks.go.plugin.access.authorization.models.User)5 UserDetails (org.springframework.security.userdetails.UserDetails)5 SecurityAuthConfig (com.thoughtworks.go.config.SecurityAuthConfig)4 User (com.thoughtworks.go.plugin.access.authentication.models.User)4 PluginRoleConfig (com.thoughtworks.go.config.PluginRoleConfig)2 Username (com.thoughtworks.go.server.domain.Username)2 AuthorityGranter (com.thoughtworks.go.server.security.AuthorityGranter)2 PreAuthenticatedAuthenticationToken (com.thoughtworks.go.server.security.tokens.PreAuthenticatedAuthenticationToken)2 Authentication (org.springframework.security.Authentication)2 GrantedAuthority (org.springframework.security.GrantedAuthority)2 TestingAuthenticationToken (org.springframework.security.providers.TestingAuthenticationToken)2 UsernamePasswordAuthenticationToken (org.springframework.security.providers.UsernamePasswordAuthenticationToken)2 DefaultGoApiResponse (com.thoughtworks.go.plugin.api.response.DefaultGoApiResponse)1 GoApiResponse (com.thoughtworks.go.plugin.api.response.GoApiResponse)1 Arrays.asList (java.util.Arrays.asList)1 List (java.util.List)1