Search in sources :

Example 6 with UserDetails

use of org.springframework.security.userdetails.UserDetails in project gocd by gocd.

the class FileAuthenticationProviderTest method shouldAuthenticateUserWithValidPassword.

@Test
public void shouldAuthenticateUserWithValidPassword() throws Exception {
    AuthorityGranter authorityGranter = new AuthorityGranter(securityService);
    FileAuthenticationProvider provider = new FileAuthenticationProvider(goConfigService, authorityGranter, userService, securityService);
    UserDetails user = new User("jez", SHA1_BADGER, true, true, true, true, new GrantedAuthority[0]);
    provider.additionalAuthenticationChecks(user, new UsernamePasswordAuthenticationToken("jez", "badger"));
}
Also used : AuthorityGranter(com.thoughtworks.go.server.security.AuthorityGranter) UserDetails(org.springframework.security.userdetails.UserDetails) User(org.springframework.security.userdetails.User) UsernamePasswordAuthenticationToken(org.springframework.security.providers.UsernamePasswordAuthenticationToken) Test(org.junit.Test)

Example 7 with UserDetails

use of org.springframework.security.userdetails.UserDetails in project gocd by gocd.

the class FileAuthenticationProviderTest method shouldRetrieveDetailsIfUsernameSpecifiedInFile.

@Test
public void shouldRetrieveDetailsIfUsernameSpecifiedInFile() throws Exception {
    setupFile("jez=" + SHA1_BADGER);
    AuthorityGranter authorityGranter = new AuthorityGranter(securityService);
    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"));
    FileAuthenticationProvider provider = new FileAuthenticationProvider(goConfigService, authorityGranter, userService, securityService);
    final UserDetails details = provider.retrieveUser("jez", null);
    assertThat(details.getAuthorities()[0].getAuthority(), is("ROLE_SUPERVISOR"));
    assertThat(details.isAccountNonExpired(), is(true));
    assertThat(details.isAccountNonLocked(), is(true));
    assertThat(details.isCredentialsNonExpired(), is(true));
    assertThat(details.isEnabled(), is(true));
    assertThat(details.getUsername(), is("jez"));
    assertThat(details.getPassword(), is(SHA1_BADGER));
}
Also used : AuthorityGranter(com.thoughtworks.go.server.security.AuthorityGranter) UserDetails(org.springframework.security.userdetails.UserDetails) Username(com.thoughtworks.go.server.domain.Username) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) Test(org.junit.Test)

Example 8 with UserDetails

use of org.springframework.security.userdetails.UserDetails 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 9 with UserDetails

use of org.springframework.security.userdetails.UserDetails 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 10 with UserDetails

use of org.springframework.security.userdetails.UserDetails 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)

Aggregations

UserDetails (org.springframework.security.userdetails.UserDetails)12 Test (org.junit.Test)9 CaseInsensitiveString (com.thoughtworks.go.config.CaseInsensitiveString)5 AuthorityGranter (com.thoughtworks.go.server.security.AuthorityGranter)4 UsernamePasswordAuthenticationToken (org.springframework.security.providers.UsernamePasswordAuthenticationToken)4 User (org.springframework.security.userdetails.User)4 User (com.thoughtworks.go.plugin.access.authentication.models.User)3 SecurityAuthConfig (com.thoughtworks.go.config.SecurityAuthConfig)2 AuthenticationResponse (com.thoughtworks.go.plugin.access.authorization.models.AuthenticationResponse)2 Username (com.thoughtworks.go.server.domain.Username)2 OauthAuthenticationToken (com.thoughtworks.go.server.security.OauthAuthenticationToken)2 GoUserPrinciple (com.thoughtworks.go.server.security.userdetail.GoUserPrinciple)2 GrantedAuthority (org.springframework.security.GrantedAuthority)2 PluginRoleConfig (com.thoughtworks.go.config.PluginRoleConfig)1 X509CertificateGenerator (com.thoughtworks.go.security.X509CertificateGenerator)1 OauthDataSource (com.thoughtworks.go.server.oauth.OauthDataSource)1 IOException (java.io.IOException)1 X509Certificate (java.security.cert.X509Certificate)1 BadCredentialsException (org.springframework.security.BadCredentialsException)1 GrantedAuthorityImpl (org.springframework.security.GrantedAuthorityImpl)1