Skip to content

Commit

Permalink
RE #20269 - API Keystone HubSpot to EMG order system (#44)
Browse files Browse the repository at this point in the history
* RE #20269 - API Keystone HubSpot to EMG order system

Remove usage of ITokenSelector and include InnerHandler for OAuthenticator

* RE #20269 - API Keystone HubSpot to EMG order system
Fix unit tests

* RE #20269 - API Keystone HubSpot to EMG order system

Cleanup OAuthSettings model
  • Loading branch information
diaconu96vi authored Sep 13, 2022
1 parent db212ce commit 5d421f5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
10 changes: 4 additions & 6 deletions src/HubSpot.Client/Authentication/OAuthHubSpotAuthenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,20 @@ public class OAuthHubSpotAuthenticator : DelegatingHandler
{
private readonly OAuthOptions _options;

public OAuthHubSpotAuthenticator(IOptions<OAuthOptions> options, ITokenSelector tokenSelector)
public OAuthHubSpotAuthenticator(IOptions<OAuthOptions> options, HttpMessageHandler handler) : base(handler)
{
_options = options?.Value ?? throw new ArgumentNullException(nameof(options));
_tokenSelector = tokenSelector ?? throw new ArgumentNullException(nameof(options));
}

private readonly ConcurrentDictionary<string, AuthToken> _tokens = new ConcurrentDictionary<string, AuthToken>();
private readonly ITokenSelector _tokenSelector;

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var requestUri = request.RequestUri;

var key = ExtractKey(requestUri);

if (!_tokens.TryGetValue(key, out var token) || !IsTokenValid(token) || _tokenSelector.IsTokenChanged())
if (!_tokens.TryGetValue(key, out var token) || !IsTokenValid(token))
{
token = await GetToken(requestUri, cancellationToken);

Expand Down Expand Up @@ -64,7 +62,7 @@ private async Task<AuthToken> GetToken(Uri requestUri, CancellationToken cancell
["client_id"] = _options.ClientId,
["client_secret"] = _options.SecretKey,
["redirect_uri"] = _options.RedirectUri.ToString(),
["refresh_token"] = _tokenSelector.SelectToken(_options.RefreshTokens)
["refresh_token"] = _options.RefreshToken
};

request.Content = new FormUrlEncodedContent(form);
Expand Down Expand Up @@ -116,7 +114,7 @@ public class OAuthOptions

public Uri RedirectUri { get; set; } = new Uri("https://www.hubspot.com");

public Dictionary<string, string> RefreshTokens { get; set; }
public string RefreshToken { get; set; }

public string SecretKey { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ private HttpClient CreateClient(OAuthOptions options, params HttpMessageOptions[

var wrapper = new OptionsWrapper<OAuthOptions>(options);

var testTokenSelector = new TestTokenSelector();

var authenticator = new OAuthHubSpotAuthenticator(wrapper, testTokenSelector) { InnerHandler = handler };
var authenticator = new OAuthHubSpotAuthenticator(wrapper, handler);

var client = new HttpClient(authenticator, false);

Expand Down Expand Up @@ -53,7 +51,7 @@ public async Task Single_request_is_authenticated_and_executed(string methodName
["client_id"] = options.ClientId,
["client_secret"] = options.SecretKey,
["redirect_uri"] = options.RedirectUri.ToString(),
["refresh_token"] = tokenSelector.SelectToken(options.RefreshTokens)
["refresh_token"] = options.RefreshToken
}),
HttpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK)
{
Expand Down Expand Up @@ -106,7 +104,7 @@ public async Task Multiple_requests_reuse_the_same_token(string methodName, OAut
["client_id"] = options.ClientId,
["client_secret"] = options.SecretKey,
["redirect_uri"] = options.RedirectUri.ToString(),
["refresh_token"] = tokenSelector.SelectToken(options.RefreshTokens)
["refresh_token"] = options.RefreshToken
}),
HttpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK)
{
Expand Down Expand Up @@ -164,7 +162,7 @@ public async Task Token_is_refreshed_if_expired(string methodName, OAuthOptions
["client_id"] = options.ClientId,
["client_secret"] = options.SecretKey,
["redirect_uri"] = options.RedirectUri.ToString(),
["refresh_token"] = tokenSelector.SelectToken(options.RefreshTokens)
["refresh_token"] = options.RefreshToken
}),
HttpResponseMessage = CreateNewResponse()
};
Expand Down Expand Up @@ -235,7 +233,7 @@ public async Task Token_is_not_attached_if_request_fails(string methodName, OAut
["client_id"] = options.ClientId,
["client_secret"] = options.SecretKey,
["redirect_uri"] = options.RedirectUri.ToString(),
["refresh_token"] = tokenSelector.SelectToken(options.RefreshTokens)
["refresh_token"] = options.RefreshToken
}),
HttpResponseMessage = new HttpResponseMessage(HttpStatusCode.Unauthorized)
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using NUnit.Framework;
using System.Net.Http;
using Tests.Model;

namespace Tests.Configuration
Expand Down Expand Up @@ -35,7 +36,7 @@ public void UseApiKeyAuthentication_registers_needed_services(HubSpotClientConfi
}

[Test, CustomAutoData]
public void UseOAuthAuthentication_registers_needed_services(HubSpotClientConfigurator configurator, OAuthOptions apiKeyOptions, TestTokenSelector tokenSelector, ServiceCollection services)
public void UseOAuthAuthentication_registers_needed_services(HubSpotClientConfigurator configurator, OAuthOptions apiKeyOptions, HttpMessageHandler httpMessageHandler, ServiceCollection services)
{
var configurationBuilder = new ConfigurationBuilder();

Expand All @@ -47,7 +48,7 @@ public void UseOAuthAuthentication_registers_needed_services(HubSpotClientConfig

configurator.ApplyServiceConfigurations(services);

services.AddSingleton<ITokenSelector, TestTokenSelector>();
services.AddSingleton(httpMessageHandler);

var serviceProvider = services.BuildServiceProvider();

Expand Down

0 comments on commit 5d421f5

Please sign in to comment.