Skip to content

Commit

Permalink
refactored logic in unblockAccount method and generating token
Browse files Browse the repository at this point in the history
  • Loading branch information
KizerovDmitriy committed Oct 14, 2024
1 parent 669f26c commit 4e72d6e
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/src/main/resources/messages_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ sincerely.yours.greenCity=Sincerely yours, Green City team.
sincerely.yours.Ubs=Sincerely yours, Pick Up City team.
unsubscribe.text=If you no longer wish to receive these emails, you can
unsubscribe=unsubscribe
unlock=Unlock account
unlock=Unblock account
block.account=Account blocked
text.account.ban=Your account has been locked for security reasons due to a possible hacking attempt. For your safety, we recommend changing your password after restoring your account.
warning=Warning!
Expand Down
6 changes: 6 additions & 0 deletions service-api/src/main/java/greencity/security/jwt/JwtTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,17 @@ public String generateTokenKeyWithCodedDate() {
*/
public String generateUnblockToken(String email) {
ClaimsBuilder claims = Jwts.claims().subject(email);
Date now = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(now);
calendar.add(Calendar.MONTH, 1);
return Jwts.builder()
.claims(claims.build())
.signWith(Keys.hmacShaKeyFor(
accessTokenKey.getBytes(StandardCharsets.UTF_8)),
Jwts.SIG.HS256)
.issuedAt(now)
.expiration(calendar.getTime())
.compact();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,11 @@ public interface LoginAttemptService {
* @return true if user is blocked, false otherwise.
*/
boolean isBlockedByWrongPassword(String email);

/**
* Deletes the given {@code email} from cache.
*
* @param email identifies the user.
*/
void deleteEmailFromCache(String email);
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,13 @@ public boolean isBlockedByWrongPassword(String email) {
return false;
}
}

/**
* {@inheritDoc}
*/
@Override
public void deleteEmailFromCache(String email) {
attemptsByCaptchaCache.invalidate(email);
attemptsByWrongPasswordCache.invalidate(email);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,8 @@ public void unblockAccount(String token) {
} catch (IllegalArgumentException e) {
throw new BadRequestException(ErrorMessage.TOKEN_FOR_RESTORE_IS_INVALID);
}
loginAttemptService.deleteEmailFromCache(email);

User user = userRepo.findByEmail(email)
.orElseThrow(() -> new NotFoundException(ErrorMessage.USER_NOT_FOUND_BY_EMAIL));
user.setUserStatus(UserStatus.ACTIVATED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,14 @@ void testIsBlockedByPasswordExecutionException() throws ExecutionException {

verify(attemptsByWrongPasswordCache).get("test@test.com");
}

@Test
void deleteEmailFromCaches() {
String email = "test@mail.com";

loginAttemptService.deleteEmailFromCache(email);

verify(attemptsByCaptchaCache).invalidate(email);
verify(attemptsByWrongPasswordCache).invalidate(email);
}
}

0 comments on commit 4e72d6e

Please sign in to comment.