2023-08-13 01:14:14 +01:00
|
|
|
package stirling.software.SPDF.config.security;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2023-08-26 12:27:52 +01:00
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
2023-08-13 01:14:14 +01:00
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
import jakarta.annotation.PostConstruct;
|
2023-08-26 17:30:49 +01:00
|
|
|
import stirling.software.SPDF.model.ApplicationProperties;
|
2023-08-13 01:14:14 +01:00
|
|
|
import stirling.software.SPDF.model.Role;
|
2023-08-26 12:27:52 +01:00
|
|
|
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.nio.file.*;
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
import org.springframework.core.env.Environment;
|
|
|
|
import org.springframework.core.io.ClassPathResource;
|
|
|
|
|
2023-08-13 01:14:14 +01:00
|
|
|
@Component
|
|
|
|
public class InitialSetup {
|
|
|
|
|
2023-08-26 17:30:49 +01:00
|
|
|
@Autowired
|
|
|
|
private UserService userService;
|
|
|
|
|
2023-08-26 22:33:23 +01:00
|
|
|
@Autowired
|
|
|
|
ApplicationProperties applicationProperties;
|
|
|
|
|
2023-08-26 17:30:49 +01:00
|
|
|
@PostConstruct
|
|
|
|
public void init() {
|
|
|
|
if (!userService.hasUsers()) {
|
2023-08-26 22:33:23 +01:00
|
|
|
String initialUsername = applicationProperties.getSecurity().getInitialLogin().getUsername();
|
|
|
|
String initialPassword = applicationProperties.getSecurity().getInitialLogin().getPassword();
|
2023-08-26 17:30:49 +01:00
|
|
|
if (initialUsername != null && initialPassword != null) {
|
|
|
|
userService.saveUser(initialUsername, initialPassword, Role.ADMIN.getRoleId());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-26 22:33:23 +01:00
|
|
|
|
2023-08-26 17:30:49 +01:00
|
|
|
|
|
|
|
@PostConstruct
|
|
|
|
public void initSecretKey() throws IOException {
|
|
|
|
String secretKey = applicationProperties.getAutomaticallyGenerated().getKey();
|
|
|
|
if (secretKey == null || secretKey.isEmpty()) {
|
|
|
|
secretKey = UUID.randomUUID().toString(); // Generating a random UUID as the secret key
|
|
|
|
saveKeyToConfig(secretKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void saveKeyToConfig(String key) throws IOException {
|
2023-08-26 22:33:23 +01:00
|
|
|
Path path = Paths.get("configs", "settings.yml"); // Target the configs/settings.yml
|
2023-08-26 17:30:49 +01:00
|
|
|
List<String> lines = Files.readAllLines(path);
|
|
|
|
boolean keyFound = false;
|
|
|
|
|
|
|
|
// Search for the existing key to replace it or place to add it
|
|
|
|
for (int i = 0; i < lines.size(); i++) {
|
|
|
|
if (lines.get(i).startsWith("AutomaticallyGenerated:")) {
|
|
|
|
keyFound = true;
|
|
|
|
if (i + 1 < lines.size() && lines.get(i + 1).trim().startsWith("key:")) {
|
|
|
|
lines.set(i + 1, " key: " + key);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
lines.add(i + 1, " key: " + key);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the section doesn't exist, append it
|
|
|
|
if (!keyFound) {
|
|
|
|
lines.add("# Automatically Generated Settings (Do Not Edit Directly)");
|
|
|
|
lines.add("AutomaticallyGenerated:");
|
|
|
|
lines.add(" key: " + key);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write back to the file
|
|
|
|
Files.write(path, lines);
|
|
|
|
}
|
|
|
|
}
|