diff --git a/DD.Persistence.Client/DD.Persistence.Client.csproj b/DD.Persistence.Client/DD.Persistence.Client.csproj index 5d60611..2395482 100644 --- a/DD.Persistence.Client/DD.Persistence.Client.csproj +++ b/DD.Persistence.Client/DD.Persistence.Client.csproj @@ -52,7 +52,6 @@ - diff --git a/DD.Persistence.Client/Helpers/ApiTokenHelper.cs b/DD.Persistence.Client/Helpers/ApiTokenHelper.cs index 9e62110..85fa428 100644 --- a/DD.Persistence.Client/Helpers/ApiTokenHelper.cs +++ b/DD.Persistence.Client/Helpers/ApiTokenHelper.cs @@ -1,16 +1,25 @@ +using DD.Persistence.Models.Configurations; using Microsoft.Extensions.Configuration; using Microsoft.IdentityModel.Tokens; -using DD.Persistence.Models.Configurations; -using RestSharp; using System.IdentityModel.Tokens.Jwt; using System.Net.Http.Headers; using System.Security.Claims; using System.Text.Json; namespace DD.Persistence.Client.Helpers; + +/// +/// Класс, позволяющий генерировать api-token +/// public static class ApiTokenHelper { - public static void Authorize(this HttpClient httpClient, IConfiguration configuration) + /// + /// Метод авториации + /// + /// + /// + /// + public static async Task Authorize(this HttpClient httpClient, IConfiguration configuration) { var authUser = configuration .GetSection(nameof(AuthUser)) @@ -21,29 +30,29 @@ public static class ApiTokenHelper var keycloakGetTokenUrl = configuration.GetSection("KeycloakGetTokenUrl").Get() ?? string.Empty; var jwtToken = needUseKeyCloak - ? authUser.CreateKeyCloakJwtToken(keycloakGetTokenUrl) + ? await authUser.CreateKeyCloakJwtToken(keycloakGetTokenUrl) : authUser.CreateDefaultJwtToken(); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwtToken); } - public static void Authorize(this HttpClient httpClient, string jwtToken) - { - httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwtToken); - } - + /// + /// Авторизация через собственный jwt-токен + /// + /// + /// private static string CreateDefaultJwtToken(this AuthUser authUser) - { - var nameIdetifier = Guid.NewGuid().ToString(); - var claims = new List() - { - new(ClaimTypes.NameIdentifier, nameIdetifier), - new("client_id", authUser.ClientId), - new("username", authUser.Username), - new("password", authUser.Password), - new("grant_type", authUser.GrantType), - new(ClaimTypes.NameIdentifier.ToString(), Guid.NewGuid().ToString()) - }; + { + var nameIdetifier = Guid.NewGuid().ToString(); + var claims = new List() + { + new(ClaimTypes.NameIdentifier, nameIdetifier), + new("client_id", authUser.ClientId), + new("username", authUser.Username), + new("password", authUser.Password), + new("grant_type", authUser.GrantType), + new(ClaimTypes.NameIdentifier.ToString(), Guid.NewGuid().ToString()) + }; var tokenDescriptor = new SecurityTokenDescriptor { @@ -58,23 +67,31 @@ public static class ApiTokenHelper return tokenHandler.WriteToken(token); } - private static string CreateKeyCloakJwtToken(this AuthUser authUser, string keycloakGetTokenUrl) + /// + /// Авторизация через jwt-токен keycloak + /// + /// + /// + /// + private static async Task CreateKeyCloakJwtToken(this AuthUser authUser, string keycloakGetTokenUrl) { - var restClient = new RestClient(); + var sharedClient = new HttpClient(); + var parameters = new Dictionary { + { "username", authUser.Username }, + { "password", authUser.Password }, + { "client_id", authUser.ClientId }, + { "grant_type", authUser.GrantType }, + }; + var encodedContent = new FormUrlEncodedContent(parameters); - var request = new RestRequest(keycloakGetTokenUrl, Method.Post); - request.AddParameter("username", authUser.Username); - request.AddParameter("password", authUser.Password); - request.AddParameter("client_id", authUser.ClientId); - request.AddParameter("grant_type", authUser.GrantType); + using HttpResponseMessage response = await sharedClient.PostAsync(keycloakGetTokenUrl, encodedContent); - var keycloakResponse = restClient.Post(request); - if (keycloakResponse.IsSuccessful && !String.IsNullOrEmpty(keycloakResponse.Content)) + if (response.IsSuccessStatusCode == true) { - var token = JsonSerializer.Deserialize(keycloakResponse.Content)!; + var data = await response.Content.ReadAsStreamAsync(); + var token = JsonSerializer.Deserialize(data)!; return token.AccessToken; } - return String.Empty; } } diff --git a/DD.Persistence.IntegrationTests/TestHttpClientFactory.cs b/DD.Persistence.IntegrationTests/TestHttpClientFactory.cs index 51b8783..4c0cd04 100644 --- a/DD.Persistence.IntegrationTests/TestHttpClientFactory.cs +++ b/DD.Persistence.IntegrationTests/TestHttpClientFactory.cs @@ -19,7 +19,7 @@ namespace DD.Persistence.IntegrationTests public HttpClient CreateClient(string name) { var client = factory.CreateClient(); - client.Authorize(configuration); + client.Authorize(configuration).GetAwaiter().GetResult(); return client; } diff --git a/DD.Persistence.IntegrationTests/WebAppFactoryFixture.cs b/DD.Persistence.IntegrationTests/WebAppFactoryFixture.cs index 31b4da4..435287a 100644 --- a/DD.Persistence.IntegrationTests/WebAppFactoryFixture.cs +++ b/DD.Persistence.IntegrationTests/WebAppFactoryFixture.cs @@ -8,7 +8,6 @@ using Microsoft.Extensions.Logging; using DD.Persistence.API; using DD.Persistence.Client; using DD.Persistence.Database.Model; -using RestSharp; using DD.Persistence.App; using DD.Persistence.Client.Helpers; using DD.Persistence.Factories;