Example #1
0
 public FhirClient(
     HttpClient httpClient,
     TestFhirServer testFhirServer,
     ResourceFormat format,
     TestApplication clientApplication,
     TestUser user,
     (bool SecurityEnabled, string AuthorizeUrl, string TokenUrl) securitySettings)
        private async Task Authenticate(TestApplication clientApplication, TestUser user)
        {
            if (clientApplication.Equals(TestApplications.InvalidClient))
            {
                return;
            }

            if (user == null)
            {
                string scope    = clientApplication.Equals(TestApplications.WrongAudienceClient) ? clientApplication.ClientId : AuthenticationSettings.Scope;
                string resource = clientApplication.Equals(TestApplications.WrongAudienceClient) ? clientApplication.ClientId : AuthenticationSettings.Resource;

                await this.AuthenticateOpenIdClientCredentials(
                    clientApplication.ClientId,
                    clientApplication.ClientSecret,
                    resource,
                    scope,
                    cancellationToken : default);
            }
            else
            {
                await this.AuthenticateOpenIdUserPassword(
                    clientApplication.ClientId,
                    clientApplication.ClientSecret,
                    AuthenticationSettings.Resource,
                    AuthenticationSettings.Scope,
                    user.UserId,
                    user.Password,
                    cancellationToken : default);
            }
        }
 public TestFhirClient(
     HttpClient httpClient,
     TestFhirServer testFhirServer,
     ResourceFormat format,
     TestApplication clientApplication,
     TestUser user)
     : base(httpClient, format)
 {
     _testFhirServer    = testFhirServer;
     _clientApplication = clientApplication;
     _user = user;
 }
        public TestFhirClient(
            HttpClient httpClient,
            TestFhirServer testFhirServer,
            ResourceFormat format,
            TestApplication clientApplication,
            TestUser user)
            : base(httpClient, format)
        {
            _testFhirServer    = testFhirServer;
            _clientApplication = clientApplication;
            _user = user;

            ConfigureSecurityOptions().GetAwaiter().GetResult();
            SetupAuthenticationAsync(clientApplication, user).GetAwaiter().GetResult();
        }
        private async Task SetupAuthenticationAsync(TestApplication clientApplication, TestUser user = null)
        {
            if (SecurityEnabled == true)
            {
                var tokenKey = $"{clientApplication.ClientId}:{(user == null ? string.Empty : user.UserId)}";

                if (!_bearerTokens.ContainsKey(tokenKey))
                {
                    await Authenticate(clientApplication, user);

                    _bearerTokens[tokenKey] = HttpClient.DefaultRequestHeaders?.Authorization?.Parameter;
                }
                else
                {
                    SetBearerToken(_bearerTokens[tokenKey]);
                }
            }
        }
 public TestFhirClient CreateClientForClientApplication(TestApplication clientApplication)
 {
     EnsureArg.IsNotNull(clientApplication, nameof(clientApplication));
     return(_testFhirServer.GetTestFhirClient(Format, clientApplication, null));
 }
 public TestFhirClient CreateClientForUser(TestUser user, TestApplication clientApplication)
 {
     EnsureArg.IsNotNull(user, nameof(user));
     EnsureArg.IsNotNull(clientApplication, nameof(clientApplication));
     return(_testFhirServer.GetTestFhirClient(Format, clientApplication, user));
 }