From 375ee585c13eaa9a036061dcec1b9d15e872c89c Mon Sep 17 00:00:00 2001 From: Jeidnx Date: Mon, 19 Jun 2023 13:11:20 +0200 Subject: [PATCH] Better Error handling for oidc config --- config.properties | 4 +- .../java/me/kavin/piped/consts/Constants.java | 39 ++++++++++--------- .../kavin/piped/utils/obj/OidcProvider.java | 13 +++++-- 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/config.properties b/config.properties index bd6efcb..916cffb 100644 --- a/config.properties +++ b/config.properties @@ -84,4 +84,6 @@ hibernate.connection.password:changeme #oidc.provider.INSERT_HERE.name:INSERT_HERE #oidc.provider.INSERT_HERE.clientId:INSERT_HERE #oidc.provider.INSERT_HERE.clientSecret:INSERT_HERE -#oidc.provider.INSERT_HERE.authUrl:INSERT_HERE +#oidc.provider.INSERT_HERE.authUri:INSERT_HERE +#oidc.provider.INSERT_HERE.tokenUri:INSERT_HERE +#oidc.provider.INSERT_HERE.userinfoUri:INSERT_HERE \ No newline at end of file diff --git a/src/main/java/me/kavin/piped/consts/Constants.java b/src/main/java/me/kavin/piped/consts/Constants.java index ef2fd82..d8586be 100644 --- a/src/main/java/me/kavin/piped/consts/Constants.java +++ b/src/main/java/me/kavin/piped/consts/Constants.java @@ -26,7 +26,7 @@ import java.io.File; import java.io.FileReader; import java.net.InetSocketAddress; import java.net.ProxySelector; -import java.util.LinkedList; +import java.util.ArrayList; import java.util.List; import java.util.Properties; import java.util.regex.Pattern; @@ -102,7 +102,7 @@ public class Constants { public static final String YOUTUBE_COUNTRY; public static final String VERSION; - public static final LinkedList OIDC_PROVIDERS; + public static final ArrayList OIDC_PROVIDERS; public static final ObjectMapper mapper = JsonMapper.builder() .addMixIn(Page.class, PageMixin.class) @@ -167,7 +167,7 @@ public class Constants { MATRIX_TOKEN = getProperty(prop, "MATRIX_TOKEN"); GEO_RESTRICTION_CHECKER_URL = getProperty(prop, "GEO_RESTRICTION_CHECKER_URL"); - OIDC_PROVIDERS = new LinkedList<>(); + OIDC_PROVIDERS = new ArrayList<>(); ArrayNode providerNames = frontendProperties.putArray("oidcProviders"); prop.forEach((_key, _value) -> { String key = String.valueOf(_key), value = String.valueOf(_value); @@ -178,21 +178,15 @@ public class Constants { else if (key.startsWith("oidc.provider")) { String[] split = key.split("\\."); if (split.length != 4 || !split[3].equals("name")) return; - - try { - OIDC_PROVIDERS.add(new OidcProvider( - value, - getProperty(prop, "oidc.provider." + value + ".clientId"), - getProperty(prop, "oidc.provider." + value + ".clientSecret"), - getProperty(prop, "oidc.provider." + value + ".authUrl"), - getProperty(prop, "oidc.provider." + value + ".tokenUrl"), - getProperty(prop, "oidc.provider." + value + ".userinfoUrl") - )); - } catch (Exception e) { - System.err.println("Error while getting properties for oidc provider '" + value + "'"); - throw new RuntimeException(e); - } - providerNames.add(value); + 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); } }); frontendProperties.put("imageProxyUrl", IMAGE_PROXY_PART); @@ -256,4 +250,13 @@ 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 + "'"); + System.exit(1); + } + return value; + } } diff --git a/src/main/java/me/kavin/piped/utils/obj/OidcProvider.java b/src/main/java/me/kavin/piped/utils/obj/OidcProvider.java index a869c2a..79216ea 100644 --- a/src/main/java/me/kavin/piped/utils/obj/OidcProvider.java +++ b/src/main/java/me/kavin/piped/utils/obj/OidcProvider.java @@ -14,12 +14,17 @@ public class OidcProvider { public URI tokenUri; public URI userinfoUri; - public OidcProvider(String name, String clientID, String clientSecret, String authUri, String tokenUri, String userinfoUri) throws URISyntaxException { + public OidcProvider(String name, String clientID, String clientSecret, String authUri, String tokenUri, String userinfoUri) { this.name = name; this.clientID = new ClientID(clientID); this.clientSecret = new Secret(clientSecret); - this.authUri = new URI(authUri); - this.tokenUri = new URI(tokenUri); - this.userinfoUri = new URI(userinfoUri); + try { + this.authUri = new URI(authUri); + this.tokenUri = new URI(tokenUri); + this.userinfoUri = new URI(userinfoUri); + } catch(URISyntaxException e) { + System.err.println("Malformed URI for oidc provider '" + name + "' found."); + System.exit(1); + } } }