diff --git a/src/main/java/hs/fulda/de/ci/exam/project/Account.java b/src/main/java/hs/fulda/de/ci/exam/project/Account.java index b5bbaa8..7097e7e 100644 --- a/src/main/java/hs/fulda/de/ci/exam/project/Account.java +++ b/src/main/java/hs/fulda/de/ci/exam/project/Account.java @@ -33,6 +33,15 @@ public class Account { this.status = status; } + public boolean isEnabled() { + if(status == AccountStatus.ACTIVE) return true; + return false; + } + + public String getPasswordHash() { + return new PasswordEncoder().encode(password); + } + public enum AccountStatus { ACTIVE, CLOSED, @@ -67,4 +76,17 @@ public class Account { } } +class PasswordEncoder{ + String encode(String password){ + int p = 31; + int m = (int) Math.pow(10, 9) + 9; + int hash_value = 0; + int p_pow = 1; + for (char c : password.toCharArray()) { + hash_value = (hash_value + (c - 'a' + 1) * p_pow) % m; + p_pow = (p_pow * p) % m; + } + return Integer.toString(hash_value); + } +} diff --git a/src/main/java/hs/fulda/de/ci/exam/project/AccountRepository.java b/src/main/java/hs/fulda/de/ci/exam/project/AccountRepository.java index db9d3e8..90a96db 100644 --- a/src/main/java/hs/fulda/de/ci/exam/project/AccountRepository.java +++ b/src/main/java/hs/fulda/de/ci/exam/project/AccountRepository.java @@ -14,6 +14,11 @@ public class AccountRepository { } return false; } + Account findById(String id){ + Account account = accountList.get(id); + return account; + } + private String generateKey(Account account) { return String.format("%s", account.getId()); } diff --git a/src/main/java/hs/fulda/de/ci/exam/project/AccountService.java b/src/main/java/hs/fulda/de/ci/exam/project/AccountService.java new file mode 100644 index 0000000..789ecba --- /dev/null +++ b/src/main/java/hs/fulda/de/ci/exam/project/AccountService.java @@ -0,0 +1,24 @@ +package hs.fulda.de.ci.exam.project; + +public class AccountService { + private final AccountRepository accountRepository; + private final PasswordEncoder passwordEncoder; + + public AccountService(AccountRepository accountRepository, PasswordEncoder passwordEncoder) { + this.accountRepository = accountRepository; + this.passwordEncoder = passwordEncoder; + } + public boolean isValidAccount(String id, String password){ + Account account = accountRepository.findById(id); + return isEnabledAccount(account) && isValidPassword(account, password); + } + + private boolean isEnabledAccount(Account account) { + return account!= null && account.isEnabled(); + } + + private boolean isValidPassword(Account account, String password) { + String encodedPassword = passwordEncoder.encode(password); + return encodedPassword.equals(account.getPasswordHash()); + } +} diff --git a/src/test/java/hs/fulda/de/ci/exam/project/AccountServiceTest.java b/src/test/java/hs/fulda/de/ci/exam/project/AccountServiceTest.java new file mode 100644 index 0000000..c9f1bdc --- /dev/null +++ b/src/test/java/hs/fulda/de/ci/exam/project/AccountServiceTest.java @@ -0,0 +1,35 @@ +package hs.fulda.de.ci.exam.project; + +import org.junit.Before; + +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class AccountServiceTest { + private static final String PASSWORD = "password"; + + private static final Account ENABLED_USER = + new Account("user id", "hash", Account.AccountStatus.ACTIVE); + + private static final Account DISABLED_USER = + new Account("disabled user id", "disabled user password hash", Account.AccountStatus.CLOSED); + + private AccountRepository accountRepository; + private PasswordEncoder passwordEncoder; + private AccountService accountService; + + @Before + public void setup() { + passwordEncoder = createPasswordEncoder(); + accountService = new AccountService(accountRepository, passwordEncoder); + } + + private PasswordEncoder createPasswordEncoder() { + PasswordEncoder mock = mock(PasswordEncoder.class); + when(mock.encode(anyString())).thenReturn("any password hash"); + when(mock.encode(PASSWORD)).thenReturn(ENABLED_USER.getPasswordHash()); + return mock; + } + +}