/// <inheritdoc /> public async Task <TokenExchangeResult> ExchangeBankTokenAsync(string publicToken, string accountId) { Condition.Requires(publicToken).IsNotNullOrWhiteSpace(); ExchangeTokenRequest exchangeRequest = new ExchangeTokenRequest(this.clientId, this.clientSecret, publicToken, accountId); HttpResponseMessage response = await this.httpClient.PostAsJsonAsync("exchange_token", exchangeRequest); string responseJson = await response.Content.ReadAsStringAsync(); TokenExchangeResult result = new TokenExchangeResult(); if (response.StatusCode == HttpStatusCode.OK) { ExchangeTokenResponse tokenResponse = JsonConvert.DeserializeObject <ExchangeTokenResponse>(responseJson); result.AccessToken = new AccessToken(tokenResponse.AccessToken); result.BankAccountToken = tokenResponse.BankToken; return(result); } result.Exception = await this.ParseException(response, responseJson); return(result); }
public async Task <TokenExchangeResult> ExchangeTokenAsync(string referenceToken, ReferenceTokenExchangeOptions options) { if (referenceToken == null) { referenceToken = string.Empty; } var payload = new { token = referenceToken }; var identityServer = await DiscoverIdentityServerAsync(options.IdentityServerRootUrl, options.RequireHttpsEndpoints, options.AdditionalEndpointBaseAddresses); var client = new TokenClient(identityServer.TokenEndpoint, options.ClientId, options.ClientSecret, options.HttpHandler); _logger.LogInformation($"Requesting token exchange from Identity Server: {identityServer.TokenEndpoint}"); var tokenResponse = await client.RequestCustomGrantAsync(options.GrantType, options.Scope, payload); if (tokenResponse.IsError) { _logger.LogWarning($"An error occured during reference token exchange: {tokenResponse.Error}"); } var result = new TokenExchangeResult { ReferenceToken = referenceToken, AccessToken = tokenResponse.AccessToken, AccessTokenExpiryTime = DateTimeOffset.UtcNow.AddSeconds(tokenResponse.ExpiresIn) }; _logger.LogInformation("Token exchange with Identity Server completed successfully"); return(result); }
private void AddTokenToCache(TokenExchangeResult tokenExchangeResult) { if (tokenExchangeResult.AccessToken == null) { return; } _cache.AddToken(tokenExchangeResult); }
public async Task ExchangeTokenFailed() { IHttpClientWrapper httpClient = this.GetMockHttpClient("InvalidCredentials.json", HttpStatusCode.Unauthorized, HttpMethod.Post, "exchange_token"); IPlaidClient testClient = this.GetPlaidClient(httpClient); TokenExchangeResult result = await testClient.ExchangeBankTokenAsync("test_public_token", "test_account_id"); Assert.IsNull(result.AccessToken); Assert.IsNull(result.BankAccountToken); Assert.IsTrue(result.IsError); Assert.IsNotNull(result.Exception); Assert.AreEqual(401, result.Exception.HttpStatusCode); Assert.AreEqual(ErrorCode.InvalidCredentials, result.Exception.ErrorCode); Assert.IsFalse(string.IsNullOrWhiteSpace(result.Exception.Message)); Assert.IsFalse(string.IsNullOrWhiteSpace(result.Exception.Resolution)); }
/// <inheritdoc /> public async Task <TokenExchangeResult> StripeBankTokenAsync(string accessToken, string accountId) { Condition.Requires(accessToken).IsNotNullOrWhiteSpace(); StripeBankAccountRequest exchangeRequest = new StripeBankAccountRequest(this.clientId, this.clientSecret, accessToken, accountId); HttpResponseMessage response = await this.httpClient.PostAsJsonAsync("processor/stripe/bank_account_token/create", exchangeRequest); string responseJson = await response.Content.ReadAsStringAsync(); TokenExchangeResult result = new TokenExchangeResult(); result.RawResponse = responseJson; if (response.StatusCode == HttpStatusCode.OK) { StripeBankAccountResponse tokenResponse = JsonConvert.DeserializeObject <StripeBankAccountResponse>(responseJson); result.BankAccountToken = tokenResponse.StripeBankAccount; return(result); } result.Exception = await this.ParseException(response, responseJson); return(result); }
public void RemoveToken(TokenExchangeResult tokenResult) { _tokens.Remove(tokenResult); }
public void AddToken(TokenExchangeResult tokenResult) { _tokens.Add(tokenResult); }