-
Notifications
You must be signed in to change notification settings - Fork 43
fix(webauthn): apply PR #256 review fixes #258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
49 changes: 49 additions & 0 deletions
49
src/main/java/com/digitalsanctuary/spring/user/listener/WebAuthnPreDeleteEventListener.java
This file contains hidden or 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,49 @@ | ||
| package com.digitalsanctuary.spring.user.listener; | ||
|
|
||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
| import org.springframework.context.event.EventListener; | ||
| import org.springframework.stereotype.Component; | ||
| import com.digitalsanctuary.spring.user.event.UserPreDeleteEvent; | ||
| import com.digitalsanctuary.spring.user.persistence.model.WebAuthnUserEntity; | ||
| import com.digitalsanctuary.spring.user.persistence.repository.WebAuthnCredentialRepository; | ||
| import com.digitalsanctuary.spring.user.persistence.repository.WebAuthnUserEntityRepository; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| /** | ||
| * Listens for {@link UserPreDeleteEvent} and cleans up all WebAuthn data for the user being deleted. | ||
| * | ||
| * <p> | ||
| * Deletes all credentials associated with the user's WebAuthn entity, then deletes the entity itself. | ||
| * This listener is synchronous so that failures cause the enclosing transaction to roll back, preventing | ||
| * orphaned user accounts. | ||
| * </p> | ||
| */ | ||
| @Slf4j | ||
| @Component | ||
| @ConditionalOnProperty(name = "user.webauthn.enabled", havingValue = "true", matchIfMissing = false) | ||
| @RequiredArgsConstructor | ||
| public class WebAuthnPreDeleteEventListener { | ||
|
|
||
| private final WebAuthnCredentialRepository credentialRepository; | ||
| private final WebAuthnUserEntityRepository userEntityRepository; | ||
|
|
||
| /** | ||
| * Handle user pre-delete by removing all WebAuthn credentials and the user entity. | ||
| * | ||
| * @param event the user pre-delete event | ||
| */ | ||
| @EventListener | ||
| public void onUserPreDelete(UserPreDeleteEvent event) { | ||
| Long userId = event.getUserId(); | ||
| log.debug("Cleaning up WebAuthn data for user {}", userId); | ||
|
|
||
| userEntityRepository.findByUserId(userId).ifPresent(this::deleteUserEntityAndCredentials); | ||
| } | ||
|
|
||
| private void deleteUserEntityAndCredentials(WebAuthnUserEntity userEntity) { | ||
| credentialRepository.findByUserEntity(userEntity).forEach(credentialRepository::delete); | ||
| userEntityRepository.delete(userEntity); | ||
| log.info("Deleted WebAuthn data for user entity {}", userEntity.getName()); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
49 changes: 49 additions & 0 deletions
49
src/main/java/com/digitalsanctuary/spring/user/security/WebAuthnAuthenticationToken.java
This file contains hidden or 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,49 @@ | ||
| package com.digitalsanctuary.spring.user.security; | ||
|
|
||
| import java.util.Collection; | ||
| import org.springframework.security.authentication.AbstractAuthenticationToken; | ||
| import org.springframework.security.core.GrantedAuthority; | ||
| import org.springframework.security.core.userdetails.UserDetails; | ||
|
|
||
| /** | ||
| * Authentication token representing a successful WebAuthn/passkey authentication. | ||
| * | ||
| * <p> | ||
| * This token replaces the use of {@link org.springframework.security.authentication.UsernamePasswordAuthenticationToken} | ||
| * for WebAuthn logins, making it possible to distinguish passkey-based sessions from password-based sessions | ||
| * in security logic and audit trails. | ||
| * </p> | ||
| * | ||
| * <p> | ||
| * The principal is the application's {@link UserDetails} (typically {@code DSUserDetails}), and | ||
| * {@link #getCredentials()} returns {@code null} because no password is involved in WebAuthn authentication. | ||
| * </p> | ||
| */ | ||
| public class WebAuthnAuthenticationToken extends AbstractAuthenticationToken { | ||
|
|
||
| private static final long serialVersionUID = 1L; | ||
|
|
||
| private final UserDetails principal; | ||
|
|
||
| /** | ||
| * Creates a new WebAuthn authentication token. | ||
| * | ||
| * @param principal the authenticated user details | ||
| * @param authorities the granted authorities | ||
| */ | ||
| public WebAuthnAuthenticationToken(UserDetails principal, Collection<? extends GrantedAuthority> authorities) { | ||
| super(authorities); | ||
| this.principal = principal; | ||
| setAuthenticated(true); | ||
| } | ||
|
|
||
| @Override | ||
| public Object getCredentials() { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public Object getPrincipal() { | ||
| return principal; | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The concurrent-creation handling is unlikely to work reliably with JPA:
webAuthnUserEntityRepository.save(entity)typically does not flush immediately, so a unique-constraint conflict may surface at transaction commit rather than inside this try/catch, bypassing the retry path. To make the retry deterministic, force the insert to flush within the try block (e.g., viasaveAndFlush/flush) and consider running the retry lookup in a clean/new persistence context/transaction after a constraint violation.