Search in sources :

Example 1 with BroadleafExternalAuthenticationUserDetails

use of org.broadleafcommerce.common.security.BroadleafExternalAuthenticationUserDetails in project BroadleafCommerce by BroadleafCommerce.

the class AdminExternalLoginStateFilter method doFilter.

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    if (request.getSession(true).getAttribute(BLC_ADMIN_PROVISION_USER_CHECK) == null) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null && authentication.isAuthenticated()) {
            if (authentication.getPrincipal() instanceof UserDetails) {
                UserDetails userDetails = (UserDetails) authentication.getPrincipal();
                if (userDetails != null && userDetails.getUsername() != null) {
                    AdminUser user = adminSecurityService.readAdminUserByUserName(userDetails.getUsername());
                    if (userDetails instanceof BroadleafExternalAuthenticationUserDetails) {
                        BroadleafExternalAuthenticationUserDetails broadleafUser = (BroadleafExternalAuthenticationUserDetails) userDetails;
                        if (user == null) {
                            // Provision a new user...
                            user = (AdminUser) entityConfiguration.createEntityInstance(AdminUser.class.getName());
                        }
                        saveAdminUser(broadleafUser, user);
                        request.getSession().setAttribute(BLC_ADMIN_PROVISION_USER_CHECK, Boolean.TRUE);
                    }
                }
            }
        }
    }
    filterChain.doFilter(servletRequest, servletResponse);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) BroadleafExternalAuthenticationUserDetails(org.broadleafcommerce.common.security.BroadleafExternalAuthenticationUserDetails) UserDetails(org.springframework.security.core.userdetails.UserDetails) BroadleafExternalAuthenticationUserDetails(org.broadleafcommerce.common.security.BroadleafExternalAuthenticationUserDetails) Authentication(org.springframework.security.core.Authentication) AdminUser(org.broadleafcommerce.openadmin.server.security.domain.AdminUser)

Example 2 with BroadleafExternalAuthenticationUserDetails

use of org.broadleafcommerce.common.security.BroadleafExternalAuthenticationUserDetails in project BroadleafCommerce by BroadleafCommerce.

the class BroadleafActiveDirectoryUserDetailsMapper method mapUserFromContext.

@Override
public UserDetails mapUserFromContext(DirContextOperations ctx, String username, Collection<? extends GrantedAuthority> authorities) {
    Collection<GrantedAuthority> newAuthorities = new HashSet<GrantedAuthority>();
    if (roleNameSubstitutions != null && !roleNameSubstitutions.isEmpty()) {
        for (GrantedAuthority authority : authorities) {
            if (roleNameSubstitutions.containsKey(authority.getAuthority())) {
                String[] roles = roleNameSubstitutions.get(authority.getAuthority());
                for (String role : roles) {
                    newAuthorities.add(new SimpleGrantedAuthority(role.trim()));
                }
                if (additiveRoleNameSubstitutions) {
                    newAuthorities.add(authority);
                }
            } else {
                newAuthorities.add(authority);
            }
        }
    } else {
        newAuthorities.addAll(authorities);
    }
    String email = (String) ctx.getObjectAttribute("mail");
    UserDetails userDetails = null;
    if (useEmailAddressAsUsername) {
        if (email != null) {
            userDetails = super.mapUserFromContext(ctx, email, newAuthorities);
        }
    }
    if (userDetails == null) {
        userDetails = super.mapUserFromContext(ctx, username, newAuthorities);
    }
    String password = userDetails.getPassword();
    if (password == null) {
        password = userDetails.getUsername();
    }
    BroadleafExternalAuthenticationUserDetails broadleafUser = new BroadleafExternalAuthenticationUserDetails(userDetails.getUsername(), password, userDetails.getAuthorities());
    broadleafUser.setFirstName((String) ctx.getObjectAttribute("givenName"));
    broadleafUser.setLastName((String) ctx.getObjectAttribute("sn"));
    broadleafUser.setEmail(email);
    return broadleafUser;
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) UserDetails(org.springframework.security.core.userdetails.UserDetails) BroadleafExternalAuthenticationUserDetails(org.broadleafcommerce.common.security.BroadleafExternalAuthenticationUserDetails) BroadleafExternalAuthenticationUserDetails(org.broadleafcommerce.common.security.BroadleafExternalAuthenticationUserDetails) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) HashSet(java.util.HashSet)

Example 3 with BroadleafExternalAuthenticationUserDetails

use of org.broadleafcommerce.common.security.BroadleafExternalAuthenticationUserDetails in project BroadleafCommerce by BroadleafCommerce.

the class BroadleafAdminLdapUserDetailsMapper method mapUserFromContext.

@Override
public UserDetails mapUserFromContext(DirContextOperations ctx, String username, Collection<? extends GrantedAuthority> authorities) {
    String email = (String) ctx.getObjectAttribute("mail");
    String firstName = (String) ctx.getObjectAttribute("givenName");
    String lastName = (String) ctx.getObjectAttribute("sn");
    BroadleafExternalAuthenticationUserDetails details = new BroadleafExternalAuthenticationUserDetails(username, "", authorities);
    details.setEmail(email);
    details.setFirstName(firstName);
    details.setLastName(lastName);
    details.setSite(determineSite(ctx, username, authorities));
    return provisioningService.provisionAdminUser(details);
}
Also used : BroadleafExternalAuthenticationUserDetails(org.broadleafcommerce.common.security.BroadleafExternalAuthenticationUserDetails)

Aggregations

BroadleafExternalAuthenticationUserDetails (org.broadleafcommerce.common.security.BroadleafExternalAuthenticationUserDetails)3 UserDetails (org.springframework.security.core.userdetails.UserDetails)2 HashSet (java.util.HashSet)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 AdminUser (org.broadleafcommerce.openadmin.server.security.domain.AdminUser)1 Authentication (org.springframework.security.core.Authentication)1 GrantedAuthority (org.springframework.security.core.GrantedAuthority)1 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)1