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 deleted file mode 100644 index 9e62110..0000000 --- a/DD.Persistence.Client/Helpers/ApiTokenHelper.cs +++ /dev/null @@ -1,80 +0,0 @@ -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; -public static class ApiTokenHelper -{ - public static void Authorize(this HttpClient httpClient, IConfiguration configuration) - { - var authUser = configuration - .GetSection(nameof(AuthUser)) - .Get()!; - var needUseKeyCloak = configuration - .GetSection("NeedUseKeyCloak") - .Get()!; - var keycloakGetTokenUrl = configuration.GetSection("KeycloakGetTokenUrl").Get() ?? string.Empty; - - var jwtToken = needUseKeyCloak - ? 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); - } - - 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 tokenDescriptor = new SecurityTokenDescriptor - { - Issuer = JwtParams.Issuer, - Audience = JwtParams.Audience, - Subject = new ClaimsIdentity(claims), - Expires = DateTime.UtcNow.AddHours(1), - SigningCredentials = new SigningCredentials(JwtParams.SecurityKey, SecurityAlgorithms.HmacSha256Signature) - }; - var tokenHandler = new JwtSecurityTokenHandler(); - var token = tokenHandler.CreateToken(tokenDescriptor); - return tokenHandler.WriteToken(token); - } - - private static string CreateKeyCloakJwtToken(this AuthUser authUser, string keycloakGetTokenUrl) - { - var restClient = new RestClient(); - - 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); - - var keycloakResponse = restClient.Post(request); - if (keycloakResponse.IsSuccessful && !String.IsNullOrEmpty(keycloakResponse.Content)) - { - var token = JsonSerializer.Deserialize(keycloakResponse.Content)!; - return token.AccessToken; - } - - return String.Empty; - } -} diff --git a/DD.Persistence.IntegrationTests/ApiTokenHelper.cs b/DD.Persistence.IntegrationTests/ApiTokenHelper.cs new file mode 100644 index 0000000..a93fb0d --- /dev/null +++ b/DD.Persistence.IntegrationTests/ApiTokenHelper.cs @@ -0,0 +1,63 @@ +using DD.Persistence.Models.Configurations; +using Microsoft.Extensions.Configuration; +using Microsoft.IdentityModel.Tokens; +using System.IdentityModel.Tokens.Jwt; +using System.Net.Http.Headers; +using System.Security.Claims; +using System.Text.Json; + +namespace DD.Persistence.IntegrationTests; + +/// +///  Класс, позволяющий генерировать api-token +/// +public static class ApiTokenHelper +{ + /// + /// Метод авторизации + /// + /// + /// + /// + public static void Authorize(this HttpClient httpClient, IConfiguration configuration) + { + var authUser = configuration + .GetSection(nameof(AuthUser)) + .Get()!; + + var jwtToken = authUser.CreateDefaultJwtToken(); + + 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 tokenDescriptor = new SecurityTokenDescriptor + { + Issuer = JwtParams.Issuer, + Audience = JwtParams.Audience, + Subject = new ClaimsIdentity(claims), + Expires = DateTime.UtcNow.AddHours(1), + SigningCredentials = new SigningCredentials(JwtParams.SecurityKey, SecurityAlgorithms.HmacSha256Signature) + }; + var tokenHandler = new JwtSecurityTokenHandler(); + var token = tokenHandler.CreateToken(tokenDescriptor); + return tokenHandler.WriteToken(token); + } +} 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;