|
| 1 | +package com.digitalsanctuary.spring.user.dev; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 5 | +import org.springframework.context.annotation.Profile; |
| 6 | +import org.springframework.http.HttpStatus; |
| 7 | +import org.springframework.http.ResponseEntity; |
| 8 | +import org.springframework.web.bind.annotation.GetMapping; |
| 9 | +import org.springframework.web.bind.annotation.PathVariable; |
| 10 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 11 | +import org.springframework.web.bind.annotation.RestController; |
| 12 | +import com.digitalsanctuary.spring.user.persistence.model.User; |
| 13 | +import com.digitalsanctuary.spring.user.persistence.repository.UserRepository; |
| 14 | +import com.digitalsanctuary.spring.user.service.UserService; |
| 15 | +import com.digitalsanctuary.spring.user.util.JSONResponse; |
| 16 | +import lombok.RequiredArgsConstructor; |
| 17 | +import lombok.extern.slf4j.Slf4j; |
| 18 | + |
| 19 | +/** |
| 20 | + * Development-only controller providing quick login-as functionality. |
| 21 | + * <p> |
| 22 | + * This controller is only active when the "local" Spring profile is active AND |
| 23 | + * {@code user.dev.auto-login-enabled} is set to {@code true}. It allows developers |
| 24 | + * to quickly switch between user accounts without entering passwords. |
| 25 | + * </p> |
| 26 | + * <p> |
| 27 | + * <strong>SECURITY WARNING:</strong> This controller must NEVER be enabled in |
| 28 | + * production environments. It bypasses all password authentication. |
| 29 | + * </p> |
| 30 | + */ |
| 31 | +@Slf4j |
| 32 | +@RestController |
| 33 | +@RequestMapping("/dev") |
| 34 | +@RequiredArgsConstructor |
| 35 | +@Profile("local") |
| 36 | +@ConditionalOnProperty(name = "user.dev.auto-login-enabled", havingValue = "true", matchIfMissing = false) |
| 37 | +public class DevLoginController { |
| 38 | + |
| 39 | + private final UserService userService; |
| 40 | + private final UserRepository userRepository; |
| 41 | + private final DevLoginConfigProperties devLoginConfigProperties; |
| 42 | + |
| 43 | + /** |
| 44 | + * Logs in as the specified user by email without requiring a password. |
| 45 | + * After successful authentication, returns a redirect response to the configured URL. |
| 46 | + * |
| 47 | + * @param email the email of the user to log in as |
| 48 | + * @return a redirect response on success, or an error response with 404/403 status |
| 49 | + */ |
| 50 | + @GetMapping("/login-as/{email}") |
| 51 | + public ResponseEntity<JSONResponse> loginAs(@PathVariable String email) { |
| 52 | + log.warn("Dev login attempt for user: {}", email); |
| 53 | + |
| 54 | + User user = userService.findUserByEmail(email); |
| 55 | + if (user == null) { |
| 56 | + log.warn("Dev login failed: user not found for email: {}", email); |
| 57 | + return ResponseEntity.status(HttpStatus.NOT_FOUND) |
| 58 | + .body(JSONResponse.builder().success(false).message("User not found: " + email).code(404).build()); |
| 59 | + } |
| 60 | + |
| 61 | + if (!user.isEnabled()) { |
| 62 | + log.warn("Dev login failed: user is disabled: {}", email); |
| 63 | + return ResponseEntity.status(HttpStatus.FORBIDDEN) |
| 64 | + .body(JSONResponse.builder().success(false).message("User is disabled: " + email).code(403) |
| 65 | + .build()); |
| 66 | + } |
| 67 | + |
| 68 | + userService.authWithoutPassword(user); |
| 69 | + log.warn("Dev login successful for user: {}", email); |
| 70 | + |
| 71 | + return ResponseEntity.status(HttpStatus.FOUND) |
| 72 | + .header("Location", devLoginConfigProperties.getLoginRedirectUrl()) |
| 73 | + .build(); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Lists all enabled user emails available for dev login. |
| 78 | + * |
| 79 | + * @return a JSONResponse containing the list of enabled user emails |
| 80 | + */ |
| 81 | + @GetMapping("/users") |
| 82 | + public ResponseEntity<JSONResponse> listUsers() { |
| 83 | + List<String> enabledEmails = userRepository.findAllByEnabledTrue().stream() |
| 84 | + .map(User::getEmail) |
| 85 | + .toList(); |
| 86 | + |
| 87 | + return ResponseEntity.ok(JSONResponse.builder().success(true) |
| 88 | + .message("Found " + enabledEmails.size() + " enabled users").data(enabledEmails).build()); |
| 89 | + } |
| 90 | +} |
0 commit comments