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")));
// this route is excluded from the auth handler (it represents your login endpoint)
router.get("/api/newToken").handler(ctx -> {
List<String> authorities = new ArrayList<>();
for (String authority : ctx.request().params().getAll("authority")) {
authorities.add(authority);
}
ctx.response().putHeader("Content-Type", "text/plain");
ctx.response().end(jwt.generateToken(new JsonObject(), new JWTOptions().setExpiresInSeconds(60).setPermissions(authorities)));
});
// protect the API (any authority is allowed)
router.route("/api/protected").handler(JWTAuthHandler.create(jwt));
router.get("/api/protected").handler(ctx -> {
ctx.response().putHeader("Content-Type", "text/plain");
ctx.response().end("this secret is not defcon!");
});
// protect the API (defcon1 authority is required)
router.route("/api/protected/defcon1").handler(JWTAuthHandler.create(jwt).addAuthority("defcon1"));
router.get("/api/protected/defcon1").handler(ctx -> {
ctx.response().putHeader("Content-Type", "text/plain");
ctx.response().end("this secret is defcon1!");
});
// protect the API (defcon2 authority is required)
router.route("/api/protected/defcon2").handler(JWTAuthHandler.create(jwt).addAuthority("defcon2"));
router.get("/api/protected/defcon2").handler(ctx -> {
ctx.response().putHeader("Content-Type", "text/plain");
ctx.response().end("this secret is defcon2!");
});
// protect the API (defcon3 authority is required)
router.route("/api/protected/defcon3").handler(JWTAuthHandler.create(jwt).addAuthority("defcon3"));
router.get("/api/protected/defcon3").handler(ctx -> {
ctx.response().putHeader("Content-Type", "text/plain");
ctx.response().end("this secret is defcon3!");
});
// Serve the non private static pages
router.route().handler(StaticHandler.create());
vertx.createHttpServer().requestHandler(router).listen(8080);
}
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")));
// this route is excluded from the auth handler (it represents your login endpoint)
router.get("/api/newToken").handler(ctx -> {
List<String> authorities = new ArrayList<>();
for (String authority : ctx.request().params().getAll("authority")) {
authorities.add(authority);
}
ctx.response().putHeader("Content-Type", "text/plain");
ctx.response().end(jwt.generateToken(new JsonObject(), new JWTOptions().setExpiresInSeconds(60).setPermissions(authorities)));
});
router.route("/api/protected*").handler(JWTAuthHandler.create(jwt));
router.get("/api/protected").handler(ctx -> {
// protected the API (any authority is allowed)
ctx.response().putHeader("Content-Type", "text/plain");
ctx.response().end("this secret is not defcon!");
});
router.get("/api/protected/defcon1").handler(ctx -> {
// protect the API (defcon1 authority is required)
ctx.user().isAuthorised("defcon1", allowed -> {
if (allowed.failed()) {
ctx.fail(allowed.cause());
return;
}
// user does not have the required authority
if (!allowed.result()) {
ctx.response().setStatusCode(403).end();
return;
}
ctx.response().putHeader("Content-Type", "text/plain");
ctx.response().end("this secret is defcon1!");
});
});
router.get("/api/protected/defcon2").handler(ctx -> {
// protect the API (defcon2 authority is required)
ctx.user().isAuthorised("defcon2", allowed -> {
if (allowed.failed()) {
ctx.fail(allowed.cause());
return;
}
// user does not have the required authority
if (!allowed.result()) {
ctx.response().setStatusCode(403).end();
return;
}
ctx.response().putHeader("Content-Type", "text/plain");
ctx.response().end("this secret is defcon2!");
});
});
router.get("/api/protected/defcon3").handler(ctx -> {
// protect the API (defcon3 authority is required)
ctx.user().isAuthorised("defcon3", allowed -> {
if (allowed.failed()) {
ctx.fail(allowed.cause());
return;
}
// user does not have the required authority
if (!allowed.result()) {
ctx.response().setStatusCode(403).end();
return;
}
ctx.response().putHeader("Content-Type", "text/plain");
ctx.response().end("this secret is defcon3!");
});
});
// Serve the non private static pages
router.route().handler(StaticHandler.create());
vertx.createHttpServer().requestHandler(router).listen(8080);
}
use of io.vertx.ext.jwt.JWTOptions in project vertx-auth by vert-x3.
the class JWTAuthProviderTest method testBadAudience.
@Test
public void testBadAudience() {
authProvider = JWTAuth.create(vertx, getConfig().setJWTOptions(new JWTOptions().addAudience("e").addAudience("d")));
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);
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 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();
}
use of io.vertx.ext.jwt.JWTOptions in project vertx-auth by vert-x3.
the class JWTAuthProviderTest method testAlgNone.
@Test
public void testAlgNone() {
JWTAuth authProvider = JWTAuth.create(vertx, new JWTAuthOptions());
JsonObject payload = new JsonObject().put("sub", "UserUnderTest").put("aud", "OrganizationUnderTest").put("iat", 1431695313).put("exp", 1747055313).put("roles", new JsonArray().add("admin").add("developer").add("user")).put("permissions", new JsonArray().add("read").add("write").add("execute"));
final String token = authProvider.generateToken(payload, new JWTOptions().setSubject("UserUnderTest").setAlgorithm("none"));
assertNotNull(token);
JsonObject authInfo = new JsonObject().put("jwt", token);
authProvider.authenticate(authInfo, onSuccess(res -> {
assertNotNull(res);
testComplete();
}));
await();
}
Aggregations