diff --git a/src/main/java/me/kavin/piped/consts/Constants.java b/src/main/java/me/kavin/piped/consts/Constants.java index 3340cc6..3b44b07 100644 --- a/src/main/java/me/kavin/piped/consts/Constants.java +++ b/src/main/java/me/kavin/piped/consts/Constants.java @@ -8,6 +8,7 @@ import com.fasterxml.jackson.databind.node.JsonNodeFactory; import com.fasterxml.jackson.databind.node.ObjectNode; import io.minio.MinioClient; import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import me.kavin.piped.utils.PageMixin; import me.kavin.piped.utils.RequestUtils; import me.kavin.piped.utils.obj.OidcProvider; @@ -25,10 +26,8 @@ import rocks.kavin.reqwest4j.ReqwestUtils; import java.io.File; import java.io.FileReader; -import java.net.InetSocketAddress; -import java.net.ProxySelector; -import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.Properties; import java.util.regex.Pattern; @@ -103,7 +102,7 @@ public class Constants { public static final String YOUTUBE_COUNTRY; public static final String VERSION; - public static final ArrayList OIDC_PROVIDERS; + public static final List OIDC_PROVIDERS; public static final ObjectMapper mapper = JsonMapper.builder() .addMixIn(Page.class, PageMixin.class) @@ -169,7 +168,9 @@ public class Constants { MATRIX_TOKEN = getProperty(prop, "MATRIX_TOKEN"); GEO_RESTRICTION_CHECKER_URL = getProperty(prop, "GEO_RESTRICTION_CHECKER_URL"); - OIDC_PROVIDERS = new ArrayList<>(); + OIDC_PROVIDERS = new ObjectArrayList<>(); + + Map> oidcProviderConfig = new Object2ObjectOpenHashMap<>(); ArrayNode providerNames = frontendProperties.putArray("oidcProviders"); prop.forEach((_key, _value) -> { String key = String.valueOf(_key), value = String.valueOf(_value); @@ -178,19 +179,26 @@ public class Constants { else if (key.startsWith("frontend.")) frontendProperties.put(StringUtils.substringAfter(key, "frontend."), value); else if (key.startsWith("oidc.provider")) { - String[] split = key.split("\\."); - if (split.length != 4 || !split[3].equals("name")) return; - OIDC_PROVIDERS.add(new OidcProvider( - value, - getRequiredOidcProperty(prop, value, "clientId"), - getRequiredOidcProperty(prop, value, "clientSecret"), - getRequiredOidcProperty(prop, value, "authUri"), - getRequiredOidcProperty(prop, value, "tokenUri"), - getRequiredOidcProperty(prop, value, "userinfoUri")) - ); - providerNames.add(value); + String[] split = key.split("\\."); + if (split.length != 4) return; + oidcProviderConfig + .computeIfAbsent(split[2], k -> new Object2ObjectOpenHashMap<>()) + .put(split[3], value); } }); + oidcProviderConfig.forEach((provider, config) -> { + ObjectNode providerNode = frontendProperties.putObject(provider); + OIDC_PROVIDERS.add(new OidcProvider( + getRequiredMapValue(config, "name"), + getRequiredMapValue(config, "clientId"), + getRequiredMapValue(config, "clientSecret"), + getRequiredMapValue(config, "authUri"), + getRequiredMapValue(config, "tokenUri"), + getRequiredMapValue(config, "userinfoUri") + )); + providerNames.add(provider); + config.forEach(providerNode::put); + }); frontendProperties.put("imageProxyUrl", IMAGE_PROXY_PART); frontendProperties.putArray("countries").addAll( YOUTUBE_SERVICE.getSupportedCountries().stream().map(ContentCountry::getCountryCode) @@ -247,10 +255,10 @@ public class Constants { return prop.getProperty(key, def); } - private static String getRequiredOidcProperty(final Properties prop, String provider, String key) { - String value = getProperty(prop, "oidc.provider." + provider + "." + key); - if(value == null || value.equals("")){ - System.err.println("Missing " + key + " for oidc provider '" + provider + "'"); + private static String getRequiredMapValue(final Map map, Object key) { + String value = map.get(key); + if (StringUtils.isBlank(value)) { + System.err.println("Missing '" + key + "' in sub-configuration"); System.exit(1); } return value;