From 44240b20f863fbd437e06947b539f76e73e91668 Mon Sep 17 00:00:00 2001 From: Thomas <> Date: Mon, 2 Oct 2023 13:07:50 +0200 Subject: chore(core): code clean-up and optimization --- .../services/ProtocolAuthenticationService.java | 2 +- .../impl/idp/controller/protocols/RequestImpl.java | 13 ++-------- .../at/gv/egiz/eaaf/core/impl/http/HttpUtils.java | 28 ++++++++++++++++++++-- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/auth/services/ProtocolAuthenticationService.java b/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/auth/services/ProtocolAuthenticationService.java index 8dd208a9..0e454c7a 100644 --- a/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/auth/services/ProtocolAuthenticationService.java +++ b/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/auth/services/ProtocolAuthenticationService.java @@ -494,7 +494,7 @@ public class ProtocolAuthenticationService implements IProtocolAuthenticationSer try { final IGuiBuilderConfiguration config = guiConfigFactory - .getDefaultErrorGui(HttpUtils.extractAuthUrlFromRequest(httpReq)); + .getDefaultErrorGui(HttpUtils.extractAuthUrlStringFromRequest(httpReq)); String[] errorCodeParams = null; diff --git a/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/controller/protocols/RequestImpl.java b/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/controller/protocols/RequestImpl.java index 3b3e7cd8..f5703cab 100644 --- a/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/controller/protocols/RequestImpl.java +++ b/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/controller/protocols/RequestImpl.java @@ -21,7 +21,6 @@ package at.gv.egiz.eaaf.core.impl.idp.controller.protocols; import java.io.Serializable; import java.lang.reflect.InvocationTargetException; -import java.net.MalformedURLException; import java.net.URL; import java.time.Instant; import java.util.HashMap; @@ -182,21 +181,13 @@ public abstract class RequestImpl implements IRequest, Serializable { // Random.nextLongRandom()); // check if End-Point is valid - final String authUrlString = HttpUtils.extractAuthUrlFromRequest(req); - URL authReqUrl; - try { - authReqUrl = new URL(authUrlString); - - } catch (final MalformedURLException e) { - log.error("IDP AuthenticationServiceURL Prefix is not a valid URL." + authUrlString, e); - throw new EaafAuthenticationException(ERROR_CODE_INTERNAL_00, new Object[] { authUrlString }, e); + URL authReqUrl = HttpUtils.extractAuthUrlFromRequest(req); - } this.idpAuthUrl = authConfig.validateIdpUrl(authReqUrl); if (this.idpAuthUrl == null) { log.warn( "Extract AuthenticationServiceURL: " + authReqUrl + " is NOT found in configuration."); - throw new EaafAuthenticationException(ERROR_CODE_INTERNAL_00, new Object[] { authUrlString }); + throw new EaafAuthenticationException(ERROR_CODE_INTERNAL_00, new Object[] { authReqUrl }); } diff --git a/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/http/HttpUtils.java b/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/http/HttpUtils.java index caa73e04..1b6df3de 100644 --- a/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/http/HttpUtils.java +++ b/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/http/HttpUtils.java @@ -21,6 +21,8 @@ package at.gv.egiz.eaaf.core.impl.http; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; import java.security.KeyManagementException; import java.security.KeyStore; import java.security.KeyStoreException; @@ -45,6 +47,7 @@ import org.apache.hc.core5.http.message.StatusLine; import org.apache.hc.core5.ssl.TrustStrategy; import org.bouncycastle.jsse.provider.BouncyCastleJsseProvider; +import at.gv.egiz.eaaf.core.exceptions.EaafAuthenticationException; import at.gv.egiz.eaaf.core.exceptions.EaafConfigurationException; import at.gv.egiz.eaaf.core.exceptions.EaafFactoryException; import at.gv.egiz.eaaf.core.impl.data.Pair; @@ -56,6 +59,7 @@ import lombok.extern.slf4j.Slf4j; @Slf4j public class HttpUtils { + private static final String ERROR_CODE_INTERNAL_00 = "eaaf.core.00"; private static final String ERROR_03 = "internal.httpclient.03"; /** @@ -148,7 +152,7 @@ public class HttpUtils { * @param req HttpServletRequest * @return PublicURLPrefix which ends always without / */ - public static String extractAuthUrlFromRequest(final HttpServletRequest req) { + public static String extractAuthUrlStringFromRequest(final HttpServletRequest req) { String authUrl = req.getScheme() + "://" + req.getServerName(); if (req.getScheme().equalsIgnoreCase("https") && req.getServerPort() != 443 || req.getScheme().equalsIgnoreCase("http") && req.getServerPort() != 80) { @@ -159,6 +163,26 @@ public class HttpUtils { } + /** + * Extract the IDP PublicURLPrefix from authrequest. + * + * @param req HttpServletRequest + * @return PublicURLPrefix which ends always without / + */ + public static URL extractAuthUrlFromRequest(final HttpServletRequest req) + throws EaafAuthenticationException { + String authUrlString = extractAuthUrlStringFromRequest(req); + try { + return new URL(authUrlString); + + } catch (final MalformedURLException e) { + log.error("URL Prefix is not a valid URL." + authUrlString, e); + throw new EaafAuthenticationException(ERROR_CODE_INTERNAL_00, new Object[] { authUrlString }, e); + + } + + } + /** * Extract the IDP requested URL from authrequest. * @@ -166,7 +190,7 @@ public class HttpUtils { * @return RequestURL which ends always without / */ public static String extractAuthServletPathFromRequest(final HttpServletRequest req) { - return extractAuthUrlFromRequest(req).concat(req.getServletPath()); + return extractAuthUrlStringFromRequest(req).concat(req.getServletPath()); } -- cgit v1.2.3