111 lines
5.6 KiB
Java
111 lines
5.6 KiB
Java
package SuMoC.backend.security.controller;
|
|
|
|
|
|
import SuMoC.backend.security.dtos.*;
|
|
import SuMoC.backend.security.exceptions.InvalidTokenException;
|
|
import SuMoC.backend.security.services.AuthenticationService;
|
|
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.responses.ApiResponses;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.mail.MailException;
|
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
@RestController
|
|
@RequestMapping("/auth/user")
|
|
public class UserAuthenticationController {
|
|
|
|
private AuthenticationService authenticationService;
|
|
|
|
@Autowired
|
|
public UserAuthenticationController(AuthenticationService authenticationService) {
|
|
this.authenticationService = authenticationService;
|
|
}
|
|
|
|
/**
|
|
* Registriert einen neuen Benutzer
|
|
*
|
|
* @param registrationDTO Daten des zu registrierenden Benutzers
|
|
* @return UserDTO mit den registrierten Benutzerdaten
|
|
*/
|
|
@Operation(summary = "Registriert einen neuen Benutzer", description = "Erstellt einen neuen Benutzer und gibt die Benutzerdaten zurück")
|
|
@ApiResponses(value = {
|
|
@ApiResponse(responseCode = "200", description = "Benutzer erfolgreich registriert",
|
|
content = @Content(mediaType = "application/json", schema = @Schema(implementation = RegistrationResponseDTO.class))),
|
|
@ApiResponse(responseCode = "400", description = "Ungültige Eingabedaten",
|
|
content = @Content(mediaType = "application/json"))
|
|
})
|
|
@PostMapping("/registration")
|
|
public ResponseEntity<RegistrationResponseDTO> registerUser(@RequestBody UserRegistrationDTO registrationDTO) {
|
|
|
|
System.out.println("###################### Register Request #####################");
|
|
System.out.println(registrationDTO.getPublicName() + " " + registrationDTO.getPrivateName() + " " + registrationDTO.getPassword() + " " + registrationDTO.getEmail());
|
|
return new ResponseEntity<>(authenticationService.registerUser(registrationDTO), HttpStatus.OK);
|
|
}
|
|
|
|
/**
|
|
* Authentifiziert einen Benutzer und gibt ein JWT zurück
|
|
*
|
|
* @param auth Anmeldedaten des Benutzers
|
|
* @return LoginResponseDTO mit JWT und Benutzerdaten
|
|
*/
|
|
@Operation(summary = "Authentifiziert einen Benutzer", description = "Prüft den Benutzernamen und das Passwort und gibt ein JWT zurück")
|
|
@ApiResponses(value = {
|
|
@ApiResponse(responseCode = "200", description = "Erfolgreiche Anmeldung",
|
|
content = @Content(mediaType = "application/json", schema = @Schema(implementation = LoginResponseDTO.class))),
|
|
@ApiResponse(responseCode = "401", description = "Ungültiger Benutzername oder Passwort",
|
|
content = @Content(mediaType = "application/json"))
|
|
})
|
|
@PostMapping("/login")
|
|
public ResponseEntity<LoginResponseDTO> login(@RequestBody UserAuthenticationPairDTO auth) {
|
|
LoginResponseDTO responseDTO = authenticationService.login(auth);
|
|
|
|
if (responseDTO.getUserDTO() == null) {
|
|
return new ResponseEntity<>(responseDTO, HttpStatus.UNAUTHORIZED);
|
|
}
|
|
|
|
return new ResponseEntity<>(responseDTO, HttpStatus.OK);
|
|
}
|
|
|
|
@Operation(summary = "OTP Code zur änderung des Passworts", description = "Erstellt bei Anfrage einen neuen 6-stelligen OTP Code, " +
|
|
"mit welchem das Passwort des Accounts mit der zugehörigen Email einmalig verändert werden kann." +
|
|
" Dieser wird per Mail an den Nutzer gesendet.")
|
|
@PostMapping("/request-otp-for-password-reset")
|
|
public ResponseEntity<String> requestOtpForPasswordReset(@RequestBody OtpRequestDTO otpRequestDTO) {
|
|
|
|
try{
|
|
authenticationService.generateNewPasswordResetTokenForUser(otpRequestDTO.getEmail());
|
|
return ResponseEntity.ok("OTP erstellt");
|
|
} catch (UsernameNotFoundException e) {
|
|
return ResponseEntity.badRequest().body(e.getMessage());
|
|
} catch(MailException e){
|
|
System.out.println(" ## MAIL EXCEPTION \n" +e.getMessage());
|
|
return ResponseEntity.internalServerError().body("Fehler beim versenden der Email, versuche es später erneut");
|
|
} catch (Exception e) {
|
|
return ResponseEntity.internalServerError().body("Fehler beim erstellen des OTP");
|
|
}
|
|
}
|
|
|
|
@Operation(summary = "Veränderung des Passworts eines Profils ", description = "Durch Eingabe eines Validen OTP Codes " +
|
|
"zu einer zugehörigen Email kann das Passwort des Profils geändert werden.")
|
|
@PostMapping("/reset-password")
|
|
public ResponseEntity<String> resetPassword(@RequestBody PasswordResetDTO passwordResetDTO){
|
|
try {
|
|
authenticationService.resetPassword(passwordResetDTO);
|
|
return ResponseEntity.ok("Passwort erfolgreich geändert");
|
|
} catch (InvalidTokenException | UsernameNotFoundException e) {
|
|
return ResponseEntity.badRequest().body(e.getMessage());
|
|
} catch (Exception e) {
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Fehler beim Zurücksetzen des Passworts");
|
|
}
|
|
}
|
|
}
|