|
|
|
@ -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;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Êëàññ, ïîçâîëÿþùèé ãåíåðèðîâàòü api-token
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class ApiTokenHelper
|
|
|
|
|
{
|
|
|
|
|
public static void Authorize(this HttpClient httpClient, IConfiguration configuration)
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Ìåòîä àâòîðèàöèè
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="httpClient"></param>
|
|
|
|
|
/// <param name="configuration"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static async Task Authorize(this HttpClient httpClient, IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
var authUser = configuration
|
|
|
|
|
.GetSection(nameof(AuthUser))
|
|
|
|
@ -21,17 +30,17 @@ public static class ApiTokenHelper
|
|
|
|
|
var keycloakGetTokenUrl = configuration.GetSection("KeycloakGetTokenUrl").Get<string>() ?? 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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Àâòîðèçàöèÿ ÷åðåç ñîáñòâåííûé jwt-òîêåí
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="authUser"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private static string CreateDefaultJwtToken(this AuthUser authUser)
|
|
|
|
|
{
|
|
|
|
|
var nameIdetifier = Guid.NewGuid().ToString();
|
|
|
|
@ -58,23 +67,31 @@ public static class ApiTokenHelper
|
|
|
|
|
return tokenHandler.WriteToken(token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string CreateKeyCloakJwtToken(this AuthUser authUser, string keycloakGetTokenUrl)
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Àâòîðèçàöèÿ ÷åðåç jwt-òîêåí keycloak
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="authUser"></param>
|
|
|
|
|
/// <param name="keycloakGetTokenUrl"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private static async Task<string> CreateKeyCloakJwtToken(this AuthUser authUser, string keycloakGetTokenUrl)
|
|
|
|
|
{
|
|
|
|
|
var restClient = new RestClient();
|
|
|
|
|
var sharedClient = new HttpClient();
|
|
|
|
|
var parameters = new Dictionary<string, string> {
|
|
|
|
|
{ "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<JwtToken>(keycloakResponse.Content)!;
|
|
|
|
|
var data = await response.Content.ReadAsStreamAsync();
|
|
|
|
|
var token = JsonSerializer.Deserialize<JwtToken>(data)!;
|
|
|
|
|
return token.AccessToken;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return String.Empty;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|