public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", "put", "patch", "delete", Route = "fhirproxy/{res?}/{id?}/{hist?}/{vid?}")] HttpRequest req, ILogger log, ClaimsPrincipal principal, string res, string id, string hist, string vid) { if (!Utils.isServerAccessAuthorized(req)) { return(new ContentResult() { Content = Utils.genOOErrResponse("auth-access", req.Headers[Utils.AUTH_STATUS_MSG_HEADER].First()), StatusCode = (int)System.Net.HttpStatusCode.Unauthorized, ContentType = "application/json" }); } //Load Request Body string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); //Call Configured Pre-Processor Modules ProxyProcessResult prerslt = ProxyProcessManager.RunPreProcessors(requestBody, req, log, principal, res, id, hist, vid); if (!prerslt.Continue) { //Pre-Processor didn't like something or exception was called so return FHIRResponse preresp = prerslt.Response; if (preresp == null) { string errmsg = (string.IsNullOrEmpty(prerslt.ErrorMsg) ? "No message" : prerslt.ErrorMsg); FHIRResponse fer = new FHIRResponse(); fer.StatusCode = System.Net.HttpStatusCode.InternalServerError; fer.Content = Utils.genOOErrResponse("internalerror", $"A Proxy Pre-Processor halted execution for an unknown reason. Check logs. Message is {errmsg}"); return(generateJSONResult(fer)); } return(generateJSONResult(preresp)); } log.LogInformation("Calling FHIR Server..."); //Proxy the call to the FHIR Server FHIRResponse serverresponse = FHIRClientFactory.callFHIRServer(prerslt.Request, req, log, res, id, hist, vid); //Call Configured Post-Processor Modules ProxyProcessResult postrslt = ProxyProcessManager.RunPostProcessors(serverresponse, req, log, principal, res, id, hist, vid); if (postrslt.Response == null) { string errmsg = (string.IsNullOrEmpty(postrslt.ErrorMsg) ? "No message" : postrslt.ErrorMsg); postrslt.Response = new FHIRResponse(); postrslt.Response.StatusCode = System.Net.HttpStatusCode.InternalServerError; postrslt.Response.Content = Utils.genOOErrResponse("internalerror", $"A Proxy Post-Processor halted execution for an unknown reason. Check logs. Message is {errmsg}"); } //Reverse Proxy Response postrslt.Response = Utils.reverseProxyResponse(postrslt.Response, req, res); //return ActionResult if (postrslt.Response.StatusCode == HttpStatusCode.NoContent) { return(null); } return(generateJSONResult(postrslt.Response)); }
public static async Task <FHIRResponse> callFHIRServer(string requestBody, HttpRequest req, ILogger log, string res, string id, string hist, string vid) { FHIRClient fhirClient = FHIRClientFactory.getClient(log); FHIRResponse fhirresp = null; if (req.Method.Equals("GET")) { var qs = req.QueryString.HasValue ? req.QueryString.Value : null; StringBuilder sb = new StringBuilder(); sb.Append(res); if (!string.IsNullOrEmpty(id)) { sb.Append("/" + id); if (!string.IsNullOrEmpty(hist)) { sb.Append("/" + hist); if (!string.IsNullOrEmpty(vid)) { sb.Append("/" + vid); } } } fhirresp = await fhirClient.LoadResource(sb.ToString(), qs, false, req.Headers); } else { if (req.Method.Equals("DELETE")) { fhirresp = await fhirClient.DeleteResource(res + (id == null ? "" : "/" + id), req.Headers); } else if (req.Method.Equals("POST") && !string.IsNullOrEmpty(id) && id.StartsWith("_search")) { var qs = req.QueryString.HasValue ? req.QueryString.Value : null; fhirresp = await fhirClient.PostCommand(res + "/" + id, requestBody, qs, req.Headers); } else { fhirresp = await fhirClient.SaveResource(res, requestBody, req.Method, req.Headers); } } return(fhirresp); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "manage/{cmd}/{res}/{id}/{name}")] HttpRequest req, ILogger log, ClaimsPrincipal principal, string cmd, string res, string id, string name) { log.LogInformation("SecureLink Function Invoked"); //Is the principal authenticated if (!Utils.isServerAccessAuthorized(req)) { return(new ContentResult() { Content = "User is not Authenticated", StatusCode = (int)System.Net.HttpStatusCode.Unauthorized }); } if (!Utils.inServerAccessRole(req, "A")) { return(new ContentResult() { Content = "User does not have suffiecient rights (Administrator required)", StatusCode = (int)System.Net.HttpStatusCode.Unauthorized }); } if (string.IsNullOrEmpty(cmd) || !validcmds.Any(cmd.Contains)) { return(new BadRequestObjectResult("Invalid Command....Valid commands are link, unlink and list")); } //Are we linking the correct resource type if (string.IsNullOrEmpty(res) || !allowedresources.Any(res.Contains)) { return(new BadRequestObjectResult("Resource must be Patient,Practitioner or RelatedPerson")); } ClaimsIdentity ci = (ClaimsIdentity)principal.Identity; string aadten = (string.IsNullOrEmpty(ci.Tenant()) ? "Unknown" : ci.Tenant()); FhirJsonParser _parser = new FhirJsonParser(); _parser.Settings.AcceptUnknownMembers = true; _parser.Settings.AllowUnrecognizedEnums = true; //Get a FHIR Client so we can talk to the FHIR Server log.LogInformation($"Instanciating FHIR Client Proxy"); FHIRClient fhirClient = FHIRClientFactory.getClient(log); int i_link_days = 0; int.TryParse(System.Environment.GetEnvironmentVariable("FP-LINK-DAYS"), out i_link_days); if (i_link_days == 0) { i_link_days = 365; } //Load the resource to Link var fhirresp = await fhirClient.LoadResource(res + "/" + id, null, false, req.Headers); var lres = _parser.Parse <Resource>((string)fhirresp.Content); if (lres.ResourceType == Hl7.Fhir.Model.ResourceType.OperationOutcome) { return(new BadRequestObjectResult(lres.ToString())); } CloudTable table = Utils.getTable(); switch (cmd) { case "link": LinkEntity linkentity = new LinkEntity(res, aadten + "-" + name); linkentity.ValidUntil = DateTime.Now.AddDays(i_link_days); linkentity.LinkedResourceId = id; Utils.setLinkEntity(table, linkentity); return(new OkObjectResult($"Identity: {name} in directory {aadten} is now linked to {res}/{id}")); case "unlink": LinkEntity delentity = Utils.getLinkEntity(table, res, aadten + "-" + name); if (delentity == null) { return(new OkObjectResult($"Resource {res}/{id} has no links to Identity {name} in directory {aadten}")); } Utils.deleteLinkEntity(table, delentity); return(new OkObjectResult($"Identity: {name} in directory {aadten} has been unlinked from {res}/{id}")); case "list": LinkEntity entity = Utils.getLinkEntity(table, res, aadten + "-" + name); if (entity != null) { return(new OkObjectResult($"Resource {res}/{id} is linked to Identity: {name} in directory {aadten}")); } else { return(new OkObjectResult($"Resource {res}/{id} has no links to Identity {name} in directory {aadten}")); } } return(new OkObjectResult($"No action taken Identity: {name}")); }