Search in sources :

Example 1 with UserDetails

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

the class FileAuthenticationProvider method retrieveUser.

protected UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
    final String passwordFilePath = goConfigService.security().passwordFileConfig().path();
    try {
        UserMap userMap = UserMapEditor.addUsersFromProperties(new UserMap(), addDummyRoleToPropertiesIfRequired(stripShaFromPasswordsIfRequired(loadPasswordFile(passwordFilePath))));
        final UserDetails details = userMap.getUser(username);
        return userStrippedOfAnyAuthoritiesSpecifiedInFile(username, details);
    } catch (IOException e) {
        throw new UsernameNotFoundException("Trying to authenticate user " + username + " but could not open file: " + passwordFilePath);
    }
}
Also used : UsernameNotFoundException(org.springframework.security.userdetails.UsernameNotFoundException) UserMap(org.springframework.security.userdetails.memory.UserMap) UserDetails(org.springframework.security.userdetails.UserDetails) IOException(java.io.IOException)

Example 2 with UserDetails

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

the class FileAuthenticationProviderTest method shouldNotUserWithoutValidPassword.

@Test(expected = BadCredentialsException.class)
public void shouldNotUserWithoutValidPassword() throws Exception {
    AuthorityGranter authorityGranter = new AuthorityGranter(securityService);
    FileAuthenticationProvider provider = new FileAuthenticationProvider(goConfigService, authorityGranter, userService, securityService);
    UserDetails user = new User("jez", "something", true, true, true, true, new GrantedAuthority[0]);
    provider.additionalAuthenticationChecks(user, new UsernamePasswordAuthenticationToken("jez", "nothing"));
}
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 3 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 4 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 5 with UserDetails

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

Aggregations

UserDetails (org.springframework.security.userdetails.UserDetails)19 Test (org.junit.Test)15 CaseInsensitiveString (com.thoughtworks.go.config.CaseInsensitiveString)11 AuthenticationResponse (com.thoughtworks.go.plugin.access.authorization.models.AuthenticationResponse)9 SecurityAuthConfig (com.thoughtworks.go.config.SecurityAuthConfig)8 UsernamePasswordAuthenticationToken (org.springframework.security.providers.UsernamePasswordAuthenticationToken)8 User (com.thoughtworks.go.plugin.access.authorization.models.User)7 GoUserPrinciple (com.thoughtworks.go.server.security.userdetail.GoUserPrinciple)5 AuthorityGranter (com.thoughtworks.go.server.security.AuthorityGranter)4 User (org.springframework.security.userdetails.User)4 PluginRoleConfig (com.thoughtworks.go.config.PluginRoleConfig)2 User (com.thoughtworks.go.plugin.access.authentication.models.User)2 Username (com.thoughtworks.go.server.domain.Username)2 OauthAuthenticationToken (com.thoughtworks.go.server.security.OauthAuthenticationToken)2 BadCredentialsException (org.springframework.security.BadCredentialsException)2 GrantedAuthority (org.springframework.security.GrantedAuthority)2 X509CertificateGenerator (com.thoughtworks.go.security.X509CertificateGenerator)1 OauthDataSource (com.thoughtworks.go.server.oauth.OauthDataSource)1 PreAuthenticatedAuthenticationToken (com.thoughtworks.go.server.security.tokens.PreAuthenticatedAuthenticationToken)1 IOException (java.io.IOException)1