JWTAuth jwt = JWTAuth.create(vertx, new JsonObject() .put("keyStore", new JsonObject() .put("type", "jceks")
JWTAuth jwt = JWTAuth.create(vertx, new JsonObject() .put("keyStore", new JsonObject() .put("type", "jceks")
JWTAuth jwt = JWTAuth.create(vertx, new JsonObject() .put("keyStore", new JsonObject() .put("type", "jceks")
@Before public void setup() throws Exception { JsonObject authConfig = new JsonObject().put("keyStore", new JsonObject() .put("type", "jceks") .put("path", "keystore.jceks") .put("password", "secret")); authProvider = JWTAuth.create(vertx, new JWTAuthOptions(authConfig)); }
public void start() { System.out.println(this); JWTAuth.create(vertx, config); } }
/** * Create a JWT auth provider * @param vertx the Vertx instance * @param config the config * @return the auth provider */ public static io.vertx.rxjava.ext.auth.jwt.JWTAuth create(io.vertx.rxjava.core.Vertx vertx, JWTAuthOptions config) { io.vertx.rxjava.ext.auth.jwt.JWTAuth ret = io.vertx.rxjava.ext.auth.jwt.JWTAuth.newInstance(io.vertx.ext.auth.jwt.JWTAuth.create(vertx.getDelegate(), config)); return ret; }
/** * Create a JWT auth provider * @param vertx the Vertx instance * @param config the config * @return the auth provider */ public static io.vertx.rxjava.ext.auth.jwt.JWTAuth create(io.vertx.rxjava.core.Vertx vertx, JWTAuthOptions config) { io.vertx.rxjava.ext.auth.jwt.JWTAuth ret = io.vertx.rxjava.ext.auth.jwt.JWTAuth.newInstance(io.vertx.ext.auth.jwt.JWTAuth.create(vertx.getDelegate(), config)); return ret; }
@Inject public MeshJWTAuthProvider(Vertx vertx, BCryptPasswordEncoder passwordEncoder, Database database, BootstrapInitializer boot) { this.passwordEncoder = passwordEncoder; this.db = database; this.boot = boot; // Use the mesh JWT options in order to setup the JWTAuth provider AuthenticationOptions options = Mesh.mesh().getOptions().getAuthenticationOptions(); String keystorePassword = options.getKeystorePassword(); if (keystorePassword == null) { throw new RuntimeException("The keystore password could not be found within the authentication options."); } String keyStorePath = options.getKeystorePath(); String type = "jceks"; JsonObject config = new JsonObject().put("keyStore", new JsonObject().put("path", keyStorePath).put("type", type).put("password", keystorePassword)); jwtProvider = JWTAuth.create(vertx, config); }
public static io.vertx.ext.auth.jwt.JWTAuth create(io.vertx.ext.auth.jwt.JWTAuth j_receiver, io.vertx.core.Vertx vertx, java.util.Map<String, Object> config) { return io.vertx.core.impl.ConversionHelper.fromObject(io.vertx.ext.auth.jwt.JWTAuth.create(vertx, config != null ? new io.vertx.ext.auth.jwt.JWTAuthOptions(io.vertx.core.impl.ConversionHelper.toJsonObject(config)) : null)); } }
@Before public void setup() throws Exception { JsonObject authConfig = new JsonObject().put("keyStore", new JsonObject() .put("type", "jceks") .put("path", "keystore.jceks") .put("password", "secret")); authProvider = JWTAuth.create(vertx, new JWTAuthOptions(authConfig)); }
@Test public void testJWKShouldNotCrash() { authProvider = JWTAuth.create(vertx, new JWTAuthOptions().addJwk( new JsonObject() .put("kty", "RSA") .put("n", "0vx7agoebGcQSuuPiLJXZptN9nndrQmbXEps2aiAFbWhM78LhWx4cbbfAAtVT86zwu1RK7aPFFxuhDR1L6tSoc_BJECPebWKRXjBZCiFV4n3oknjhMstn64tZ_2W-5JsGY4Hc5n9yBXArwl93lqt7_RN5w6Cf0h4QyQ5v-65YGjQR0_FDW2QvzqY368QQMicAtaSqzs8KJZgnYb9c7d0zgdAZHzu6qMQvRL5hajrn1n91CbOpbISD08qNLyrdkt-bFTWhAI4vMQFh6WeZu0fM4lFd2NcRwr3XPksINHaQ-G_xBniIqbw0Ls1jF44-csFCur-kEgU8awapJzKnqDKgw") .put("e", "AQAB") .put("alg", "RS256") .put("kid", "2011-04-29"))); } }
@Override public void setUp() throws Exception { super.setUp(); authProvider = JWTAuth.create(vertx, getConfig()); }
@Test public void testLeeway3() { authProvider = JWTAuth.create(vertx, getConfig().setJWTOptions(new JWTOptions().setLeeway(5))); long now = System.currentTimeMillis() / 1000; JsonObject payload = new JsonObject() .put("sub", "Paulo") .put("exp", now) .put("iat", now); String token = authProvider.generateToken(payload); assertNotNull(token); JsonObject authInfo = new JsonObject().put("jwt", token); // fail because exp is <= to now authProvider.authenticate(authInfo, onSuccess(t -> testComplete())); await(); }
@Test public void testLeeway2() { authProvider = JWTAuth.create(vertx, getConfig().setJWTOptions(new JWTOptions().setLeeway(0))); long now = (System.currentTimeMillis() / 1000) + 2; JsonObject payload = new JsonObject() .put("sub", "Paulo") .put("iat", now); String token = authProvider.generateToken(payload); assertNotNull(token); JsonObject authInfo = new JsonObject().put("jwt", token); // fail because iat is > now (clock drifted 2 sec) authProvider.authenticate(authInfo, onFailure(t -> testComplete())); await(); }
@Test public void testLeeway() { authProvider = JWTAuth.create(vertx, getConfig().setJWTOptions(new JWTOptions().setLeeway(0))); long now = System.currentTimeMillis() / 1000; JsonObject payload = new JsonObject() .put("sub", "Paulo") .put("exp", now); String token = authProvider.generateToken(payload); assertNotNull(token); JsonObject authInfo = new JsonObject().put("jwt", token); // fail because exp is <= to now authProvider.authenticate(authInfo, onFailure(t -> testComplete())); await(); }
@Test public void testLeeway4() { authProvider = JWTAuth.create(vertx, getConfig().setJWTOptions(new JWTOptions().setLeeway(5))); long now = (System.currentTimeMillis() / 1000) + 2; JsonObject payload = new JsonObject() .put("sub", "Paulo") .put("iat", now); String token = authProvider.generateToken(payload); assertNotNull(token); JsonObject authInfo = new JsonObject().put("jwt", token); // pass because iat is > now (clock drifted 2 sec) and we have a leeway of 5sec authProvider.authenticate(authInfo, onSuccess(t -> testComplete())); await(); }
@Test public void testBadIssuer() { authProvider = JWTAuth.create(vertx, getConfig().setJWTOptions(new JWTOptions().setIssuer("https://vertx.io"))); JsonObject payload = new JsonObject().put("sub", "Paulo"); final String token = authProvider.generateToken(payload, new JWTOptions().setIssuer("https://auth0.io")); assertNotNull(token); JsonObject authInfo = new JsonObject() .put("jwt", token); authProvider.authenticate(authInfo, onFailure(thr -> { assertNotNull(thr); testComplete(); })); await(); }
@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(); }
@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(); }
@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(); }