- Introduce /email code
- Add max tries for /email code - Introduce a PasswordRecoveryService
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
package fr.xephi.authme.service;
|
||||
|
||||
import fr.xephi.authme.datasource.DataSource;
|
||||
import fr.xephi.authme.initialization.Reloadable;
|
||||
import fr.xephi.authme.mail.EmailService;
|
||||
import fr.xephi.authme.message.MessageKey;
|
||||
import fr.xephi.authme.message.Messages;
|
||||
import fr.xephi.authme.security.PasswordSecurity;
|
||||
import fr.xephi.authme.security.crypts.HashedPassword;
|
||||
import fr.xephi.authme.settings.properties.SecuritySettings;
|
||||
import fr.xephi.authme.util.RandomStringUtils;
|
||||
import fr.xephi.authme.util.expiring.Duration;
|
||||
import fr.xephi.authme.util.expiring.ExpiringSet;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.inject.Inject;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static fr.xephi.authme.settings.properties.EmailSettings.RECOVERY_PASSWORD_LENGTH;
|
||||
|
||||
/**
|
||||
* Manager for password recovery.
|
||||
*/
|
||||
public class PasswordRecoveryService implements Reloadable {
|
||||
|
||||
@Inject
|
||||
private CommonService commonService;
|
||||
|
||||
@Inject
|
||||
private RecoveryCodeService codeService;
|
||||
|
||||
@Inject
|
||||
private DataSource dataSource;
|
||||
|
||||
@Inject
|
||||
private EmailService emailService;
|
||||
|
||||
@Inject
|
||||
private PasswordSecurity passwordSecurity;
|
||||
|
||||
@Inject
|
||||
private RecoveryCodeService recoveryCodeService;
|
||||
|
||||
@Inject
|
||||
private Messages messages;
|
||||
|
||||
private ExpiringSet<String> emailCooldown;
|
||||
|
||||
@PostConstruct
|
||||
private void initEmailCooldownSet() {
|
||||
emailCooldown = new ExpiringSet<>(
|
||||
commonService.getProperty(SecuritySettings.EMAIL_RECOVERY_COOLDOWN_SECONDS), TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new recovery code and send it to the player
|
||||
* via email.
|
||||
*
|
||||
* @param player The player getting the code.
|
||||
* @param email The email to send the code to.
|
||||
*/
|
||||
public void createAndSendRecoveryCode(Player player, String email) {
|
||||
if (!checkEmailCooldown(player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
String recoveryCode = recoveryCodeService.generateCode(player.getName());
|
||||
boolean couldSendMail = emailService.sendRecoveryCode(player.getName(), email, recoveryCode);
|
||||
if (couldSendMail) {
|
||||
commonService.send(player, MessageKey.RECOVERY_CODE_SENT);
|
||||
emailCooldown.add(player.getName().toLowerCase());
|
||||
} else {
|
||||
commonService.send(player, MessageKey.EMAIL_SEND_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a new password and send it to the player via
|
||||
* email. This will update the database with the new password.
|
||||
*
|
||||
* @param player The player recovering their password.
|
||||
* @param email The email to send the password to.
|
||||
*/
|
||||
public void generateAndSendNewPassword(Player player, String email) {
|
||||
if (!checkEmailCooldown(player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
String name = player.getName();
|
||||
String thePass = RandomStringUtils.generate(commonService.getProperty(RECOVERY_PASSWORD_LENGTH));
|
||||
HashedPassword hashNew = passwordSecurity.computeHash(thePass, name);
|
||||
|
||||
dataSource.updatePassword(name, hashNew);
|
||||
boolean couldSendMail = emailService.sendPasswordMail(name, email, thePass);
|
||||
if (couldSendMail) {
|
||||
commonService.send(player, MessageKey.RECOVERY_EMAIL_SENT_MESSAGE);
|
||||
emailCooldown.add(player.getName().toLowerCase());
|
||||
} else {
|
||||
commonService.send(player, MessageKey.EMAIL_SEND_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a player is able to have emails sent.
|
||||
*
|
||||
* @param player The player to check.
|
||||
* @return True if the player is not on cooldown.
|
||||
*/
|
||||
public boolean checkEmailCooldown(Player player) {
|
||||
Duration waitDuration = emailCooldown.getExpiration(player.getName().toLowerCase());
|
||||
if (waitDuration.getDuration() > 0) {
|
||||
String durationText = messages.formatDuration(waitDuration);
|
||||
messages.send(player, MessageKey.EMAIL_COOLDOWN_ERROR, durationText);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reload() {
|
||||
emailCooldown.setExpiration(
|
||||
commonService.getProperty(SecuritySettings.EMAIL_RECOVERY_COOLDOWN_SECONDS), TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
@@ -6,26 +6,29 @@ import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.settings.properties.SecuritySettings;
|
||||
import fr.xephi.authme.util.RandomStringUtils;
|
||||
import fr.xephi.authme.util.expiring.ExpiringMap;
|
||||
import fr.xephi.authme.util.expiring.TimedCounter;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static fr.xephi.authme.settings.properties.SecuritySettings.RECOVERY_CODE_HOURS_VALID;
|
||||
|
||||
/**
|
||||
* Manager for recovery codes.
|
||||
*/
|
||||
public class RecoveryCodeService implements SettingsDependent, HasCleanup {
|
||||
|
||||
private final ExpiringMap<String, String> recoveryCodes;
|
||||
private final TimedCounter<String> playerTries;
|
||||
private int recoveryCodeLength;
|
||||
private int recoveryCodeExpiration;
|
||||
private int recoveryCodeMaxTries;
|
||||
|
||||
@Inject
|
||||
RecoveryCodeService(Settings settings) {
|
||||
recoveryCodeLength = settings.getProperty(SecuritySettings.RECOVERY_CODE_LENGTH);
|
||||
recoveryCodeExpiration = settings.getProperty(SecuritySettings.RECOVERY_CODE_HOURS_VALID);
|
||||
recoveryCodeMaxTries = settings.getProperty(SecuritySettings.RECOVERY_CODE_MAX_TRIES);
|
||||
recoveryCodes = new ExpiringMap<>(recoveryCodeExpiration, TimeUnit.HOURS);
|
||||
playerTries = new TimedCounter<>(recoveryCodeExpiration, TimeUnit.HOURS);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -43,6 +46,8 @@ public class RecoveryCodeService implements SettingsDependent, HasCleanup {
|
||||
*/
|
||||
public String generateCode(String player) {
|
||||
String code = RandomStringUtils.generateHex(recoveryCodeLength);
|
||||
|
||||
playerTries.put(player, recoveryCodeMaxTries);
|
||||
recoveryCodes.put(player, code);
|
||||
return code;
|
||||
}
|
||||
@@ -56,9 +61,30 @@ public class RecoveryCodeService implements SettingsDependent, HasCleanup {
|
||||
*/
|
||||
public boolean isCodeValid(String player, String code) {
|
||||
String storedCode = recoveryCodes.get(player);
|
||||
playerTries.decrement(player);
|
||||
return storedCode != null && storedCode.equals(code);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether a player has tries remaining to enter a code.
|
||||
*
|
||||
* @param player The player to check for.
|
||||
* @return True if the player has tries left.
|
||||
*/
|
||||
public boolean hasTriesLeft(String player) {
|
||||
return playerTries.get(player) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of attempts a player has to enter a code.
|
||||
*
|
||||
* @param player The player to check for.
|
||||
* @return The number of tries left.
|
||||
*/
|
||||
public int getTriesLeft(String player) {
|
||||
return playerTries.get(player);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the player's recovery code if present.
|
||||
*
|
||||
@@ -66,17 +92,20 @@ public class RecoveryCodeService implements SettingsDependent, HasCleanup {
|
||||
*/
|
||||
public void removeCode(String player) {
|
||||
recoveryCodes.remove(player);
|
||||
playerTries.remove(player);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reload(Settings settings) {
|
||||
recoveryCodeLength = settings.getProperty(SecuritySettings.RECOVERY_CODE_LENGTH);
|
||||
recoveryCodeExpiration = settings.getProperty(RECOVERY_CODE_HOURS_VALID);
|
||||
recoveryCodeExpiration = settings.getProperty(SecuritySettings.RECOVERY_CODE_HOURS_VALID);
|
||||
recoveryCodeMaxTries = settings.getProperty(SecuritySettings.RECOVERY_CODE_MAX_TRIES);
|
||||
recoveryCodes.setExpiration(recoveryCodeExpiration, TimeUnit.HOURS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performCleanup() {
|
||||
recoveryCodes.removeExpiredEntries();
|
||||
playerTries.removeExpiredEntries();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user