From 80bf7c5860f96633afc8201464d4a6895c705abd Mon Sep 17 00:00:00 2001 From: Lukas Karsch <lk224@hdm-stuttgart.de> Date: Sat, 13 Jan 2024 09:06:46 +0100 Subject: [PATCH] #45 - fixed API status codes in auth controller (swagger) --- .../growbros/auth/AuthenticationResponse.java | 1 - .../growbros/auth/AuthenticationService.java | 9 ++-- .../controllers/AuthenticationController.java | 43 +++++++------------ .../controllers/RestErrorHandler.java | 1 + .../EmailAlreadyExistsException.java | 6 +-- 5 files changed, 24 insertions(+), 36 deletions(-) diff --git a/src/main/java/hdm/mi/growbros/auth/AuthenticationResponse.java b/src/main/java/hdm/mi/growbros/auth/AuthenticationResponse.java index e0f1555..51786bf 100644 --- a/src/main/java/hdm/mi/growbros/auth/AuthenticationResponse.java +++ b/src/main/java/hdm/mi/growbros/auth/AuthenticationResponse.java @@ -11,6 +11,5 @@ import lombok.NoArgsConstructor; @NoArgsConstructor public class AuthenticationResponse { private String token; - private String error; private String message; } diff --git a/src/main/java/hdm/mi/growbros/auth/AuthenticationService.java b/src/main/java/hdm/mi/growbros/auth/AuthenticationService.java index 8aef84e..42e92e7 100644 --- a/src/main/java/hdm/mi/growbros/auth/AuthenticationService.java +++ b/src/main/java/hdm/mi/growbros/auth/AuthenticationService.java @@ -1,17 +1,18 @@ package hdm.mi.growbros.auth; -import hdm.mi.growbros.exceptions.*; +import hdm.mi.growbros.exceptions.EmailAlreadyExistsException; +import hdm.mi.growbros.exceptions.InvalidDataException; +import hdm.mi.growbros.exceptions.InvalidEmailException; +import hdm.mi.growbros.exceptions.UserNotFoundException; import hdm.mi.growbros.models.user.Role; import hdm.mi.growbros.models.user.User; import hdm.mi.growbros.repositories.UserRepository; import hdm.mi.growbros.security.JwtService; import lombok.RequiredArgsConstructor; import org.springframework.dao.DataIntegrityViolationException; -import org.springframework.http.HttpStatus; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; -import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; @@ -60,7 +61,7 @@ public class AuthenticationService { .token(jwtToken) .build(); } catch (DataIntegrityViolationException e) { - throw new EmailAlreadyExistsException(HttpStatus.BAD_REQUEST, "Email ist bereits registriert."); + throw new EmailAlreadyExistsException("Email ist bereits registriert."); } } diff --git a/src/main/java/hdm/mi/growbros/controllers/AuthenticationController.java b/src/main/java/hdm/mi/growbros/controllers/AuthenticationController.java index 19c14a5..3e31cc8 100644 --- a/src/main/java/hdm/mi/growbros/controllers/AuthenticationController.java +++ b/src/main/java/hdm/mi/growbros/controllers/AuthenticationController.java @@ -4,11 +4,10 @@ import hdm.mi.growbros.auth.AuthenticationRequest; import hdm.mi.growbros.auth.AuthenticationResponse; import hdm.mi.growbros.auth.AuthenticationService; import hdm.mi.growbros.auth.RegisterRequest; -import hdm.mi.growbros.exceptions.EmailAlreadyExistsException; -import hdm.mi.growbros.exceptions.GrowBrosException; -import io.swagger.annotations.ApiOperation; -import io.swagger.annotations.ApiResponse; -import io.swagger.annotations.ApiResponses; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -21,44 +20,32 @@ import org.springframework.web.bind.annotation.RestController; @RequestMapping("/api/v1/auth") @RequiredArgsConstructor public class AuthenticationController { - private final AuthenticationService authenticationService; @PostMapping("/register") - @ApiOperation(value = "Register user", response = AuthenticationResponse.class) + @Operation(description = "Register a new user") @ApiResponses(value = { - @ApiResponse(code = 200, message = "Successfully registered", response = AuthenticationResponse.class), - @ApiResponse(code = 409, message = "Email already exists"), - @ApiResponse(code = 500, message = "Internal Server Error") + @ApiResponse(responseCode = "200", description = "Successfully registered"), + @ApiResponse(responseCode = "400", description = "Invalid data", content = @Content(mediaType = "text/plain")), + @ApiResponse(responseCode = "409", description = "Email already exists", content = @Content(mediaType = "text/plain")) }) public ResponseEntity<AuthenticationResponse> register( @RequestBody RegisterRequest request ) { - try { - AuthenticationResponse response = authenticationService.register(request); - return new ResponseEntity<>(response, HttpStatus.OK); - } catch (EmailAlreadyExistsException e) { - return new ResponseEntity<>(HttpStatus.CONFLICT); - } catch (GrowBrosException e) { - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); - } + AuthenticationResponse response = authenticationService.register(request); + return new ResponseEntity<>(response, HttpStatus.OK); } @PostMapping("/authenticate") - @ApiOperation(value = "Authenticate user", response = AuthenticationResponse.class) + @Operation(description = "Authenticate user") @ApiResponses(value = { - @ApiResponse(code = 200, message = "Successfully authenticated", response = AuthenticationResponse.class), - @ApiResponse(code = 401, message = "Unauthorized"), - @ApiResponse(code = 500, message = "Internal Server Error") + @ApiResponse(responseCode = "200", description = "Successfully authenticated"), + @ApiResponse(responseCode = "401", description = "Unauthorized (wrong credentials)", content = @Content(mediaType = "text/plain")) }) public ResponseEntity<AuthenticationResponse> authenticate( @RequestBody AuthenticationRequest request ) { - try { - AuthenticationResponse response = authenticationService.authenticate(request); - return new ResponseEntity<>(response, HttpStatus.OK); - } catch (GrowBrosException e) { - return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); - } + AuthenticationResponse response = authenticationService.authenticate(request); + return new ResponseEntity<>(response, HttpStatus.OK); } } diff --git a/src/main/java/hdm/mi/growbros/controllers/RestErrorHandler.java b/src/main/java/hdm/mi/growbros/controllers/RestErrorHandler.java index 3098d48..4fcd1f2 100644 --- a/src/main/java/hdm/mi/growbros/controllers/RestErrorHandler.java +++ b/src/main/java/hdm/mi/growbros/controllers/RestErrorHandler.java @@ -25,6 +25,7 @@ public class RestErrorHandler extends ResponseEntityExceptionHandler { @ExceptionHandler(AccessDeniedException.class) protected ResponseEntity<String> handleAccessDeniedException() { + log.warn("handling access denied exception"); return ResponseEntity.status(401).body("Access denied: role not sufficient"); } diff --git a/src/main/java/hdm/mi/growbros/exceptions/EmailAlreadyExistsException.java b/src/main/java/hdm/mi/growbros/exceptions/EmailAlreadyExistsException.java index 800fc13..227eda7 100644 --- a/src/main/java/hdm/mi/growbros/exceptions/EmailAlreadyExistsException.java +++ b/src/main/java/hdm/mi/growbros/exceptions/EmailAlreadyExistsException.java @@ -2,8 +2,8 @@ package hdm.mi.growbros.exceptions; import org.springframework.http.HttpStatus; -public class EmailAlreadyExistsException extends GrowBrosException{ - public EmailAlreadyExistsException(HttpStatus httpStatus, String message) { - super(HttpStatus.BAD_REQUEST, message); +public class EmailAlreadyExistsException extends GrowBrosException { + public EmailAlreadyExistsException(String message) { + super(HttpStatus.CONFLICT, message); } } -- GitLab