use of com.haulmont.cuba.security.global.UserIpRestrictedException in project cuba by cuba-platform.
the class IpMaskUserAccessChecker method check.
@Override
public void check(Credentials credentials, AuthenticationDetails authenticationDetails) throws LoginException {
if (credentials instanceof AbstractClientCredentials) {
AbstractClientCredentials clientCredentials = (AbstractClientCredentials) credentials;
if (clientCredentials.isCheckClientPermissions() && clientCredentials.getIpAddress() != null) {
String ipAddress = clientCredentials.getIpAddress();
UserSession session = authenticationDetails.getSession();
if (session.getUser().getIpMask() != null) {
IpMatcher ipMatcher = new IpMatcher(session.getUser().getIpMask());
if (!ipMatcher.match(ipAddress)) {
log.info("IP address {} is not permitted for user {}", ipAddress, session.getUser());
throw new UserIpRestrictedException(messages.getMessage(MSG_PACK, "LoginException.invalidIP"));
}
}
}
}
}
use of com.haulmont.cuba.security.global.UserIpRestrictedException 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