99d25adbb1
Backend CI / build-and-test (push) Failing after 13m11s
🚀 Create and publish a Docker image / Detect changes in backend and frontend (push) Failing after 10m12s
Frontend CI / build-and-check (push) Failing after 16m9s
🚀 Create and publish a Docker image / Build & publish frontend image (push) Failing after 14m6s
🚀 Create and publish a Docker image / Build & publish backend image (push) Failing after 14m58s
🚀 Create and publish a Docker image / Update stack on Portainer (push) Failing after 14m58s
53 lines
1.8 KiB
C#
53 lines
1.8 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Identity.Client;
|
|
using UniVerse.Application.Interfaces;
|
|
using UniVerse.Domain.Exceptions;
|
|
|
|
namespace UniVerse.Infrastructure.ExternalServices;
|
|
|
|
public class MicrosoftAuthClient : IMicrosoftAuthClient
|
|
{
|
|
private readonly IConfiguration _config;
|
|
|
|
public MicrosoftAuthClient(IConfiguration config)
|
|
{
|
|
_config = config;
|
|
}
|
|
|
|
public async Task<MicrosoftTokenResult> ExchangeAuthorizationCodeAsync(
|
|
string authorizationCode,
|
|
string redirectUri,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
var tenantId = _config["AzureAd:TenantId"];
|
|
var clientId = _config["AzureAd:ClientId"];
|
|
var clientSecret = _config["AzureAd:ClientSecret"];
|
|
var instance = _config["AzureAd:Instance"] ?? "https://login.microsoftonline.com/";
|
|
|
|
if (string.IsNullOrWhiteSpace(tenantId)
|
|
|| string.IsNullOrWhiteSpace(clientId)
|
|
|| string.IsNullOrWhiteSpace(clientSecret))
|
|
throw new UnauthorizedException("Аутентификация Microsoft не настроена (AzureAd:TenantId/ClientId/ClientSecret).");
|
|
|
|
var authority = $"{instance.TrimEnd('/')}/{tenantId}";
|
|
|
|
var app = ConfidentialClientApplicationBuilder.Create(clientId)
|
|
.WithClientSecret(clientSecret)
|
|
.WithAuthority(new Uri(authority))
|
|
.WithRedirectUri(redirectUri)
|
|
.Build();
|
|
|
|
try
|
|
{
|
|
var result = await app.AcquireTokenByAuthorizationCode(["User.Read"], authorizationCode)
|
|
.ExecuteAsync(cancellationToken);
|
|
|
|
return new MicrosoftTokenResult(result.IdToken);
|
|
}
|
|
catch (MsalException ex)
|
|
{
|
|
throw new UnauthorizedException($"Ошибка аутентификации Microsoft: {ex.Message}");
|
|
}
|
|
}
|
|
}
|