From 81601277060c3a31b031ba662a24bf9e3fb0f7db Mon Sep 17 00:00:00 2001 From: Luiz Adolfo Date: Thu, 20 Mar 2025 09:24:09 -0300 Subject: [PATCH] Updated readme TargetApiAuthClass example to improve authentication logic and readability --- README.md | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 73e3721..c44f2ba 100644 --- a/README.md +++ b/README.md @@ -39,26 +39,29 @@ This will make the `TargetApi` HttpClient use the Authorization Interceptor hand The `TargetApiAuthClass` must implement the `IAuthenticationHandler` interface, so that the package can perform the necessary dependency and know where and when to generate the authorization headers. An example implementation of the class would look like this: ```csharp -public async ValueTask AuthenticateAsync(AuthorizationHeaders? expiredHeaders, CancellationToken cancellation) +public class TargetApiAuthClass : IAuthenticationHandler { - HttpResponseMessage? response = null; - - if (expiredHeaders == null) + public async ValueTask AuthenticateAsync(AuthorizationHeaders? expiredHeaders, CancellationToken cancellation) { - // Generate the login for the first time and return the authorization headers - response = await _client.PostAsync("auth", null, cancellation); - } - else - { - // If a previous login was made, the expiredHeaders will be passed and you can reuse to reauthenticate. It is most commonly used for integrations with APIs that use RefreshToken. - // If the target API does not have the refresh token functionality, you will not need to implement this if condition e you can ignore the parameter 'expiredHeaders' performing always a new login - response = await _client.PostAsync($"refresh?refresh={expiredHeaders.OAuthHeaders!.RefreshToken}", null, cancellation); - } + HttpResponseMessage? response = null; - var content = await response.Content.ReadAsStringAsync(cancellation); - var newHeaders = JsonSerializer.Deserialize(content)!; + if (expiredHeaders == null) + { + // Generate the login for the first time and return the authorization headers + response = await _client.PostAsync("auth", null, cancellation); + } + else + { + // If a previous login was made, the expiredHeaders will be passed and you can reuse to reauthenticate. It is most commonly used for integrations with APIs that use RefreshToken. + // If the target API does not have the refresh token functionality, you will not need to implement this if condition e you can ignore the parameter 'expiredHeaders' performing always a new login + response = await _client.PostAsync($"refresh?refresh={expiredHeaders.OAuthHeaders!.RefreshToken}", null, cancellation); + } - return new OAuthHeaders(newHeaders.AccessToken, newHeaders.TokenType, newHeaders.ExpiresIn, newHeaders.RefreshToken, newHeaders.RefreshTokenExpiresIn); + var content = await response.Content.ReadAsStringAsync(cancellation); + var newHeaders = JsonSerializer.Deserialize(content)!; + + return new OAuthHeaders(newHeaders.AccessToken, newHeaders.TokenType, newHeaders.ExpiresIn, newHeaders.RefreshToken, newHeaders.RefreshTokenExpiresIn); + } } ```