JWTAuth jwt = JWTAuth.create(vertx, new JsonObject() .put("keyStore", new JsonObject() .put("type", "jceks") ctx.response().end(jwt.generateToken(new JsonObject(), new JWTOptions().setExpiresInSeconds(60))); });
@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(); }
/** * Generate a new JWT token. * @param claims Json with user defined claims for a list of official claims * @param options extra options for the generation * @return JWT encoded token */ public String generateToken(JsonObject claims, JWTOptions options) { String ret = delegate.generateToken(claims, options); return ret; }
@Test public void testGoodIssuer() { JsonObject payload = new JsonObject() .put("sub", "Paulo"); final String token = authProvider.generateToken(payload, new JWTOptions().setIssuer("https://vertx.io")); assertNotNull(token); JsonObject authInfo = new JsonObject() .put("jwt", token) .put("options", new JsonObject() .put("issuer", "https://vertx.io")); authProvider.authenticate(authInfo, onSuccess(res -> { assertNotNull(res); testComplete(); })); await(); }
@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 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(); }
jwtAuth.authenticate(new JsonObject().put("jwt", authorization), authenticate -> { if (authenticate.failed()) { fut.fail(new ReplyException(ReplyFailure.RECIPIENT_FAILURE, 500, authenticate.cause().getMessage()));
@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(); }
/** * Generate a new JWT token. * @param claims Json with user defined claims for a list of official claims * @return JWT encoded token */ public String generateToken(JsonObject claims) { String ret = delegate.generateToken(claims); return ret; }
@Test public void testTokenWithTimestamp() { JsonObject payload = new JsonObject() .put("sub", "Paulo"); final String token = authProvider.generateToken(payload, new JWTOptions()); assertNotNull(token); JsonObject authInfo = new JsonObject().put("jwt", token); authProvider.authenticate(authInfo, onSuccess(res -> { assertNotNull(res); assertTrue(res.principal().containsKey("iat")); testComplete(); })); await(); }
public void start() { System.out.println(this); JWTAuth.create(vertx, config); } }
@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(); }
jwtProvider.authenticate(authInfo, rh -> { if (rh.failed()) { if (log.isDebugEnabled()) {
@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(); }
/** * Generate a new JWT token. * @param claims Json with user defined claims for a list of official claims * @param options extra options for the generation * @return JWT encoded token */ public String generateToken(JsonObject claims, JWTOptions options) { String ret = delegate.generateToken(claims, options); return ret; }
JWTAuth jwt = JWTAuth.create(vertx, new JsonObject() .put("keyStore", new JsonObject() .put("type", "jceks") ctx.response().end(jwt.generateToken(new JsonObject(), new JWTOptions().setExpiresInSeconds(60).setPermissions (authorities))); });
@Test public void testGoodAudience() { JsonObject payload = new JsonObject() .put("sub", "Paulo"); final String token = authProvider.generateToken(payload, new JWTOptions().addAudience("a").addAudience("b").addAudience("c")); assertNotNull(token); JsonObject authInfo = new JsonObject() .put("jwt", token) .put("options", new JsonObject() .put("audience", new JsonArray().add("b").add("d"))); authProvider.authenticate(authInfo, onSuccess(res -> { assertNotNull(res); testComplete(); })); await(); }
/** * 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; }
@Test public void testAcceptInvalidJWT() { String[] segments = JWT_INVALID.split("\\."); // All segment should be base64 String headerSeg = segments[0]; // change alg to none JsonObject headerJson = new JsonObject(new String(Base64.getUrlDecoder().decode(headerSeg.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8)); headerJson.put("alg", "none"); headerSeg = Base64.getUrlEncoder().encodeToString(headerJson.encode().getBytes(StandardCharsets.UTF_8)); // fix time exp String payloadSeg = segments[1]; JsonObject bodyJson = new JsonObject(new String(Base64.getUrlDecoder().decode(payloadSeg.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8)); bodyJson.put("exp", System.currentTimeMillis() + 10000); payloadSeg = Base64.getUrlEncoder().encodeToString(headerJson.encode().getBytes(StandardCharsets.UTF_8)); String signatureSeg = segments[2]; // build attack token String attackerJWT = headerSeg + "." + payloadSeg + "." + signatureSeg; JsonObject authInfo = new JsonObject().put("jwt", attackerJWT); authProvider.authenticate(authInfo, onFailure(thr -> { assertNotNull(thr); testComplete(); })); await(); }
@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(); }