Search in sources :

Example 1 with KeyStoreOptions

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

the class JWTAuthProviderTest method testGenerateNewTokenES256.

@Test
public void testGenerateNewTokenES256() {
    authProvider = JWTAuth.create(vertx, new JWTAuthOptions().setKeyStore(new KeyStoreOptions().setPath("es256-keystore.jceks").setPassword("secret")));
    String token = authProvider.generateToken(new JsonObject().put("sub", "paulo"), new JWTOptions().setAlgorithm("ES256"));
    assertNotNull(token);
    JsonObject authInfo = new JsonObject().put("jwt", token);
    authProvider.authenticate(authInfo, res -> {
        if (res.failed()) {
            res.cause().printStackTrace();
            fail();
        }
        assertNotNull(res.result());
        testComplete();
    });
    await();
}
Also used : JWTAuthOptions(io.vertx.ext.auth.jwt.JWTAuthOptions) JsonObject(io.vertx.core.json.JsonObject) JWTOptions(io.vertx.ext.jwt.JWTOptions) KeyStoreOptions(io.vertx.ext.auth.KeyStoreOptions) Test(org.junit.Test)

Example 2 with KeyStoreOptions

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

the class AuthJWTExamples method example6.

public void example6(Vertx vertx) {
    JWTAuthOptions config = new JWTAuthOptions().setKeyStore(new KeyStoreOptions().setPath("keystore.jceks").setPassword("secret"));
    AuthProvider provider = JWTAuth.create(vertx, config);
}
Also used : JWTAuthOptions(io.vertx.ext.auth.jwt.JWTAuthOptions) AuthProvider(io.vertx.ext.auth.AuthProvider) KeyStoreOptions(io.vertx.ext.auth.KeyStoreOptions)

Example 3 with KeyStoreOptions

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

the class AuthJWTExamples method example7.

public void example7(Vertx vertx, String username, String password) {
    JWTAuthOptions config = new JWTAuthOptions().setKeyStore(new KeyStoreOptions().setPath("keystore.jceks").setPassword("secret"));
    JWTAuth provider = JWTAuth.create(vertx, config);
    // on the verify endpoint once you verify the identity of the user by its username/password
    if ("paulo".equals(username) && "super_secret".equals(password)) {
        String token = provider.generateToken(new JsonObject().put("sub", "paulo"), new JWTOptions());
    // now for any request to protected resources you should pass this string in the HTTP header Authorization as:
    // Authorization: Bearer <token>
    }
}
Also used : JWTAuthOptions(io.vertx.ext.auth.jwt.JWTAuthOptions) JsonObject(io.vertx.core.json.JsonObject) JWTOptions(io.vertx.ext.auth.jwt.JWTOptions) KeyStoreOptions(io.vertx.ext.auth.KeyStoreOptions) JWTAuth(io.vertx.ext.auth.jwt.JWTAuth)

Example 4 with KeyStoreOptions

use of io.vertx.ext.auth.KeyStoreOptions 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)

Example 5 with KeyStoreOptions

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

the class JWTAuthProviderTest method testGenerateNewTokenForceAlgorithm.

@Test
public void testGenerateNewTokenForceAlgorithm() {
    authProvider = JWTAuth.create(vertx, new JWTAuthOptions().setKeyStore(new KeyStoreOptions().setPath("gce.jks").setType("jks").setPassword("notasecret")));
    String token = authProvider.generateToken(new JsonObject(), new JWTOptions().setAlgorithm("RS256"));
    assertNotNull(token);
    // reverse
    JsonObject authInfo = new JsonObject().put("jwt", token);
    authProvider.authenticate(authInfo, onSuccess(res -> {
        assertNotNull(res);
        testComplete();
    }));
    await();
}
Also used : KeyStoreOptions(io.vertx.ext.auth.KeyStoreOptions) JsonArray(io.vertx.core.json.JsonArray) Base64(java.util.Base64) JWTOptions(io.vertx.ext.jwt.JWTOptions) SecretOptions(io.vertx.ext.auth.SecretOptions) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test) JWTAuthOptions(io.vertx.ext.auth.jwt.JWTAuthOptions) JWTAuth(io.vertx.ext.auth.jwt.JWTAuth) VertxTestBase(io.vertx.test.core.VertxTestBase) StandardCharsets(java.nio.charset.StandardCharsets) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) JWTAuthOptions(io.vertx.ext.auth.jwt.JWTAuthOptions) JsonObject(io.vertx.core.json.JsonObject) JWTOptions(io.vertx.ext.jwt.JWTOptions) KeyStoreOptions(io.vertx.ext.auth.KeyStoreOptions) Test(org.junit.Test)

Aggregations

KeyStoreOptions (io.vertx.ext.auth.KeyStoreOptions)5 JWTAuthOptions (io.vertx.ext.auth.jwt.JWTAuthOptions)4 JsonObject (io.vertx.core.json.JsonObject)3 SecretOptions (io.vertx.ext.auth.SecretOptions)2 JWTAuth (io.vertx.ext.auth.jwt.JWTAuth)2 JWTOptions (io.vertx.ext.jwt.JWTOptions)2 Test (org.junit.Test)2 Buffer (io.vertx.core.buffer.Buffer)1 FileSystemException (io.vertx.core.file.FileSystemException)1 JsonArray (io.vertx.core.json.JsonArray)1 AuthProvider (io.vertx.ext.auth.AuthProvider)1 PubSecKeyOptions (io.vertx.ext.auth.PubSecKeyOptions)1 JWTOptions (io.vertx.ext.auth.jwt.JWTOptions)1 JWK (io.vertx.ext.jwt.JWK)1 JWT (io.vertx.ext.jwt.JWT)1 VertxTestBase (io.vertx.test.core.VertxTestBase)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