public async Task <CapabilityStatement> ExecuteFhirCapabilityStatement(RequestParameters requestParameters, string baseAddress)
        {
            _spineMessage = new SpineMessage();
            var capabilityStatement = await GetCapabilityStatement(requestParameters, baseAddress);

            return(capabilityStatement);
        }
        public async Task <SlotSimple> ExecuteFreeSlotSearch(RequestParameters requestParameters, DateTime startDate, DateTime endDate, string baseAddress)
        {
            _spineMessage = new SpineMessage();
            var freeSlots = await GetFreeSlots(requestParameters, startDate, endDate, baseAddress);

            return(freeSlots);
        }
Example #3
0
        public Task <List <SlotEntrySummaryCount> > ExecuteFreeSlotSearchSummary(List <OrganisationErrorCodeOrDetail> organisationErrorCodeOrDetails, List <RequestParametersList> requestParameterList, DateTime startDate, DateTime endDate, SearchType searchType)
        {
            var tokenSource = new CancellationTokenSource();
            var token       = tokenSource.Token;

            _spineMessage = new SpineMessage();
            var freeSlotsSummary = GetFreeSlotsSummary(organisationErrorCodeOrDetails, requestParameterList, startDate, endDate, token, searchType);

            return(freeSlotsSummary);
        }
Example #4
0
        public Task <List <CapabilityStatementList> > ExecuteFhirCapabilityStatement(List <RequestParametersList> requestParameterList)
        {
            _logger.LogInformation("Executing ExecuteFhirCapabilityStatement");

            var tokenSource = new CancellationTokenSource();
            var token       = tokenSource.Token;

            _spineMessage = new SpineMessage();
            var capabilityStatement = GetCapabilityStatement(requestParameterList, token);

            return(capabilityStatement);
        }
Example #5
0
        public async Task <SlotSimple> ExecuteFreeSlotSearch(RequestParameters requestParameters, DateTime startDate, DateTime endDate, string baseAddress, int userId)
        {
            _spineMessage = new SpineMessage();
            var freeSlots = await GetFreeSlots(requestParameters, startDate, endDate, baseAddress);

            var searchExport = new SearchExport
            {
                SearchExportData = freeSlots.ExportStreamData,
                UserId           = userId
            };

            var searchExportInstance = _applicationService.AddSearchExport(searchExport);

            freeSlots.SearchExportId = searchExportInstance.SearchExportId;
            return(freeSlots);
        }
