use of io.vertx.ext.auth.jwt.JWTAuth in project vertx-web by vert-x3.
the class WebExamples method example50.
public void example50(Vertx vertx) {
Router router = Router.router(vertx);
JWTAuthOptions authConfig = new JWTAuthOptions().setKeyStore(new KeyStoreOptions().setType("jceks").setPath("keystore.jceks").setPassword("secret"));
JWTAuth jwt = 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(jwt.generateToken(new JsonObject().put("sub", "paulo")));
} else {
ctx.fail(401);
}
});
}
use of io.vertx.ext.auth.jwt.JWTAuth in project vertx-web by vert-x3.
the class WebExamples method example52.
public void example52(Vertx vertx) {
JWTAuthOptions authConfig = new JWTAuthOptions().setKeyStore(new KeyStoreOptions().setType("jceks").setPath("keystore.jceks").setPassword("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.JWTAuth in project vertx-web by vert-x3.
the class ChainAuthMixHandlerTest method test2JWTIssuers.
@Test
public void test2JWTIssuers() throws Exception {
JsonObject key = new JsonObject().put("kty", "oct").put("use", "sig").put("k", "wuSPxS64NYh4ohDpZWOMNtawhBLHVn8dhKuIxnsLLd-dfKzIb5FL7r-vXTJ3MjtqnBlh_piKjn6qvb8os00MXNEyJWhgbPsnZEfqj6wMsJiH3uDcEgDuBMVbsuMlVbyX3x0Cd6qn0qvF8JZaLxSR6JNEEOGnbkUXqF9ghcI2y8rooN6ivQJ0-SiCqtQSkVrSO4H65lHagUus0XjTErL4GypbcO6PBIZMtHBW4UZHVcl86IhDxj5v0xf3WSuDGxkrbw5rpM_eVUR1eu71XPoTXD4WgDRtq4CoQcIFeSpqJuKZvzDJ47zV3wgnqKZ6G-RkiSKLBUj5_4Ur_YWHw2h-CQ").put("alg", "HS256");
JWTAuth me = JWTAuth.create(vertx, new JWTAuthOptions().addJwk(key).setJWTOptions(new JWTOptions().setIssuer("me")));
JWTAuth you = JWTAuth.create(vertx, new JWTAuthOptions().addJwk(key).setJWTOptions(new JWTOptions().setIssuer("you")));
ChainAuthHandler chain = ChainAuthHandler.any().add(JWTAuthHandler.create(me)).add(JWTAuthHandler.create(you));
router.route().handler(chain);
router.route().handler(ctx -> ctx.response().end());
// Payload with right issuer
final JsonObject payloadA = new JsonObject().put("sub", "Paulo").put("iss", "me");
testRequest(HttpMethod.GET, "/", req -> req.putHeader("Authorization", "Bearer " + me.generateToken(payloadA)), 200, "OK", null);
// Payload with right issuer
final JsonObject payloadB = new JsonObject().put("sub", "Paulo").put("iss", "you");
testRequest(HttpMethod.GET, "/", req -> req.putHeader("Authorization", "Bearer " + you.generateToken(payloadB)), 200, "OK", null);
}
use of io.vertx.ext.auth.jwt.JWTAuth 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();
}
use of io.vertx.ext.auth.jwt.JWTAuth in project vertx-auth by vert-x3.
the class AuthJWTExamples method example17.
public void example17(Vertx vertx) {
JWTAuth provider = JWTAuth.create(vertx, new JWTAuthOptions().addPubSecKey(new PubSecKeyOptions().setAlgorithm("ES256").setSecretKey("MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgeRyEfU1NSHPTCuC9\n" + "rwLZMukaWCH2Fk6q5w+XBYrKtLihRANCAAStpUnwKmSvBM9EI+W5QN3ALpvz6bh0\n" + "SPCXyz5KfQZQuSj4f3l+xNERDUDaygIUdLjBXf/bc15ur2iZjcq4r0Mr")));
String token = provider.generateToken(new JsonObject(), new io.vertx.ext.jwt.JWTOptions().setAlgorithm("ES256"));
}
Aggregations