From 2e58a0b3648163ec23e96ee9cf242d9509f6ec55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B0=D0=B2=D0=B5=D0=BB=D0=B8=D0=B9=20=D0=92=D0=BE?= =?UTF-8?q?=D1=82=D0=B8=D0=BD=D1=86=D0=B5=D0=B2?= Date: Mon, 25 Aug 2025 17:55:28 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D0=BD=D1=82=D0=B5=D0=B3=D1=80=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F=20Auth=20=D0=B8=20=D0=BA=D0=BE=D1=80=D1=80?= =?UTF-8?q?=D0=B5=D0=BA=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=BA=D0=B0=20GitHub?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Добавлена поддержка внешней аутентификации через Auth - Обновлены настройки и API для GitHub - Обновлена история изменений и интерфейс страницы входа --- .../Controllers/ExternalAuthController.cs | 24 +++++++++++- .../Definitions/OpenIddictDefinition.cs | 39 +++++++++++++++---- backend/Money.Api/appsettings.json | 7 +++- frontend/Money.Web/Pages/Account/Login.razor | 7 ++++ .../Money.Web/Pages/Account/Login.razor.cs | 6 +++ frontend/Money.Web/Pages/Home.razor.cs | 7 +++- 6 files changed, 80 insertions(+), 10 deletions(-) diff --git a/backend/Money.Api/Controllers/ExternalAuthController.cs b/backend/Money.Api/Controllers/ExternalAuthController.cs index 8658e3a..d909ffa 100644 --- a/backend/Money.Api/Controllers/ExternalAuthController.cs +++ b/backend/Money.Api/Controllers/ExternalAuthController.cs @@ -16,8 +16,30 @@ namespace Money.Api.Controllers; public class ExternalAuthController( SignInManager signInManager, UserManager userManager, - AccountsService accountsService) : ControllerBase + AccountsService accountsService, + IConfiguration configuration) : ControllerBase { + [HttpGet("login/auth")] + public IActionResult LoginWithAuth([FromQuery] string? returnUrl = null) + { + var properties = new AuthenticationProperties { RedirectUri = Url.Content("~/connect/callback") }; + properties.SetString(OpenIddictClientAspNetCoreConstants.Properties.ProviderName, "Auth"); + + var issuer = configuration["AUTH_AUTHORITY"]; + + if (string.IsNullOrWhiteSpace(issuer) == false) + { + properties.SetString(OpenIddictClientAspNetCoreConstants.Properties.Issuer, issuer); + } + + if (string.IsNullOrWhiteSpace(returnUrl) == false) + { + properties.Items["returnUrl"] = returnUrl; + } + + return Challenge(properties, OpenIddictClientAspNetCoreDefaults.AuthenticationScheme); + } + [HttpGet("login/github")] public IActionResult LoginWithGitHub([FromQuery] string? returnUrl = null) { diff --git a/backend/Money.Api/Definitions/OpenIddictDefinition.cs b/backend/Money.Api/Definitions/OpenIddictDefinition.cs index 73a948c..fd24c29 100644 --- a/backend/Money.Api/Definitions/OpenIddictDefinition.cs +++ b/backend/Money.Api/Definitions/OpenIddictDefinition.cs @@ -51,7 +51,14 @@ public override void ConfigureServices(WebApplicationBuilder builder) .DisableTransportSecurityRequirement(); }); - if (builder.Configuration["GITHUB_CLIENT_ID"] is not null && builder.Configuration["GITHUB_CLIENT_SECRET"] is not null) + var authAuthority = builder.Configuration["AUTH_AUTHORITY"]; + var authClientId = builder.Configuration["AUTH_CLIENT_ID"]; + + var githubClientId = builder.Configuration["GITHUB_CLIENT_ID"]; + var githubClientSecret = builder.Configuration["GITHUB_CLIENT_SECRET"]; + + if (authAuthority is not null && authClientId is not null + || githubClientId is not null && githubClientSecret is not null) { openIddictBuilder .AddClient(options => @@ -64,14 +71,32 @@ public override void ConfigureServices(WebApplicationBuilder builder) options.AddDevelopmentEncryptionCertificate() .AddDevelopmentSigningCertificate(); - options.UseWebProviders() - .AddGitHub(github => + if (authAuthority is not null && authClientId is not null) + { + options.AddRegistration(new() { - github.SetClientId(builder.Configuration["GITHUB_CLIENT_ID"] ?? string.Empty); - github.SetClientSecret(builder.Configuration["GITHUB_CLIENT_SECRET"] ?? string.Empty); - github.SetRedirectUri(new Uri("/connect/callback", UriKind.Relative)); - github.AddScopes("read:user", "user:email"); + Issuer = new(authAuthority, UriKind.Absolute), + ProviderName = "Auth", + ProviderDisplayName = "Auth", + + ClientId = authClientId, + Scopes = { "email", "profile", "roles" }, + + RedirectUri = new("/connect/callback", UriKind.Relative), }); + } + + if (githubClientId is not null && githubClientSecret is not null) + { + options.UseWebProviders() + .AddGitHub(github => + { + github.SetClientId(githubClientId); + github.SetClientSecret(githubClientSecret); + github.SetRedirectUri(new Uri("/connect/callback", UriKind.Relative)); + github.AddScopes("read:user", "user:email"); + }); + } }); } diff --git a/backend/Money.Api/appsettings.json b/backend/Money.Api/appsettings.json index cd916fd..30ddaa9 100644 --- a/backend/Money.Api/appsettings.json +++ b/backend/Money.Api/appsettings.json @@ -26,5 +26,10 @@ "Password": "***", "EnableSSL": "true", "SenderEmail": "bobgroup.money@mail.ru" - } + }, + "AUTH_AUTHORITY": "https://localhost:7166/", + "AUTH_CLIENT_ID": "money-api", + "AUTH_CLIENT_SECRET": "Не нужен, потому что используется PKCE", + "GITHUB_CLIENT_ID": "REPLACE_ME", + "GITHUB_CLIENT_SECRET": "REPLACE_ME" } diff --git a/frontend/Money.Web/Pages/Account/Login.razor b/frontend/Money.Web/Pages/Account/Login.razor index 29bdc40..6013e29 100644 --- a/frontend/Money.Web/Pages/Account/Login.razor +++ b/frontend/Money.Web/Pages/Account/Login.razor @@ -60,6 +60,13 @@ + Войти через Auth + + Войти через GitHub diff --git a/frontend/Money.Web/Pages/Account/Login.razor.cs b/frontend/Money.Web/Pages/Account/Login.razor.cs index f1fdb5c..80c207d 100644 --- a/frontend/Money.Web/Pages/Account/Login.razor.cs +++ b/frontend/Money.Web/Pages/Account/Login.razor.cs @@ -38,6 +38,12 @@ protected override void OnParametersSet() Input = new(); } + private void OnAuthLogin() + { + var url = AuthenticationService.GetExternalAuthUrl("auth", NavigationManager.BaseUri + "Account/Callback"); + NavigationManager.NavigateTo(url, true); + } + private void OnGitHubLogin() { var url = AuthenticationService.GetExternalAuthUrl("github", NavigationManager.BaseUri + "Account/Callback"); diff --git a/frontend/Money.Web/Pages/Home.razor.cs b/frontend/Money.Web/Pages/Home.razor.cs index ed9665e..65c9320 100644 --- a/frontend/Money.Web/Pages/Home.razor.cs +++ b/frontend/Money.Web/Pages/Home.razor.cs @@ -4,6 +4,11 @@ public partial class Home { private readonly List _versionHistory = [ + new("1.2.8", new(2025, 8, 20), [ + new("Интегрированы внешние провайдеры аутентификации: Auth и GitHub.", ChangeType.Feature), + new("Добавлены кнопки входа для Auth и GitHub на странице входа.", ChangeType.UiUx), + ]), + new("1.2.7", new(2025, 7, 21), [ new("Добавлена возможность выбора долгов для прощения.", ChangeType.Feature), new("Реализованы действия \"выбрать все\" и \"очистить\" для долгов.", ChangeType.Feature), @@ -206,7 +211,7 @@ private void ChangeHistoryVisible() private string GetCurrentVersion() { - return _versionHistory.FirstOrDefault()?.Version ?? "1.2.7"; + return _versionHistory.FirstOrDefault()?.Version ?? "1.2.8"; } private Color GetVersionColor(VersionHistoryEntry entry)