Skip to content

Commit 25a89c8

Browse files
committed
fix: Add null validation to RegistrationContext and extract error code constant
- Add compact constructor to RegistrationContext requiring non-null source; email remains nullable since some OAuth2 providers don't expose it - Extract ERROR_CODE_REGISTRATION_DENIED constant in UserAPI to replace magic number 6
1 parent 47483e0 commit 25a89c8

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

src/main/java/com/digitalsanctuary/spring/user/api/UserAPI.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@
6565
@RequestMapping(path = "/user", produces = "application/json")
6666
public class UserAPI {
6767

68+
/** Error code returned when the {@link RegistrationGuard} denies a registration attempt. */
69+
private static final int ERROR_CODE_REGISTRATION_DENIED = 6;
70+
6871
private final UserService userService;
6972
private final UserEmailService userEmailService;
7073
private final MessageSource messages;
@@ -113,7 +116,7 @@ public ResponseEntity<JSONResponse> registerUserAccount(@Valid @RequestBody User
113116
new RegistrationContext(userDto.getEmail(), RegistrationSource.FORM, null));
114117
if (!decision.allowed()) {
115118
log.info("Registration denied for email: {} source: FORM reason: {}", userDto.getEmail(), decision.reason());
116-
return buildErrorResponse(decision.reason(), 6, HttpStatus.FORBIDDEN);
119+
return buildErrorResponse(decision.reason(), ERROR_CODE_REGISTRATION_DENIED, HttpStatus.FORBIDDEN);
117120
}
118121

119122
User registeredUser = userService.registerNewUserAccount(userDto);
@@ -411,7 +414,7 @@ public ResponseEntity<JSONResponse> registerPasswordlessAccount(@Valid @RequestB
411414
new RegistrationContext(dto.getEmail(), RegistrationSource.PASSWORDLESS, null));
412415
if (!decision.allowed()) {
413416
log.info("Registration denied for email: {} source: PASSWORDLESS reason: {}", dto.getEmail(), decision.reason());
414-
return buildErrorResponse(decision.reason(), 6, HttpStatus.FORBIDDEN);
417+
return buildErrorResponse(decision.reason(), ERROR_CODE_REGISTRATION_DENIED, HttpStatus.FORBIDDEN);
415418
}
416419

417420
User registeredUser = userService.registerPasswordlessAccount(dto);
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
package com.digitalsanctuary.spring.user.registration;
22

3+
import java.util.Objects;
4+
35
/**
46
* Immutable context passed to {@link RegistrationGuard#evaluate(RegistrationContext)}
57
* describing the registration attempt being evaluated.
68
*
7-
* @param email the email address of the user attempting to register
8-
* @param source the registration path (form, passwordless, OAuth2, or OIDC)
9+
* @param email the email address of the user attempting to register; may be {@code null}
10+
* if the OAuth2/OIDC provider does not expose the user's email
11+
* @param source the registration path (form, passwordless, OAuth2, or OIDC); never {@code null}
912
* @param providerName the OAuth2/OIDC provider registration ID (e.g. {@code "google"},
1013
* {@code "keycloak"}), or {@code null} for form/passwordless registrations
1114
*/
1215
public record RegistrationContext(String email, RegistrationSource source, String providerName) {
16+
17+
public RegistrationContext {
18+
Objects.requireNonNull(source, "source must not be null");
19+
}
1320
}

0 commit comments

Comments
 (0)