Search in sources :

Example 1 with PasswordEncryption

use of com.haulmont.cuba.core.global.PasswordEncryption in project cuba by cuba-platform.

the class Connection method login.

/**
 * Log in to the system.
 *
 * @param login    user login name
 * @param password encrypted user password
 * @param locale   user locale
 * @throws LoginException in case of unsuccessful login due to wrong credentials or other issues
 */
@Deprecated
default void login(String login, String password, Locale locale) throws LoginException {
    PasswordEncryption passwordEncryption = AppBeans.get(PasswordEncryption.class);
    login(new LoginPasswordCredentials(login, passwordEncryption.getPlainHash(password), locale));
}
Also used : PasswordEncryption(com.haulmont.cuba.core.global.PasswordEncryption) LoginPasswordCredentials(com.haulmont.cuba.security.auth.LoginPasswordCredentials)

Example 2 with PasswordEncryption

use of com.haulmont.cuba.core.global.PasswordEncryption in project cuba by cuba-platform.

the class PermissionTest method setUp.

@Before
public void setUp() throws Exception {
    passwordEncryption = AppBeans.get(PasswordEncryption.class);
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        Role role1 = new Role();
        role1Id = role1.getId();
        role1.setName("testRole1");
        em.persist(role1);
        Role role2 = new Role();
        role2Id = role2.getId();
        role2.setName("testRole2");
        em.persist(role2);
        Permission permission1 = new Permission();
        permission1Id = permission1.getId();
        permission1.setRole(role1);
        permission1.setType(PermissionType.SCREEN);
        permission1.setTarget(PERM_TARGET_SCREEN);
        permission1.setValue(0);
        em.persist(permission1);
        Permission permission2 = new Permission();
        permission2Id = permission2.getId();
        permission2.setRole(role2);
        permission2.setType(PermissionType.ENTITY_ATTR);
        permission2.setTarget(PERM_TARGET_ATTR);
        permission2.setValue(1);
        em.persist(permission2);
        Group group = new Group();
        groupId = group.getId();
        group.setName("testGroup");
        em.persist(group);
        User user = new User();
        userId = user.getId();
        user.setName(USER_NAME);
        user.setLogin(USER_NAME);
        String pwd = passwordEncryption.getPasswordHash(userId, USER_PASSW);
        user.setPassword(pwd);
        user.setGroup(group);
        em.persist(user);
        UserRole userRole1 = new UserRole();
        userRole1Id = userRole1.getId();
        userRole1.setUser(user);
        userRole1.setRole(role1);
        em.persist(userRole1);
        UserRole userRole2 = new UserRole();
        userRole2Id = userRole2.getId();
        userRole2.setUser(user);
        userRole2.setRole(role2);
        em.persist(userRole2);
        tx.commit();
    } finally {
        tx.end();
    }
}
Also used : PasswordEncryption(com.haulmont.cuba.core.global.PasswordEncryption) EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) Before(org.junit.Before)

Example 3 with PasswordEncryption

use of com.haulmont.cuba.core.global.PasswordEncryption in project cuba by cuba-platform.

the class PortalAuthenticationProvider method authenticate.

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    if (authentication instanceof UsernamePasswordAuthenticationToken) {
        UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) authentication;
        PortalSession session;
        String login = null;
        String ipAddress = null;
        try {
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            login = (String) token.getPrincipal();
            ipAddress = request.getRemoteAddr();
            HttpSession httpSession = request.getSession();
            Connection connection = (Connection) httpSession.getAttribute(Connection.NAME);
            if (connection == null || connection.getSession() == null || !connection.isConnected()) {
                connection = AppBeans.get(Connection.NAME);
            }
            PasswordEncryption passwordEncryption = AppBeans.get(PasswordEncryption.NAME);
            connection.login(login, passwordEncryption.getPlainHash((String) token.getCredentials()), request.getLocale(), ipAddress, request.getHeader("User-Agent"));
            httpSession.setAttribute(Connection.NAME, connection);
            session = connection.getSession();
        } catch (AccountLockedException e) {
            log.info("Blocked user login attempt: login={}, ip={}", login, ipAddress);
            throw new LockedException(e.getMessage());
        } catch (UserIpRestrictedException e) {
            log.info("Incorrect user IP: {} {} - {}", login, ipAddress);
            throw new BadCredentialsException(e.getMessage());
        } catch (LoginException e) {
            log.info("Authentication failed: {} {} - {}", login, ipAddress, e.getMessage());
            throw new BadCredentialsException(e.getMessage());
        }
        return new UsernamePasswordAuthenticationToken(session, session.getId(), getRoleUserAuthorities(session));
    }
    return null;
}
Also used : AccountLockedException(com.haulmont.cuba.security.global.AccountLockedException) LockedException(org.springframework.security.authentication.LockedException) AccountLockedException(com.haulmont.cuba.security.global.AccountLockedException) HttpSession(javax.servlet.http.HttpSession) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) Connection(com.haulmont.cuba.portal.Connection) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) HttpServletRequest(javax.servlet.http.HttpServletRequest) PasswordEncryption(com.haulmont.cuba.core.global.PasswordEncryption) UserIpRestrictedException(com.haulmont.cuba.security.global.UserIpRestrictedException) LoginException(com.haulmont.cuba.security.global.LoginException)

Aggregations

PasswordEncryption (com.haulmont.cuba.core.global.PasswordEncryption)3 EntityManager (com.haulmont.cuba.core.EntityManager)1 Transaction (com.haulmont.cuba.core.Transaction)1 Connection (com.haulmont.cuba.portal.Connection)1 LoginPasswordCredentials (com.haulmont.cuba.security.auth.LoginPasswordCredentials)1 AccountLockedException (com.haulmont.cuba.security.global.AccountLockedException)1 LoginException (com.haulmont.cuba.security.global.LoginException)1 UserIpRestrictedException (com.haulmont.cuba.security.global.UserIpRestrictedException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpSession (javax.servlet.http.HttpSession)1 Before (org.junit.Before)1 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)1 LockedException (org.springframework.security.authentication.LockedException)1 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)1 ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)1