2023-08-13 01:14:14 +01:00
|
|
|
package stirling.software.SPDF.config.security;
|
|
|
|
|
2025-01-06 18:58:26 +00:00
|
|
|
import java.sql.SQLException;
|
2023-08-27 00:39:22 +01:00
|
|
|
import java.util.UUID;
|
|
|
|
|
2023-08-13 01:14:14 +01:00
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
import jakarta.annotation.PostConstruct;
|
2025-02-23 13:36:21 +00:00
|
|
|
|
2025-04-25 15:35:12 +02:00
|
|
|
import lombok.RequiredArgsConstructor;
|
2024-07-05 21:48:33 +02:00
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2025-02-23 13:36:21 +00:00
|
|
|
|
2025-01-06 18:58:26 +00:00
|
|
|
import stirling.software.SPDF.config.interfaces.DatabaseInterface;
|
2023-08-13 01:14:14 +01:00
|
|
|
import stirling.software.SPDF.model.Role;
|
2025-04-28 17:36:27 +01:00
|
|
|
import stirling.software.common.configuration.ApplicationProperties;
|
|
|
|
import stirling.software.common.model.exception.UnsupportedProviderException;
|
2023-12-30 19:11:27 +00:00
|
|
|
|
2024-07-05 21:48:33 +02:00
|
|
|
@Slf4j
|
2025-01-06 18:58:26 +00:00
|
|
|
@Component
|
2025-04-25 15:35:12 +02:00
|
|
|
@RequiredArgsConstructor
|
2023-08-27 11:59:08 +01:00
|
|
|
public class InitialSecuritySetup {
|
2023-08-13 01:14:14 +01:00
|
|
|
|
2024-12-24 09:52:53 +00:00
|
|
|
private final UserService userService;
|
2023-09-02 00:05:50 +01:00
|
|
|
|
2024-12-24 09:52:53 +00:00
|
|
|
private final ApplicationProperties applicationProperties;
|
2023-08-26 17:30:49 +01:00
|
|
|
|
2025-01-06 18:58:26 +00:00
|
|
|
private final DatabaseInterface databaseService;
|
2024-12-24 09:52:53 +00:00
|
|
|
|
2023-08-26 17:30:49 +01:00
|
|
|
@PostConstruct
|
2025-01-06 18:58:26 +00:00
|
|
|
public void init() {
|
|
|
|
try {
|
|
|
|
|
|
|
|
if (!userService.hasUsers()) {
|
2025-02-25 22:24:01 +01:00
|
|
|
if (databaseService.hasBackup()) {
|
|
|
|
databaseService.importDatabase();
|
|
|
|
} else {
|
|
|
|
initializeAdminUser();
|
|
|
|
}
|
2025-01-06 18:58:26 +00:00
|
|
|
}
|
|
|
|
|
2024-11-29 15:11:59 +00:00
|
|
|
userService.migrateOauth2ToSSO();
|
2025-01-06 18:58:26 +00:00
|
|
|
initializeInternalApiUser();
|
|
|
|
} catch (IllegalArgumentException | SQLException | UnsupportedProviderException e) {
|
|
|
|
log.error("Failed to initialize security setup.", e);
|
|
|
|
System.exit(1);
|
2024-05-18 23:47:05 +02:00
|
|
|
}
|
|
|
|
}
|
2023-08-26 22:33:23 +01:00
|
|
|
|
2025-01-06 18:58:26 +00:00
|
|
|
private void initializeAdminUser() throws SQLException, UnsupportedProviderException {
|
2024-05-18 23:47:05 +02:00
|
|
|
String initialUsername =
|
|
|
|
applicationProperties.getSecurity().getInitialLogin().getUsername();
|
|
|
|
String initialPassword =
|
|
|
|
applicationProperties.getSecurity().getInitialLogin().getPassword();
|
|
|
|
if (initialUsername != null
|
|
|
|
&& !initialUsername.isEmpty()
|
|
|
|
&& initialPassword != null
|
|
|
|
&& !initialPassword.isEmpty()
|
2025-01-06 18:58:26 +00:00
|
|
|
&& userService.findByUsernameIgnoreCase(initialUsername).isEmpty()) {
|
|
|
|
|
|
|
|
userService.saveUser(initialUsername, initialPassword, Role.ADMIN.getRoleId());
|
|
|
|
log.info("Admin user created: {}", initialUsername);
|
2024-05-18 23:47:05 +02:00
|
|
|
} else {
|
|
|
|
createDefaultAdminUser();
|
2023-12-30 19:11:27 +00:00
|
|
|
}
|
2024-05-18 23:47:05 +02:00
|
|
|
}
|
|
|
|
|
2025-01-06 18:58:26 +00:00
|
|
|
private void createDefaultAdminUser() throws SQLException, UnsupportedProviderException {
|
2024-05-18 23:47:05 +02:00
|
|
|
String defaultUsername = "admin";
|
|
|
|
String defaultPassword = "stirling";
|
2025-01-06 18:58:26 +00:00
|
|
|
|
|
|
|
if (userService.findByUsernameIgnoreCase(defaultUsername).isEmpty()) {
|
2024-05-18 23:47:05 +02:00
|
|
|
userService.saveUser(defaultUsername, defaultPassword, Role.ADMIN.getRoleId(), true);
|
2025-01-06 18:58:26 +00:00
|
|
|
log.info("Default admin user created: {}", defaultUsername);
|
2024-05-18 23:47:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-06 18:58:26 +00:00
|
|
|
private void initializeInternalApiUser()
|
|
|
|
throws IllegalArgumentException, SQLException, UnsupportedProviderException {
|
2024-04-14 23:07:03 +02:00
|
|
|
if (!userService.usernameExistsIgnoreCase(Role.INTERNAL_API_USER.getRoleId())) {
|
2023-12-25 12:58:49 +00:00
|
|
|
userService.saveUser(
|
|
|
|
Role.INTERNAL_API_USER.getRoleId(),
|
|
|
|
UUID.randomUUID().toString(),
|
|
|
|
Role.INTERNAL_API_USER.getRoleId());
|
2023-12-24 17:12:32 +00:00
|
|
|
userService.addApiKeyToUser(Role.INTERNAL_API_USER.getRoleId());
|
2025-01-06 18:58:26 +00:00
|
|
|
log.info("Internal API user created: {}", Role.INTERNAL_API_USER.getRoleId());
|
2023-08-26 17:30:49 +01:00
|
|
|
}
|
2024-12-10 20:39:24 +00:00
|
|
|
userService.syncCustomApiUser(applicationProperties.getSecurity().getCustomGlobalAPIKey());
|
2023-08-26 17:30:49 +01:00
|
|
|
}
|
|
|
|
}
|