Skip to content

Commit

Permalink
Cherry-pick forgotten changes from private repo (#60)
Browse files Browse the repository at this point in the history
* Cherry-pick forgotten changes from private repo

* Cherry-pick other changes
  • Loading branch information
s-vitaliy authored Jun 25, 2024
1 parent 26d754b commit 1fb3cad
Showing 1 changed file with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public record DynamicBearerAuthenticatedMessageProvider : IRestApiAuthenticatedM
private readonly HttpMethod requestMethod;
private readonly string tokenPropertyName;
private readonly string tokenRequestBody;
private readonly string tokenRequestContentType;
private readonly string authHeaderName;
private readonly string authScheme;
private readonly Uri tokenSource;
Expand All @@ -36,6 +37,7 @@ public record DynamicBearerAuthenticatedMessageProvider : IRestApiAuthenticatedM
/// <param name="expirationPeriodPropertyName">Token expiration property name</param>
/// <param name="requestMethod">HTTP method for token request</param>
/// <param name="tokenRequestBody">HTTP body for token request</param>
/// <param name="tokenRequestContentType">HTTP content-type header for the token request</param>
/// <param name="authHeaderName">Authorization header name</param>
/// <param name="authScheme">Authorization scheme</param>
/// <param name="additionalHeaders">Additional token headers</param>
Expand All @@ -44,6 +46,7 @@ public DynamicBearerAuthenticatedMessageProvider(string tokenSource,
string expirationPeriodPropertyName,
HttpMethod requestMethod = null,
string tokenRequestBody = null,
string tokenRequestContentType = null,
Dictionary<string, string> additionalHeaders = null,
string authHeaderName = null,
string authScheme = null)
Expand All @@ -52,6 +55,7 @@ public DynamicBearerAuthenticatedMessageProvider(string tokenSource,
this.tokenPropertyName = tokenPropertyName;
this.expirationPeriodPropertyName = expirationPeriodPropertyName;
this.tokenRequestBody = tokenRequestBody;
this.tokenRequestContentType = tokenRequestContentType;
this.requestMethod = requestMethod ?? HttpMethod.Get;
this.authHeaderName = authHeaderName;
this.authScheme = authScheme;
Expand All @@ -66,6 +70,7 @@ public DynamicBearerAuthenticatedMessageProvider(string tokenSource,
/// <param name="expirationPeriod">Token expiration period</param>
/// <param name="requestMethod">HTTP method for token request</param>
/// <param name="tokenRequestBody">HTTP body for token request</param>
/// <param name="tokenRequestContentType">HTTP content-type header for the token request</param>
/// <param name="additionalHeaders">Additional token headers</param>
/// <param name="authHeaderName">Authorization header name</param>
/// <param name="authScheme">Authorization scheme</param>
Expand All @@ -74,6 +79,7 @@ public DynamicBearerAuthenticatedMessageProvider(string tokenSource,
TimeSpan expirationPeriod,
HttpMethod requestMethod = null,
string tokenRequestBody = null,
string tokenRequestContentType = null,
Dictionary<string, string> additionalHeaders = null,
string authHeaderName = null,
string authScheme = null)
Expand All @@ -82,6 +88,7 @@ public DynamicBearerAuthenticatedMessageProvider(string tokenSource,
this.tokenPropertyName = tokenPropertyName;
this.expirationPeriod = expirationPeriod;
this.tokenRequestBody = tokenRequestBody;
this.tokenRequestContentType = tokenRequestContentType;
this.requestMethod = requestMethod ?? HttpMethod.Get;
this.authHeaderName = authHeaderName;
this.authScheme = authScheme;
Expand All @@ -91,17 +98,22 @@ public DynamicBearerAuthenticatedMessageProvider(string tokenSource,
/// <inheritdoc cref="IRestApiAuthenticatedMessageProvider.GetAuthenticatedMessage"/>
public Task<HttpRequestMessage> GetAuthenticatedMessage(HttpClient httpClient)
{

if (this.validTo.GetValueOrDefault(DateTimeOffset.MaxValue) < DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1)))
{
return Task.FromResult(this.GetRequest());
}

var tokenHrm = new HttpRequestMessage(this.requestMethod, this.tokenSource);

if (!string.IsNullOrEmpty(this.tokenRequestBody))
if (!string.IsNullOrEmpty(tokenRequestBody))
{
tokenHrm.Content = new StringContent(this.tokenRequestBody, Encoding.UTF8, "application/json");
tokenHrm.Content = this.tokenRequestContentType switch
{
null or "application/json" => new StringContent(tokenRequestBody, Encoding.UTF8, "application/json"),
"application/x-www-form-urlencoded" => new FormUrlEncodedContent(JsonSerializer.Deserialize<Dictionary<string, string>>(tokenRequestBody)),
_ => throw new ArgumentException($"Unsupported content type for authentication: {this.tokenRequestContentType}")
};

}

return httpClient.SendAsync(tokenHrm, CancellationToken.None).Map(response =>
Expand All @@ -112,14 +124,10 @@ public Task<HttpRequestMessage> GetAuthenticatedMessage(HttpClient httpClient)
{
var tokenResponse = JsonSerializer.Deserialize<JsonElement>(result);
this.currentToken = tokenResponse.GetProperty(this.tokenPropertyName).GetString();
this.validTo = !string.IsNullOrEmpty(this.expirationPeriodPropertyName)
? DateTimeOffset.UtcNow.AddSeconds(tokenResponse.GetProperty(this.expirationPeriodPropertyName)
.GetInt32())
this.validTo = !string.IsNullOrEmpty(expirationPeriodPropertyName)
? DateTimeOffset.UtcNow.AddSeconds(tokenResponse.GetProperty(this.expirationPeriodPropertyName).GetInt32())
: DateTimeOffset.UtcNow.Add(this.expirationPeriod);
return new HttpRequestMessage
{
Headers = { Authorization = new AuthenticationHeaderValue("Bearer", this.currentToken) }
};
return this.GetRequest();
});
}

Expand Down

0 comments on commit 1fb3cad

Please # to comment.