use of org.folio.okapi.common.OkapiClient in project okapi by folio-org.
the class ProxyService method doCallSystemInterface.
/**
* Actually make a request to a system interface, like _tenant. Assumes we are
* operating as the correct tenant.
*/
private void doCallSystemInterface(String tenantId, String authToken, ModuleInstance inst, String modPerms, String request, ProxyContext pc, Handler<ExtendedAsyncResult<OkapiClient>> fut) {
String curTenant = pc.getTenant();
pc.debug("doCallSystemInterface on " + Json.encode(inst) + " for " + tenantId + " as " + curTenant + " with token " + authToken);
discoveryManager.get(inst.getModuleDescriptor().getId(), gres -> {
if (gres.failed()) {
pc.warn("doCallSystemInterface on " + inst.getModuleDescriptor().getId() + " " + inst.getPath() + " failed. Could not find the module in discovery", gres.cause());
fut.handle(new Failure<>(gres.getType(), gres.cause()));
return;
}
DeploymentDescriptor instance = pickInstance(gres.result());
if (instance == null) {
fut.handle(new Failure<>(USER, "No running instances for module " + inst.getModuleDescriptor().getId() + ". Can not invoke " + inst.getPath()));
return;
}
String baseurl = instance.getUrl();
pc.debug("doCallSystemInterface Url: " + baseurl + " and " + inst.getPath());
Map<String, String> headers = sysReqHeaders(pc.getCtx(), tenantId, authToken);
if (modPerms != null) {
// We are making an auth call
RoutingEntry re = inst.getRoutingEntry();
if (re != null) {
headers.put(XOkapiHeaders.FILTER, re.getPhase());
}
if (!modPerms.isEmpty()) {
headers.put(XOkapiHeaders.MODULE_PERMISSIONS, modPerms);
}
// Clear the permissions-required header that we inherited from the
// original request (e.g. to tenant-enable), as we do not have those
// perms set in the target tenant
headers.put(XOkapiHeaders.PERMISSIONS_REQUIRED, "");
headers.put(XOkapiHeaders.PERMISSIONS_DESIRED, "");
logger.debug("Auth call, some tricks with permissions");
}
pc.debug("doCallSystemInterface: About to create OkapiClient with headers " + Json.encode(headers));
OkapiClient cli = new OkapiClient(baseurl, vertx, headers);
String reqId = inst.getPath().replaceFirst("^[/_]*([^/]+).*", "$1");
// "tenant" or "tenantpermissions"
cli.newReqId(reqId);
cli.enableInfoLog();
HttpMethod meth = HttpMethod.POST;
if (request.isEmpty()) {
pc.debug("doCallSystemInterface: No Req, making a HEAD req");
meth = HttpMethod.HEAD;
}
HttpMethod finalMeth = meth;
cli.request(meth, inst.getPath(), request, cres -> {
cli.close();
if (cres.failed()) {
String msg = finalMeth + " request for " + inst.getModuleDescriptor().getId() + " " + inst.getPath() + " failed with " + cres.cause().getMessage();
pc.warn(msg);
fut.handle(new Failure<>(INTERNAL, msg));
return;
}
// Pass response headers - needed for unit test, if nothing else
String body = cres.result();
pc.debug("doCallSystemInterface response: " + body);
pc.debug("doCallSystemInterface ret " + " hdrs: " + Json.encode(cli.getRespHeaders().entries()));
pc.passOkapiTraceHeaders(cli);
fut.handle(new Success<>(cli));
});
});
}
use of org.folio.okapi.common.OkapiClient in project okapi by folio-org.
the class ProxyService method authForSystemInterface.
/**
* Helper to get a new authtoken before invoking doCallSystemInterface.
*/
private void authForSystemInterface(ModuleDescriptor authMod, RoutingEntry filt, String tenantId, ModuleInstance inst, String request, ProxyContext pc, Handler<ExtendedAsyncResult<OkapiClient>> fut) {
pc.debug("Calling doCallSystemInterface to get auth token");
RoutingEntry re = inst.getRoutingEntry();
String modPerms = "";
if (re != null) {
String[] modulePermissions = re.getModulePermissions();
Map<String, String[]> mpMap = new HashMap<>();
if (modulePermissions != null) {
mpMap.put(inst.getModuleDescriptor().getId(), modulePermissions);
logger.debug("authForSystemInterface: Found modPerms:" + modPerms);
} else {
logger.debug("authForSystemInterface: Got RoutingEntry, but null modulePermissions");
}
modPerms = Json.encode(mpMap);
} else {
logger.debug("authForSystemInterface: re is null, can't find modPerms");
}
ModuleInstance authInst = new ModuleInstance(authMod, filt, inst.getPath());
doCallSystemInterface(tenantId, null, authInst, modPerms, "", pc, res -> {
if (res.failed()) {
pc.warn("Auth check for systemInterface failed!");
fut.handle(new Failure<>(res.getType(), res.cause()));
return;
}
OkapiClient cli = res.result();
String deftok = cli.getRespHeaders().get(XOkapiHeaders.TOKEN);
logger.debug("authForSystemInterface:" + Json.encode(cli.getRespHeaders().entries()));
String modTok = cli.getRespHeaders().get(XOkapiHeaders.MODULE_TOKENS);
JsonObject jo = new JsonObject(modTok);
String token = jo.getString(inst.getModuleDescriptor().getId(), deftok);
logger.debug("authForSystemInterface: Got token " + token);
doCallSystemInterface(tenantId, token, inst, null, request, pc, fut);
});
}
use of org.folio.okapi.common.OkapiClient in project okapi by folio-org.
the class AuthModuleTest method testGetLogin.
@Test
public void testGetLogin(TestContext context) {
Async async = context.async();
HashMap<String, String> headers = new HashMap<>();
headers.put(XOkapiHeaders.URL, URL);
OkapiClient cli = new OkapiClient(URL, vertx, headers);
cli.get("/authn/login", res -> {
cli.close();
context.assertTrue(res.succeeded());
async.complete();
});
}
use of org.folio.okapi.common.OkapiClient in project okapi by folio-org.
the class AuthModuleTest method testBadTokenJwt.
@Test
public void testBadTokenJwt(TestContext context) {
Async async = context.async();
HashMap<String, String> headers = new HashMap<>();
headers.put(XOkapiHeaders.URL, URL);
headers.put(XOkapiHeaders.TENANT, "my-lib");
OkapiClient cli = new OkapiClient(URL, vertx, headers);
cli.setOkapiToken("a.b.c");
cli.get("/badjwt", res -> {
cli.close();
context.assertTrue(res.failed());
context.assertEquals(ErrorType.USER, res.getType());
async.complete();
});
}
use of org.folio.okapi.common.OkapiClient in project okapi by folio-org.
the class AuthModuleTest method testBadLogin.
@Test
public void testBadLogin(TestContext context) {
Async async = context.async();
HashMap<String, String> headers = new HashMap<>();
headers.put(XOkapiHeaders.URL, URL);
headers.put(XOkapiHeaders.TENANT, "my-lib");
OkapiClient cli = new OkapiClient(URL, vertx, headers);
JsonObject j = new JsonObject();
j.put("tenant", "my-lib");
j.put("username", "foo");
j.put("password", "badpassword");
String body = j.encodePrettily();
cli.post("/authn/login", body, res -> {
cli.close();
context.assertTrue(res.failed());
context.assertEquals(ErrorType.INTERNAL, res.getType());
async.complete();
});
}
Aggregations