Search in sources :

Example 1 with SecretOptions

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

the class JWTAuthProviderTest method testGenerateNewTokenWithMacSecret.

@Test
public void testGenerateNewTokenWithMacSecret() {
    authProvider = JWTAuth.create(vertx, new JWTAuthOptions().addSecret(new SecretOptions().setType("HS256").setSecret("notasecret")));
    String token = authProvider.generateToken(new JsonObject(), new JWTOptions().setAlgorithm("HS256"));
    assertNotNull(token);
    // reverse
    JsonObject authInfo = new JsonObject().put("jwt", token);
    authProvider.authenticate(authInfo, onSuccess(res -> {
        assertNotNull(res);
        testComplete();
    }));
    await();
}
Also used : SecretOptions(io.vertx.ext.auth.SecretOptions) 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) Test(org.junit.Test)

Example 2 with SecretOptions

use of io.vertx.ext.auth.SecretOptions 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 3 with SecretOptions

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

the class JWTAuthProviderTest method testValidateTokenWithInvalidMacSecret.

@Test
public void testValidateTokenWithInvalidMacSecret() {
    String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1MDE3ODUyMDZ9.08K_rROcCmKTF1cKfPCli2GQFYIOP8dePxeS1SE4dc8";
    authProvider = JWTAuth.create(vertx, new JWTAuthOptions().addSecret(new SecretOptions().setType("HS256").setSecret("a bad secret")));
    JsonObject authInfo = new JsonObject().put("jwt", token);
    authProvider.authenticate(authInfo, onFailure(res -> {
        assertNotNull(res);
        testComplete();
    }));
    await();
}
Also used : SecretOptions(io.vertx.ext.auth.SecretOptions) 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) Test(org.junit.Test)

Example 4 with SecretOptions

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

the class JWTAuthProviderTest method testValidateTokenWithValidMacSecret.

@Test
public void testValidateTokenWithValidMacSecret() {
    String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1MDE3ODUyMDZ9.08K_rROcCmKTF1cKfPCli2GQFYIOP8dePxeS1SE4dc8";
    authProvider = JWTAuth.create(vertx, new JWTAuthOptions().addSecret(new SecretOptions().setType("HS256").setSecret("notasecret")));
    JsonObject authInfo = new JsonObject().put("jwt", token);
    authProvider.authenticate(authInfo, onSuccess(res -> {
        assertNotNull(res);
        testComplete();
    }));
    await();
}
Also used : SecretOptions(io.vertx.ext.auth.SecretOptions) 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) Test(org.junit.Test)

Aggregations

KeyStoreOptions (io.vertx.ext.auth.KeyStoreOptions)4 SecretOptions (io.vertx.ext.auth.SecretOptions)4 JsonArray (io.vertx.core.json.JsonArray)3 JsonObject (io.vertx.core.json.JsonObject)3 JWTAuth (io.vertx.ext.auth.jwt.JWTAuth)3 JWTAuthOptions (io.vertx.ext.auth.jwt.JWTAuthOptions)3 JWTOptions (io.vertx.ext.jwt.JWTOptions)3 VertxTestBase (io.vertx.test.core.VertxTestBase)3 StandardCharsets (java.nio.charset.StandardCharsets)3 Base64 (java.util.Base64)3 Assert.assertNotEquals (org.junit.Assert.assertNotEquals)3 Test (org.junit.Test)3 Buffer (io.vertx.core.buffer.Buffer)1 FileSystemException (io.vertx.core.file.FileSystemException)1 PubSecKeyOptions (io.vertx.ext.auth.PubSecKeyOptions)1 JWK (io.vertx.ext.jwt.JWK)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