Search in sources :

Example 6 with DeploymentException

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);
    }
}
Also used : JsonArray(javax.json.JsonArray) JoseException(org.jose4j.lang.JoseException) StringReader(java.io.StringReader) JsonValue(javax.json.JsonValue) JsonObject(javax.json.JsonObject) DeploymentException(javax.enterprise.inject.spi.DeploymentException) JsonWebKeySet(org.jose4j.jwk.JsonWebKeySet) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) URISyntaxException(java.net.URISyntaxException) DeploymentException(javax.enterprise.inject.spi.DeploymentException) IOException(java.io.IOException) JoseException(org.jose4j.lang.JoseException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) JsonParsingException(javax.json.stream.JsonParsingException) JsonWebKey(org.jose4j.jwk.JsonWebKey) Key(java.security.Key) JsonParsingException(javax.json.stream.JsonParsingException)

Example 7 with DeploymentException

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);
    }
}
Also used : JoseException(org.jose4j.lang.JoseException) StringReader(java.io.StringReader) JsonWebKey(org.jose4j.jwk.JsonWebKey) JsonObject(javax.json.JsonObject) DeploymentException(javax.enterprise.inject.spi.DeploymentException) JsonParsingException(javax.json.stream.JsonParsingException)

Example 8 with DeploymentException

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);
    }
}
Also used : DeploymentException(javax.enterprise.inject.spi.DeploymentException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) File(java.io.File) URL(java.net.URL)

Example 9 with DeploymentException

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;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ConfigValueResolver(fish.payara.nucleus.microprofile.config.spi.ConfigValueResolver) Config(org.eclipse.microprofile.config.Config) List(java.util.List) Supplier(java.util.function.Supplier) DeploymentException(javax.enterprise.inject.spi.DeploymentException) OptionalInt(java.util.OptionalInt)

Example 10 with DeploymentException

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");
    }
}
Also used : JsonArray(javax.json.JsonArray) ECGenParameterSpec(java.security.spec.ECGenParameterSpec) JsonObject(javax.json.JsonObject) BigInteger(java.math.BigInteger) DeploymentException(javax.enterprise.inject.spi.DeploymentException) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) ECPoint(java.security.spec.ECPoint) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) AlgorithmParameters(java.security.AlgorithmParameters)

Aggregations

DeploymentException (javax.enterprise.inject.spi.DeploymentException)14 InjectionPoint (javax.enterprise.inject.spi.InjectionPoint)4 JsonObject (javax.json.JsonObject)4 Claim (org.eclipse.microprofile.jwt.Claim)4 IOException (java.io.IOException)3 ParameterizedType (java.lang.reflect.ParameterizedType)3 Type (java.lang.reflect.Type)3 URISyntaxException (java.net.URISyntaxException)3 JsonArray (javax.json.JsonArray)3 File (java.io.File)2 StringReader (java.io.StringReader)2 BigInteger (java.math.BigInteger)2 URL (java.net.URL)2 HashMap (java.util.HashMap)2 Properties (java.util.Properties)2 ProcessInjectionPoint (javax.enterprise.inject.spi.ProcessInjectionPoint)2 JsonParsingException (javax.json.stream.JsonParsingException)2 JsonWebKey (org.jose4j.jwk.JsonWebKey)2 JoseException (org.jose4j.lang.JoseException)2 ConfigValueResolver (fish.payara.nucleus.microprofile.config.spi.ConfigValueResolver)1