Remove all files
This commit is contained in:
@@ -1,94 +0,0 @@
|
||||
package fr.xephi.authme.data.captcha;
|
||||
|
||||
import fr.xephi.authme.util.RandomStringUtils;
|
||||
import fr.xephi.authme.util.expiring.ExpiringMap;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Primitive service for storing captcha codes.
|
||||
*/
|
||||
public class CaptchaCodeStorage {
|
||||
|
||||
/** Map of captcha codes (with player name as key, case-insensitive). */
|
||||
private ExpiringMap<String, String> captchaCodes;
|
||||
/** Number of characters newly generated captcha codes should have. */
|
||||
private int captchaLength;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param expirationInMinutes minutes after which a saved captcha code expires
|
||||
* @param captchaLength the number of characters a captcha code should have
|
||||
*/
|
||||
public CaptchaCodeStorage(long expirationInMinutes, int captchaLength) {
|
||||
this.captchaCodes = new ExpiringMap<>(expirationInMinutes, TimeUnit.MINUTES);
|
||||
this.captchaLength = captchaLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the expiration of captcha codes.
|
||||
*
|
||||
* @param expirationInMinutes minutes after which a saved captcha code expires
|
||||
*/
|
||||
public void setExpirationInMinutes(long expirationInMinutes) {
|
||||
captchaCodes.setExpiration(expirationInMinutes, TimeUnit.MINUTES);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the captcha length.
|
||||
*
|
||||
* @param captchaLength number of characters a captcha code should have
|
||||
*/
|
||||
public void setCaptchaLength(int captchaLength) {
|
||||
this.captchaLength = captchaLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the stored captcha for the player or generates and saves a new one.
|
||||
*
|
||||
* @param name the player's name
|
||||
* @return the code the player is required to enter
|
||||
*/
|
||||
public String getCodeOrGenerateNew(String name) {
|
||||
String code = captchaCodes.get(name.toLowerCase(Locale.ROOT));
|
||||
return code == null ? generateCode(name) : code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a code for the player and returns it.
|
||||
*
|
||||
* @param name the name of the player to generate a code for
|
||||
* @return the generated code
|
||||
*/
|
||||
private String generateCode(String name) {
|
||||
String code = RandomStringUtils.generate(captchaLength);
|
||||
captchaCodes.put(name.toLowerCase(Locale.ROOT), code);
|
||||
return code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the given code against the existing one. Upon success, the saved captcha code is removed from storage.
|
||||
* Upon failure, a new code is generated.
|
||||
*
|
||||
* @param name the name of the player to check
|
||||
* @param code the supplied code
|
||||
* @return true if the code matches, false otherwise
|
||||
*/
|
||||
public boolean checkCode(String name, String code) {
|
||||
String nameLowerCase = name.toLowerCase(Locale.ROOT);
|
||||
String savedCode = captchaCodes.get(nameLowerCase);
|
||||
if (savedCode != null && savedCode.equalsIgnoreCase(code)) {
|
||||
captchaCodes.remove(nameLowerCase);
|
||||
return true;
|
||||
} else {
|
||||
generateCode(name);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void removeExpiredEntries() {
|
||||
captchaCodes.removeExpiredEntries();
|
||||
}
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
package fr.xephi.authme.data.captcha;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
/**
|
||||
* Manages captcha codes.
|
||||
*/
|
||||
public interface CaptchaManager {
|
||||
|
||||
/**
|
||||
* Returns whether the given player is required to solve a captcha.
|
||||
*
|
||||
* @param name the name of the player to verify
|
||||
* @return true if the player has to solve a captcha, false otherwise
|
||||
*/
|
||||
boolean isCaptchaRequired(String name);
|
||||
|
||||
/**
|
||||
* Returns the stored captcha for the player or generates and saves a new one.
|
||||
*
|
||||
* @param name the player's name
|
||||
* @return the code the player is required to enter
|
||||
*/
|
||||
String getCaptchaCodeOrGenerateNew(String name);
|
||||
|
||||
/**
|
||||
* Checks the given code against the existing one. This method is not reentrant, i.e. it performs additional
|
||||
* state changes on success or failure, such as modifying some counter or setting a player as verified.
|
||||
* <p>
|
||||
* On success, the code associated with the player is cleared; on failure, a new code is generated.
|
||||
*
|
||||
* @param player the player to check
|
||||
* @param code the supplied code
|
||||
* @return true if the code matches, false otherwise
|
||||
*/
|
||||
boolean checkCode(Player player, String code);
|
||||
|
||||
}
|
||||
@@ -1,95 +0,0 @@
|
||||
package fr.xephi.authme.data.captcha;
|
||||
|
||||
import fr.xephi.authme.initialization.HasCleanup;
|
||||
import fr.xephi.authme.initialization.SettingsDependent;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.settings.properties.SecuritySettings;
|
||||
import fr.xephi.authme.util.expiring.TimedCounter;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.util.Locale;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Manager for the handling of captchas after too many failed login attempts.
|
||||
*/
|
||||
public class LoginCaptchaManager implements CaptchaManager, SettingsDependent, HasCleanup {
|
||||
|
||||
private final TimedCounter<String> playerCounts;
|
||||
private final CaptchaCodeStorage captchaCodeStorage;
|
||||
|
||||
private boolean isEnabled;
|
||||
private int threshold;
|
||||
|
||||
@Inject
|
||||
LoginCaptchaManager(Settings settings) {
|
||||
// Note: Proper values are set in reload()
|
||||
this.captchaCodeStorage = new CaptchaCodeStorage(30, 4);
|
||||
this.playerCounts = new TimedCounter<>(9, TimeUnit.MINUTES);
|
||||
reload(settings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Increases the failure count for the given player.
|
||||
*
|
||||
* @param name the player's name
|
||||
*/
|
||||
public void increaseLoginFailureCount(String name) {
|
||||
if (isEnabled) {
|
||||
String playerLower = name.toLowerCase(Locale.ROOT);
|
||||
playerCounts.increment(playerLower);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCaptchaRequired(String playerName) {
|
||||
return isEnabled && playerCounts.get(playerName.toLowerCase(Locale.ROOT)) >= threshold;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCaptchaCodeOrGenerateNew(String name) {
|
||||
return captchaCodeStorage.getCodeOrGenerateNew(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkCode(Player player, String code) {
|
||||
String nameLower = player.getName().toLowerCase(Locale.ROOT);
|
||||
boolean isCodeCorrect = captchaCodeStorage.checkCode(nameLower, code);
|
||||
if (isCodeCorrect) {
|
||||
playerCounts.remove(nameLower);
|
||||
}
|
||||
return isCodeCorrect;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the login count of the given player to 0.
|
||||
*
|
||||
* @param name the player's name
|
||||
*/
|
||||
public void resetLoginFailureCount(String name) {
|
||||
if (isEnabled) {
|
||||
playerCounts.remove(name.toLowerCase(Locale.ROOT));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reload(Settings settings) {
|
||||
int expirationInMinutes = settings.getProperty(SecuritySettings.CAPTCHA_COUNT_MINUTES_BEFORE_RESET);
|
||||
captchaCodeStorage.setExpirationInMinutes(expirationInMinutes);
|
||||
int captchaLength = settings.getProperty(SecuritySettings.CAPTCHA_LENGTH);
|
||||
captchaCodeStorage.setCaptchaLength(captchaLength);
|
||||
|
||||
int countTimeout = settings.getProperty(SecuritySettings.CAPTCHA_COUNT_MINUTES_BEFORE_RESET);
|
||||
playerCounts.setExpiration(countTimeout, TimeUnit.MINUTES);
|
||||
|
||||
isEnabled = settings.getProperty(SecuritySettings.ENABLE_LOGIN_FAILURE_CAPTCHA);
|
||||
threshold = settings.getProperty(SecuritySettings.MAX_LOGIN_TRIES_BEFORE_CAPTCHA);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performCleanup() {
|
||||
playerCounts.removeExpiredEntries();
|
||||
captchaCodeStorage.removeExpiredEntries();
|
||||
}
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
package fr.xephi.authme.data.captcha;
|
||||
|
||||
import fr.xephi.authme.initialization.HasCleanup;
|
||||
import fr.xephi.authme.initialization.SettingsDependent;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.settings.properties.SecuritySettings;
|
||||
import fr.xephi.authme.util.expiring.ExpiringSet;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.util.Locale;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Captcha manager for registration.
|
||||
*/
|
||||
public class RegistrationCaptchaManager implements CaptchaManager, SettingsDependent, HasCleanup {
|
||||
|
||||
private static final int MINUTES_VALID_FOR_REGISTRATION = 30;
|
||||
|
||||
private final ExpiringSet<String> verifiedNamesForRegistration;
|
||||
private final CaptchaCodeStorage captchaCodeStorage;
|
||||
private boolean isEnabled;
|
||||
|
||||
@Inject
|
||||
RegistrationCaptchaManager(Settings settings) {
|
||||
// NOTE: proper captcha length is set in reload()
|
||||
this.captchaCodeStorage = new CaptchaCodeStorage(MINUTES_VALID_FOR_REGISTRATION, 4);
|
||||
this.verifiedNamesForRegistration = new ExpiringSet<>(MINUTES_VALID_FOR_REGISTRATION, TimeUnit.MINUTES);
|
||||
reload(settings);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCaptchaRequired(String name) {
|
||||
return isEnabled && !verifiedNamesForRegistration.contains(name.toLowerCase(Locale.ROOT));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCaptchaCodeOrGenerateNew(String name) {
|
||||
return captchaCodeStorage.getCodeOrGenerateNew(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkCode(Player player, String code) {
|
||||
String nameLower = player.getName().toLowerCase(Locale.ROOT);
|
||||
boolean isCodeCorrect = captchaCodeStorage.checkCode(nameLower, code);
|
||||
if (isCodeCorrect) {
|
||||
verifiedNamesForRegistration.add(nameLower);
|
||||
}
|
||||
return isCodeCorrect;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reload(Settings settings) {
|
||||
int captchaLength = settings.getProperty(SecuritySettings.CAPTCHA_LENGTH);
|
||||
captchaCodeStorage.setCaptchaLength(captchaLength);
|
||||
|
||||
isEnabled = settings.getProperty(SecuritySettings.ENABLE_CAPTCHA_FOR_REGISTRATION);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performCleanup() {
|
||||
verifiedNamesForRegistration.removeExpiredEntries();
|
||||
captchaCodeStorage.removeExpiredEntries();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user