Search in sources :

Example 1 with JWK

use of io.vertx.ext.jwt.JWK in project vertx-auth by vert-x3.

the class OAuth2AuthProviderImpl method loadJWK.

@Override
public OAuth2Auth loadJWK(Handler<AsyncResult<Void>> handler) {
    if (config.getJwkPath() == null) {
        handler.handle(Future.succeededFuture());
    } else {
        final JsonObject headers = new JsonObject();
        // specify preferred accepted content type
        headers.put("Accept", "application/json");
        fetch(this, HttpMethod.GET, config.getJwkPath(), headers, null, res -> {
            if (res.failed()) {
                handler.handle(Future.failedFuture(res.cause()));
                return;
            }
            final OAuth2Response reply = res.result();
            if (reply.body() == null || reply.body().length() == 0) {
                handler.handle(Future.failedFuture("No Body"));
                return;
            }
            JsonObject json;
            if (reply.is("application/json")) {
                try {
                    json = reply.jsonObject();
                } catch (RuntimeException e) {
                    handler.handle(Future.failedFuture(e));
                    return;
                }
            } else {
                handler.handle(Future.failedFuture("Cannot handle content type: " + reply.headers().get("Content-Type")));
                return;
            }
            try {
                if (json.containsKey("error")) {
                    String description;
                    Object error = json.getValue("error");
                    if (error instanceof JsonObject) {
                        description = ((JsonObject) error).getString("message");
                    } else {
                        // attempt to handle the error as a string
                        try {
                            description = json.getString("error_description", json.getString("error"));
                        } catch (RuntimeException e) {
                            description = error.toString();
                        }
                    }
                    handler.handle(Future.failedFuture(description));
                } else {
                    JsonArray keys = json.getJsonArray("keys");
                    for (Object key : keys) {
                        jwt.addJWK(new JWK((JsonObject) key));
                    }
                    // as of this moment we can handle JWTs
                    config.setJWTToken(true);
                    handler.handle(Future.succeededFuture());
                }
            } catch (RuntimeException e) {
                handler.handle(Future.failedFuture(e));
            }
        });
    }
    return this;
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject) JWK(io.vertx.ext.jwt.JWK)

Example 2 with JWK

use of io.vertx.ext.jwt.JWK in project vertx-zero by silentbalanceyh.

the class UxJwt method createDirect.

private static JWT createDirect(final JWTAuthOptions config, final Function<String, Buffer> funcBuffer) {
    final JWT reference;
    final KeyStoreOptions keyStore = config.getKeyStore();
    try {
        if (keyStore != null) {
            final KeyStore ks = KeyStore.getInstance(keyStore.getType());
            final Class var5 = JwtAuthProvider.class;
            synchronized (JwtAuthProvider.class) {
                final Buffer keystore = funcBuffer.apply(keyStore.getPath());
                final InputStream in = new ByteArrayInputStream(keystore.getBytes());
                Throwable var8 = null;
                try {
                    ks.load(in, keyStore.getPassword().toCharArray());
                } catch (final Throwable var20) {
                    var8 = var20;
                    throw var20;
                } finally {
                    if (in != null) {
                        if (var8 != null) {
                            try {
                                in.close();
                            } catch (final Throwable var19) {
                                var8.addSuppressed(var19);
                            }
                        } else {
                            in.close();
                        }
                    }
                }
            }
            reference = new JWT(ks, keyStore.getPassword().toCharArray());
        } else {
            reference = new JWT();
            final List<PubSecKeyOptions> keys = config.getPubSecKeys();
            if (keys != null) {
                final Iterator var25 = config.getPubSecKeys().iterator();
                while (var25.hasNext()) {
                    final PubSecKeyOptions pubSecKey = (PubSecKeyOptions) var25.next();
                    if (pubSecKey.isSymmetric()) {
                        reference.addJWK(new JWK(pubSecKey.getAlgorithm(), pubSecKey.getPublicKey()));
                    } else {
                        reference.addJWK(new JWK(pubSecKey.getAlgorithm(), pubSecKey.isCertificate(), pubSecKey.getPublicKey(), pubSecKey.getSecretKey()));
                    }
                }
            }
            final List<SecretOptions> secrets = config.getSecrets();
            if (secrets != null) {
                final Iterator var28 = secrets.iterator();
                while (var28.hasNext()) {
                    final SecretOptions secret = (SecretOptions) var28.next();
                    reference.addSecret(secret.getType(), secret.getSecret());
                }
            }
        }
    } catch (IOException | FileSystemException | CertificateException | NoSuchAlgorithmException | KeyStoreException var23) {
        throw new _500JwtRuntimeException(UxJwt.class, var23);
    }
    return reference;
}
Also used : Buffer(io.vertx.core.buffer.Buffer) io.vertx.up.exception._500JwtRuntimeException(io.vertx.up.exception._500JwtRuntimeException) PubSecKeyOptions(io.vertx.ext.auth.PubSecKeyOptions) JWT(io.vertx.ext.jwt.JWT) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CertificateException(java.security.cert.CertificateException) KeyStoreOptions(io.vertx.ext.auth.KeyStoreOptions) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) KeyStore(java.security.KeyStore) SecretOptions(io.vertx.ext.auth.SecretOptions) FileSystemException(io.vertx.core.file.FileSystemException) ByteArrayInputStream(java.io.ByteArrayInputStream) JwtAuthProvider(io.vertx.up.secure.provider.JwtAuthProvider) Iterator(java.util.Iterator) JWK(io.vertx.ext.jwt.JWK)

Aggregations

JWK (io.vertx.ext.jwt.JWK)2 Buffer (io.vertx.core.buffer.Buffer)1 FileSystemException (io.vertx.core.file.FileSystemException)1 JsonArray (io.vertx.core.json.JsonArray)1 JsonObject (io.vertx.core.json.JsonObject)1 KeyStoreOptions (io.vertx.ext.auth.KeyStoreOptions)1 PubSecKeyOptions (io.vertx.ext.auth.PubSecKeyOptions)1 SecretOptions (io.vertx.ext.auth.SecretOptions)1 JWT (io.vertx.ext.jwt.JWT)1 io.vertx.up.exception._500JwtRuntimeException (io.vertx.up.exception._500JwtRuntimeException)1 JwtAuthProvider (io.vertx.up.secure.provider.JwtAuthProvider)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 KeyStore (java.security.KeyStore)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 CertificateException (java.security.cert.CertificateException)1 Iterator (java.util.Iterator)1