mirror of
https://github.com/TeamPiped/Piped-Backend.git
synced 2025-01-10 11:30:29 +05:30
Simplify config handling.
This commit is contained in:
parent
97889f393e
commit
847f80c0d4
@ -8,6 +8,7 @@ import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
|||||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||||
import io.minio.MinioClient;
|
import io.minio.MinioClient;
|
||||||
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
|
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.PageMixin;
|
||||||
import me.kavin.piped.utils.RequestUtils;
|
import me.kavin.piped.utils.RequestUtils;
|
||||||
import me.kavin.piped.utils.obj.OidcProvider;
|
import me.kavin.piped.utils.obj.OidcProvider;
|
||||||
@ -25,10 +26,8 @@ import rocks.kavin.reqwest4j.ReqwestUtils;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.net.InetSocketAddress;
|
|
||||||
import java.net.ProxySelector;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
@ -103,7 +102,7 @@ public class Constants {
|
|||||||
public static final String YOUTUBE_COUNTRY;
|
public static final String YOUTUBE_COUNTRY;
|
||||||
|
|
||||||
public static final String VERSION;
|
public static final String VERSION;
|
||||||
public static final ArrayList<OidcProvider> OIDC_PROVIDERS;
|
public static final List<OidcProvider> OIDC_PROVIDERS;
|
||||||
|
|
||||||
public static final ObjectMapper mapper = JsonMapper.builder()
|
public static final ObjectMapper mapper = JsonMapper.builder()
|
||||||
.addMixIn(Page.class, PageMixin.class)
|
.addMixIn(Page.class, PageMixin.class)
|
||||||
@ -169,7 +168,9 @@ public class Constants {
|
|||||||
MATRIX_TOKEN = getProperty(prop, "MATRIX_TOKEN");
|
MATRIX_TOKEN = getProperty(prop, "MATRIX_TOKEN");
|
||||||
GEO_RESTRICTION_CHECKER_URL = getProperty(prop, "GEO_RESTRICTION_CHECKER_URL");
|
GEO_RESTRICTION_CHECKER_URL = getProperty(prop, "GEO_RESTRICTION_CHECKER_URL");
|
||||||
|
|
||||||
OIDC_PROVIDERS = new ArrayList<>();
|
OIDC_PROVIDERS = new ObjectArrayList<>();
|
||||||
|
|
||||||
|
Map<String, Map<String, String>> oidcProviderConfig = new Object2ObjectOpenHashMap<>();
|
||||||
ArrayNode providerNames = frontendProperties.putArray("oidcProviders");
|
ArrayNode providerNames = frontendProperties.putArray("oidcProviders");
|
||||||
prop.forEach((_key, _value) -> {
|
prop.forEach((_key, _value) -> {
|
||||||
String key = String.valueOf(_key), value = String.valueOf(_value);
|
String key = String.valueOf(_key), value = String.valueOf(_value);
|
||||||
@ -179,18 +180,25 @@ public class Constants {
|
|||||||
frontendProperties.put(StringUtils.substringAfter(key, "frontend."), value);
|
frontendProperties.put(StringUtils.substringAfter(key, "frontend."), value);
|
||||||
else if (key.startsWith("oidc.provider")) {
|
else if (key.startsWith("oidc.provider")) {
|
||||||
String[] split = key.split("\\.");
|
String[] split = key.split("\\.");
|
||||||
if (split.length != 4 || !split[3].equals("name")) return;
|
if (split.length != 4) return;
|
||||||
OIDC_PROVIDERS.add(new OidcProvider(
|
oidcProviderConfig
|
||||||
value,
|
.computeIfAbsent(split[2], k -> new Object2ObjectOpenHashMap<>())
|
||||||
getRequiredOidcProperty(prop, value, "clientId"),
|
.put(split[3], value);
|
||||||
getRequiredOidcProperty(prop, value, "clientSecret"),
|
|
||||||
getRequiredOidcProperty(prop, value, "authUri"),
|
|
||||||
getRequiredOidcProperty(prop, value, "tokenUri"),
|
|
||||||
getRequiredOidcProperty(prop, value, "userinfoUri"))
|
|
||||||
);
|
|
||||||
providerNames.add(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.put("imageProxyUrl", IMAGE_PROXY_PART);
|
||||||
frontendProperties.putArray("countries").addAll(
|
frontendProperties.putArray("countries").addAll(
|
||||||
YOUTUBE_SERVICE.getSupportedCountries().stream().map(ContentCountry::getCountryCode)
|
YOUTUBE_SERVICE.getSupportedCountries().stream().map(ContentCountry::getCountryCode)
|
||||||
@ -247,10 +255,10 @@ public class Constants {
|
|||||||
return prop.getProperty(key, def);
|
return prop.getProperty(key, def);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String getRequiredOidcProperty(final Properties prop, String provider, String key) {
|
private static String getRequiredMapValue(final Map<?, String> map, Object key) {
|
||||||
String value = getProperty(prop, "oidc.provider." + provider + "." + key);
|
String value = map.get(key);
|
||||||
if(value == null || value.equals("")){
|
if (StringUtils.isBlank(value)) {
|
||||||
System.err.println("Missing " + key + " for oidc provider '" + provider + "'");
|
System.err.println("Missing '" + key + "' in sub-configuration");
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
|
Loading…
Reference in New Issue
Block a user