Убран ApiTokenHelper из Clients и добавлен в IntegrationTests
This commit is contained in:
parent
9659b7bc8c
commit
e892adbd96
@ -6,7 +6,7 @@ using System.Net.Http.Headers;
|
|||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
|
||||||
namespace DD.Persistence.Client.Helpers;
|
namespace DD.Persistence.IntegrationTests;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Класс, позволяющий генерировать api-token
|
/// Класс, позволяющий генерировать api-token
|
||||||
@ -24,14 +24,8 @@ public static class ApiTokenHelper
|
|||||||
var authUser = configuration
|
var authUser = configuration
|
||||||
.GetSection(nameof(AuthUser))
|
.GetSection(nameof(AuthUser))
|
||||||
.Get<AuthUser>()!;
|
.Get<AuthUser>()!;
|
||||||
var needUseKeyCloak = configuration
|
|
||||||
.GetSection("NeedUseKeyCloak")
|
|
||||||
.Get<bool>()!;
|
|
||||||
var keycloakGetTokenUrl = configuration.GetSection("KeycloakGetTokenUrl").Get<string>() ?? string.Empty;
|
|
||||||
|
|
||||||
var jwtToken = needUseKeyCloak
|
var jwtToken = authUser.CreateDefaultJwtToken();
|
||||||
? authUser.CreateKeyCloakJwtToken(keycloakGetTokenUrl, httpClient)
|
|
||||||
: authUser.CreateDefaultJwtToken();
|
|
||||||
|
|
||||||
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwtToken);
|
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwtToken);
|
||||||
}
|
}
|
||||||
@ -66,31 +60,4 @@ public static class ApiTokenHelper
|
|||||||
var token = tokenHandler.CreateToken(tokenDescriptor);
|
var token = tokenHandler.CreateToken(tokenDescriptor);
|
||||||
return tokenHandler.WriteToken(token);
|
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