#930 Change captcha storage to change code internally upon failure

- Within CaptchaStorage#checkCode, a player's captcha code is overridden with a new one on failure or cleared on success
- Fixes inconsistencies in the retrieval / regeneration of codes
This commit is contained in:
ljacqu
2018-01-06 02:31:26 +01:00
parent 180bbbf0be
commit 84b376d2a5
8 changed files with 48 additions and 34 deletions
@@ -53,7 +53,7 @@ public class CaptchaCommand extends PlayerCommand {
commonService.send(player, MessageKey.LOGIN_MESSAGE);
limboService.unmuteMessageTask(player);
} else {
String newCode = loginCaptchaManager.generateCode(player.getName());
String newCode = loginCaptchaManager.getCaptchaCodeOrGenerateNew(player.getName());
commonService.send(player, MessageKey.CAPTCHA_WRONG_ERROR, newCode);
}
}
@@ -64,7 +64,7 @@ public class CaptchaCommand extends PlayerCommand {
commonService.send(player, MessageKey.CAPTCHA_SUCCESS);
commonService.send(player, MessageKey.REGISTER_MESSAGE);
} else {
String newCode = registrationCaptchaManager.generateCode(player.getName());
String newCode = registrationCaptchaManager.getCaptchaCodeOrGenerateNew(player.getName());
commonService.send(player, MessageKey.CAPTCHA_WRONG_ERROR, newCode);
}
}
@@ -61,7 +61,7 @@ public class CaptchaCodeStorage {
* @param name the name of the player to generate a code for
* @return the generated code
*/
public String generateCode(String name) {
private String generateCode(String name) {
String code = RandomStringUtils.generate(captchaLength);
captchaCodes.put(name.toLowerCase(), code);
return code;
@@ -69,6 +69,7 @@ public class CaptchaCodeStorage {
/**
* 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
@@ -80,6 +81,8 @@ public class CaptchaCodeStorage {
if (savedCode != null && savedCode.equalsIgnoreCase(code)) {
captchaCodes.remove(nameLowerCase);
return true;
} else {
generateCode(name);
}
return false;
}
@@ -24,16 +24,10 @@ public interface CaptchaManager {
String getCaptchaCodeOrGenerateNew(String name);
/**
* 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
*/
String generateCode(String name);
/**
* Checks the given code against the existing one. This method may perform additional state changes
* on success or failure, such as modifying some counter or setting a player as verified.
* 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
@@ -51,11 +51,6 @@ public class LoginCaptchaManager implements CaptchaManager, SettingsDependent, H
return captchaCodeStorage.getCodeOrGenerateNew(name);
}
@Override
public String generateCode(String name) {
return captchaCodeStorage.generateCode(name);
}
@Override
public boolean checkCode(Player player, String code) {
String nameLower = player.getName().toLowerCase();
@@ -45,20 +45,14 @@ public class RegistrationCaptchaManager
return captchaCodeStorage.getCodeOrGenerateNew(name);
}
@Override
public String generateCode(String name) {
return captchaCodeStorage.generateCode(name);
}
@Override
public boolean checkCode(Player player, String code) {
String nameLower = player.getName().toLowerCase();
boolean isCodeCorrect = captchaCodeStorage.checkCode(nameLower, code);
if (isCodeCorrect) {
verifiedNamesForRegistration.add(nameLower);
} else {
limboService.resetMessageTask(player, false);
}
limboService.resetMessageTask(player, false);
return isCodeCorrect;
}