Merge branch '427-register-with-pass-and-email' of https://github.com/AuthMe-Team/AuthMeReloaded

This commit is contained in:
ljacqu
2016-12-21 21:39:30 +01:00
27 changed files with 1147 additions and 261 deletions
@@ -4,10 +4,11 @@ import fr.xephi.authme.AuthMe;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.service.PluginHookService;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.process.register.executors.RegistrationExecutorProvider;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.service.PluginHookService;
import fr.xephi.authme.service.ValidationService;
import org.bukkit.Bukkit;
import org.bukkit.Location;
@@ -33,13 +34,15 @@ public class NewAPI {
private final Management management;
private final ValidationService validationService;
private final PlayerCache playerCache;
private final RegistrationExecutorProvider registrationExecutorProvider;
/*
* Constructor for NewAPI.
*/
@Inject
NewAPI(AuthMe plugin, PluginHookService pluginHookService, DataSource dataSource, PasswordSecurity passwordSecurity,
Management management, ValidationService validationService, PlayerCache playerCache) {
Management management, ValidationService validationService, PlayerCache playerCache,
RegistrationExecutorProvider registrationExecutorProvider) {
this.plugin = plugin;
this.pluginHookService = pluginHookService;
this.dataSource = dataSource;
@@ -47,6 +50,7 @@ public class NewAPI {
this.management = management;
this.validationService = validationService;
this.playerCache = playerCache;
this.registrationExecutorProvider = registrationExecutorProvider;
NewAPI.singleton = this;
}
@@ -199,7 +203,8 @@ public class NewAPI {
* @param autoLogin Should the player be authenticated automatically after the registration?
*/
public void forceRegister(Player player, String password, boolean autoLogin) {
management.performRegister(player, password, null, autoLogin);
management.performRegister(player,
registrationExecutorProvider.getPasswordRegisterExecutor(player, password, autoLogin));
}
/**
@@ -5,21 +5,23 @@ import fr.xephi.authme.command.PlayerCommand;
import fr.xephi.authme.mail.SendMailSSL;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.process.register.executors.RegistrationExecutorProvider;
import fr.xephi.authme.security.HashAlgorithm;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.settings.properties.EmailSettings;
import fr.xephi.authme.settings.properties.RegistrationArgumentType;
import fr.xephi.authme.settings.properties.RegistrationArgumentType.Execution;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.util.RandomStringUtils;
import org.bukkit.entity.Player;
import javax.inject.Inject;
import java.util.List;
import static fr.xephi.authme.settings.properties.EmailSettings.RECOVERY_PASSWORD_LENGTH;
import static fr.xephi.authme.settings.properties.RegistrationSettings.ENABLE_CONFIRM_EMAIL;
import static fr.xephi.authme.settings.properties.RegistrationSettings.USE_EMAIL_REGISTRATION;
import static fr.xephi.authme.settings.properties.RestrictionSettings.ENABLE_PASSWORD_CONFIRMATION;
import static fr.xephi.authme.settings.properties.RegistrationArgumentType.EMAIL_WITH_CONFIRMATION;
import static fr.xephi.authme.settings.properties.RegistrationArgumentType.PASSWORD_WITH_CONFIRMATION;
import static fr.xephi.authme.settings.properties.RegistrationArgumentType.PASSWORD_WITH_EMAIL;
import static fr.xephi.authme.settings.properties.RegistrationSettings.REGISTRATION_TYPE;
/**
* Command for /register.
@@ -38,22 +40,26 @@ public class RegisterCommand extends PlayerCommand {
@Inject
private ValidationService validationService;
@Inject
private RegistrationExecutorProvider registrationExecutorProvider;
@Override
public void runCommand(Player player, List<String> arguments) {
if (commonService.getProperty(SecuritySettings.PASSWORD_HASH) == HashAlgorithm.TWO_FACTOR) {
//for two factor auth we don't need to check the usage
management.performRegister(player, "", "", true);
management.performRegister(player,
registrationExecutorProvider.getTwoFactorRegisterExecutor(player));
return;
}
// Ensure that there is 1 argument, or 2 if confirmation is required
final boolean useConfirmation = isConfirmationRequired();
if (arguments.isEmpty() || useConfirmation && arguments.size() < 2) {
RegistrationArgumentType registerType = commonService.getProperty(REGISTRATION_TYPE);
if (registerType.getRequiredNumberOfArgs() > arguments.size()) {
commonService.send(player, MessageKey.USAGE_REGISTER);
return;
}
if (commonService.getProperty(USE_EMAIL_REGISTRATION)) {
if (registerType.getExecution() == Execution.EMAIL) {
handleEmailRegistration(player, arguments);
} else {
handlePasswordRegistration(player, arguments);
@@ -66,10 +72,23 @@ public class RegisterCommand extends PlayerCommand {
}
private void handlePasswordRegistration(Player player, List<String> arguments) {
if (commonService.getProperty(ENABLE_PASSWORD_CONFIRMATION) && !arguments.get(0).equals(arguments.get(1))) {
RegistrationArgumentType registrationType = commonService.getProperty(REGISTRATION_TYPE);
if (registrationType == PASSWORD_WITH_CONFIRMATION && !arguments.get(0).equals(arguments.get(1))) {
commonService.send(player, MessageKey.PASSWORD_MATCH_ERROR);
} else if (registrationType == PASSWORD_WITH_EMAIL) {
handlePasswordWithEmailRegistration(player, arguments);
} else {
management.performRegister(player, arguments.get(0), "", true);
management.performRegister(player, registrationExecutorProvider
.getPasswordRegisterExecutor(player, arguments.get(0)));
}
}
private void handlePasswordWithEmailRegistration(Player player, List<String> arguments) {
if (validationService.validateEmail(arguments.get(1))) {
management.performRegister(player, registrationExecutorProvider
.getPasswordRegisterExecutor(player, arguments.get(0), arguments.get(1)));
} else {
commonService.send(player, MessageKey.INVALID_EMAIL);
}
}
@@ -84,22 +103,10 @@ public class RegisterCommand extends PlayerCommand {
final String email = arguments.get(0);
if (!validationService.validateEmail(email)) {
commonService.send(player, MessageKey.INVALID_EMAIL);
} else if (commonService.getProperty(ENABLE_CONFIRM_EMAIL) && !email.equals(arguments.get(1))) {
} else if (commonService.getProperty(REGISTRATION_TYPE) == EMAIL_WITH_CONFIRMATION && !email.equals(arguments.get(1))) {
commonService.send(player, MessageKey.USAGE_REGISTER);
} else {
String thePass = RandomStringUtils.generate(commonService.getProperty(RECOVERY_PASSWORD_LENGTH));
management.performRegister(player, thePass, email, true);
management.performRegister(player, registrationExecutorProvider.getEmailRegisterExecutor(player, email));
}
}
/**
* Return whether the password or email has to be confirmed.
*
* @return True if the confirmation is needed, false otherwise
*/
private boolean isConfirmationRequired() {
return commonService.getProperty(USE_EMAIL_REGISTRATION)
? commonService.getProperty(ENABLE_CONFIRM_EMAIL)
: commonService.getProperty(ENABLE_PASSWORD_CONFIRMATION);
}
}
@@ -8,6 +8,7 @@ import fr.xephi.authme.process.login.AsynchronousLogin;
import fr.xephi.authme.process.logout.AsynchronousLogout;
import fr.xephi.authme.process.quit.AsynchronousQuit;
import fr.xephi.authme.process.register.AsyncRegister;
import fr.xephi.authme.process.register.executors.RegistrationExecutor;
import fr.xephi.authme.process.unregister.AsynchronousUnregister;
import fr.xephi.authme.service.BukkitService;
import org.bukkit.command.CommandSender;
@@ -59,8 +60,8 @@ public class Management {
runTask(() -> asynchronousLogout.logout(player));
}
public void performRegister(Player player, String password, String email, boolean autoLogin) {
runTask(() -> asyncRegister.register(player, password, email, autoLogin));
public void performRegister(Player player, RegistrationExecutor registrationExecutor) {
runTask(() -> asyncRegister.register(player, registrationExecutor));
}
public void performUnregister(Player player, String password) {
@@ -9,6 +9,7 @@ import fr.xephi.authme.process.AsynchronousProcess;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RegistrationArgumentType.Execution;
import org.bukkit.entity.Player;
import javax.inject.Inject;
@@ -63,7 +64,7 @@ public class AsyncAddEmail implements AsynchronousProcess {
private void sendUnloggedMessage(Player player) {
if (dataSource.isAuthAvailable(player.getName())) {
service.send(player, MessageKey.LOGIN_MESSAGE);
} else if (service.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)) {
} else if (service.getProperty(RegistrationSettings.REGISTRATION_TYPE).getExecution() == Execution.EMAIL) {
service.send(player, MessageKey.REGISTER_EMAIL_MESSAGE);
} else {
service.send(player, MessageKey.REGISTER_MESSAGE);
@@ -8,6 +8,7 @@ import fr.xephi.authme.process.AsynchronousProcess;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RegistrationArgumentType.Execution;
import org.bukkit.entity.Player;
import javax.inject.Inject;
@@ -66,7 +67,7 @@ public class AsyncChangeEmail implements AsynchronousProcess {
private void outputUnloggedMessage(Player player) {
if (dataSource.isAuthAvailable(player.getName())) {
service.send(player, MessageKey.LOGIN_MESSAGE);
} else if (service.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)) {
} else if (service.getProperty(RegistrationSettings.REGISTRATION_TYPE).getExecution() == Execution.EMAIL) {
service.send(player, MessageKey.REGISTER_EMAIL_MESSAGE);
} else {
service.send(player, MessageKey.REGISTER_MESSAGE);
@@ -3,28 +3,14 @@ package fr.xephi.authme.process.register;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.mail.SendMailSSL;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.process.AsynchronousProcess;
import fr.xephi.authme.process.register.executors.RegistrationExecutor;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.process.SyncProcessManager;
import fr.xephi.authme.process.login.AsynchronousLogin;
import fr.xephi.authme.security.HashAlgorithm;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.security.crypts.TwoFactor;
import fr.xephi.authme.settings.properties.EmailSettings;
import fr.xephi.authme.settings.properties.PluginSettings;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.util.PlayerUtils;
import fr.xephi.authme.util.StringUtils;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.service.ValidationService.ValidationResult;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import javax.inject.Inject;
@@ -37,38 +23,31 @@ import static fr.xephi.authme.permission.PlayerStatePermission.ALLOW_MULTIPLE_AC
*/
public class AsyncRegister implements AsynchronousProcess {
/**
* Number of ticks to wait before running the login action when it is run synchronously.
* A small delay is necessary or the database won't return the newly saved PlayerAuth object
* and the login process thinks the user is not registered.
*/
private static final int SYNC_LOGIN_DELAY = 5;
@Inject
private DataSource database;
@Inject
private PlayerCache playerCache;
@Inject
private PasswordSecurity passwordSecurity;
@Inject
private CommonService service;
@Inject
private SyncProcessManager syncProcessManager;
@Inject
private PermissionsManager permissionsManager;
@Inject
private ValidationService validationService;
@Inject
private SendMailSSL sendMailSsl;
@Inject
private AsynchronousLogin asynchronousLogin;
@Inject
private BukkitService bukkitService;
AsyncRegister() {
}
private boolean preRegisterCheck(Player player, String password) {
/**
* Performs the registration process for the given player.
*
* @param player the player to register
* @param executor the registration executor to perform the registration with
*/
public void register(Player player, RegistrationExecutor executor) {
if (preRegisterCheck(player) && executor.isRegistrationAdmitted()) {
executeRegistration(player, executor);
}
}
private boolean preRegisterCheck(Player player) {
final String name = player.getName().toLowerCase();
if (playerCache.isAuthenticated(name)) {
service.send(player, MessageKey.ALREADY_LOGGED_IN_ERROR);
@@ -76,23 +55,36 @@ public class AsyncRegister implements AsynchronousProcess {
} else if (!service.getProperty(RegistrationSettings.IS_ENABLED)) {
service.send(player, MessageKey.REGISTRATION_DISABLED);
return false;
}
//check the password safety only if it's not a automatically generated password
if (service.getProperty(SecuritySettings.PASSWORD_HASH) != HashAlgorithm.TWO_FACTOR) {
ValidationResult passwordValidation = validationService.validatePassword(password, player.getName());
if (passwordValidation.hasError()) {
service.send(player, passwordValidation.getMessageKey(), passwordValidation.getArgs());
return false;
}
}
//check this in both possibilities so don't use 'else if'
if (database.isAuthAvailable(name)) {
} else if (database.isAuthAvailable(name)) {
service.send(player, MessageKey.NAME_ALREADY_REGISTERED);
return false;
}
return isPlayerIpAllowedToRegister(player);
}
/**
* Executes the registration.
*
* @param player the player to register
* @param executor the executor to perform the registration process with
*/
private void executeRegistration(Player player, RegistrationExecutor executor) {
PlayerAuth auth = executor.buildPlayerAuth();
if (database.saveAuth(auth)) {
executor.executePostPersistAction();
} else {
service.send(player, MessageKey.ERROR);
}
}
/**
* Checks whether the registration threshold has been exceeded for the given player's IP address.
*
* @param player the player to check
* @return true if registration may take place, false otherwise (IP check failed)
*/
private boolean isPlayerIpAllowedToRegister(Player player) {
final int maxRegPerIp = service.getProperty(RestrictionSettings.MAX_REGISTRATION_PER_IP);
final String ip = PlayerUtils.getPlayerIp(player);
if (maxRegPerIp > 0
@@ -108,84 +100,4 @@ public class AsyncRegister implements AsynchronousProcess {
}
return true;
}
public void register(Player player, String password, String email, boolean autoLogin) {
if (preRegisterCheck(player, password)) {
if (!StringUtils.isEmpty(email)) {
emailRegister(player, password, email);
} else {
passwordRegister(player, password, autoLogin);
}
}
}
private void emailRegister(Player player, String password, String email) {
final String name = player.getName().toLowerCase();
final int maxRegPerEmail = service.getProperty(EmailSettings.MAX_REG_PER_EMAIL);
if (maxRegPerEmail > 0 && !permissionsManager.hasPermission(player, ALLOW_MULTIPLE_ACCOUNTS)) {
int otherAccounts = database.countAuthsByEmail(email);
if (otherAccounts >= maxRegPerEmail) {
service.send(player, MessageKey.MAX_REGISTER_EXCEEDED, Integer.toString(maxRegPerEmail),
Integer.toString(otherAccounts), "@");
return;
}
}
final HashedPassword hashedPassword = passwordSecurity.computeHash(password, name);
final String ip = PlayerUtils.getPlayerIp(player);
PlayerAuth auth = PlayerAuth.builder()
.name(name)
.realName(player.getName())
.password(hashedPassword)
.ip(ip)
.location(player.getLocation())
.email(email)
.build();
if (!database.saveAuth(auth)) {
service.send(player, MessageKey.ERROR);
return;
}
database.updateEmail(auth);
database.updateSession(auth);
boolean couldSendMail = sendMailSsl.sendPasswordMail(name, email, password);
if (couldSendMail) {
syncProcessManager.processSyncEmailRegister(player);
} else {
service.send(player, MessageKey.EMAIL_SEND_FAILURE);
}
}
private void passwordRegister(final Player player, String password, boolean autoLogin) {
final String name = player.getName().toLowerCase();
final String ip = PlayerUtils.getPlayerIp(player);
final HashedPassword hashedPassword = passwordSecurity.computeHash(password, name);
PlayerAuth auth = PlayerAuth.builder()
.name(name)
.realName(player.getName())
.password(hashedPassword)
.ip(ip)
.location(player.getLocation())
.build();
if (!database.saveAuth(auth)) {
service.send(player, MessageKey.ERROR);
return;
}
if (!service.getProperty(RegistrationSettings.FORCE_LOGIN_AFTER_REGISTER) && autoLogin) {
if (service.getProperty(PluginSettings.USE_ASYNC_TASKS)) {
bukkitService.runTaskAsynchronously(() -> asynchronousLogin.forceLogin(player));
} else {
bukkitService.scheduleSyncDelayedTask(() -> asynchronousLogin.forceLogin(player), SYNC_LOGIN_DELAY);
}
}
syncProcessManager.processSyncPasswordRegister(player);
//give the user the secret code to setup their app code generation
if (service.getProperty(SecuritySettings.PASSWORD_HASH) == HashAlgorithm.TWO_FACTOR) {
String qrCodeUrl = TwoFactor.getQRBarcodeURL(player.getName(), Bukkit.getIp(), hashedPassword.getHash());
service.send(player, MessageKey.TWO_FACTOR_CREATE, hashedPassword.getHash(), qrCodeUrl);
}
}
}
@@ -0,0 +1,91 @@
package fr.xephi.authme.process.register.executors;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.mail.SendMailSSL;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.process.SyncProcessManager;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.settings.properties.EmailSettings;
import fr.xephi.authme.util.RandomStringUtils;
import org.bukkit.entity.Player;
import javax.inject.Inject;
import static fr.xephi.authme.process.register.executors.PlayerAuthBuilderHelper.createPlayerAuth;
import static fr.xephi.authme.permission.PlayerStatePermission.ALLOW_MULTIPLE_ACCOUNTS;
import static fr.xephi.authme.settings.properties.EmailSettings.RECOVERY_PASSWORD_LENGTH;
/**
* Provides a registration executor for email registration.
*/
class EmailRegisterExecutorProvider {
@Inject
private PermissionsManager permissionsManager;
@Inject
private DataSource dataSource;
@Inject
private CommonService commonService;
@Inject
private SendMailSSL sendMailSsl;
@Inject
private SyncProcessManager syncProcessManager;
@Inject
private PasswordSecurity passwordSecurity;
EmailRegisterExecutorProvider() {
}
/** Registration executor implementation for email registration. */
class EmailRegisterExecutor implements RegistrationExecutor {
private final Player player;
private final String email;
private String password;
EmailRegisterExecutor(Player player, String email) {
this.player = player;
this.email = email;
}
@Override
public boolean isRegistrationAdmitted() {
final int maxRegPerEmail = commonService.getProperty(EmailSettings.MAX_REG_PER_EMAIL);
if (maxRegPerEmail > 0 && !permissionsManager.hasPermission(player, ALLOW_MULTIPLE_ACCOUNTS)) {
int otherAccounts = dataSource.countAuthsByEmail(email);
if (otherAccounts >= maxRegPerEmail) {
commonService.send(player, MessageKey.MAX_REGISTER_EXCEEDED, Integer.toString(maxRegPerEmail),
Integer.toString(otherAccounts), "@");
return false;
}
}
return true;
}
@Override
public PlayerAuth buildPlayerAuth() {
password = RandomStringUtils.generate(commonService.getProperty(RECOVERY_PASSWORD_LENGTH));
HashedPassword hashedPassword = passwordSecurity.computeHash(password, player.getName());
return createPlayerAuth(player, hashedPassword, email);
}
@Override
public void executePostPersistAction() {
boolean couldSendMail = sendMailSsl.sendPasswordMail(player.getName(), email, password);
if (couldSendMail) {
syncProcessManager.processSyncEmailRegister(player);
} else {
commonService.send(player, MessageKey.EMAIL_SEND_FAILURE);
}
}
}
}
@@ -0,0 +1,155 @@
package fr.xephi.authme.process.register.executors;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.SyncProcessManager;
import fr.xephi.authme.process.login.AsynchronousLogin;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.security.crypts.TwoFactor;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.service.ValidationService.ValidationResult;
import fr.xephi.authme.settings.properties.PluginSettings;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import javax.inject.Inject;
import static fr.xephi.authme.process.register.executors.PlayerAuthBuilderHelper.createPlayerAuth;
/**
* Provides registration executors for password-based registration variants.
*/
class PasswordRegisterExecutorProvider {
/**
* Number of ticks to wait before running the login action when it is run synchronously.
* A small delay is necessary or the database won't return the newly saved PlayerAuth object
* and the login process thinks the user is not registered.
*/
private static final int SYNC_LOGIN_DELAY = 5;
@Inject
private ValidationService validationService;
@Inject
private CommonService commonService;
@Inject
private PasswordSecurity passwordSecurity;
@Inject
private BukkitService bukkitService;
@Inject
private SyncProcessManager syncProcessManager;
@Inject
private AsynchronousLogin asynchronousLogin;
PasswordRegisterExecutorProvider() {
}
/** Registration executor for password registration. */
class PasswordRegisterExecutor implements RegistrationExecutor {
protected final Player player;
private final String password;
private final String email;
protected HashedPassword hashedPassword;
/**
* Constructor.
*
* @param player the player to register
* @param password the password to register with
* @param email the email of the player (may be null)
*/
PasswordRegisterExecutor(Player player, String password, String email) {
this.player = player;
this.password = password;
this.email = email;
}
@Override
public boolean isRegistrationAdmitted() {
ValidationResult passwordValidation = validationService.validatePassword(password, player.getName());
if (passwordValidation.hasError()) {
commonService.send(player, passwordValidation.getMessageKey(), passwordValidation.getArgs());
return false;
}
return true;
}
@Override
public PlayerAuth buildPlayerAuth() {
hashedPassword = passwordSecurity.computeHash(password, player.getName().toLowerCase());
return createPlayerAuth(player, hashedPassword, email);
}
protected boolean performLoginAfterRegister() {
return !commonService.getProperty(RegistrationSettings.FORCE_LOGIN_AFTER_REGISTER);
}
@Override
public void executePostPersistAction() {
if (performLoginAfterRegister()) {
if (commonService.getProperty(PluginSettings.USE_ASYNC_TASKS)) {
bukkitService.runTaskAsynchronously(() -> asynchronousLogin.forceLogin(player));
} else {
bukkitService.scheduleSyncDelayedTask(() -> asynchronousLogin.forceLogin(player), SYNC_LOGIN_DELAY);
}
}
syncProcessManager.processSyncPasswordRegister(player);
}
}
/** Executor for password registration via API call. */
class ApiPasswordRegisterExecutor extends PasswordRegisterExecutor {
private final boolean loginAfterRegister;
/**
* Constructor.
*
* @param player the player to register
* @param password the password to register with
* @param loginAfterRegister whether the user should be automatically logged in after registration
*/
ApiPasswordRegisterExecutor(Player player, String password, boolean loginAfterRegister) {
super(player, password, null);
this.loginAfterRegister = loginAfterRegister;
}
@Override
protected boolean performLoginAfterRegister() {
return loginAfterRegister;
}
}
/** Executor for two factor registration. */
class TwoFactorRegisterExecutor extends PasswordRegisterExecutor {
TwoFactorRegisterExecutor(Player player) {
super(player, "", null);
}
@Override
public boolean isRegistrationAdmitted() {
// nothing to check
return true;
}
@Override
public void executePostPersistAction() {
super.executePostPersistAction();
String qrCodeUrl = TwoFactor.getQRBarcodeURL(player.getName(), Bukkit.getIp(), hashedPassword.getHash());
commonService.send(player, MessageKey.TWO_FACTOR_CREATE, hashedPassword.getHash(), qrCodeUrl);
}
}
}
@@ -0,0 +1,26 @@
package fr.xephi.authme.process.register.executors;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.util.PlayerUtils;
import org.bukkit.entity.Player;
/**
* Helper for constructing PlayerAuth objects.
*/
final class PlayerAuthBuilderHelper {
private PlayerAuthBuilderHelper() {
}
static PlayerAuth createPlayerAuth(Player player, HashedPassword hashedPassword, String email) {
return PlayerAuth.builder()
.name(player.getName().toLowerCase())
.realName(player.getName())
.password(hashedPassword)
.email(email)
.ip(PlayerUtils.getPlayerIp(player))
.location(player.getLocation())
.build();
}
}
@@ -0,0 +1,33 @@
package fr.xephi.authme.process.register.executors;
import fr.xephi.authme.data.auth.PlayerAuth;
/**
* Performs the registration action.
*/
public interface RegistrationExecutor {
/**
* Returns whether the registration may take place. Use this method to execute
* checks specific to the registration method.
* <p>
* If this method returns {@code false}, it is expected that the executor inform
* the player about the error within this method call.
*
* @return true if registration may be performed, false otherwise
*/
boolean isRegistrationAdmitted();
/**
* Constructs the PlayerAuth object to persist into the database.
*
* @return the player auth to register in the data source
*/
PlayerAuth buildPlayerAuth();
/**
* Follow-up method called after the player auth could be added into the database.
*/
void executePostPersistAction();
}
@@ -0,0 +1,40 @@
package fr.xephi.authme.process.register.executors;
import org.bukkit.entity.Player;
import javax.inject.Inject;
/**
* Provides a {@link RegistrationExecutor} for various registration methods.
*/
public class RegistrationExecutorProvider {
@Inject
private PasswordRegisterExecutorProvider passwordRegisterExecutorProvider;
@Inject
private EmailRegisterExecutorProvider emailRegisterExecutorProvider;
RegistrationExecutorProvider() {
}
public RegistrationExecutor getPasswordRegisterExecutor(Player player, String password) {
return getPasswordRegisterExecutor(player, password, null);
}
public RegistrationExecutor getPasswordRegisterExecutor(Player player, String password, String email) {
return passwordRegisterExecutorProvider.new PasswordRegisterExecutor(player, password, email);
}
public RegistrationExecutor getPasswordRegisterExecutor(Player player, String password, boolean loginAfterRegister) {
return passwordRegisterExecutorProvider.new ApiPasswordRegisterExecutor(player, password, loginAfterRegister);
}
public RegistrationExecutor getTwoFactorRegisterExecutor(Player player) {
return passwordRegisterExecutorProvider.new TwoFactorRegisterExecutor(player);
}
public RegistrationExecutor getEmailRegisterExecutor(Player player, String email) {
return emailRegisterExecutorProvider.new EmailRegisterExecutor(player, email);
}
}
@@ -9,6 +9,8 @@ import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.initialization.DataFolder;
import fr.xephi.authme.output.LogLevel;
import fr.xephi.authme.settings.properties.PluginSettings;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RegistrationArgumentType;
import fr.xephi.authme.settings.properties.SecuritySettings;
import javax.inject.Inject;
@@ -67,6 +69,7 @@ public class SettingsMigrationService extends PlainMigrationService {
| changeBooleanSettingToLogLevelProperty(resource)
| hasOldHelpHeaderProperty(resource)
| hasSupportOldPasswordProperty(resource)
| convertToRegistrationType(resource)
|| hasDeprecatedProperties(resource);
}
@@ -224,6 +227,34 @@ public class SettingsMigrationService extends PlainMigrationService {
return false;
}
private static boolean convertToRegistrationType(PropertyResource resource) {
if (RegistrationSettings.REGISTRATION_TYPE.isPresent(resource)) {
return false;
}
boolean useEmail = newProperty("settings.registration.enableEmailRegistrationSystem", false).getValue(resource);
String useConfirmationPath = useEmail
? "settings.registration.doubleEmailCheck"
: "settings.restrictions.enablePasswordConfirmation";
boolean hasConfirmation = newProperty(useConfirmationPath, false).getValue(resource);
RegistrationArgumentType registerType;
if (useEmail) {
registerType = hasConfirmation
? RegistrationArgumentType.EMAIL_WITH_CONFIRMATION
: RegistrationArgumentType.EMAIL;
} else {
registerType = hasConfirmation
? RegistrationArgumentType.PASSWORD_WITH_CONFIRMATION
: RegistrationArgumentType.PASSWORD;
}
ConsoleLogger.warning("Merging old registration settings into '"
+ RegistrationSettings.REGISTRATION_TYPE.getPath() + "'");
resource.setValue(RegistrationSettings.REGISTRATION_TYPE.getPath(), registerType);
return true;
}
/**
* Checks for an old property path and moves it to a new path if present.
*
@@ -0,0 +1,60 @@
package fr.xephi.authme.settings.properties;
/**
* Type of arguments used for the login command.
*/
public enum RegistrationArgumentType {
/** /register [password] */
PASSWORD(Execution.PASSWORD, 1),
/** /register [password] [password] */
PASSWORD_WITH_CONFIRMATION(Execution.PASSWORD, 2),
/** /register [email] */
EMAIL(Execution.EMAIL, 1),
/** /register [email] [email] */
EMAIL_WITH_CONFIRMATION(Execution.EMAIL, 2),
/** /register [password] [email] */
PASSWORD_WITH_EMAIL(Execution.PASSWORD, 2);
private final Execution execution;
private final int requiredNumberOfArgs;
/**
* Constructor.
*
* @param execution the registration process
* @param requiredNumberOfArgs the required number of arguments
*/
RegistrationArgumentType(Execution execution, int requiredNumberOfArgs) {
this.execution = execution;
this.requiredNumberOfArgs = requiredNumberOfArgs;
}
/**
* @return the registration execution that is used for this argument type
*/
public Execution getExecution() {
return execution;
}
/**
* @return number of arguments required to process the register command
*/
public int getRequiredNumberOfArgs() {
return requiredNumberOfArgs;
}
/**
* Registration execution (the type of registration).
*/
public enum Execution {
PASSWORD,
EMAIL
}
}
@@ -24,16 +24,12 @@ public class RegistrationSettings implements SettingsHolder {
public static final Property<Boolean> FORCE =
newProperty("settings.registration.force", true);
@Comment("Do we replace password registration by an email registration method?")
public static final Property<Boolean> USE_EMAIL_REGISTRATION =
newProperty("settings.registration.enableEmailRegistrationSystem", false);
@Comment({
"Enable double check of email when you register",
"when it's true, registration requires that kind of command:",
"/register <email> <confirmEmail>"})
public static final Property<Boolean> ENABLE_CONFIRM_EMAIL =
newProperty("settings.registration.doubleEmailCheck", false);
"Type of registration: PASSWORD, PASSWORD_WITH_CONFIRMATION, EMAIL",
"EMAIL_WITH_CONFIRMATION, PASSWORD_WITH_EMAIL"
})
public static final Property<RegistrationArgumentType> REGISTRATION_TYPE =
newProperty(RegistrationArgumentType.class, "settings.registration.type", RegistrationArgumentType.PASSWORD_WITH_CONFIRMATION);
@Comment({
"Do we force kick a player after a successful registration?",
@@ -126,13 +126,6 @@ public class RestrictionSettings implements SettingsHolder {
public static final Property<Integer> ALLOWED_MOVEMENT_RADIUS =
newProperty("settings.restrictions.allowedMovementRadius", 100);
@Comment({
"Enable double check of password when you register",
"when it's true, registration requires that kind of command:",
"/register <password> <confirmPassword>"})
public static final Property<Boolean> ENABLE_PASSWORD_CONFIRMATION =
newProperty("settings.restrictions.enablePasswordConfirmation", true);
@Comment("Should we protect the player inventory before logging in? Requires ProtocolLib.")
public static final Property<Boolean> PROTECT_INVENTORY_BEFORE_LOGIN =
newProperty("settings.restrictions.ProtectInventoryBeforeLogIn", true);
@@ -6,10 +6,11 @@ import fr.xephi.authme.data.limbo.LimboCache;
import fr.xephi.authme.data.limbo.LimboPlayer;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.message.Messages;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RegistrationArgumentType.Execution;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.service.BukkitService;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
@@ -95,7 +96,7 @@ public class LimboPlayerTaskManager {
if (isRegistered) {
return MessageKey.LOGIN_MESSAGE;
} else {
return settings.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)
return settings.getProperty(RegistrationSettings.REGISTRATION_TYPE).getExecution() == Execution.EMAIL
? MessageKey.REGISTER_EMAIL_MESSAGE
: MessageKey.REGISTER_MESSAGE;
}