-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathWebAuthnManagementAPIAdvice.java
More file actions
46 lines (40 loc) · 2.21 KB
/
Copy pathWebAuthnManagementAPIAdvice.java
File metadata and controls
46 lines (40 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.digitalsanctuary.spring.user.api;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import com.digitalsanctuary.spring.user.exceptions.WebAuthnException;
import com.digitalsanctuary.spring.user.exceptions.WebAuthnUserNotFoundException;
import com.digitalsanctuary.spring.user.util.GenericResponse;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
/**
* Centralized exception handling for WebAuthn credential management endpoints.
*/
@RestControllerAdvice(assignableTypes = WebAuthnManagementAPI.class)
@ConditionalOnProperty(name = "user.webauthn.enabled", havingValue = "true", matchIfMissing = false)
@Slf4j
public class WebAuthnManagementAPIAdvice {
@ExceptionHandler(WebAuthnUserNotFoundException.class)
public ResponseEntity<GenericResponse> handleUserNotFound(WebAuthnUserNotFoundException ex) {
log.warn("WebAuthn user not found: {}", ex.getMessage());
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new GenericResponse(ex.getMessage()));
}
@ExceptionHandler(WebAuthnException.class)
public ResponseEntity<GenericResponse> handleWebAuthnError(WebAuthnException ex) {
log.warn("WebAuthn error: {}", ex.getMessage());
return ResponseEntity.badRequest().body(new GenericResponse(ex.getMessage()));
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<GenericResponse> handleValidation(MethodArgumentNotValidException ex) {
log.warn("WebAuthn validation error: {}", ex.getMessage());
return ResponseEntity.badRequest().body(new GenericResponse(ex.getBindingResult().getAllErrors(), "Validation failed"));
}
@ExceptionHandler(ConstraintViolationException.class)
public ResponseEntity<GenericResponse> handleConstraintViolation(ConstraintViolationException ex) {
log.warn("WebAuthn constraint violation: {}", ex.getMessage());
return ResponseEntity.badRequest().body(new GenericResponse("Validation failed"));
}
}