private async Task <BankClientProfile> PersistOpenBankingClient(
            BankClientProfile value,
            OpenIdConfiguration openIdConfiguration,
            OpenBankingClientRegistrationClaims registrationClaims,
            BankClientRegistrationData openBankingRegistrationData)
        {
            value.State = "ok";
            value.OpenIdConfiguration          = openIdConfiguration;
            value.BankClientRegistrationClaims =
                _mapper.Map <BankClientRegistrationClaims>(registrationClaims);
            value.BankClientRegistrationData = openBankingRegistrationData;

            await _bankClientProfileRepo.UpsertAsync(value);

            return(value);
        }
        public async Task <BankClientProfileResponse> CreateAsync(BankClientProfilePublic bankClientProfile)
        {
            bankClientProfile.ArgNotNull(nameof(bankClientProfile));

            // Load relevant objects
            SoftwareStatementProfile softwareStatementProfile =
                _softwareStatementProfileService.GetSoftwareStatementProfile(
                    bankClientProfile.SoftwareStatementProfileId);

            // STEP 1
            // Compute claims associated with Open Banking client

            // Get OpenID Connect configuration info
            OpenIdConfiguration openIdConfiguration =
                await GetOpenIdConfigurationAsync(bankClientProfile.IssuerUrl);

            new OpenBankingOpenIdConfigurationResponseValidator().Validate(openIdConfiguration)
            .RaiseErrorOnValidationError();

            // Create claims for client reg
            OpenBankingClientRegistrationClaims registrationClaims = Factories.CreateRegistrationClaims(
                issuerUrl: bankClientProfile.IssuerUrl,
                sProfile: softwareStatementProfile,
                concatScopes: false);
            BankClientRegistrationClaimsOverrides registrationClaimsOverrides =
                bankClientProfile.BankClientRegistrationClaimsOverrides;

            if (!(registrationClaimsOverrides is null))
            {
                if (!(registrationClaimsOverrides.RequestAudience is null))
                {
                    registrationClaims.Aud = registrationClaimsOverrides.RequestAudience;
                }
            }

            BankClientRegistrationClaims persistentRegistrationClaims =
                _mapper.Map <BankClientRegistrationClaims>(registrationClaims);

            // STEP 2
            // Check for existing Open Banking client for issuer URL
            // If we have an Open Banking client with the same issuer URL we will check if the claims match.
            // If they do, we will re-use this client.
            // Otherwise we will return an error as only support a single client per issuer URL at present.
            IQueryable <BankClientProfile> clientList = await _bankClientProfileRepo
                                                        .GetAsync(c => c.IssuerUrl == bankClientProfile.IssuerUrl);

            BankClientProfile existingClient = clientList
                                               .SingleOrDefault();

            if (existingClient is object)
            {
                if (existingClient.BankClientRegistrationClaims != persistentRegistrationClaims)
                {
                    throw new Exception(
                              "There is already a client for this issuer URL but it cannot be re-used because claims are different.");
                }
            }

            // STEP 3
            // Create new Open Banking client by posting JWT
            BankClientProfile client;

            if (existingClient is null)
            {
                JwtFactory jwtFactory = new JwtFactory();
                string     jwt        = jwtFactory.CreateJwt(
                    profile: softwareStatementProfile,
                    claims: registrationClaims,
                    useOpenBankingJwtHeaders: false);

                OpenBankingClientRegistrationResponse registrationResponse = await new HttpRequestBuilder()
                                                                             .SetMethod(HttpMethod.Post)
                                                                             .SetUri(openIdConfiguration.RegistrationEndpoint)
                                                                             .SetContent(jwt)
                                                                             .SetContentType("application/jwt")
                                                                             .Create()
                                                                             .RequestJsonAsync <OpenBankingClientRegistrationResponse>(
                    client: _apiClient,
                    requestContentIsJson: false);

                BankClientRegistrationData openBankingClientResponse = new BankClientRegistrationData
                {
                    ClientId              = registrationResponse.ClientId,
                    ClientIdIssuedAt      = registrationResponse.ClientIdIssuedAt,
                    ClientSecret          = registrationResponse.ClientSecret,
                    ClientSecretExpiresAt = registrationResponse.ClientSecretExpiresAt
                };

                // Create and store Open Banking client
                BankClientProfile newClient = _mapper.Map <BankClientProfile>(bankClientProfile);
                client = await PersistOpenBankingClient(
                    value : newClient,
                    openIdConfiguration : openIdConfiguration,
                    registrationClaims : registrationClaims,
                    openBankingRegistrationData : openBankingClientResponse);

                await _dbMultiEntityMethods.SaveChangesAsync();
            }
            else
            {
                client = existingClient;
            }

            // Return
            return(new BankClientProfileResponse(client));
        }