use of io.vertx.ext.auth.jwt.JWTOptions in project vertx-web by vert-x3.
the class JWTAuthHandlerTest method testLoginFail.
@Test
public void testLoginFail() throws Exception {
Handler<RoutingContext> handler = rc -> {
fail("should not get here");
rc.response().end("Welcome to the protected resource!");
};
router.route("/protected/*").handler(JWTAuthHandler.create(authProvider));
router.route("/protected/somepage").handler(handler);
testRequest(HttpMethod.GET, "/protected/somepage", null, 401, "Unauthorized", null);
// Now try again with bad token
final String token = authProvider.generateToken(new JsonObject().put("sub", "paulo"), new JWTOptions());
testRequest(HttpMethod.GET, "/protected/somepage", req -> req.putHeader("Authorization", "Bearer x" + token), 401, "Unauthorized", null);
testRequest(HttpMethod.GET, "/protected/somepage", req -> req.putHeader("Authorization", "Basic " + token), 401, "Unauthorized", null);
}
use of io.vertx.ext.auth.jwt.JWTOptions in project vertx-web by vert-x3.
the class JWTAuthHandlerTest method testLogin.
@Test
public void testLogin() throws Exception {
Handler<RoutingContext> handler = rc -> {
assertNotNull(rc.user());
assertEquals("paulo", rc.user().principal().getString("sub"));
rc.response().end("Welcome to the protected resource!");
};
router.route("/protected/*").handler(JWTAuthHandler.create(authProvider));
router.route("/protected/somepage").handler(handler);
testRequest(HttpMethod.GET, "/protected/somepage", null, resp -> {
}, 401, "Unauthorized", null);
// Now try again with credentials
testRequest(HttpMethod.GET, "/protected/somepage", req -> req.putHeader("Authorization", "Bearer " + authProvider.generateToken(new JsonObject().put("sub", "paulo"), new JWTOptions())), 200, "OK", "Welcome to the protected resource!");
}
use of io.vertx.ext.auth.jwt.JWTOptions in project vertx-web by vert-x3.
the class WebExamples method example52.
public void example52(Vertx vertx) {
JsonObject authConfig = new JsonObject().put("keyStore", new JsonObject().put("type", "jceks").put("path", "keystore.jceks").put("password", "secret"));
JWTAuth authProvider = JWTAuth.create(vertx, authConfig);
authProvider.generateToken(new JsonObject().put("sub", "paulo").put("someKey", "some value"), new JWTOptions());
}
use of io.vertx.ext.auth.jwt.JWTOptions in project vertx-web by vert-x3.
the class WebExamples method example50.
public void example50(Vertx vertx) {
Router router = Router.router(vertx);
JsonObject authConfig = new JsonObject().put("keyStore", new JsonObject().put("type", "jceks").put("path", "keystore.jceks").put("password", "secret"));
JWTAuth authProvider = JWTAuth.create(vertx, authConfig);
router.route("/login").handler(ctx -> {
// this is an example, authentication should be done with another provider...
if ("paulo".equals(ctx.request().getParam("username")) && "secret".equals(ctx.request().getParam("password"))) {
ctx.response().end(authProvider.generateToken(new JsonObject().put("sub", "paulo"), new JWTOptions()));
} else {
ctx.fail(401);
}
});
}
use of io.vertx.ext.auth.jwt.JWTOptions in project vertx-auth by vert-x3.
the class AuthJWTExamples method example7.
public void example7(Vertx vertx, String username, String password) {
JWTAuthOptions config = new JWTAuthOptions().setKeyStore(new KeyStoreOptions().setPath("keystore.jceks").setPassword("secret"));
JWTAuth provider = JWTAuth.create(vertx, config);
// on the verify endpoint once you verify the identity of the user by its username/password
if ("paulo".equals(username) && "super_secret".equals(password)) {
String token = provider.generateToken(new JsonObject().put("sub", "paulo"), new JWTOptions());
// now for any request to protected resources you should pass this string in the HTTP header Authorization as:
// Authorization: Bearer <token>
}
}
Aggregations