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));
}
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();
}
}
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;
}
Aggregations