Skip to content

Commit

Permalink
refactor: Updated project files
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladik-gif committed Sep 7, 2024
1 parent 489d13b commit 9974c26
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ public void changePassword(@Valid @RequestBody ChangePasswordDto request) {
changePasswordService.changePassword(request);
}

@Operation(summary = "Send email to restore password",
responses = {
@Operation(summary = "Send email to restore password", responses = {
@ApiResponse(responseCode = "200", description = SUCCESSFULLY_SEND_REQUEST_RESTORE_PASSWORD,
content = @Content),
@ApiResponse(responseCode = "400", description = ERR_SENDING_EMAIL,
Expand All @@ -53,8 +52,7 @@ public void sendRequestToRestorePassword(@RequestParam String email,
changePasswordService.sendEmailToRestorePassword(email, clientHost);
}

@Operation(summary = "Restore password",
responses = {
@Operation(summary = "Restore password", responses = {
@ApiResponse(responseCode = "200", description = SUCCESSFULLY_RESTORED_PASSWORD,
content = @Content),
@ApiResponse(responseCode = "404", description = EMAIL_TOKEN_NOT_FOUND,
Expand All @@ -64,4 +62,4 @@ public void sendRequestToRestorePassword(@RequestParam String email,
public void restorePassword(@Valid @RequestBody RestorePasswordDto restorePasswordDto) {
changePasswordService.restorePassword(restorePasswordDto);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ public void reportMessage(@PathVariable UUID id) {
messageService.reportMessageById(id);
}

@Operation(summary = "Mark a message as read",
responses = {
@Operation(summary = "Mark a message as read", responses = {
@ApiResponse(responseCode = "200", description = SUCCESSFULLY,
content = @Content),
@ApiResponse(responseCode = "400", description = MESSAGE_HAS_ALREADY_REPORTED,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@

public record EmailMessageDto(String to,
String subject,
String text
) {
}

String text) { }
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,4 @@ public record EmailMessageInfoDto(String username,
String email,
UUID uuidToken,
String path,
EmailMessageType emailMessageType) {

}
EmailMessageType emailMessageType) { }
12 changes: 5 additions & 7 deletions src/main/java/com/chat/yourway/security/JwtAuthFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
import com.chat.yourway.repository.redis.TokenRedisRepository;
import io.jsonwebtoken.JwtException;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.lang.NonNull;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
Expand All @@ -34,9 +33,10 @@ public class JwtAuthFilter extends OncePerRequestFilter {
private final HandlerExceptionResolver handlerExceptionResolver;

@Override
@SneakyThrows
protected void doFilterInternal(@NonNull HttpServletRequest request,
@NonNull HttpServletResponse response,
@NonNull FilterChain filterChain) throws ServletException, IOException {
@NonNull FilterChain filterChain) {

if (isNotAuthorizationHeader(request) && isNotTokenParameter(request)) {
log.warn("Request without authorization. Header or parameter does not contain {}", AUTHORIZATION);
Expand Down Expand Up @@ -75,8 +75,7 @@ private Boolean isTokenValid(String jwtToken, UserDetails userDetails) {
}

private void setAuthentication(UserDetails userDetails, HttpServletRequest request) {
var authToken = new UsernamePasswordAuthenticationToken(userDetails, null,
userDetails.getAuthorities());
var authToken = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
authToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
SecurityContextHolder.getContext().setAuthentication(authToken);
}
Expand All @@ -88,5 +87,4 @@ private boolean isNotAuthorizationHeader(HttpServletRequest request) {
private boolean isNotTokenParameter(HttpServletRequest request) {
return request.getParameter(AUTHORIZATION) == null;
}

}
}
1 change: 1 addition & 0 deletions src/main/java/com/chat/yourway/security/JwtService.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public String generateRefreshToken(UserDetails userDetails) {

public String extractToken(HttpServletRequest request) {
String token = request.getHeader(AUTHORIZATION);

if (token == null) {
token = request.getParameter(AUTHORIZATION);
}
Expand Down
18 changes: 11 additions & 7 deletions src/main/java/com/chat/yourway/security/TokenService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,24 @@ public void saveToken(Token token) {
}

public Token findByToken(String token) {
return tokenRedisRepository.findByToken(token)
.orElseThrow(() -> new TokenNotFoundException("Token wasn't found in repository"));
return tokenRedisRepository.findByToken(token).orElseThrow(
() -> new TokenNotFoundException("Token wasn't found in repository")
);
}

public void revokeAllContactTokens(Contact contact) {
var validUserTokens = tokenRedisRepository.findAllByEmail(contact.getEmail());

if (validUserTokens.isEmpty()) {
return;
}

validUserTokens.forEach(token -> {
token.setExpired(true);
token.setRevoked(true);
});
token.setExpired(true);
token.setRevoked(true);
}
);

tokenRedisRepository.saveAll(validUserTokens);
}

}
}

0 comments on commit 9974c26

Please sign in to comment.