Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package io.quarkus.security.runtime;
import static io.quarkus.security.runtime.QuarkusSecurityRolesAllowedConfigBuilder.transformToKey;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.InvocationTargetException;
import java.security.Permission;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Supplier;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.spi.ConfigProviderResolver;
import io.quarkus.arc.Arc;
import io.quarkus.runtime.RuntimeValue;
import io.quarkus.runtime.ShutdownContext;
import io.quarkus.runtime.annotations.Recorder;
import io.quarkus.security.StringPermission;
import io.quarkus.security.runtime.interceptor.SecurityCheckStorageBuilder;
import io.quarkus.security.runtime.interceptor.SecurityConstrainer;
import io.quarkus.security.runtime.interceptor.check.AuthenticatedCheck;
import io.quarkus.security.runtime.interceptor.check.DenyAllCheck;
import io.quarkus.security.runtime.interceptor.check.PermissionSecurityCheck;
import io.quarkus.security.runtime.interceptor.check.PermitAllCheck;
import io.quarkus.security.runtime.interceptor.check.RolesAllowedCheck;
import io.quarkus.security.runtime.interceptor.check.SupplierRolesAllowedCheck;
import io.quarkus.security.spi.runtime.AuthorizationFailureEvent;
import io.quarkus.security.spi.runtime.AuthorizationSuccessEvent;
import io.quarkus.security.spi.runtime.SecurityCheck;
import io.quarkus.security.spi.runtime.SecurityCheckStorage;
import io.smallrye.config.Expressions;
import io.smallrye.config.common.utils.StringUtil;
@Recorder
public class SecurityCheckRecorder {
private static volatile SecurityCheckStorage storage;
private static final Set configExpRolesAllowedChecks = ConcurrentHashMap.newKeySet();
private static volatile boolean runtimeConfigReady = false;
public static SecurityCheckStorage getStorage() {
return storage;
}
public SecurityCheck denyAll() {
return DenyAllCheck.INSTANCE;
}
public SecurityCheck permitAll() {
return PermitAllCheck.INSTANCE;
}
public SecurityCheck rolesAllowed(String... roles) {
return RolesAllowedCheck.of(roles);
}
public SecurityCheck rolesAllowedSupplier(String[] allowedRoles, int[] configExpIndexes, int[] configKeys) {
// here we add generated keys and values with the property expressions to the config source,
// the config source will be registered with the Config system,
// and we get all features available from Config
for (int i = 0; i < configExpIndexes.length; i++) {
QuarkusSecurityRolesAllowedConfigBuilder.addProperty(configKeys[i], allowedRoles[configExpIndexes[i]]);
}
final var check = new SupplierRolesAllowedCheck(
resolveRolesAllowedConfigExp(allowedRoles, configExpIndexes, configKeys));
configExpRolesAllowedChecks.add(check);
return check;
}
/* STATIC INIT */
public void recordRolesAllowedConfigExpression(String configExpression, int configKeyIndex,
BiConsumer> configValueRecorder) {
QuarkusSecurityRolesAllowedConfigBuilder.addProperty(configKeyIndex, configExpression);
// one configuration expression resolves to string array because the expression can be list treated as list
Supplier configValSupplier = resolveRolesAllowedConfigExp(new String[] { configExpression },
new int[] { 0 }, new int[] { configKeyIndex });
configValueRecorder.accept(configExpression, configValSupplier);
}
private static Supplier resolveRolesAllowedConfigExp(String[] allowedRoles, int[] configExpIndexes,
int[] configKeys) {
final List roles = new ArrayList<>(Arrays.asList(allowedRoles));
return new Supplier() {
@Override
public String[] get() {
final var config = ConfigProviderResolver.instance().getConfig(Thread.currentThread().getContextClassLoader());
if (config.getOptionalValue(Config.PROPERTY_EXPRESSIONS_ENABLED, Boolean.class).orElse(Boolean.TRUE)
&& Expressions.isEnabled()) {
// property expressions are enabled
for (int i = 0; i < configExpIndexes.length; i++) {
// resolve configuration expressions specified as value of the @RolesAllowed annotation
var strVal = config.getValue(transformToKey(configKeys[i]), String.class);
// treat config value that contains collection separator as a list
// @RolesAllowed({"${my.roles}"}) => my.roles=one,two <=> @RolesAllowed({"one", "two"})
if (strVal != null && strVal.contains(",")) {
var strArr = StringUtil.split(strVal);
if (strArr.length >= 1) {
// role order is irrelevant as logical operator between them is OR
// first role will go to the original place, double escaped comma will be parsed correctly
strVal = strArr[0];
if (strArr.length > 1) {
// the rest of the roles will be appended at the end
for (int i1 = 1; i1 < strArr.length; i1++) {
roles.add(strArr[i1]);
}
}
}
}
roles.set(configExpIndexes[i], strVal);
}
}
return roles.toArray(String[]::new);
}
};
}
public SecurityCheck authenticated() {
return AuthenticatedCheck.INSTANCE;
}
/**
* Creates {@link SecurityCheck} for a single permission.
*
* @return SecurityCheck
*/
public SecurityCheck permissionsAllowed(Function