Skip to content

Commit

Permalink
Merge pull request #135 from AZhiKai/feature/issue-98
Browse files Browse the repository at this point in the history
v1.3: Mask password
  • Loading branch information
azhikai authored Oct 24, 2018
2 parents a6a6a17 + 2818612 commit 0d36a52
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 24 deletions.
1 change: 0 additions & 1 deletion src/main/java/seedu/address/logic/CommandHistory.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public CommandHistory(CommandHistory commandHistory) {
*/
public void add(String userInput) {
requireNonNull(userInput);
//TODO: Should not show the password here
userInputHistory.add(userInput);
}

Expand Down
10 changes: 9 additions & 1 deletion src/main/java/seedu/address/logic/LogicManager.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package seedu.address.logic;

import static seedu.address.logic.parser.CliSyntax.PREFIX_PASSWORD;

import java.util.logging.Logger;

import javafx.collections.ObservableList;
Expand All @@ -17,6 +19,7 @@
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.Model;
import seedu.address.model.accounts.Account;
import seedu.address.model.accounts.Password;
import seedu.address.model.ingredient.Ingredient;
import seedu.address.model.menu.Item;
import seedu.address.model.person.Person;
Expand Down Expand Up @@ -49,10 +52,15 @@ private boolean isPublicCommand(Command command) {

@Override
public CommandResult execute(String commandText) throws CommandException, ParseException {
logger.info("----------------[USER COMMAND][" + commandText + "]");
String commandTextToLog = commandText;
if (commandText.contains(PREFIX_PASSWORD.getPrefix())) {
commandTextToLog = Password.maskPassword(commandText);
}
logger.info("----------------[USER COMMAND][" + commandTextToLog + "]");

try {
Command command = addressBookParser.parseCommand(commandText);

if (!isPublicCommand(command) && !UserSession.isAuthenticated()) {
throw new CommandException(Messages.MESSAGE_COMMAND_FORBIDDEN);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public class DeregisterCommand extends Command {
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_ID + "azhikai";

public static final String MESSAGE_SUCCESS = "Account deregistered: %1$s!";
public static final String MESSAGE_USERNAME_NOT_FOUND = "This username does not exists.";
public static final String MESSAGE_SUCCESS = "Account deregistered: %1$s";
public static final String MESSAGE_USERNAME_NOT_FOUND = "This username does not exist";

private final Account account;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public class LoginCommand extends Command {
+ PREFIX_ID + "azhikai "
+ PREFIX_PASSWORD + "1122qq";

public static final String MESSAGE_SUCCESS = "Successfully logged in to '%s'!";
public static final String MESSAGE_ACCOUNT_NOT_FOUND = "The account does not exist.";
public static final String MESSAGE_WRONG_PASSWORD = "The credential is invalid.";
public static final String MESSAGE_ALREADY_AUTHENTICATED = "You are already logged in.";
public static final String MESSAGE_SUCCESS = "Successfully logged in to %s";
public static final String MESSAGE_ACCOUNT_NOT_FOUND = "The account does not exist";
public static final String MESSAGE_WRONG_PASSWORD = "The credential is invalid";
public static final String MESSAGE_ALREADY_AUTHENTICATED = "You are already logged in";

private final Account toLogin;

Expand All @@ -60,7 +60,7 @@ public CommandResult execute(Model model, CommandHistory history) throws Command
retrievedAccount.getPassword().toString().getBytes());

if (!isVerified) {
return new CommandResult(MESSAGE_WRONG_PASSWORD);
throw new CommandException(MESSAGE_WRONG_PASSWORD);
}

EventsCenter.getInstance().post(new LoginEvent(toLogin));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public class LogoutCommand extends Command {
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Logout of the system. "
+ "Example: " + COMMAND_WORD;

public static final String MESSAGE_SUCCESS = "You have been logged out.";
public static final String MESSAGE_NOT_AUTHENTICATED = "You are not logged in.";
public static final String MESSAGE_SUCCESS = "You have been logged out";
public static final String MESSAGE_NOT_AUTHENTICATED = "You are not logged in";

@Override
public CommandResult execute(Model model, CommandHistory history) throws CommandException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public class RegisterCommand extends Command {
+ PREFIX_ID + "azhikai "
+ PREFIX_PASSWORD + "1122qq";

public static final String MESSAGE_SUCCESS = "New account registered: %1$s!";
public static final String MESSAGE_DUPLICATE_USERNAME = "This username already exists.";
public static final String MESSAGE_SUCCESS = "New account registered: %1$s";
public static final String MESSAGE_DUPLICATE_USERNAME = "This username already exists";

private final Account account;

Expand Down
14 changes: 14 additions & 0 deletions src/main/java/seedu/address/model/accounts/Password.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.AppUtil.checkArgument;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PASSWORD;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
Expand All @@ -26,6 +27,8 @@ public class Password {
*/
private static final String PASSWORD_VALIDATION_REGEX = "[\\p{ASCII}&&[\\S]]{6,20}";

private static final String PASSWORD_MASK = "*****";

private static final int MAX_SALT_LENGTH = 16;

private String password;
Expand Down Expand Up @@ -75,6 +78,17 @@ public static boolean isHashed(String password) {
return password.contains("$2a$06$") && password.length() > 20;
}

/**
* Mask the password in the command
*
* @param commandText the command that contains the password.
* @return the commandText with the masked password.
*/
public static String maskPassword(String commandText) {
String[] splitCommandText = commandText.split(PREFIX_PASSWORD.getPrefix());
return splitCommandText[0] + PREFIX_PASSWORD + PASSWORD_MASK;
}

/**
* Hash the password.
*
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/seedu/address/ui/CommandBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ private void handleKeyPress(KeyEvent keyEvent) {
}

/**
* Updates the text field with the previous input in {@code historySnapshot},
* if there exists a previous input in {@code historySnapshot}
* Updates the text field with the previous input in {@code historySnapshot}, if there exists a previous input in
* {@code historySnapshot}
*/
private void navigateToPreviousInput() {
assert historySnapshot != null;
Expand All @@ -74,8 +74,8 @@ private void navigateToPreviousInput() {
}

/**
* Updates the text field with the next input in {@code historySnapshot},
* if there exists a next input in {@code historySnapshot}
* Updates the text field with the next input in {@code historySnapshot}, if there exists a next input in {@code
* historySnapshot}
*/
private void navigateToNextInput() {
assert historySnapshot != null;
Expand All @@ -87,8 +87,7 @@ private void navigateToNextInput() {
}

/**
* Sets {@code CommandBox}'s text field with {@code text} and
* positions the caret to the end of the {@code text}.
* Sets {@code CommandBox}'s text field with {@code text} and positions the caret to the end of the {@code text}.
*/
private void replaceText(String text) {
commandTextField.setText(text);
Expand All @@ -113,7 +112,7 @@ private void handleCommandEntered() {
initHistory();
// handle command failure
setStyleToIndicateCommandFailure();
logger.info("Invalid command: " + commandTextField.getText());
logger.info("Exception: " + e.getMessage());
raise(new NewResultAvailableEvent(e.getMessage()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ public void execute_loginIfAlreadyLoggedIn() throws CommandException {

@Test
public void execute_invalidPassword() throws CommandException {
Account invalidAccount = new AccountBuilder().withPassword("1122qq!@#123").build();
CommandResult commandResult = new LoginCommand(invalidAccount).execute(model, commandHistory);
thrown.expect(CommandException.class);
thrown.expectMessage(LoginCommand.MESSAGE_WRONG_PASSWORD);

assertEquals(LoginCommand.MESSAGE_WRONG_PASSWORD, commandResult.feedbackToUser);
assertEquals(EMPTY_COMMAND_HISTORY, commandHistory);
Account invalidAccount = new AccountBuilder().withPassword("1122qq!@#123").build();
new LoginCommand(invalidAccount).execute(model, commandHistory);
}

@Test
Expand Down

0 comments on commit 0d36a52

Please sign in to comment.