use of io.vertx.ext.jwt.JWTOptions in project vertx-auth by vert-x3.
the class JWTAuthProviderTest method testBadIssuer.
@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();
}
use of io.vertx.ext.jwt.JWTOptions in project vertx-auth by vert-x3.
the class JWTAuthProviderTest method testLeeway4.
@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();
}
use of io.vertx.ext.jwt.JWTOptions in project vertx-auth by vert-x3.
the class JWTAuthProviderTest method testTokenWithTimestamp.
@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();
}
use of io.vertx.ext.jwt.JWTOptions in project vertx-auth by vert-x3.
the class JWTAuthProviderTest method testGoodIssuer.
@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();
}
use of io.vertx.ext.jwt.JWTOptions in project vertx-examples by vert-x3.
the class Server method start.
@Override
public void start() throws Exception {
Router router = Router.router(vertx);
// Create a JWT Auth Provider
JWTAuth jwt = JWTAuth.create(vertx, new JsonObject().put("keyStore", new JsonObject().put("type", "jceks").put("path", "keystore.jceks").put("password", "secret")));
// protect the API
router.route("/api/*").handler(JWTAuthHandler.create(jwt, "/api/newToken"));
// this route is excluded from the auth handler
router.get("/api/newToken").handler(ctx -> {
ctx.response().putHeader("Content-Type", "text/plain");
ctx.response().end(jwt.generateToken(new JsonObject(), new JWTOptions().setExpiresInSeconds(60)));
});
// this is the secret API
router.get("/api/protected").handler(ctx -> {
ctx.response().putHeader("Content-Type", "text/plain");
ctx.response().end("a secret you should keep for yourself...");
});
// Serve the non private static pages
router.route().handler(StaticHandler.create());
vertx.createHttpServer().requestHandler(router).listen(8080);
}
Aggregations