Search in sources :

Example 91 with UserDetails

use of org.springframework.security.core.userdetails.UserDetails in project incubator-atlas by apache.

the class AtlasLdapAuthenticationProvider method getLdapBindAuthentication.

private Authentication getLdapBindAuthentication(Authentication authentication) {
    try {
        if (isDebugEnabled) {
            LOG.debug("==> AtlasLdapAuthenticationProvider getLdapBindAuthentication");
        }
        String userName = authentication.getName();
        String userPassword = "";
        if (authentication.getCredentials() != null) {
            userPassword = authentication.getCredentials().toString();
        }
        LdapContextSource ldapContextSource = getLdapContextSource();
        DefaultLdapAuthoritiesPopulator defaultLdapAuthoritiesPopulator = getDefaultLdapAuthoritiesPopulator(ldapContextSource);
        if (ldapUserSearchFilter == null || ldapUserSearchFilter.trim().isEmpty()) {
            ldapUserSearchFilter = "(uid={0})";
        }
        FilterBasedLdapUserSearch userSearch = new FilterBasedLdapUserSearch(ldapBase, ldapUserSearchFilter, ldapContextSource);
        userSearch.setSearchSubtree(true);
        BindAuthenticator bindAuthenticator = getBindAuthenticator(userSearch, ldapContextSource);
        LdapAuthenticationProvider ldapAuthenticationProvider = new LdapAuthenticationProvider(bindAuthenticator, defaultLdapAuthoritiesPopulator);
        if (userName != null && userPassword != null && !userName.trim().isEmpty() && !userPassword.trim().isEmpty()) {
            final List<GrantedAuthority> grantedAuths = getAuthorities(userName);
            final UserDetails principal = new User(userName, userPassword, grantedAuths);
            final Authentication finalAuthentication = new UsernamePasswordAuthenticationToken(principal, userPassword, grantedAuths);
            authentication = ldapAuthenticationProvider.authenticate(finalAuthentication);
            if (groupsFromUGI) {
                authentication = getAuthenticationWithGrantedAuthorityFromUGI(authentication);
            }
            return authentication;
        } else {
            LOG.error("LDAP Authentication::userName or userPassword is null or empty for userName " + userName);
        }
    } catch (Exception e) {
        LOG.error(" getLdapBindAuthentication LDAP Authentication Failed:", e);
    }
    if (isDebugEnabled) {
        LOG.debug("<== AtlasLdapAuthenticationProvider getLdapBindAuthentication");
    }
    return authentication;
}
Also used : BindAuthenticator(org.springframework.security.ldap.authentication.BindAuthenticator) User(org.apache.atlas.web.model.User) LdapContextSource(org.springframework.ldap.core.support.LdapContextSource) DefaultLdapAuthoritiesPopulator(org.springframework.security.ldap.userdetails.DefaultLdapAuthoritiesPopulator) GrantedAuthority(org.springframework.security.core.GrantedAuthority) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) AuthenticationException(org.springframework.security.core.AuthenticationException) UserDetails(org.springframework.security.core.userdetails.UserDetails) Authentication(org.springframework.security.core.Authentication) FilterBasedLdapUserSearch(org.springframework.security.ldap.search.FilterBasedLdapUserSearch) LdapAuthenticationProvider(org.springframework.security.ldap.authentication.LdapAuthenticationProvider)

Example 92 with UserDetails

use of org.springframework.security.core.userdetails.UserDetails in project incubator-atlas by apache.

the class AtlasFileAuthenticationProvider method authenticate.

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    String username = authentication.getName();
    String password = (String) authentication.getCredentials();
    if (username == null || username.isEmpty()) {
        logger.error("Username can't be null or empty.");
        throw new BadCredentialsException("Username can't be null or empty.");
    }
    if (password == null || password.isEmpty()) {
        logger.error("Password can't be null or empty.");
        throw new BadCredentialsException("Password can't be null or empty.");
    }
    UserDetails user = userDetailsService.loadUserByUsername(username);
    String encodedPassword = UserDao.getSha256Hash(password);
    if (!encodedPassword.equals(user.getPassword())) {
        logger.error("Wrong password " + username);
        throw new BadCredentialsException("Wrong password");
    }
    Collection<? extends GrantedAuthority> authorities = user.getAuthorities();
    authentication = new UsernamePasswordAuthenticationToken(username, password, authorities);
    return authentication;
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException)

