diff --git a/src/main/java/com/epam/esm/filter/ServiceAuthenticationFilter.java b/src/main/java/com/epam/esm/filter/ServiceAuthenticationFilter.java index e8d9c88..66c6a9e 100644 --- a/src/main/java/com/epam/esm/filter/ServiceAuthenticationFilter.java +++ b/src/main/java/com/epam/esm/filter/ServiceAuthenticationFilter.java @@ -1,7 +1,7 @@ package com.epam.esm.filter; -import com.epam.esm.model.UserDTO; +import com.epam.esm.model.AuthenticatedUser; import com.epam.esm.utils.openfeign.AuthFeignClient; import jakarta.servlet.FilterChain; import jakarta.servlet.ServletException; @@ -40,7 +40,7 @@ protected void doFilterInternal(@NonNull HttpServletRequest request, return; } try { - UserDTO user = authClient.getUserFromJwt(bearerToken).getBody(); + AuthenticatedUser user = authClient.getAuthenticatedUserFromJwt(bearerToken).getBody(); UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(user, null, Collections.singleton(new SimpleGrantedAuthority(user.getRole().name()))); diff --git a/src/main/java/com/epam/esm/model/UserDTO.java b/src/main/java/com/epam/esm/model/AuthenticatedUser.java similarity index 68% rename from src/main/java/com/epam/esm/model/UserDTO.java rename to src/main/java/com/epam/esm/model/AuthenticatedUser.java index df1ebc7..4544ffe 100644 --- a/src/main/java/com/epam/esm/model/UserDTO.java +++ b/src/main/java/com/epam/esm/model/AuthenticatedUser.java @@ -8,11 +8,8 @@ @ToString @NoArgsConstructor @AllArgsConstructor -public class UserDTO { +public class AuthenticatedUser { private Long id; - private String name; - private String surname; - private String phone; private String email; private Provider provider; private Role role; diff --git a/src/main/java/com/epam/esm/utils/openfeign/AuthFeignClient.java b/src/main/java/com/epam/esm/utils/openfeign/AuthFeignClient.java index 2c91fbf..c38a265 100644 --- a/src/main/java/com/epam/esm/utils/openfeign/AuthFeignClient.java +++ b/src/main/java/com/epam/esm/utils/openfeign/AuthFeignClient.java @@ -1,6 +1,6 @@ package com.epam.esm.utils.openfeign; -import com.epam.esm.model.UserDTO; +import com.epam.esm.model.AuthenticatedUser; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; @@ -9,5 +9,5 @@ @FeignClient(name = "auth-service", configuration = CustomFeignClientConfiguration.class) public interface AuthFeignClient { @GetMapping("api/v1/auth/user") - ResponseEntity getUserFromJwt(@RequestHeader(value = "Authorization") String authorizationHeader); + ResponseEntity getAuthenticatedUserFromJwt(@RequestHeader(value = "Authorization") String authorizationHeader); } \ No newline at end of file diff --git a/src/test/java/com/epam/esm/filter/ServiceAuthenticationFilterTest.java b/src/test/java/com/epam/esm/filter/ServiceAuthenticationFilterTest.java index 1fbb426..0b5e9f6 100644 --- a/src/test/java/com/epam/esm/filter/ServiceAuthenticationFilterTest.java +++ b/src/test/java/com/epam/esm/filter/ServiceAuthenticationFilterTest.java @@ -1,7 +1,7 @@ package com.epam.esm.filter; import com.epam.esm.model.Role; -import com.epam.esm.model.UserDTO; +import com.epam.esm.model.AuthenticatedUser; import com.epam.esm.utils.openfeign.AuthFeignClient; import jakarta.servlet.FilterChain; import jakarta.servlet.ServletException; @@ -57,17 +57,17 @@ void testFilterInvalidBearerToken() throws ServletException, IOException { @Test void testFilterValidBearerTokenSuccessfulAuthentication() throws ServletException, IOException { - String validToken = "Bearer BearerValidToken"; // Replace with a valid token format + String validToken = "Bearer BearerValidToken"; when(request.getHeader(HttpHeaders.AUTHORIZATION)).thenReturn(validToken); - UserDTO userDTO = new UserDTO(); + AuthenticatedUser userDTO = new AuthenticatedUser(); userDTO.setRole(Role.USER); - when(authClient.getUserFromJwt(validToken)).thenReturn(ResponseEntity.ok(userDTO)); + when(authClient.getAuthenticatedUserFromJwt(validToken)).thenReturn(ResponseEntity.ok(userDTO)); authenticationFilter.doFilterInternal(request, response, filterChain); assertEquals(userDTO, SecurityContextHolder.getContext().getAuthentication().getPrincipal()); - UserDTO authUser = (UserDTO) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); + AuthenticatedUser authUser = (AuthenticatedUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); assertEquals(Role.USER, authUser.getRole()); verify(filterChain).doFilter(request, response); } @@ -77,7 +77,7 @@ void testFilterAuthenticationErrorHttpClientErrorException() throws ServletExcep String validToken = "Bearer BearerValidToken"; when(request.getHeader(HttpHeaders.AUTHORIZATION)).thenReturn(validToken); - when(authClient.getUserFromJwt(validToken)) + when(authClient.getAuthenticatedUserFromJwt(validToken)) .thenThrow(new HttpClientErrorException(HttpStatus.UNAUTHORIZED)); PrintWriter writer = mock(PrintWriter.class); @@ -92,10 +92,10 @@ void testFilterAuthenticationErrorHttpClientErrorException() throws ServletExcep @Test void testFilterAuthenticationErrorHttpServerErrorException() throws ServletException, IOException { - String validToken = "Bearer BearerValidToken"; // Replace with a valid token format + String validToken = "Bearer BearerValidToken"; when(request.getHeader(HttpHeaders.AUTHORIZATION)).thenReturn(validToken); - when(authClient.getUserFromJwt(validToken)) + when(authClient.getAuthenticatedUserFromJwt(validToken)) .thenThrow(new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR)); PrintWriter writer = mock(PrintWriter.class);