List of usage examples for io.vertx.core Future future
future
From source file:org.folio.auth.login_module.impl.ModuleUserSource.java
@Override public Future<UserResult> getUser(String username) { if (okapiUrl == null || vertx == null || requestToken == null || tenant == null) { throw new RuntimeException( "You must call setOkapiUrl, setVertx, setRequestToken and setTenant before calling this method"); }/*from www . j a v a2s. c o m*/ Future<UserResult> future = Future.future(); HttpClientOptions options = new HttpClientOptions(); options.setConnectTimeout(10); options.setIdleTimeout(10); HttpClient client = vertx.createHttpClient(options); JsonObject query = new JsonObject().put("username", username); String requestUrl = null; try { requestUrl = okapiUrl + "/users/?query=" + URLEncoder.encode(query.encode(), "UTF-8"); } catch (Exception e) { future.fail(e); return future; } logger.debug("Requesting userdata from URL at " + requestUrl); client.getAbs(requestUrl, res -> { if (res.statusCode() != 200) { future.fail("Got status code " + res.statusCode()); } else { res.bodyHandler(buf -> { logger.debug("Got content from server: " + buf.toString()); JsonObject result = buf.toJsonObject(); if (result.getInteger("total_records") > 1) { future.fail("Not unique username"); } else if (result.getInteger("total_records") == 0) { UserResult userResult = new UserResult(username, false); future.complete(userResult); } else { UserResult userResult = new UserResult(username, true, result.getJsonArray("users").getJsonObject(0).getBoolean("active")); future.complete(userResult); } }); } }).putHeader("X-Okapi-Tenant", tenant).putHeader("Authorization", "Bearer " + requestToken) .putHeader("Content-type", "application/json").putHeader("Accept", "application/json").end(); return future; }
From source file:org.folio.auth.login_module.impl.MongoAuthSource.java
@Override public Future<AuthResult> authenticate(JsonObject credentials, String tenant) { Future<AuthResult> future = Future.future(); String username = credentials.getString("username"); String password = credentials.getString("password"); if (username == null || password == null) { return Future.failedFuture("Credentials must contain a username and password"); }/* w w w . j ava 2s .co m*/ JsonObject query = new JsonObject().put("username", username).put("tenant", tenant); logger.debug("Calling MongoDB to retrieve credentials"); mongoClient.find("credentials", query, res -> { if (res.succeeded() && !res.result().isEmpty()) { JsonObject user = res.result().get(0); String storedHash = user.getString("hash"); String storedSalt = user.getString("salt"); String calculatedHash = authUtil.calculateHash(password, storedSalt); if (calculatedHash.equals(storedHash)) { future.complete(new AuthResult(true, username, user.getJsonObject("metadata"))); logger.debug("Future completed (good)"); } else { future.complete(new AuthResult(false, username, user.getJsonObject("metadata"))); logger.debug("Future completed (bad)"); } } else { //username not found logger.error("No such user: " + username); future.complete(new AuthResult(false, username, null)); } logger.debug("Lambda completed"); }); logger.debug("Returning"); return future; }
From source file:org.folio.auth.login_module.impl.MongoAuthSource.java
@Override public Future<Boolean> addAuth(JsonObject credentials, JsonObject metadata, String tenant) { Future<Boolean> future = Future.future(); String username = credentials.getString("username"); String password = credentials.getString("password"); JsonObject query = new JsonObject().put("username", username).put("tenant", tenant); mongoClient.find("credentials", query, res -> { if (res.succeeded()) { //username already exists! future.complete(Boolean.FALSE); } else {/*from w w w.j a v a2 s .co m*/ String newSalt = authUtil.getSalt(); String newHash = authUtil.calculateHash(username, password); JsonObject insert = new JsonObject().put("username", username).put("tenant", tenant) //.put("password", password) .put("hash", newHash).put("salt", newSalt).put("metadata", metadata); mongoClient.insert("credentials", insert, res2 -> { if (res2.succeeded()) { future.complete(Boolean.TRUE); } else { future.complete(Boolean.FALSE); } }); } }); return future; }
From source file:org.folio.auth.login_module.impl.MongoAuthSource.java
@Override public Future<Boolean> updateAuth(JsonObject credentials, JsonObject metadata, String tenant) { Future<Boolean> future = Future.future(); String username = credentials.getString("username"); JsonObject query = new JsonObject().put("username", username).put("tenant", tenant); JsonObject update = new JsonObject(); String newSalt = authUtil.getSalt(); if (credentials.containsKey("password")) { String password = credentials.getString("password"); String newHash = authUtil.calculateHash(password, newSalt); update.put("salt", newSalt).put("hash", newHash); }//from w ww. j a va 2s .c om if (metadata != null) { update.put("metadata", metadata); } mongoClient.updateCollection("credentials", query, update, res -> { if (res.succeeded()) { future.complete(Boolean.TRUE); } else { future.complete(Boolean.FALSE); } }); return future; }
From source file:org.folio.auth.login_module.impl.MongoAuthSource.java
@Override public Future<Boolean> deleteAuth(String username, String tenant) { Future<Boolean> future = Future.future(); JsonObject query = new JsonObject().put("username", username).put("tenant", tenant); mongoClient.removeDocument("credentials", query, res -> { if (res.succeeded()) { future.complete(Boolean.TRUE); } else {/*from w w w . j av a2 s . com*/ future.complete(Boolean.FALSE); } }); return future; }
From source file:org.folio.auth.login_module.MainVerticle.java
private Future<String> fetchToken(JsonObject payload, String url, String requestToken, String tenant) { Future<String> future = Future.future(); HttpClient client = vertx.createHttpClient(); logger.debug("Attempting to request token from url " + url + " for claims " + payload.encode()); logger.debug("Using token: Bearer " + requestToken); HttpClientRequest request = client.postAbs(url); request.putHeader("Authorization", "Bearer " + requestToken); request.putHeader("X-Okapi-Tenant", tenant); request.handler(result -> {/*from w w w. ja v a2 s.c om*/ if (result.statusCode() != 200) { future.fail("Got error " + result.statusCode() + " fetching token"); logger.debug("Fetching token trace: " + result.getHeader("X-Okapi-Trace")); result.bodyHandler(buf -> { logger.debug("Output from token fetch is: " + buf.toString()); }); } else { String token = result.getHeader("Authorization"); future.complete(token); } }); request.end(new JsonObject().put("payload", payload).encode()); return future; }
From source file:org.folio.auth.permissions_module.impl.MongoPermissionsStore.java
@Override public Future<Boolean> addPermission(String permission, String tenant) { Future<Boolean> future = Future.future(); JsonObject query = new JsonObject().put("permission_name", permission).put("tenant", tenant); mongoClient.find("permissions", query, res -> { if (res.succeeded() && res.result().size() > 0) { //permission already exists future.fail("Permission already exists"); } else {/*from w w w . j a v a2 s. co m*/ JsonObject insert = new JsonObject().put("permission_name", permission).put("tenant", tenant) .put("sub_permissions", new JsonArray()); mongoClient.insert("permissions", insert, res2 -> { if (res2.succeeded()) { future.complete(true); } else { future.fail("Unable to insert permission: " + res2.cause().getMessage()); } }); } }); return future; }
From source file:org.folio.auth.permissions_module.impl.MongoPermissionsStore.java
@Override public Future<Boolean> addSubPermission(String permission, String sub, String tenant) { //TODO: Check for circular permissions Future<Boolean> future = Future.future(); JsonObject query = new JsonObject().put("permission_name", permission).put("tenant", tenant); mongoClient.find("permissions", query, res -> { if (res.failed() || res.result().size() < 1) { future.fail("Unable to find permission to add subs to"); //Can't add a sub to a non-existent perm } else {/*from ww w .j ava2 s . c om*/ //JsonArray permissionList = res.result().get(0).getJsonArray("sub_permissions"); //permissionList.add(sub); JsonObject update = new JsonObject().put("$addToSet", new JsonObject().put("sub_permissions", sub)); mongoClient.updateCollection("permissions", query, update, res2 -> { if (res2.succeeded()) { future.complete(true); } else { future.fail("Unable to update records: " + res2.cause().getMessage()); } }); } }); return future; }
From source file:org.folio.auth.permissions_module.impl.MongoPermissionsStore.java
@Override public Future<JsonArray> getSubPermissions(String permission, String tenant) { JsonObject query = new JsonObject().put("permission_name", permission).put("tenant", tenant); JsonArray permList = new JsonArray(); Future<JsonArray> future = Future.future(); mongoClient.find("permissions", query, res -> { if (res.failed() || res.result().size() < 1) { future.fail("Query unsuccessful"); } else {/*from w w w . j a va 2s .com*/ JsonObject permObject = res.result().get(0); future.complete(permObject.getJsonArray("sub_permissions")); } }); return future; }
From source file:org.folio.auth.permissions_module.impl.MongoPermissionsStore.java
@Override public Future<JsonArray> getExpandedPermissions(String permission, String tenant) { //System.out.println("Permissions> Expanding permission '"+ permission + "' on tenant '"+ // tenant + "'"); JsonObject query = new JsonObject().put("permission_name", permission).put("tenant", tenant); JsonArray permList = new JsonArray(); Future<JsonArray> future = Future.future(); mongoClient.find("permissions", query, res -> { if (res.succeeded() && res.result().size() > 0) { //System.out.println("Permissions> Successfully queried mongo for '"+ permission + "' on tenant '"+ // tenant + "'"); /*// w ww. j av a 2 s .c o m If there are no subpermissions, go ahead and complete the future with the given value of the JsonArray If there are subpermissions, create a list of new futures, by calling walkPerms for each sub permission, then create a composite future from these new futures, with a handler that completes the original future when they return */ JsonObject permObj = res.result().get(0); permList.add(permission); JsonArray subPerms = permObj.getJsonArray("sub_permissions"); LinkedList<Future> futureList = new LinkedList<>(); if (subPerms != null && !subPerms.isEmpty()) { logger.debug("Permissions> " + subPerms.size() + " subs to process for '" + permission + "'"); for (Object o : subPerms) { String sub = (String) o; Future<JsonArray> newFuture = getExpandedPermissions(sub, tenant); futureList.add(newFuture); } logger.debug("Permissions> Creating CompositeFuture to expand " + permission + " into " + futureList.size() + " subs: " + subPerms.encode()); CompositeFuture compositeFuture = CompositeFuture.all(futureList); compositeFuture.setHandler(res2 -> { if (res2.succeeded()) { //Get output of contained futures and complete the future here for (Future f : futureList) { JsonArray arr = (JsonArray) f.result(); for (Object o : arr) { permList.add(o); } } future.complete(permList); } else { future.fail("Unable to populate permissions: " + res2.cause().getMessage()); } }); } else { //System.out.println("Permissions> No sub-permissions found for '" + permission + "'"); future.complete(permList); } } else { future.fail("No permission '" + permission + "' found for tenant '" + tenant + "'"); } }); return future; }