-
Notifications
You must be signed in to change notification settings - Fork 44
fix(auth): publish auth event from authWithoutPassword + add DevLoginAutoConfiguration #262
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 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6132ea6
fix(auth): publish InteractiveAuthenticationSuccessEvent from authWit…
devondragon 5d785a8
feat(dev): add DevLoginAutoConfiguration for local development
devondragon ad8ace7
docs: document dev login feature and authWithoutPassword event fix
devondragon 74cb1ee
fix(review): address PR #262 review feedback
devondragon 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
35 changes: 35 additions & 0 deletions
35
src/main/java/com/digitalsanctuary/spring/user/dev/DevLoginConfigProperties.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,35 @@ | ||
| package com.digitalsanctuary.spring.user.dev; | ||
|
|
||
| import org.springframework.boot.context.properties.ConfigurationProperties; | ||
| import org.springframework.context.annotation.PropertySource; | ||
| import org.springframework.stereotype.Component; | ||
| import lombok.Data; | ||
|
|
||
| /** | ||
| * Configuration properties for the dev login feature. | ||
| * <p> | ||
| * This enables a quick "login as" endpoint for local development, removing the need | ||
| * for consuming applications to write boilerplate dev-login controllers. | ||
| * </p> | ||
| * <p> | ||
| * <strong>SECURITY WARNING:</strong> This feature should only be enabled in local/dev | ||
| * environments. It allows authentication without a password via a simple GET request. | ||
| * </p> | ||
| */ | ||
| @Data | ||
| @Component | ||
| @PropertySource("classpath:config/dsspringuserconfig.properties") | ||
| @ConfigurationProperties(prefix = "user.dev") | ||
| public class DevLoginConfigProperties { | ||
|
|
||
| /** | ||
| * Whether the dev auto-login feature is enabled. Defaults to false. | ||
| * Must be explicitly set to true AND the "local" profile must be active. | ||
| */ | ||
| private boolean autoLoginEnabled = false; | ||
|
|
||
| /** | ||
| * The URL to redirect to after a successful dev login. Defaults to "/". | ||
| */ | ||
| private String loginRedirectUrl = "/"; | ||
| } |
94 changes: 94 additions & 0 deletions
94
src/main/java/com/digitalsanctuary/spring/user/dev/DevLoginController.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,94 @@ | ||
| package com.digitalsanctuary.spring.user.dev; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
| import org.springframework.context.annotation.Profile; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
| import com.digitalsanctuary.spring.user.persistence.model.User; | ||
| import com.digitalsanctuary.spring.user.persistence.repository.UserRepository; | ||
| import com.digitalsanctuary.spring.user.service.UserService; | ||
| import com.digitalsanctuary.spring.user.util.JSONResponse; | ||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| /** | ||
| * Development-only controller providing quick login-as functionality. | ||
| * <p> | ||
| * This controller is only active when the "local" Spring profile is active AND | ||
| * {@code user.dev.auto-login-enabled} is set to {@code true}. It allows developers | ||
| * to quickly switch between user accounts without entering passwords. | ||
| * </p> | ||
| * <p> | ||
| * <strong>SECURITY WARNING:</strong> This controller must NEVER be enabled in | ||
| * production environments. It bypasses all password authentication. | ||
| * </p> | ||
| */ | ||
| @Slf4j | ||
| @RestController | ||
| @RequestMapping("/dev") | ||
| @RequiredArgsConstructor | ||
| @Profile("local") | ||
| @ConditionalOnProperty(name = "user.dev.auto-login-enabled", havingValue = "true", matchIfMissing = false) | ||
| public class DevLoginController { | ||
|
|
||
| private final UserService userService; | ||
| private final UserRepository userRepository; | ||
| private final DevLoginConfigProperties devLoginConfigProperties; | ||
|
|
||
| /** | ||
| * Logs in as the specified user by email without requiring a password. | ||
| * After successful authentication, redirects to the configured redirect URL. | ||
| * | ||
| * @param email the email of the user to log in as | ||
| * @param response the HTTP servlet response for redirect | ||
| * @return a ResponseEntity with error details if authentication fails | ||
| * @throws IOException if the redirect fails | ||
| */ | ||
| @GetMapping("/login-as/{email}") | ||
| public ResponseEntity<JSONResponse> loginAs(@PathVariable String email, HttpServletResponse response) | ||
| throws IOException { | ||
| log.warn("Dev login attempt for user: {}", email); | ||
|
|
||
| User user = userService.findUserByEmail(email); | ||
| if (user == null) { | ||
| log.warn("Dev login failed: user not found for email: {}", email); | ||
| return ResponseEntity.status(HttpStatus.NOT_FOUND) | ||
| .body(JSONResponse.builder().success(false).message("User not found: " + email).code(404).build()); | ||
| } | ||
|
|
||
| if (!user.isEnabled()) { | ||
| log.warn("Dev login failed: user is disabled: {}", email); | ||
| return ResponseEntity.status(HttpStatus.FORBIDDEN) | ||
| .body(JSONResponse.builder().success(false).message("User is disabled: " + email).code(403) | ||
| .build()); | ||
| } | ||
|
|
||
| userService.authWithoutPassword(user); | ||
| log.warn("Dev login successful for user: {}", email); | ||
|
|
||
| response.sendRedirect(devLoginConfigProperties.getLoginRedirectUrl()); | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Lists all enabled user emails available for dev login. | ||
| * | ||
| * @return a JSONResponse containing the list of enabled user emails | ||
| */ | ||
| @GetMapping("/users") | ||
| public ResponseEntity<JSONResponse> listUsers() { | ||
| List<String> enabledEmails = userRepository.findAll().stream().filter(User::isEnabled).map(User::getEmail) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| return ResponseEntity.ok(JSONResponse.builder().success(true).message("Found " + enabledEmails.size() | ||
| + " enabled users").data(enabledEmails).build()); | ||
| } | ||
| } | ||
27 changes: 27 additions & 0 deletions
27
src/main/java/com/digitalsanctuary/spring/user/dev/DevLoginStartupWarning.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,27 @@ | ||
| package com.digitalsanctuary.spring.user.dev; | ||
|
|
||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
| import org.springframework.context.annotation.Profile; | ||
| import org.springframework.stereotype.Component; | ||
| import jakarta.annotation.PostConstruct; | ||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| /** | ||
| * Logs a prominent warning on startup when the dev login feature is active. | ||
| * This ensures developers are aware that passwordless authentication is enabled. | ||
| */ | ||
| @Slf4j | ||
| @Component | ||
| @Profile("local") | ||
| @ConditionalOnProperty(name = "user.dev.auto-login-enabled", havingValue = "true", matchIfMissing = false) | ||
| public class DevLoginStartupWarning { | ||
|
|
||
| @PostConstruct | ||
| public void logWarning() { | ||
| log.warn("========================================================"); | ||
| log.warn(" DEV LOGIN IS ACTIVE"); | ||
| log.warn(" Passwordless authentication is enabled at /dev/login-as/{{email}}"); | ||
| log.warn(" DO NOT enable this in production!"); | ||
| log.warn("========================================================"); | ||
| } | ||
| } |
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
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.
Calling
findAll()on the UserRepository loads all users into memory, which could be inefficient if the user table is large. Consider adding a custom repository method likefindAllByEnabledTrue()to filter at the database level, or add a@Queryto retrieve only enabled user emails.