use of javax.enterprise.inject.spi.DeploymentException in project tomee by apache.
the class PublicKeyResolver method parseJwks.
private Map<String, Key> parseJwks(final String publicKey) {
final JsonObject jwks;
try {
jwks = Json.createReader(new StringReader(publicKey)).readObject();
} catch (final JsonParsingException e) {
return Collections.emptyMap();
}
try {
final JsonArray keys = jwks.getJsonArray(JWK_SET_MEMBER_NAME);
for (final JsonValue key : keys) {
validateJwk(key.asJsonObject());
}
} catch (final Exception e) {
throw new DeploymentException("MicroProfile Public Key JWKS invalid format.");
}
try {
final JsonWebKeySet keySet = new JsonWebKeySet(publicKey);
final Map<String, Key> keys = keySet.getJsonWebKeys().stream().collect(Collectors.toMap(JsonWebKey::getKeyId, JsonWebKey::getKey));
return Collections.unmodifiableMap(keys);
} catch (final JoseException e) {
throw new DeploymentException(JWTAuthConfigurationProperties.PUBLIC_KEY_ERROR + " JWK.", e);
}
}
use of javax.enterprise.inject.spi.DeploymentException in project tomee by apache.
the class PublicKeyResolver method parseJwk.
private Map<String, Key> parseJwk(final String publicKey) {
final JsonObject jwk;
try {
jwk = Json.createReader(new StringReader(publicKey)).readObject();
} catch (final JsonParsingException e) {
return Collections.emptyMap();
}
if (jwk.containsKey(JWK_SET_MEMBER_NAME)) {
return Collections.emptyMap();
}
validateJwk(jwk);
try {
final JsonWebKey key = JsonWebKey.Factory.newJwk(publicKey);
return Collections.singletonMap(key.getKeyId(), key.getKey());
} catch (final JoseException e) {
throw new DeploymentException(JWTAuthConfigurationProperties.PUBLIC_KEY_ERROR + " JWK.", e);
}
}
use of javax.enterprise.inject.spi.DeploymentException in project tomee by apache.
the class PublicKeyResolver method readPublicKeysFromFile.
private Optional<String> readPublicKeysFromFile(final String publicKeyLocation) {
if (!publicKeyLocation.startsWith("file")) {
return Optional.empty();
}
try {
final URL locationURL = new URL(publicKeyLocation);
final File publicKeyFile = new File(locationURL.toURI());
if (!publicKeyFile.exists() || publicKeyFile.isDirectory()) {
throw new DeploymentException(JWTAuthConfigurationProperties.PUBLIC_KEY_ERROR_LOCATION + publicKeyLocation + ". File does not exist or it is a directory.");
}
return Optional.of(readPublicKeyFromInputStream(locationURL.openStream()));
} catch (final IOException | URISyntaxException e) {
throw new DeploymentException(JWTAuthConfigurationProperties.PUBLIC_KEY_ERROR_LOCATION + publicKeyLocation, e);
}
}
use of javax.enterprise.inject.spi.DeploymentException in project Payara by payara.
the class ConfigPropertyProducer method getGenericPropertyFromModel.
public static final Object getGenericPropertyFromModel(ConfigPropertyModel property) {
Object result = null;
Config config = ConfigProvider.getConfig();
String name = property.getName();
Type type = property.getInjectionPoint().getType();
String defaultValue = property.getDefaultValue();
if (type instanceof Class) {
if (type == OptionalDouble.class) {
result = config.getValue(property.getName(), ConfigValueResolver.class).throwOnFailedConversion().withDefault(property.getDefaultValue()).as(OptionalDouble.class).orElse(OptionalDouble.empty());
} else if (type == OptionalInt.class) {
result = config.getValue(property.getName(), ConfigValueResolver.class).throwOnFailedConversion().withDefault(property.getDefaultValue()).as(OptionalInt.class).orElse(OptionalInt.empty());
} else if (type == OptionalLong.class) {
result = config.getValue(property.getName(), ConfigValueResolver.class).throwOnFailedConversion().withDefault(property.getDefaultValue()).as(OptionalLong.class).orElse(OptionalLong.empty());
} else {
result = config.getValue(name, ConfigValueResolver.class).throwOnMissingProperty(defaultValue == null).throwOnFailedConversion().withDefault(defaultValue).withPolicy(FAIL).as((Class<?>) type).get();
}
} else if (type instanceof ParameterizedType) {
ParameterizedType ptype = (ParameterizedType) type;
Type rawType = ptype.getRawType();
if (List.class.equals(rawType)) {
result = config.getValue(name, ConfigValueResolver.class).throwOnMissingProperty(defaultValue == null).throwOnFailedConversion().withDefault(defaultValue).withPolicy(FAIL).asList(getElementTypeFrom(ptype));
} else if (Set.class.equals(rawType)) {
result = config.getValue(name, ConfigValueResolver.class).throwOnMissingProperty(defaultValue == null).throwOnFailedConversion().withDefault(defaultValue).withPolicy(FAIL).asSet(getElementTypeFrom(ptype));
} else if (Supplier.class.equals(rawType)) {
result = config.getValue(name, ConfigValueResolver.class).throwOnMissingProperty(defaultValue == null).throwOnFailedConversion().withDefault(defaultValue).withPolicy(FAIL).asSupplier(getElementTypeFrom(ptype));
} else if (Optional.class.equals(rawType)) {
result = config.getValue(name, ConfigValueResolver.class).throwOnMissingProperty(false).throwOnFailedConversion().withDefault(defaultValue).withPolicy(FAIL).as(getElementTypeFrom(ptype));
} else {
result = config.getValue(name, (Class<?>) rawType);
}
}
if (result == null) {
throw new DeploymentException("Microprofile Config Property " + property.getName() + " can not be found");
}
return result;
}
use of javax.enterprise.inject.spi.DeploymentException in project Payara by payara.
the class JwtPublicKeyStore method createPublicKeyFromJWKS.
private PublicKey createPublicKeyFromJWKS(String jwksValue, String keyID) throws Exception {
JsonObject jwks = JwtKeyStoreUtils.parseJwks(jwksValue);
JsonArray keys = jwks.getJsonArray("keys");
JsonObject jwk = keys != null ? JwtKeyStoreUtils.findJwk(keys, keyID) : jwks;
// Check if an RSA or ECDSA key needs to be created
String kty = jwk.getString("kty");
if (kty == null) {
throw new DeploymentException("Could not determine key type - kty field not present");
}
if (kty.equals("RSA")) {
// the public exponent
byte[] exponentBytes = Base64.getUrlDecoder().decode(jwk.getString("e"));
BigInteger exponent = new BigInteger(1, exponentBytes);
// the modulus
byte[] modulusBytes = Base64.getUrlDecoder().decode(jwk.getString("n"));
BigInteger modulus = new BigInteger(1, modulusBytes);
RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(modulus, exponent);
return KeyFactory.getInstance(RSA_ALGORITHM).generatePublic(publicKeySpec);
} else if (kty.equals("EC")) {
// Get x and y to create EC point
byte[] xBytes = Base64.getUrlDecoder().decode(jwk.getString("x"));
BigInteger x = new BigInteger(1, xBytes);
byte[] yBytes = Base64.getUrlDecoder().decode(jwk.getString("y"));
BigInteger y = new BigInteger(1, yBytes);
ECPoint ecPoint = new ECPoint(x, y);
// Get params
AlgorithmParameters parameters = AlgorithmParameters.getInstance(EC_ALGORITHM);
String crv = jwk.getString("crv");
if (!crv.equals("P-256")) {
throw new DeploymentException("Could not get EC key from JWKS: crv does not equal P-256");
}
parameters.init(new ECGenParameterSpec("secp256r1"));
ECPublicKeySpec publicKeySpec = new ECPublicKeySpec(ecPoint, parameters.getParameterSpec(ECParameterSpec.class));
return KeyFactory.getInstance(EC_ALGORITHM).generatePublic(publicKeySpec);
} else {
throw new DeploymentException("Could not determine key type - JWKS kty field does not equal RSA or EC");
}
}
Aggregations