Skip to content

Commit

Permalink
Add support for NetStandard 2.0+
Browse files Browse the repository at this point in the history
  • Loading branch information
RobThree committed Feb 19, 2024
1 parent d68b336 commit 0534f74
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
Expand Down Expand Up @@ -34,7 +34,14 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Bcl.TimeProvider" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
<PackageReference Include="PolySharp" Version="1.14.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Net.Http.Json" Version="8.0.0" />
<PackageReference Include="System.Text.Json" Version="8.0.2" />
</ItemGroup>

</Project>
15 changes: 10 additions & 5 deletions AutoRefreshTokenHttpMessageHandler/TokenAuthenticationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ private async Task<Token> RetrieveTokenAsync(string grantType, CancellationToken
{
result.EnsureSuccessStatusCode();
return Token.FromTokenResponse(
await result.Content.ReadFromJsonAsync<TokenResponse>(cancellationToken) ?? throw new HttpRequestException(Translations.INVALID_OR_UNEXPECTED_RESPONSE, null, result.StatusCode),
await result.Content.ReadFromJsonAsync<TokenResponse>(cancellationToken) ?? throw new ExtendedHttpRequestException(Translations.INVALID_OR_UNEXPECTED_RESPONSE, null, result.StatusCode),
_timeprovider.GetUtcNow()
);
}
catch (HttpRequestException ex)
{
var err = await result.Content.ReadFromJsonAsync<ErrorResponse>(cancellationToken) ?? throw new HttpRequestException(Translations.INVALID_OR_UNEXPECTED_RESPONSE, null, result.StatusCode);
throw new HttpRequestException($"[{err.Error}] {err.Description}", ex, result.StatusCode);
var err = await result.Content.ReadFromJsonAsync<ErrorResponse>(cancellationToken) ?? throw new ExtendedHttpRequestException(Translations.INVALID_OR_UNEXPECTED_RESPONSE, null, result.StatusCode);
throw new ExtendedHttpRequestException($"[{err.Error}] {err.Description}", ex, result.StatusCode);
}
}

Expand Down Expand Up @@ -76,7 +76,7 @@ private async Task<Token> RefreshTokenAsync(Token token, CancellationToken cance
return await RetrieveTokenAsync(GrantTypes.Refresh, cancellationToken);
}
}
catch (HttpRequestException ex) when (ex.StatusCode is HttpStatusCode.BadRequest or HttpStatusCode.Unauthorized) { }
catch (ExtendedHttpRequestException ex) when (ex.StatusCode is HttpStatusCode.BadRequest or HttpStatusCode.Unauthorized) { }
// Refresh token expired or refresh failed? Try to get a new token
return await RetrieveTokenAsync(GetGrantType(), cancellationToken).ConfigureAwait(false);
}
Expand All @@ -99,4 +99,9 @@ private record ErrorResponse
[property: JsonPropertyName("error")] string Error,
[property: JsonPropertyName("error_description")] string Description
);
}

private class ExtendedHttpRequestException(string message, Exception? innerException, HttpStatusCode httpStatusCode) : HttpRequestException(message, innerException)
{
public HttpStatusCode StatusCode { get; private set; } = httpStatusCode;
}
}

0 comments on commit 0534f74

Please sign in to comment.