2025-01-31 16:07:17 +05:00
|
|
|
using DD.Persistence.Models.Configurations;
|
2024-11-25 10:09:38 +05:00
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
2024-12-10 10:43:12 +05:00
|
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
|
|
using System.Net.Http.Headers;
|
|
|
|
using System.Security.Claims;
|
|
|
|
using System.Text.Json;
|
2024-11-25 10:09:38 +05:00
|
|
|
|
2025-02-03 16:01:08 +05:00
|
|
|
namespace DD.Persistence.IntegrationTests;
|
2025-01-31 16:07:17 +05:00
|
|
|
|
|
|
|
/// <summary>
|
2025-02-03 09:53:13 +05:00
|
|
|
/// Класс, позволяющий генерировать api-token
|
2025-01-31 16:07:17 +05:00
|
|
|
/// </summary>
|
2024-11-21 14:50:36 +05:00
|
|
|
public static class ApiTokenHelper
|
|
|
|
{
|
2025-01-31 16:07:17 +05:00
|
|
|
/// <summary>
|
2025-02-03 09:53:13 +05:00
|
|
|
/// Метод авторизации
|
2025-01-31 16:07:17 +05:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="httpClient"></param>
|
|
|
|
/// <param name="configuration"></param>
|
|
|
|
/// <returns></returns>
|
2025-01-31 17:04:05 +05:00
|
|
|
public static void Authorize(this HttpClient httpClient, IConfiguration configuration)
|
2024-12-10 10:43:12 +05:00
|
|
|
{
|
|
|
|
var authUser = configuration
|
|
|
|
.GetSection(nameof(AuthUser))
|
|
|
|
.Get<AuthUser>()!;
|
2024-11-25 10:09:38 +05:00
|
|
|
|
2025-02-03 16:01:08 +05:00
|
|
|
var jwtToken = authUser.CreateDefaultJwtToken();
|
2024-11-25 10:09:38 +05:00
|
|
|
|
2024-12-10 10:43:12 +05:00
|
|
|
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwtToken);
|
|
|
|
}
|
2024-11-25 10:09:38 +05:00
|
|
|
|
2025-01-31 16:07:17 +05:00
|
|
|
/// <summary>
|
2025-02-03 09:53:13 +05:00
|
|
|
/// Авторизация через собственный jwt-токен
|
2025-01-31 16:07:17 +05:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="authUser"></param>
|
|
|
|
/// <returns></returns>
|
2024-11-25 10:09:38 +05:00
|
|
|
private static string CreateDefaultJwtToken(this AuthUser authUser)
|
2025-01-31 16:07:17 +05:00
|
|
|
{
|
|
|
|
var nameIdetifier = Guid.NewGuid().ToString();
|
|
|
|
var claims = new List<Claim>()
|
|
|
|
{
|
|
|
|
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())
|
|
|
|
};
|
2024-11-25 10:09:38 +05:00
|
|
|
|
2024-12-10 10:43:12 +05:00
|
|
|
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);
|
|
|
|
}
|
2024-11-21 14:50:36 +05:00
|
|
|
}
|