Удаление restsharp и использование обычного httpClient #22
@ -6,7 +6,7 @@ using System.Net.Http.Headers;
|
||||
using System.Security.Claims;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace DD.Persistence.Client.Helpers;
|
||||
namespace DD.Persistence.IntegrationTests;
|
||||
|
||||
/// <summary>
|
||||
/// Класс, позволяющий генерировать api-token
|
||||
@ -24,14 +24,8 @@ public static class ApiTokenHelper
|
||||
var authUser = configuration
|
||||
.GetSection(nameof(AuthUser))
|
||||
.Get<AuthUser>()!;
|
||||
var needUseKeyCloak = configuration
|
||||
.GetSection("NeedUseKeyCloak")
|
||||
.Get<bool>()!;
|
||||
var keycloakGetTokenUrl = configuration.GetSection("KeycloakGetTokenUrl").Get<string>() ?? string.Empty;
|
||||
|
||||
var jwtToken = needUseKeyCloak
|
||||
? authUser.CreateKeyCloakJwtToken(keycloakGetTokenUrl, httpClient)
|
||||
: authUser.CreateDefaultJwtToken();
|
||||
var jwtToken = authUser.CreateDefaultJwtToken();
|
||||
|
||||
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwtToken);
|
||||
}
|
||||
@ -66,31 +60,4 @@ public static class ApiTokenHelper
|
||||
var token = tokenHandler.CreateToken(tokenDescriptor);
|
||||
return tokenHandler.WriteToken(token);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Авторизация через jwt-токен keycloak
|
||||
/// </summary>
|
||||
/// <param name="authUser"></param>
|
||||
/// <param name="keycloakGetTokenUrl"></param>
|
||||
/// <returns></returns>
|
||||
private static string CreateKeyCloakJwtToken(this AuthUser authUser, string keycloakGetTokenUrl, HttpClient 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);
|
||||
|
||||
using HttpResponseMessage response = httpClient.PostAsync(keycloakGetTokenUrl, encodedContent).GetAwaiter().GetResult();
|
||||
|
||||
if (response.IsSuccessStatusCode == true)
|
||||
{
|
||||
var data = response.Content.ReadAsStreamAsync().GetAwaiter().GetResult();
|
||||
var token = JsonSerializer.Deserialize<JwtToken>(data)!;
|
||||
return token.AccessToken;
|
||||
}
|
||||
return String.Empty;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user