mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-10-08 14:51:49 +00:00
REPO-1687: ACE-5662: The AuthenticationServiceImpl.ProtectedUser is not suitable for clustered cache
- Changed the implementation to use Serializable objects in cache. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@133415 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -49,6 +49,7 @@ public class AuthenticationServiceImpl extends AbstractAuthenticationService imp
|
||||
private boolean allowsUserPasswordChange = true;
|
||||
|
||||
private static final String AUTHENTICATION_UNSUCCESSFUL = "Authentication was not successful.";
|
||||
private static final String BRUTE_FORCE_ATTACK_DETECTED = "Brute force attack was detected for user: %s";
|
||||
private int protectionPeriodSeconds;
|
||||
private boolean protectionEnabled;
|
||||
private int protectionLimit;
|
||||
@@ -110,13 +111,9 @@ public class AuthenticationServiceImpl extends AbstractAuthenticationService imp
|
||||
// clear context - to avoid MT concurrency issue (causing domain mismatch) - see also 'validate' below
|
||||
clearCurrentSecurityContext();
|
||||
preAuthenticationCheck(userName);
|
||||
if (protectionEnabled)
|
||||
if (isUserProtected(userName))
|
||||
{
|
||||
ProtectedUser protectedUser = protectedUsersCache.get(userName);
|
||||
if (protectedUser != null && protectedUser.isProtected())
|
||||
{
|
||||
throw new AuthenticationException(AUTHENTICATION_UNSUCCESSFUL);
|
||||
}
|
||||
throw new AuthenticationException(AUTHENTICATION_UNSUCCESSFUL);
|
||||
}
|
||||
authenticationComponent.authenticate(userName, password);
|
||||
if (tenant == null)
|
||||
@@ -136,25 +133,60 @@ public class AuthenticationServiceImpl extends AbstractAuthenticationService imp
|
||||
catch(AuthenticationException ae)
|
||||
{
|
||||
clearCurrentSecurityContext();
|
||||
if (protectionEnabled)
|
||||
{
|
||||
ProtectedUser protectedUser = protectedUsersCache.get(userName);
|
||||
if (protectedUser == null)
|
||||
{
|
||||
protectedUser = new ProtectedUser(userName);
|
||||
}
|
||||
else
|
||||
{
|
||||
protectedUser.recordUnsuccessfulLogin();
|
||||
}
|
||||
protectedUsersCache.put(userName, protectedUser);
|
||||
}
|
||||
recordFailedAuthentication(userName);
|
||||
throw ae;
|
||||
}
|
||||
ticketComponent.clearCurrentTicket();
|
||||
getCurrentTicket();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return <code>true</code> if user is 'protected' from brute force attack
|
||||
*/
|
||||
public boolean isUserProtected(String userName)
|
||||
{
|
||||
boolean isProtected = false;
|
||||
if (protectionEnabled)
|
||||
{
|
||||
ProtectedUser protectedUser = protectedUsersCache.get(userName);
|
||||
if (protectedUser != null)
|
||||
{
|
||||
long currentTimeStamp = System.currentTimeMillis();
|
||||
isProtected = protectedUser.getNumLogins() >= protectionLimit &&
|
||||
currentTimeStamp - protectedUser.getTimeStamp() < protectionPeriodSeconds * 1000;
|
||||
}
|
||||
}
|
||||
return isProtected;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method records a failed login attempt.
|
||||
* If the number of recorded failures exceeds {@link AuthenticationServiceImpl#protectionLimit}
|
||||
* the user will be considered 'protected'.
|
||||
*/
|
||||
public void recordFailedAuthentication(String userName)
|
||||
{
|
||||
if (protectionEnabled)
|
||||
{
|
||||
ProtectedUser protectedUser = protectedUsersCache.get(userName);
|
||||
if (protectedUser == null)
|
||||
{
|
||||
protectedUser = new ProtectedUser(userName);
|
||||
}
|
||||
else
|
||||
{
|
||||
protectedUser = new ProtectedUser(userName, protectedUser.getNumLogins() + 1);
|
||||
if (protectedUser.getNumLogins() == protectionLimit && logger.isWarnEnabled())
|
||||
{
|
||||
// Shows only first 2 symbols of the username and masks all other character with '*'
|
||||
logger.warn(String.format(BRUTE_FORCE_ATTACK_DETECTED,
|
||||
userName.substring(0,2) + new String(new char[(userName.length() - 2)]).replace("\0", "*")));
|
||||
}
|
||||
}
|
||||
protectedUsersCache.put(userName, protectedUser);
|
||||
}
|
||||
}
|
||||
|
||||
public String getCurrentUserName() throws AuthenticationException
|
||||
{
|
||||
return authenticationComponent.getCurrentUserName();
|
||||
@@ -409,74 +441,4 @@ public class AuthenticationServiceImpl extends AbstractAuthenticationService imp
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* An object to hold information about unsuccessful logins
|
||||
*/
|
||||
/*package*/ class ProtectedUser
|
||||
{
|
||||
private String userId;
|
||||
/** number of consecutive unsuccessful login attempts */
|
||||
private long numLogins;
|
||||
/** time stamp of last unsuccessful login attempt */
|
||||
private long timeStamp;
|
||||
|
||||
public ProtectedUser(String userId)
|
||||
{
|
||||
this.userId = userId;
|
||||
this.numLogins = 1;
|
||||
this.timeStamp = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public void recordUnsuccessfulLogin()
|
||||
{
|
||||
this.numLogins+=1;
|
||||
this.timeStamp = System.currentTimeMillis();
|
||||
if (numLogins == protectionLimit + 1 && logger.isWarnEnabled())
|
||||
{
|
||||
// Shows only first 2 symbols of the username and masks all other character with '*'
|
||||
logger.warn("Brute force attack was detected for user " +
|
||||
userId.substring(0,2) + new String(new char[(userId.length() - 2)]).replace("\0", "*"));
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isProtected()
|
||||
{
|
||||
long currentTimeStamp = System.currentTimeMillis();
|
||||
return numLogins >= protectionLimit &&
|
||||
currentTimeStamp - timeStamp < protectionPeriodSeconds * 1000;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "ProtectedUser{" +
|
||||
"userId='" + userId + '\'' +
|
||||
", numLogins=" + numLogins +
|
||||
", timeStamp=" + timeStamp +
|
||||
'}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o)
|
||||
{
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
ProtectedUser that = (ProtectedUser) o;
|
||||
|
||||
if (numLogins != that.numLogins) return false;
|
||||
if (timeStamp != that.timeStamp) return false;
|
||||
return userId.equals(that.userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
int result = userId.hashCode();
|
||||
result = 31 * result + (int) (numLogins ^ (numLogins >>> 32));
|
||||
result = 31 * result + (int) (timeStamp ^ (timeStamp >>> 32));
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* #%L
|
||||
* Alfresco Repository
|
||||
* %%
|
||||
* Copyright (C) 2005 - 2016 Alfresco Software Limited
|
||||
* %%
|
||||
* This file is part of the Alfresco software.
|
||||
* If the software was purchased under a paid Alfresco license, the terms of
|
||||
* the paid license agreement will prevail. Otherwise, the software is
|
||||
* provided under the following open source license terms:
|
||||
*
|
||||
* Alfresco is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Alfresco is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
||||
* #L%
|
||||
*/
|
||||
package org.alfresco.repo.security.authentication;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* An object to hold information about unsuccessful logins.
|
||||
* It is used for brute force attack mitigation in {@link AuthenticationServiceImpl}
|
||||
*
|
||||
* @since 5.2.0
|
||||
* @author amukha
|
||||
*/
|
||||
/*package*/ class ProtectedUser implements Serializable
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private String userId;
|
||||
/** number of consecutive unsuccessful login attempts */
|
||||
private long numLogins;
|
||||
/** time stamp of last unsuccessful login attempt */
|
||||
private long timeStamp;
|
||||
|
||||
/*package*/ ProtectedUser(String userId)
|
||||
{
|
||||
this.userId = userId;
|
||||
this.numLogins = 1;
|
||||
this.timeStamp = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
/*package*/ ProtectedUser(String userId, long numLogins)
|
||||
{
|
||||
this.userId = userId;
|
||||
this.numLogins = numLogins;
|
||||
this.timeStamp = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
/*package*/ long getNumLogins()
|
||||
{
|
||||
return numLogins;
|
||||
}
|
||||
|
||||
/*package*/ long getTimeStamp()
|
||||
{
|
||||
return timeStamp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "ProtectedUser{" +
|
||||
"userId='" + userId + '\'' +
|
||||
", numLogins=" + numLogins +
|
||||
", timeStamp=" + timeStamp +
|
||||
'}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o)
|
||||
{
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
ProtectedUser that = (ProtectedUser) o;
|
||||
|
||||
if (numLogins != that.numLogins) return false;
|
||||
if (timeStamp != that.timeStamp) return false;
|
||||
return userId.equals(that.userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
int result = userId.hashCode();
|
||||
result = 31 * result + (int) (numLogins ^ (numLogins >>> 32));
|
||||
result = 31 * result + (int) (timeStamp ^ (timeStamp >>> 32));
|
||||
return result;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user