Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FISH-5743 FISH-5741 define oicd metadata locally #138

Merged
merged 8 commits into from
Nov 11, 2021
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2021 Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://github.com/payara/Payara/blob/master/LICENSE.txt
* See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* The Payara Foundation designates this particular file as subject to the "Classpath"
* exception as provided by the Payara Foundation in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package fish.payara.security.openid;

/**
* Exception thrown during @OpenIdAuthenticationDefinition processing.
*
* @author Petr Aubrecht <[email protected]>
*/
public class OpenIdAuthenticationException extends RuntimeException {

public OpenIdAuthenticationException(String message) {
super(message);
}

public OpenIdAuthenticationException(String message, Throwable cause) {
super(message, cause);
}

}
70 changes: 69 additions & 1 deletion openid/src/main/java/fish/payara/security/openid/OpenIdUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Payara Foundation and/or its affiliates. All rights reserved.
* Copyright (c) [2020-2021] Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
Expand Down Expand Up @@ -37,23 +37,91 @@
*/
package fish.payara.security.openid;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import static java.util.Objects.isNull;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.el.ELProcessor;
import javax.enterprise.inject.spi.BeanManager;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonString;
import static javax.json.JsonValue.ValueType.STRING;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.eclipse.microprofile.config.Config;

/**
* Utility class for evaluation of OpenId values.
*
* @author Gaurav Gupta
* @author Petr Aubrecht
*/
public final class OpenIdUtil {

private static final String SET_DELIMITER = "|";
private static final String SET_DELIMITER_REGEX = "[|]";

private OpenIdUtil() {
}

public static String readConfiguredValueFromMetadataOrProvider(String metadataValue, JsonObject providerDocument, String openIdConstant, Config provider, String openIdProviderMetadataName) {
String value;
if (isEmpty(metadataValue) && providerDocument.containsKey(openIdConstant)) {
value = getConfiguredValue(String.class, providerDocument.getString(openIdConstant), provider, openIdProviderMetadataName);
} else {
value = getConfiguredValue(String.class, metadataValue, provider, openIdProviderMetadataName);
}
return value;
}

public static Set<String> readConfiguredValueFromMetadataOrProvider(String[] metadataValue, JsonObject providerDocument, String openIdConstant, Config provider, String openIdProviderMetadataName) {
Set<String> value;
// PayaraConfig can contain strings from microprofile config, e.g. parse set with '|' as separator.
if (metadataValue.length == 0 && providerDocument.containsKey(openIdConstant)) {
value = parseSet(getConfiguredValue(String.class, null, provider, openIdProviderMetadataName));
if (value == null) {
value = getValues(providerDocument, openIdConstant);
}
} else {
Set<String> metadataValueSet = Stream.of(metadataValue).collect(Collectors.toSet());
value = parseSet(getConfiguredValue(String.class, null, provider, openIdProviderMetadataName));
if (value == null) {
value = metadataValueSet;
}
}
return value;
}

private static Set<String> parseSet(String val) {
if (val == null) {
return null;
} else {
Set<String> set = new HashSet<>();
set.addAll(Arrays.asList(val.split(SET_DELIMITER_REGEX)));
return set;
}
}

private static Set<String> getValues(JsonObject document, String key) {
JsonArray jsonArray = document.getJsonArray(key);
if (isNull(jsonArray)) {
return Collections.emptySet();
} else {
return jsonArray
.stream()
.filter(element -> element.getValueType() == STRING)
.map(element -> (JsonString) element)
.map(JsonString::getString)
.collect(Collectors.toSet());
}
}

public static <T> T getConfiguredValue(Class<T> type, T value, Config provider, String mpConfigKey) {
T result = value;
Optional<T> configResult = provider.getOptionalValue(mpConfigKey, type);
Expand Down
Loading