Example #6
0
        public async Task <T> ExecuteFhirQuery <T>(string query, string baseAddress, CancellationToken cancellationToken, SpineMessageTypes spineMessageType) where T : class
        {
            var getRequest   = new HttpRequestMessage();
            var spineMessage = new SpineMessage()
            {
                SpineMessageTypeId = (int)spineMessageType
            };

            try
            {
                var sw = new Stopwatch();
                sw.Start();

                var client = _httpClientFactory.CreateClient("GpConnectClient");
                client.DefaultRequestHeaders.Add(Constants.Headers.ApiKey, _spineOptionsDelegate.CurrentValue.SpineFhirApiKey);

                getRequest.Method     = HttpMethod.Get;
                getRequest.RequestUri = GenerateUri(baseAddress, query);

                var response = client.Send(getRequest, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
                var contents = await response.Content.ReadAsStringAsync(cancellationToken);

                spineMessage.RequestHeaders  = client.DefaultRequestHeaders.ToString();
                spineMessage.ResponsePayload = contents;
                spineMessage.ResponseStatus  = response.StatusCode.ToString();
                spineMessage.RequestPayload  = getRequest.ToString();
                spineMessage.ResponseHeaders = response.Headers.ToString();

                sw.Stop();
                spineMessage.RoundTripTimeMs = sw.ElapsedMilliseconds;
                var savedSpineMessage = _logService.AddSpineMessageLog(spineMessage);

                if (response.IsSuccessStatusCode)
                {
                    var responseContents = JsonConvert.DeserializeObject <T>(contents);
                    return(responseContents);
                }
                return(null);
            }
            catch (Exception exc)
            {
                _logger.LogError(exc, "An Exception has occurred while attempting to execute a FHIR API query");
                throw;
            }
        }
Example #7
0
        public T ExecuteLdapQuery <T>(string searchBase, string filter, string[] attributes) where T : class
        {
            try
            {
                var sw     = new Stopwatch();
                T   result = null;
                sw.Start();
                var logMessage = new SpineMessage
                {
                    RequestPayload     = $"{searchBase} {filter} [{string.Join(",", attributes)}]",
                    SpineMessageTypeId = (int)GPConnect.Constants.SpineMessageTypes.SpineLdapQuery
                };

                var results          = new Dictionary <string, object>();
                var useLdaps         = bool.Parse(_configuration.GetSection("Spine:sds_use_ldaps").Value);
                var useSdsMutualAuth = bool.Parse(_configuration.GetSection("Spine:sds_use_mutualauth").Value);


                Novell.Directory.Ldap.LdapConnectionOptions ldapConnectionOptions = new LdapConnectionOptions();

                if (useLdaps)
                {
                    SslProtocols sslProtocol = ParseTlsVersion(_configuration.GetSection("Spine:sds_tls_version").Value);

                    ldapConnectionOptions.ConfigureSslProtocols(SslProtocols.Tls12);
                    ldapConnectionOptions.UseSsl();
                    ldapConnectionOptions.ConfigureLocalCertificateSelectionCallback(SelectLocalCertificate);
                    ldapConnectionOptions.ConfigureRemoteCertificateValidationCallback(ValidateServerCertificate);
                }

                using (var ldapConnection = new LdapConnection(ldapConnectionOptions)
                {
                    ConnectionTimeout = int.Parse(_configuration.GetSection("Spine:timeout_seconds").Value) * 1000
                })
                {
                    if (useSdsMutualAuth)
                    {
                        var clientCert       = _configuration.GetSection("spine:client_cert").GetConfigurationString();
                        var serverCert       = _configuration.GetSection("spine:server_ca_certchain").GetConfigurationString();
                        var clientPrivateKey = _configuration.GetSection("spine:client_private_key").GetConfigurationString();

                        var clientCertData        = CertificateHelper.ExtractCertInstances(clientCert);
                        var clientPrivateKeyData  = CertificateHelper.ExtractKeyInstance(clientPrivateKey);
                        var x509ClientCertificate = new X509Certificate2(clientCertData.FirstOrDefault());

                        var privateKey = RSA.Create();
                        privateKey.ImportRSAPrivateKey(clientPrivateKeyData, out _);
                        var x509CertificateWithPrivateKey = x509ClientCertificate.CopyWithPrivateKey(privateKey);
                        var pfxFormattedCertificate       = new X509Certificate(x509CertificateWithPrivateKey.Export(X509ContentType.Pfx, string.Empty), string.Empty);

                        _clientCertificate = pfxFormattedCertificate;
                    }

                    var hostName = _configuration.GetSection("Spine:sds_hostname").Value;
                    var hostPort = int.Parse(_configuration.GetSection("Spine:sds_port").Value);

                    ldapConnection.Connect(hostName, hostPort);
                    ldapConnection.Bind(string.Empty, string.Empty);

                    LogTlsVersionOnStartup(ldapConnection);

                    var searchResults = ldapConnection.Search(searchBase, LdapConnection.ScopeSub, filter, attributes, false);

                    while (searchResults.HasMore())
                    {
                        var nextEntry    = searchResults.Next();
                        var attributeSet = nextEntry.GetAttributeSet();

                        foreach (var attribute in attributeSet)
                        {
                            results.TryAdd(attribute.Name, attribute.StringValue);
                        }
                    }

                    ldapConnection.Disconnect();
                    ldapConnection.Dispose();
                }

                var jsonDictionary = JsonConvert.SerializeObject(results);
                if (results.Count > 0)
                {
                    result = JsonConvert.DeserializeObject <T>(jsonDictionary);
                }
                logMessage.ResponsePayload = jsonDictionary;
                logMessage.RoundTripTimeMs = sw.ElapsedMilliseconds;
                _logService.AddSpineMessageLog(logMessage);
                return(result);
            }
            catch (LdapException ldapException)
            {
                _logger.LogError(ldapException, "An LdapException has occurred while attempting to execute an LDAP query");
                throw;
            }
            catch (Exception exc)
            {
                _logger.LogError(exc, "An Exception has occurred while attempting to execute an LDAP query");
                throw;
            }
        }
Example #8
0
        public T ExecuteLdapQuery <T>(string searchBase, string filter, string[] attributes) where T : class
        {
            try
            {
                var sw     = new Stopwatch();
                T   result = null;
                sw.Start();
                var logMessage = new SpineMessage
                {
                    RequestPayload     = $"{searchBase} {filter} [{string.Join(",", attributes)}]",
                    SpineMessageTypeId = (int)GPConnect.Constants.SpineMessageTypes.SpineLdapQuery
                };

                var results = new Dictionary <string, object>();

                var ldapConnectionOptions = new LdapConnectionOptions();

                if (_spineOptionsDelegate.CurrentValue.SdsUseLdaps)
                {
                    ldapConnectionOptions.ConfigureSslProtocols(SecurityHelper.ParseTlsVersion(_spineOptionsDelegate.CurrentValue.SdsTlsVersion));
                    ldapConnectionOptions.UseSsl();
                    ldapConnectionOptions.ConfigureLocalCertificateSelectionCallback(SelectLocalCertificate);
                    ldapConnectionOptions.ConfigureRemoteCertificateValidationCallback(ValidateServerCertificate);
                }

                using (var ldapConnection = new LdapConnection(ldapConnectionOptions)
                {
                    ConnectionTimeout = _spineOptionsDelegate.CurrentValue.TimeoutMilliseconds
                })
                {
                    SetupMutualAuth();

                    ldapConnection.Connect(_spineOptionsDelegate.CurrentValue.SdsHostname, _spineOptionsDelegate.CurrentValue.SdsPort);
                    ldapConnection.Bind(string.Empty, string.Empty);

                    LogTlsVersionOnStartup(ldapConnection);

                    var searchResults = ldapConnection.Search(searchBase, LdapConnection.ScopeSub, filter, attributes, false);

                    while (searchResults.HasMore())
                    {
                        var nextEntry    = searchResults.Next();
                        var attributeSet = nextEntry.GetAttributeSet();

                        foreach (var attribute in attributeSet)
                        {
                            results.TryAdd(attribute.Name, attribute.StringValue);
                        }
                    }

                    ldapConnection.Disconnect();
                    ldapConnection.Dispose();
                }

                var jsonDictionary = JsonConvert.SerializeObject(results);
                if (results.Count > 0)
                {
                    result = JsonConvert.DeserializeObject <T>(jsonDictionary);
                }
                logMessage.ResponsePayload = jsonDictionary;
                logMessage.RoundTripTimeMs = sw.ElapsedMilliseconds;
                _logService.AddSpineMessageLog(logMessage);
                return(result);
            }
            catch (LdapException ldapException)
            {
                _logger.LogError(ldapException, "An LdapException has occurred while attempting to execute an LDAP query");
                throw;
            }
            catch (Exception exc)
            {
                _logger.LogError(exc, "An Exception has occurred while attempting to execute an LDAP query");
                throw;
            }
        }