Skip to content

Package: OpenIDUtils

OpenIDUtils

nameinstructionbranchcomplexitylinemethod
getConfigUri(String, String)
M: 98 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 25 C: 0
0%
M: 1 C: 0
0%
getOpenIdConfPath(String)
M: 43 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
getOpenIdConfPath(URI)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2020, 2022 Eurotech and/or its affiliates and others
3: *
4: * This program and the accompanying materials are made
5: * available under the terms of the Eclipse Public License 2.0
6: * which is available at https://www.eclipse.org/legal/epl-2.0/
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Eurotech - initial API and implementation
12: *******************************************************************************/
13: package org.eclipse.kapua.plugin.sso.openid.provider;
14:
15: import com.google.common.base.Strings;
16: import org.eclipse.kapua.commons.util.log.ConfigurationPrinter;
17: import org.eclipse.kapua.plugin.sso.openid.exception.OpenIDIllegalArgumentException;
18: import org.eclipse.kapua.plugin.sso.openid.exception.uri.OpenIDIllegalUriException;
19: import org.eclipse.kapua.plugin.sso.openid.exception.uri.OpenIDJwtUriException;
20: import org.eclipse.kapua.plugin.sso.openid.exception.uri.OpenIDUriException;
21: import org.eclipse.kapua.plugin.sso.openid.provider.setting.OpenIDSetting;
22: import org.eclipse.kapua.plugin.sso.openid.provider.setting.OpenIDSettingKeys;
23: import org.slf4j.Logger;
24: import org.slf4j.LoggerFactory;
25:
26: import javax.json.Json;
27: import javax.json.JsonObject;
28: import javax.json.JsonString;
29: import javax.json.JsonValue;
30: import java.io.IOException;
31: import java.io.InputStream;
32: import java.net.MalformedURLException;
33: import java.net.URI;
34: import java.net.URISyntaxException;
35: import java.net.URL;
36: import java.util.Optional;
37:
38: /**
39: * Single Sign On utility class.
40: */
41: public final class OpenIDUtils {
42:
43: private static final Logger logger = LoggerFactory.getLogger(OpenIDUtils.class);
44:
45: private static final String DEFAULT_SSO_OPENID_CONF_PATH = ".well-known/openid-configuration";
46:
47: private OpenIDUtils() {
48: }
49:
50: /**
51: * Attempts to retrieve a URI from the Well-Known OpenId Configuration using the given property.
52: *
53: * @param property the property to get from the JSON response.
54: * @param openIdConfPath the OpendID Connect configuration path.
55: * @return an Optional with a {@link URI} corresponding to the given property if everything is fine, otherwise
56: * an empty Optional.
57: * @throws OpenIDUriException if an {@link IOException}, a {@link MalformedURLException} or a {@link URISyntaxException} is caught.
58: */
59: public static Optional<URI> getConfigUri(String property, String openIdConfPath) throws OpenIDUriException {
60: final JsonObject jsonObject;
61:
62: ConfigurationPrinter reqLogger =
63: ConfigurationPrinter
64: .create()
65: .withLogger(logger)
66: .withLogLevel(ConfigurationPrinter.LogLevel.DEBUG)
67: .withTitle("OpenID Provider Configuration Information")
68: .addParameter("Requested property", property)
69: .addParameter("From well-known path", openIdConfPath);
70: try {
71: // Read .well-known resource
72: try (final InputStream stream = new URL(openIdConfPath).openStream()) {
73: // Parse json response
74: jsonObject = Json.createReader(stream).readObject();
75: }
76:
77: // Get property
78: final JsonValue uriJsonValue = jsonObject.get(property);
79:
80: // test result
81:• if (uriJsonValue instanceof JsonString) {
82: Optional<URI> optionalURI = Optional.of(new URI(((JsonString) uriJsonValue).getString()));
83: reqLogger.addParameter("Result value", optionalURI.get());
84: return optionalURI;
85: }
86:
87: // return
88: reqLogger.addHeader("No value found");
89: return Optional.empty();
90: } catch (MalformedURLException mue) {
91: logger.error("openIdConfPath parameter is malformed: {}", mue.getLocalizedMessage(), mue);
92: throw new OpenIDJwtUriException(mue);
93: } catch (IOException ioe) {
94: logger.error("IOException occurred while reading the well-known resource: {}", ioe.getLocalizedMessage(), ioe);
95: throw new OpenIDJwtUriException(ioe);
96: } catch (URISyntaxException urise) {
97: logger.error("Unable to extract the required property from the openIdConfPath: {}", urise.getLocalizedMessage(), urise);
98: throw new OpenIDJwtUriException(urise);
99: } finally {
100: reqLogger.printLog();
101: }
102:
103: }
104:
105: /**
106: * Retrieve the OpenID Connect discovery endpoint (the provider's Well-Known Configuration Endpoint).
107: *
108: * @param issuer the URI representing the JWT Issuer.
109: * @return a String representing the discovery endpoint.
110: * @throws OpenIDIllegalArgumentException if it cannot retrieve the OpenID configuration path or if the generated OpenID Connect discovery endpoint is a
111: * malformed URL
112: */
113: public static String getOpenIdConfPath(final URI issuer) throws OpenIDIllegalArgumentException {
114: return getOpenIdConfPath(issuer.toString());
115: }
116:
117: /**
118: * Retrieve the OpenID Connect discovery endpoint (the provider's Well-Known Configuration Endpoint).
119: *
120: * @param issuer the String representing the JWT Issuer URI.
121: * @return a String representing the discovery endpoint.
122: * @throws OpenIDIllegalArgumentException if it cannot retrieve the OpenID configuration path or if the generated OpenID Connect discovery endpoint is a
123: * malformed URL
124: */
125: public static String getOpenIdConfPath(String issuer) throws OpenIDIllegalArgumentException {
126: String openIDConfPathSuffix = OpenIDSetting.getInstance().getString(OpenIDSettingKeys.SSO_OPENID_CONF_PATH, DEFAULT_SSO_OPENID_CONF_PATH);
127:• if (Strings.isNullOrEmpty(openIDConfPathSuffix)) {
128: throw new OpenIDIllegalArgumentException(OpenIDSettingKeys.SSO_OPENID_CONF_PATH.key(), openIDConfPathSuffix);
129: }
130: String openIdConfPath = issuer + "/" + openIDConfPathSuffix;
131: try {
132: URL normalizedURL = new URI(openIdConfPath).normalize().toURL();
133: return normalizedURL.toString();
134: } catch (MalformedURLException | URISyntaxException mue) {
135: throw new OpenIDIllegalUriException("openIdConfPath", openIdConfPath);
136: }
137: }
138: }