-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Mykhailo Marchuk <[email protected]>
- Loading branch information
1 parent
0d84e8e
commit 95b41ae
Showing
60 changed files
with
859 additions
and
770 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
src/main/java/com/booking/app/annotation/GlobalApiResponses.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.booking.app.annotation; | ||
|
||
import com.booking.app.exception.ErrorDetails; | ||
import io.swagger.v3.oas.annotations.media.Content; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
import org.springframework.http.MediaType; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Custom annotation to define global API responses for server errors. | ||
* <p> | ||
* This annotation can be applied to both classes and methods to include | ||
* standard responses for various 5xx server error status codes in the Swagger documentation. | ||
* | ||
* <p>It defines the following responses: | ||
* <ul> | ||
* <li>500 Internal Server Error</li> | ||
* <li>501 Not Implemented</li> | ||
* <li>502 Bad Gateway</li> | ||
* <li>503 Service Unavailable</li> | ||
* <li>504 Gateway Timeout</li> | ||
* </ul> | ||
* | ||
* <p>Example usage: | ||
* <pre> | ||
* {@code | ||
* @RestController | ||
* @GlobalApiResponses | ||
* public class MyController { | ||
* | ||
* @GetMapping("/example") | ||
* public String exampleEndpoint() { | ||
* return "Example response"; | ||
* } | ||
* } | ||
* } | ||
* </pre> | ||
*/ | ||
@Target({ElementType.TYPE, ElementType.METHOD}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "500", description = "Internal Server Error", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = ErrorDetails.class))), | ||
@ApiResponse(responseCode = "501", description = "Not Implemented", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = ErrorDetails.class))), | ||
@ApiResponse(responseCode = "502", description = "Bad Gateway", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = ErrorDetails.class))), | ||
@ApiResponse(responseCode = "503", description = "Service Unavailable", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = ErrorDetails.class))), | ||
@ApiResponse(responseCode = "504", description = "Gateway Timeout", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = ErrorDetails.class))) | ||
}) | ||
public @interface GlobalApiResponses { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/com/booking/app/constant/ApiMessagesConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.booking.app.constant; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class ApiMessagesConstants { | ||
public static final String USER_HAS_BEEN_DELETED_MESSSAGE = "User was deleted successfully"; | ||
public static final String UNAUTHENTICATED_MESSAGE = "Full authentication is required to access this resource"; | ||
public static final String INVALID_CLIENT_PROVIDER_ID_MESSAGE = "Client ID is not right"; | ||
public static final String USER_HAS_BEEN_AUTHENTICATED_MESSAGE = "User has been authenticated"; | ||
public static final String NO_SOCIAL_IDENTITY_PROVIDERS_MESSAGE = "Illegal Social Identity Providers was chosen"; | ||
public static final String INVALID_REQUEST_BODY_FORMAT_OR_VALIDATION_ERROR_MESSAGE = "Invalid request body format OR validation error"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/main/java/com/booking/app/controller/CookieManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.booking.app.controller; | ||
|
||
import com.booking.app.util.CookieUtils; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
|
||
import static com.booking.app.constant.CustomHttpHeaders.REMEMBER_ME; | ||
import static com.booking.app.constant.CustomHttpHeaders.USER_ID; | ||
import static com.booking.app.constant.JwtTokenConstants.REFRESH_TOKEN; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class CookieManager { | ||
|
||
/** | ||
* Deletes cookies related to user authentication. | ||
* | ||
* @param request the HTTP request | ||
* @param response the HTTP response | ||
*/ | ||
public static void deleteCookies(HttpServletRequest request, HttpServletResponse response) { | ||
CookieUtils.deleteCookie(request, response, REFRESH_TOKEN); | ||
CookieUtils.deleteCookie(request, response, USER_ID); | ||
CookieUtils.deleteCookie(request, response, REMEMBER_ME); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 15 additions & 19 deletions
34
src/main/java/com/booking/app/controller/LogoutController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,39 @@ | ||
package com.booking.app.controller; | ||
|
||
import com.booking.app.util.CookieUtils; | ||
import com.booking.app.annotation.GlobalApiResponses; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.media.Content; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import static com.booking.app.constant.CustomHttpHeaders.REMEMBER_ME; | ||
import static com.booking.app.constant.CustomHttpHeaders.USER_ID; | ||
import static com.booking.app.constant.JwtTokenConstants.REFRESH_TOKEN; | ||
|
||
/** | ||
* Controller handling user logout functionality. | ||
*/ | ||
@RestController | ||
@RequestMapping | ||
@RequiredArgsConstructor | ||
@Tag(name = "Log out", description = "Log out authenticated user") | ||
@GlobalApiResponses | ||
public class LogoutController { | ||
|
||
/** | ||
* Handles HTTP GET request to '/logout' | ||
* | ||
* @return ResponseEntity with an HTTP status representing successful logout | ||
*/ | ||
@GetMapping("/sign-out") | ||
@Operation(summary = "Log out a user") | ||
@ApiResponse(responseCode = "200", description = "Successfully logged") | ||
public ResponseEntity<?> logout(HttpServletRequest request, HttpServletResponse response) { | ||
CookieUtils.deleteCookie(request, response, REFRESH_TOKEN); | ||
CookieUtils.deleteCookie(request, response, USER_ID); | ||
CookieUtils.deleteCookie(request, response, REMEMBER_ME); | ||
return ResponseEntity.ok().build(); | ||
@GetMapping("/auth/sign-out") | ||
@Operation(summary = "Log out a user", | ||
description = "Logs out the authenticated user by clearing the authentication cookies", | ||
responses = { | ||
@ApiResponse(responseCode = "200", | ||
description = "Successfully logged out", | ||
content = @Content(schema = @Schema(hidden = true))) | ||
} | ||
) | ||
public void logout(HttpServletRequest request, HttpServletResponse response) { | ||
CookieManager.deleteCookies(request, response); | ||
} | ||
|
||
} |
Oops, something went wrong.