Email verification code (#1382)

* Base verification code implementation, must define command, manager, events
* VerificationManager, verification command and messages, handled some sensible commands, configuration values
* Improved manager and sensible commands trigger
* Updated messages
* Updated verification code manager, fixed tests
* Switched to a permission based command
* Verification manager and command improved and added tests
* Edited messages
This commit is contained in:
HexelDev
2017-10-28 12:23:14 +02:00
committed by ljacqu
parent 8422581a82
commit ba65633182
58 changed files with 1227 additions and 67 deletions
@@ -40,6 +40,7 @@ import fr.xephi.authme.command.executable.login.LoginCommand;
import fr.xephi.authme.command.executable.logout.LogoutCommand;
import fr.xephi.authme.command.executable.register.RegisterCommand;
import fr.xephi.authme.command.executable.unregister.UnregisterCommand;
import fr.xephi.authme.command.executable.verification.VerificationCommand;
import fr.xephi.authme.permission.AdminPermission;
import fr.xephi.authme.permission.DebugSectionPermissions;
import fr.xephi.authme.permission.PlayerPermission;
@@ -136,13 +137,24 @@ public class CommandInitializer {
CommandDescription captchaBase = CommandDescription.builder()
.parent(null)
.labels("captcha")
.description("Captcha Command")
.description("Captcha command")
.detailedDescription("Captcha command for AuthMeReloaded.")
.withArgument("captcha", "The Captcha", false)
.permission(PlayerPermission.CAPTCHA)
.executableCommand(CaptchaCommand.class)
.register();
// Register the base verification code command
CommandDescription verificationBase = CommandDescription.builder()
.parent(null)
.labels("verification")
.description("Verification command")
.detailedDescription("Command to complete the verification process for AuthMeReloaded.")
.withArgument("code", "The code", false)
.permission(PlayerPermission.VERIFICATION_CODE)
.executableCommand(VerificationCommand.class)
.register();
List<CommandDescription> baseCommands = ImmutableList.of(
authMeBase,
emailBase,
@@ -151,7 +163,8 @@ public class CommandInitializer {
registerBase,
unregisterBase,
changePasswordBase,
captchaBase);
captchaBase,
verificationBase);
setHelpOnAllBases(baseCommands);
commands = baseCommands;
@@ -1,6 +1,7 @@
package fr.xephi.authme.command.executable.changepassword;
import fr.xephi.authme.command.PlayerCommand;
import fr.xephi.authme.data.VerificationCodeManager;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.Management;
@@ -29,17 +30,28 @@ public class ChangePasswordCommand extends PlayerCommand {
@Inject
private Management management;
@Inject
private VerificationCodeManager codeManager;
@Override
public void runCommand(Player player, List<String> arguments) {
String oldPassword = arguments.get(0);
String newPassword = arguments.get(1);
String name = player.getName().toLowerCase();
if (!playerCache.isAuthenticated(name)) {
commonService.send(player, MessageKey.NOT_LOGGED_IN);
return;
}
// Check if the user has been verified or not
if (codeManager.isVerificationRequired(player)) {
codeManager.codeExistOrGenerateNew(name);
commonService.send(player, MessageKey.VERIFICATION_CODE_REQUIRED);
return;
}
String oldPassword = arguments.get(0);
String newPassword = arguments.get(1);
// Make sure the password is allowed
ValidationResult passwordValidation = validationService.validatePassword(newPassword, name);
if (passwordValidation.hasError()) {
@@ -1,8 +1,10 @@
package fr.xephi.authme.command.executable.email;
import fr.xephi.authme.command.PlayerCommand;
import fr.xephi.authme.data.VerificationCodeManager;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.service.CommonService;
import org.bukkit.entity.Player;
import javax.inject.Inject;
@@ -16,11 +18,24 @@ public class ChangeEmailCommand extends PlayerCommand {
@Inject
private Management management;
@Inject
private CommonService commonService;
@Inject
private VerificationCodeManager codeManager;
@Override
public void runCommand(Player player, List<String> arguments) {
final String playerName = player.getName();
// Check if the user has been verified or not
if (codeManager.isVerificationRequired(player)) {
codeManager.codeExistOrGenerateNew(playerName);
commonService.send(player, MessageKey.VERIFICATION_CODE_REQUIRED);
return;
}
String playerMailOld = arguments.get(0);
String playerMailNew = arguments.get(1);
management.performChangeEmail(player, playerMailOld, playerMailNew);
}
@@ -1,6 +1,7 @@
package fr.xephi.authme.command.executable.unregister;
import fr.xephi.authme.command.PlayerCommand;
import fr.xephi.authme.data.VerificationCodeManager;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.Management;
@@ -24,6 +25,9 @@ public class UnregisterCommand extends PlayerCommand {
@Inject
private PlayerCache playerCache;
@Inject
private VerificationCodeManager codeManager;
@Override
public void runCommand(Player player, List<String> arguments) {
String playerPass = arguments.get(0);
@@ -35,6 +39,13 @@ public class UnregisterCommand extends PlayerCommand {
return;
}
// Check if the user has been verified or not
if (codeManager.isVerificationRequired(player)) {
codeManager.codeExistOrGenerateNew(playerName);
commonService.send(player, MessageKey.VERIFICATION_CODE_REQUIRED);
return;
}
// Unregister the player
management.performUnregister(player, playerPass);
}
@@ -0,0 +1,58 @@
package fr.xephi.authme.command.executable.verification;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.command.PlayerCommand;
import fr.xephi.authme.data.VerificationCodeManager;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.service.CommonService;
import org.bukkit.entity.Player;
import javax.inject.Inject;
import java.util.List;
/**
* Used to complete the email verification process.
*/
public class VerificationCommand extends PlayerCommand {
@Inject
private CommonService commonService;
@Inject
private VerificationCodeManager codeManager;
@Override
public void runCommand(Player player, List<String> arguments) {
final String playerName = player.getName();
if (!codeManager.canSendMail()) {
ConsoleLogger.warning("Mail API is not set");
commonService.send(player, MessageKey.INCOMPLETE_EMAIL_SETTINGS);
return;
}
if (codeManager.isVerificationRequired(player)) {
if (codeManager.isCodeRequired(playerName)) {
if (codeManager.checkCode(playerName, arguments.get(0))) {
commonService.send(player, MessageKey.VERIFICATION_CODE_VERIFIED);
} else {
commonService.send(player, MessageKey.INCORRECT_VERIFICATION_CODE);
}
} else {
commonService.send(player, MessageKey.VERIFICATION_CODE_EXPIRED);
}
} else {
if (codeManager.hasEmail(playerName)) {
commonService.send(player, MessageKey.VERIFICATION_CODE_ALREADY_VERIFIED);
} else {
commonService.send(player, MessageKey.VERIFICATION_CODE_EMAIL_NEEDED);
commonService.send(player, MessageKey.ADD_EMAIL_MESSAGE);
}
}
}
@Override
public MessageKey getArgumentsMismatchMessage() {
return MessageKey.USAGE_VERIFICATION_CODE;
}
}