Browse Source

code cleaning

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
sqlite
Stephan Richter 3 months ago
parent
commit
2702756879
  1. 137
      de.srsoftware.oidc.app/src/main/java/de/srsoftware/oidc/app/Application.java

137
de.srsoftware.oidc.app/src/main/java/de/srsoftware/oidc/app/Application.java

@ -18,7 +18,6 @@ import de.srsoftware.logging.ColorLogger;
import de.srsoftware.oidc.api.*; import de.srsoftware.oidc.api.*;
import de.srsoftware.oidc.api.data.User; import de.srsoftware.oidc.api.data.User;
import de.srsoftware.oidc.backend.*; import de.srsoftware.oidc.backend.*;
import de.srsoftware.oidc.datastore.file.FileStore;
import de.srsoftware.oidc.datastore.file.FileStoreProvider; import de.srsoftware.oidc.datastore.file.FileStoreProvider;
import de.srsoftware.oidc.datastore.file.PlaintextKeyStore; import de.srsoftware.oidc.datastore.file.PlaintextKeyStore;
import de.srsoftware.oidc.datastore.file.UuidHasher; import de.srsoftware.oidc.datastore.file.UuidHasher;
@ -28,6 +27,7 @@ import de.srsoftware.oidc.web.StaticPages;
import java.io.File; import java.io.File;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.nio.file.Path; import java.nio.file.Path;
import java.sql.SQLException;
import java.util.*; import java.util.*;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@ -49,72 +49,89 @@ public class Application {
private static final String WELL_KNOWN = "/.well-known"; private static final String WELL_KNOWN = "/.well-known";
private static System.Logger LOG = new ColorLogger("Application").setLogLevel(DEBUG); private static System.Logger LOG = new ColorLogger("Application").setLogLevel(DEBUG);
private static ConnectionProvider connectionProvider = new ConnectionProvider(); private static ConnectionProvider connectionProvider = new ConnectionProvider();
public static void main(String[] args) throws Exception {
public static void main(String[] args) throws Exception {
var argMap = map(args); var argMap = map(args);
Optional<Path> basePath = argMap.get(BASE_PATH) instanceof Path p ? Optional.of(p) : empty(); Optional<Path> basePath = argMap.get(BASE_PATH) instanceof Path p ? Optional.of(p) : empty();
var configFile = (argMap.get(CONFIG_PATH) instanceof Path p ? p : configDir(APP_NAME).resolve("config.json")).toFile(); var configFile = (argMap.get(CONFIG_PATH) instanceof Path p ? p : configDir(APP_NAME).resolve("config.json")).toFile();
var config = new Configuration(configFile); var config = new Configuration(configFile);
var defaultConfigDir = configDir(APP_NAME); var passHasher = new UuidHasher();
var passwordHasher = new UuidHasher(); var firstHash = passHasher.hash(FIRST_USER_PASS, FIRST_UUID);
var firstHash = passwordHasher.hash(FIRST_USER_PASS, FIRST_UUID); var firstUser = new User(FIRST_USER, firstHash, FIRST_USER, "%s@internal".formatted(FIRST_USER), FIRST_UUID).add(MANAGE_CLIENTS, MANAGE_PERMISSIONS, MANAGE_SMTP, MANAGE_USERS);
var firstUser = new User(FIRST_USER, firstHash, FIRST_USER, "%s@internal".formatted(FIRST_USER), FIRST_UUID).add(MANAGE_CLIENTS, MANAGE_PERMISSIONS, MANAGE_SMTP, MANAGE_USERS);
var defaultFile = defaultConfigDir.resolve("data.json");
var keyStorageLocation = new File(config.getOrDefault("key_storage", defaultConfigDir.resolve("keys"))); FileStoreProvider fileStoreProvider = new FileStoreProvider(passHasher);
KeyStorage keyStore; var defaultConfigDir = configDir(APP_NAME);
if ((keyStorageLocation.exists() && keyStorageLocation.isDirectory()) || !keyStorageLocation.getName().contains(".")) { var defaultFile = defaultConfigDir.resolve("data.json");
keyStore = new PlaintextKeyStore(keyStorageLocation.toPath()); var userService = setupUserService(config, defaultFile, fileStoreProvider).init(firstUser);
} else { // SQLite var sessionService = setupSessionService(config, defaultFile, fileStoreProvider);
var conn = connectionProvider.get(keyStorageLocation); var mailConfig = setupMailConfig(config, defaultFile, fileStoreProvider);
keyStore = new SqliteKeyStore(conn); var keyStore = setupKeyStore(config, defaultConfigDir);
} KeyManager keyManager = new RotatingKeyManager(keyStore);
var authService = setupAuthService(config, defaultFile, fileStoreProvider);
KeyManager keyManager = new RotatingKeyManager(keyStore); var clientService = setupClientService(config, defaultFile, fileStoreProvider);
FileStoreProvider fileStoreProvider = new FileStoreProvider(passwordHasher); HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
var staticPages = (StaticPages) new StaticPages(basePath).bindPath(STATIC_PATH, FAVICON).on(server);
new Forward(INDEX).bindPath(ROOT).on(server);
new WellKnownController().bindPath(WELL_KNOWN).on(server);
new UserController(mailConfig, sessionService, userService, staticPages).bindPath(API_USER).on(server);
var tokenControllerConfig = new TokenController.Configuration("https://lightoidc.srsoftware.de", 10); // TODO configure or derive from hostname
new TokenController(authService, clientService, keyManager, userService, tokenControllerConfig).bindPath(API_TOKEN).on(server);
new ClientController(authService, clientService, sessionService).bindPath(API_CLIENT).on(server);
new KeyStoreController(keyStore).bindPath(JWKS).on(server);
new EmailController(mailConfig, sessionService).bindPath(API_EMAIL).on(server);
server.setExecutor(Executors.newCachedThreadPool());
server.start();
}
var userStorageLocation = new File(config.getOrDefault("user_storage",defaultFile)); private static ClientService setupClientService(Configuration config, Path defaultFile, FileStoreProvider fileStoreProvider) {
var userService = switch (extension(userStorageLocation).toLowerCase()){ var clientStore = new File(config.getOrDefault("client_store", defaultFile));
case "db", "sqlite", "sqlite3" -> new SqliteUserService(connectionProvider.get(userStorageLocation)); return switch (extension(clientStore)) {
default -> fileStoreProvider.get(userStorageLocation); case "db", "sqlite", "sqlite3" -> new SqliteClientService(connectionProvider.get(clientStore));
default -> fileStoreProvider.get(clientStore);
}; };
userService.init(firstUser); }
var mailConfigLocation = new File(config.getOrDefault("mail_config_storage",defaultFile)); private static AuthorizationService setupAuthService(Configuration config, Path defaultFile, FileStoreProvider fileStoreProvider) {
var mailConfig = switch (extension(mailConfigLocation)){ var authServiceLocation = new File(config.getOrDefault("auth_store",defaultFile));
case "db", "sqlite", "sqlite3" -> new SqliteMailConfig(connectionProvider.get(userStorageLocation)); return switch (extension(authServiceLocation)){
default -> fileStoreProvider.get(mailConfigLocation); case "db", "sqlite", "sqlite3" -> new SqliteAuthService(connectionProvider.get(authServiceLocation));
default -> fileStoreProvider.get(authServiceLocation);
}; };
}
private static SessionService setupSessionService(Configuration config, Path defaultFile, FileStoreProvider fileStoreProvider) {
var sessionStore = new File(config.getOrDefault("session_storage",defaultFile)); var sessionStore = new File(config.getOrDefault("session_storage",defaultFile));
var sessionService = switch (extension(sessionStore)){ return switch (extension(sessionStore)){
case "db", "sqlite", "sqlite3" -> new SqliteSessionService(connectionProvider.get(sessionStore)); case "db", "sqlite", "sqlite3" -> new SqliteSessionService(connectionProvider.get(sessionStore));
default -> fileStoreProvider.get(sessionStore); default -> fileStoreProvider.get(sessionStore);
}; };
}
var authServiceLocation = new File(config.getOrDefault("auth_store",defaultFile)); private static MailConfig setupMailConfig(Configuration config, Path defaultFile, FileStoreProvider fileStoreProvider) {
AuthorizationService authService = switch (extension(authServiceLocation)){ var mailConfigLocation = new File(config.getOrDefault("mail_config_storage",defaultFile));
case "db", "sqlite", "sqlite3" -> new SqliteAuthService(connectionProvider.get(sessionStore)); return switch (extension(mailConfigLocation)){
default -> fileStoreProvider.get(sessionStore); case "db", "sqlite", "sqlite3" -> new SqliteMailConfig(connectionProvider.get(mailConfigLocation));
default -> fileStoreProvider.get(mailConfigLocation);
}; };
}
var clientStore = new File(config.getOrDefault("client_store",defaultFile)); private static UserService setupUserService(Configuration config, Path defaultFile, FileStoreProvider fileStoreProvider) {
ClientService clientService = switch (extension(clientStore)){ var userStorageLocation = new File(config.getOrDefault("user_storage",defaultFile));
case "db", "sqlite", "sqlite3" -> new SqliteClientService(connectionProvider.get(sessionStore)); return switch (extension(userStorageLocation).toLowerCase()){
default -> fileStoreProvider.get(sessionStore); case "db", "sqlite", "sqlite3" -> new SqliteUserService(connectionProvider.get(userStorageLocation));
default -> fileStoreProvider.get(userStorageLocation);
}; };
}
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0); private static KeyStorage setupKeyStore(Configuration config, Path defaultConfigDir) throws SQLException {
var staticPages = (StaticPages) new StaticPages(basePath).bindPath(STATIC_PATH, FAVICON).on(server); var keyStorageLocation = new File(config.getOrDefault("key_storage", defaultConfigDir.resolve("keys")));
new Forward(INDEX).bindPath(ROOT).on(server); if ((keyStorageLocation.exists() && keyStorageLocation.isDirectory()) || !keyStorageLocation.getName().contains(".")) {
new WellKnownController().bindPath(WELL_KNOWN).on(server); return new PlaintextKeyStore(keyStorageLocation.toPath());
new UserController(mailConfig, sessionService, userService, staticPages).bindPath(API_USER).on(server); } else { // SQLite
var tokenControllerConfig = new TokenController.Configuration("https://lightoidc.srsoftware.de", 10); // TODO configure or derive from hostname var conn = connectionProvider.get(keyStorageLocation);
new TokenController(authService, clientService, keyManager, userService, tokenControllerConfig).bindPath(API_TOKEN).on(server); return new SqliteKeyStore(conn);
new ClientController(authService, clientService, sessionService).bindPath(API_CLIENT).on(server); }
new KeyStoreController(keyStore).bindPath(JWKS).on(server);
new EmailController(mailConfig, sessionService).bindPath(API_EMAIL).on(server);
server.setExecutor(Executors.newCachedThreadPool());
server.start();
} }
private static Map<String, Object> map(String[] args) { private static Map<String, Object> map(String[] args) {
@ -129,18 +146,18 @@ public class Application {
var token = tokens.remove(0); var token = tokens.remove(0);
switch (token) { switch (token) {
case "--base": case "--base":
if (tokens.isEmpty()) throw new IllegalArgumentException("--base option requires second argument!"); if (tokens.isEmpty()) throw new IllegalArgumentException("--base option requires second argument!");
map.put(BASE_PATH, Path.of(tokens.remove(0))); map.put(BASE_PATH, Path.of(tokens.remove(0)));
break; break;
case "--config": case "--config":
if (tokens.isEmpty()) throw new IllegalArgumentException("--config option requires second argument!"); if (tokens.isEmpty()) throw new IllegalArgumentException("--config option requires second argument!");
map.put(CONFIG_PATH, Path.of(tokens.remove(0))); map.put(CONFIG_PATH, Path.of(tokens.remove(0)));
break; break;
default: default:
LOG.log(ERROR, "Unknown option: {0}", token); LOG.log(ERROR, "Unknown option: {0}", token);
}
} }
return map;
} }
return map;
}
} }

Loading…
Cancel
Save