@Test @WithMockUser(value = USERNAME, roles = ROLE_SU) void testUserIsBlocked() { User molgenisUser = mock(User.class); UserSecret userSecret = mock(UserSecret.class); when(molgenisUser.getId()).thenReturn("1324"); when(userService.getUser(USERNAME)).thenReturn(molgenisUser); Query<UserSecret> userSecretQuery = mock(Query.class, RETURNS_SELF); when(dataService.query(UserSecretMetadata.USER_SECRET, UserSecret.class)) .thenReturn(userSecretQuery); when(userSecretQuery.eq(UserSecretMetadata.USER_ID, molgenisUser.getId()).findOne()) .thenReturn(userSecret); when(userSecret.getLastFailedAuthentication()).thenReturn(Instant.now()); when(userSecret.getFailedLoginAttempts()).thenReturn(3); assertThrows( TooManyLoginAttemptsException.class, () -> twoFactorAuthenticationServiceImpl.userIsBlocked()); }
@Override public boolean userIsBlocked() { UserSecret userSecret = getSecret(); if (userSecret.getFailedLoginAttempts() >= MAX_FAILED_LOGIN_ATTEMPTS && userSecret.getLastFailedAuthentication() != null && (Instant.now().toEpochMilli() < userSecret .getLastFailedAuthentication() .plus(Duration.ofSeconds(BLOCKED_USER_INTERVAL)) .toEpochMilli())) { throw new TooManyLoginAttemptsException( format( "You entered the wrong verification code {0} times, please wait for {1} seconds before you try again", MAX_FAILED_LOGIN_ATTEMPTS, BLOCKED_USER_INTERVAL)); } return false; }
TwoFactorAuthenticationServiceImpl implements TwoFactorAuthenticationService { @Override public boolean userIsBlocked() { UserSecret userSecret = getSecret(); if (userSecret.getFailedLoginAttempts() >= MAX_FAILED_LOGIN_ATTEMPTS && userSecret.getLastFailedAuthentication() != null && (Instant.now().toEpochMilli() < userSecret .getLastFailedAuthentication() .plus(Duration.ofSeconds(BLOCKED_USER_INTERVAL)) .toEpochMilli())) { throw new TooManyLoginAttemptsException( format( "You entered the wrong verification code {0} times, please wait for {1} seconds before you try again", MAX_FAILED_LOGIN_ATTEMPTS, BLOCKED_USER_INTERVAL)); } return false; } }
TwoFactorAuthenticationServiceImpl implements TwoFactorAuthenticationService { @Override public boolean userIsBlocked() { UserSecret userSecret = getSecret(); if (userSecret.getFailedLoginAttempts() >= MAX_FAILED_LOGIN_ATTEMPTS && userSecret.getLastFailedAuthentication() != null && (Instant.now().toEpochMilli() < userSecret .getLastFailedAuthentication() .plus(Duration.ofSeconds(BLOCKED_USER_INTERVAL)) .toEpochMilli())) { throw new TooManyLoginAttemptsException( format( "You entered the wrong verification code {0} times, please wait for {1} seconds before you try again", MAX_FAILED_LOGIN_ATTEMPTS, BLOCKED_USER_INTERVAL)); } return false; } TwoFactorAuthenticationServiceImpl(
      OtpService otpService,
      DataService dataService,
      UserService userService,
      IdGenerator idGenerator,
      UserSecretFactory userSecretFactory); }
TwoFactorAuthenticationServiceImpl implements TwoFactorAuthenticationService { @Override public boolean userIsBlocked() { UserSecret userSecret = getSecret(); if (userSecret.getFailedLoginAttempts() >= MAX_FAILED_LOGIN_ATTEMPTS && userSecret.getLastFailedAuthentication() != null && (Instant.now().toEpochMilli() < userSecret .getLastFailedAuthentication() .plus(Duration.ofSeconds(BLOCKED_USER_INTERVAL)) .toEpochMilli())) { throw new TooManyLoginAttemptsException( format( "You entered the wrong verification code {0} times, please wait for {1} seconds before you try again", MAX_FAILED_LOGIN_ATTEMPTS, BLOCKED_USER_INTERVAL)); } return false; } TwoFactorAuthenticationServiceImpl(
      OtpService otpService,
      DataService dataService,
      UserService userService,
      IdGenerator idGenerator,
      UserSecretFactory userSecretFactory); @Override boolean isVerificationCodeValidForUser(String verificationCode); @Override boolean userIsBlocked(); @Override void saveSecretForUser(String secret); @Override void resetSecretForUser(); @Override void enableForUser(); @Override void disableForUser(); @Override String generateSecretKey(); @Override boolean isConfiguredForUser(); }
TwoFactorAuthenticationServiceImpl implements TwoFactorAuthenticationService { @Override public boolean userIsBlocked() { UserSecret userSecret = getSecret(); if (userSecret.getFailedLoginAttempts() >= MAX_FAILED_LOGIN_ATTEMPTS && userSecret.getLastFailedAuthentication() != null && (Instant.now().toEpochMilli() < userSecret .getLastFailedAuthentication() .plus(Duration.ofSeconds(BLOCKED_USER_INTERVAL)) .toEpochMilli())) { throw new TooManyLoginAttemptsException( format( "You entered the wrong verification code {0} times, please wait for {1} seconds before you try again", MAX_FAILED_LOGIN_ATTEMPTS, BLOCKED_USER_INTERVAL)); } return false; } TwoFactorAuthenticationServiceImpl(
      OtpService otpService,
      DataService dataService,
      UserService userService,
      IdGenerator idGenerator,
      UserSecretFactory userSecretFactory); @Override boolean isVerificationCodeValidForUser(String verificationCode); @Override boolean userIsBlocked(); @Override void saveSecretForUser(String secret); @Override void resetSecretForUser(); @Override void enableForUser(); @Override void disableForUser(); @Override String generateSecretKey(); @Override boolean isConfiguredForUser();  }