@Override public void runCommand(Player player, List<String> arguments, CommandService commandService) { if (arguments.isEmpty() || Settings.enablePasswordConfirmation && arguments.size() < 2) { commandService.send(player, MessageKey.USAGE_REGISTER); return; } final Management management = commandService.getManagement(); if (Settings.emailRegistration && !Settings.getmailAccount.isEmpty()) { if (Settings.doubleEmailCheck && arguments.size() < 2 || !arguments.get(0).equals(arguments.get(1))) { commandService.send(player, MessageKey.USAGE_REGISTER); return; } final String email = arguments.get(0); if (!Settings.isEmailCorrect(email)) { commandService.send(player, MessageKey.INVALID_EMAIL); return; } final String thePass = RandomString.generate(Settings.getRecoveryPassLength); management.performRegister(player, thePass, email); return; } if (arguments.size() > 1 && Settings.enablePasswordConfirmation && !arguments.get(0).equals(arguments.get(1))) { commandService.send(player, MessageKey.PASSWORD_MATCH_ERROR); return; } management.performRegister(player, arguments.get(0), ""); }
@Before public void setUpMocks() { wrapperMock = WrapperMock.createInstance(); cacheMock = wrapperMock.getPlayerCache(); commandService = mock(CommandService.class); when(commandService.getProperty(SecuritySettings.MIN_PASSWORD_LENGTH)).thenReturn(2); when(commandService.getProperty(SecuritySettings.MAX_PASSWORD_LENGTH)).thenReturn(50); // Only allow passwords with alphanumerical characters for the test when(commandService.getProperty(RestrictionSettings.ALLOWED_PASSWORD_REGEX)) .thenReturn("[a-zA-Z0-9]+"); when(commandService.getProperty(SecuritySettings.UNSAFE_PASSWORDS)) .thenReturn(Collections.EMPTY_LIST); }
@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)) { commandService.send(player, MessageKey.NOT_LOGGED_IN); return; } // Make sure the password is allowed ValidationResult passwordValidation = validationService.validatePassword(newPassword, name); if (passwordValidation.hasError()) { commandService.send(player, passwordValidation.getMessageKey(), passwordValidation.getArgs()); return; } // TODO ljacqu 20160117: Call async task via Management management.performPasswordChange(player, oldPassword, newPassword); }
@Test public void shouldRejectTooShortPassword() { // given CommandSender sender = initPlayerWithName("abc12", true); ChangePasswordCommand command = new ChangePasswordCommand(); given(commandService.getProperty(SecuritySettings.MIN_PASSWORD_LENGTH)).willReturn(7); // when command.executeCommand(sender, Arrays.asList("oldverylongpassword", "tester"), commandService); // then verify(commandService).send(sender, MessageKey.INVALID_PASSWORD_LENGTH); assertThat(wrapperMock.wasMockCalled(Server.class), equalTo(false)); }
@Test public void shouldRejectUnsafeCustomPassword() { // given CommandSender sender = initPlayerWithName("player", true); ChangePasswordCommand command = new ChangePasswordCommand(); given(commandService.getProperty(SecuritySettings.UNSAFE_PASSWORDS)) .willReturn(Arrays.asList("test", "abc123")); // when command.executeCommand(sender, Arrays.asList("oldpw", "abc123"), commandService); // then verify(commandService).send(sender, MessageKey.PASSWORD_UNSAFE_ERROR); assertThat(wrapperMock.wasMockCalled(Server.class), equalTo(false)); }