Example 93 with UserDetails

use of org.springframework.security.core.userdetails.UserDetails in project incubator-atlas by apache.

the class AtlasAbstractAuthenticationProvider method getAuthenticationWithGrantedAuthority.

/**
     * 
     * @param authentication
     * @return
     */
public Authentication getAuthenticationWithGrantedAuthority(Authentication authentication) {
    UsernamePasswordAuthenticationToken result = null;
    if (authentication != null && authentication.isAuthenticated()) {
        final List<GrantedAuthority> grantedAuths = getAuthorities(authentication.getName());
        final UserDetails userDetails = new User(authentication.getName(), authentication.getCredentials().toString(), grantedAuths);
        result = new UsernamePasswordAuthenticationToken(userDetails, authentication.getCredentials(), grantedAuths);
        result.setDetails(authentication.getDetails());
        return result;
    }
    return authentication;
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) User(org.springframework.security.core.userdetails.User) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Example 94 with UserDetails

use of org.springframework.security.core.userdetails.UserDetails in project dhis2-core by dhis2.

the class DhisWebSpringTest method getPrincipal.

protected UsernamePasswordAuthenticationToken getPrincipal(String... authorities) {
    User user = createAdminUser(authorities);
    List<GrantedAuthority> grantedAuthorities = user.getUserCredentials().getAllAuthorities().stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
    UserDetails userDetails = new org.springframework.security.core.userdetails.User(user.getUserCredentials().getUsername(), user.getUserCredentials().getPassword(), grantedAuthorities);
    return new UsernamePasswordAuthenticationToken(userDetails, userDetails.getPassword(), userDetails.getAuthorities());
}
Also used : User(org.hisp.dhis.user.User) UserDetails(org.springframework.security.core.userdetails.UserDetails) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Example 95 with UserDetails

use of org.springframework.security.core.userdetails.UserDetails in project dhis2-core by dhis2.

the class DhisConvenienceTest method createAndInjectAdminUser.

protected User createAndInjectAdminUser(String... authorities) {
    User user = createAdminUser(authorities);
    List<GrantedAuthority> grantedAuthorities = user.getUserCredentials().getAllAuthorities().stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
    UserDetails userDetails = new org.springframework.security.core.userdetails.User(user.getUserCredentials().getUsername(), user.getUserCredentials().getPassword(), grantedAuthorities);
    Authentication authentication = new UsernamePasswordAuthenticationToken(userDetails, "", grantedAuthorities);
    SecurityContextHolder.getContext().setAuthentication(authentication);
    return user;
}
Also used : User(org.hisp.dhis.user.User) UserDetails(org.springframework.security.core.userdetails.UserDetails) Authentication(org.springframework.security.core.Authentication) GrantedAuthority(org.springframework.security.core.GrantedAuthority) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Aggregations

UserDetails (org.springframework.security.core.userdetails.UserDetails)97 Test (org.junit.Test)37 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)32 Authentication (org.springframework.security.core.Authentication)30 GrantedAuthority (org.springframework.security.core.GrantedAuthority)16 User (org.springframework.security.core.userdetails.User)14 UserDetailsService (org.springframework.security.core.userdetails.UserDetailsService)14 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)9 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)8 LdapUserDetailsService (org.springframework.security.ldap.userdetails.LdapUserDetailsService)7 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 UserAccountBean (org.akaza.openclinica.bean.login.UserAccountBean)6 UserAccountDAO (org.akaza.openclinica.dao.login.UserAccountDAO)6 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)6 User (org.apache.atlas.web.model.User)4 User (org.hisp.dhis.user.User)4 IOException (java.io.IOException)3 Date (java.util.Date)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 DirContextAdapter (org.springframework.ldap.core.DirContextAdapter)3