From a17e384665c0773345933b471773571215e91511 Mon Sep 17 00:00:00 2001 From: Yuriy Durov Date: Thu, 17 Oct 2024 18:59:29 +0400 Subject: [PATCH] Rework sample app --- .editorconfig | 4 ++ BitzArt.Blazor.Auth.sln | 5 ++ .../Components/Redirect.razor | 11 +++ .../Components/RedirectToHome.razor | 8 --- .../Components/RedirectToSignIn.razor | 8 --- .../Components/RouteAuthorize.razor | 7 -- .../Components/Routes.razor | 2 +- .../Models/SignInPayload.cs | 1 + .../Models/SignUpPayload.cs | 1 + .../Pages/AuthPage.razor | 9 +-- .../Pages/AuthRequiredPage.razor | 12 +--- .../Pages/Base/PageBase.cs | 18 ----- .../Pages/HomePage.razor | 3 +- .../Pages/SignOutPage.razor | 15 +--- .../SampleBlazorApp.Client/Program.cs | 12 +++- .../SampleBlazorApp/Services/JwtService.cs | 71 ++----------------- 16 files changed, 46 insertions(+), 141 deletions(-) create mode 100644 .editorconfig create mode 100644 sample/SampleBlazorApp/SampleBlazorApp.Client/Components/Redirect.razor delete mode 100644 sample/SampleBlazorApp/SampleBlazorApp.Client/Components/RedirectToHome.razor delete mode 100644 sample/SampleBlazorApp/SampleBlazorApp.Client/Components/RedirectToSignIn.razor delete mode 100644 sample/SampleBlazorApp/SampleBlazorApp.Client/Components/RouteAuthorize.razor delete mode 100644 sample/SampleBlazorApp/SampleBlazorApp.Client/Pages/Base/PageBase.cs diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..2176504 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,4 @@ +[*.{cs,vb}] + +# IDE0130: Namespace does not match folder structure +dotnet_diagnostic.IDE0130.severity = none diff --git a/BitzArt.Blazor.Auth.sln b/BitzArt.Blazor.Auth.sln index 1b398ed..71c6096 100644 --- a/BitzArt.Blazor.Auth.sln +++ b/BitzArt.Blazor.Auth.sln @@ -26,6 +26,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{ .github\workflows\Tests.yml = .github\workflows\Tests.yml EndProjectSection EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sln", "sln", "{5A14673C-C4B6-4561-8F93-9577E4A046C8}" + ProjectSection(SolutionItems) = preProject + .editorconfig = .editorconfig + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU diff --git a/sample/SampleBlazorApp/SampleBlazorApp.Client/Components/Redirect.razor b/sample/SampleBlazorApp/SampleBlazorApp.Client/Components/Redirect.razor new file mode 100644 index 0000000..5e554dc --- /dev/null +++ b/sample/SampleBlazorApp/SampleBlazorApp.Client/Components/Redirect.razor @@ -0,0 +1,11 @@ +@inject NavigationManager NavigationManager + +@code { + + [Parameter, EditorRequired] public required string To { get; set; } + + protected override void OnInitialized() + { + NavigationManager.NavigateTo(To); + } +} diff --git a/sample/SampleBlazorApp/SampleBlazorApp.Client/Components/RedirectToHome.razor b/sample/SampleBlazorApp/SampleBlazorApp.Client/Components/RedirectToHome.razor deleted file mode 100644 index 95d0281..0000000 --- a/sample/SampleBlazorApp/SampleBlazorApp.Client/Components/RedirectToHome.razor +++ /dev/null @@ -1,8 +0,0 @@ -@inject NavigationManager NavigationManager - -@code { - protected override void OnInitialized() - { - NavigationManager.NavigateTo("/"); - } -} diff --git a/sample/SampleBlazorApp/SampleBlazorApp.Client/Components/RedirectToSignIn.razor b/sample/SampleBlazorApp/SampleBlazorApp.Client/Components/RedirectToSignIn.razor deleted file mode 100644 index fc8b6bd..0000000 --- a/sample/SampleBlazorApp/SampleBlazorApp.Client/Components/RedirectToSignIn.razor +++ /dev/null @@ -1,8 +0,0 @@ -@inject NavigationManager NavigationManager - -@code { - protected override void OnInitialized() - { - NavigationManager.NavigateTo("/sign-in", true); - } -} diff --git a/sample/SampleBlazorApp/SampleBlazorApp.Client/Components/RouteAuthorize.razor b/sample/SampleBlazorApp/SampleBlazorApp.Client/Components/RouteAuthorize.razor deleted file mode 100644 index a626b12..0000000 --- a/sample/SampleBlazorApp/SampleBlazorApp.Client/Components/RouteAuthorize.razor +++ /dev/null @@ -1,7 +0,0 @@ -@inject NavigationManager NavigationManager - - - - - - diff --git a/sample/SampleBlazorApp/SampleBlazorApp.Client/Components/Routes.razor b/sample/SampleBlazorApp/SampleBlazorApp.Client/Components/Routes.razor index 69a5d16..268daab 100644 --- a/sample/SampleBlazorApp/SampleBlazorApp.Client/Components/Routes.razor +++ b/sample/SampleBlazorApp/SampleBlazorApp.Client/Components/Routes.razor @@ -2,7 +2,7 @@ - + diff --git a/sample/SampleBlazorApp/SampleBlazorApp.Client/Models/SignInPayload.cs b/sample/SampleBlazorApp/SampleBlazorApp.Client/Models/SignInPayload.cs index c23fe6d..e97ad1c 100644 --- a/sample/SampleBlazorApp/SampleBlazorApp.Client/Models/SignInPayload.cs +++ b/sample/SampleBlazorApp/SampleBlazorApp.Client/Models/SignInPayload.cs @@ -2,4 +2,5 @@ public class SignInPayload { + public string MyData { get; set; } = "Some data"; } diff --git a/sample/SampleBlazorApp/SampleBlazorApp.Client/Models/SignUpPayload.cs b/sample/SampleBlazorApp/SampleBlazorApp.Client/Models/SignUpPayload.cs index 478d6cb..18fbfcf 100644 --- a/sample/SampleBlazorApp/SampleBlazorApp.Client/Models/SignUpPayload.cs +++ b/sample/SampleBlazorApp/SampleBlazorApp.Client/Models/SignUpPayload.cs @@ -2,4 +2,5 @@ public class SignUpPayload { + public string MyData { get; set; } = "Some data"; } diff --git a/sample/SampleBlazorApp/SampleBlazorApp.Client/Pages/AuthPage.razor b/sample/SampleBlazorApp/SampleBlazorApp.Client/Pages/AuthPage.razor index 5b17982..f27853a 100644 --- a/sample/SampleBlazorApp/SampleBlazorApp.Client/Pages/AuthPage.razor +++ b/sample/SampleBlazorApp/SampleBlazorApp.Client/Pages/AuthPage.razor @@ -1,18 +1,15 @@ @page "/auth" -@inherits PageBase @inject NavigationManager NavigationManager @inject IUserService UserService -Sign In - -

Sign In

+Auth Page | Blazor.Auth
-
+
-
+
diff --git a/sample/SampleBlazorApp/SampleBlazorApp.Client/Pages/AuthRequiredPage.razor b/sample/SampleBlazorApp/SampleBlazorApp.Client/Pages/AuthRequiredPage.razor index b7129a5..cf08a93 100644 --- a/sample/SampleBlazorApp/SampleBlazorApp.Client/Pages/AuthRequiredPage.razor +++ b/sample/SampleBlazorApp/SampleBlazorApp.Client/Pages/AuthRequiredPage.razor @@ -1,18 +1,10 @@ @page "/auth-required" -@inherits PageBase @attribute [Authorize] @using System.Text.Json -Auth Required +Auth Required Page | Blazor.Auth

Auth Required

- -
-        @JsonSerializer.Serialize(@context
-            .User.Claims.Select(x => new
-                { x.Type, x.Value, x.Issuer, x.Subject?.Name, x.ValueType }
-            ), new JsonSerializerOptions { WriteIndented = true })
-    
-
+You are authorized! diff --git a/sample/SampleBlazorApp/SampleBlazorApp.Client/Pages/Base/PageBase.cs b/sample/SampleBlazorApp/SampleBlazorApp.Client/Pages/Base/PageBase.cs deleted file mode 100644 index 6c95a57..0000000 --- a/sample/SampleBlazorApp/SampleBlazorApp.Client/Pages/Base/PageBase.cs +++ /dev/null @@ -1,18 +0,0 @@ -using Microsoft.AspNetCore.Components; - -namespace SampleBlazorApp.Client; - -public class PageBase : ComponentBase -{ - [Inject] - public required ILoggerFactory loggerFactory { get; set; } - - private ILogger logger => loggerFactory.CreateLogger("Page"); - - protected override void OnInitialized() - { - base.OnInitialized(); - - logger.LogInformation("Page Initialized"); - } -} diff --git a/sample/SampleBlazorApp/SampleBlazorApp.Client/Pages/HomePage.razor b/sample/SampleBlazorApp/SampleBlazorApp.Client/Pages/HomePage.razor index e0d1284..6af7d32 100644 --- a/sample/SampleBlazorApp/SampleBlazorApp.Client/Pages/HomePage.razor +++ b/sample/SampleBlazorApp/SampleBlazorApp.Client/Pages/HomePage.razor @@ -1,7 +1,6 @@ @page "/" -@inherits PageBase -Home +Home Page | Blazor.Auth

Count: @count

diff --git a/sample/SampleBlazorApp/SampleBlazorApp.Client/Pages/SignOutPage.razor b/sample/SampleBlazorApp/SampleBlazorApp.Client/Pages/SignOutPage.razor index 0f2ffcc..a5b4af4 100644 --- a/sample/SampleBlazorApp/SampleBlazorApp.Client/Pages/SignOutPage.razor +++ b/sample/SampleBlazorApp/SampleBlazorApp.Client/Pages/SignOutPage.razor @@ -1,25 +1,12 @@ @page "/sign-out" -@inherits PageBase @inject NavigationManager NavigationManager @inject IUserService UserService -Sign Out - -

Sign Out

- -
-
- -
-
- - @code { - private async Task SignOutAsync() + protected override async Task OnInitializedAsync() { await UserService.SignOutAsync(); - NavigationManager.NavigateTo("/", true); } } diff --git a/sample/SampleBlazorApp/SampleBlazorApp.Client/Program.cs b/sample/SampleBlazorApp/SampleBlazorApp.Client/Program.cs index ba4cb6e..0dfbaa6 100644 --- a/sample/SampleBlazorApp/SampleBlazorApp.Client/Program.cs +++ b/sample/SampleBlazorApp/SampleBlazorApp.Client/Program.cs @@ -1,8 +1,14 @@ using BitzArt.Blazor.Auth; using Microsoft.AspNetCore.Components.WebAssembly.Hosting; -var builder = WebAssemblyHostBuilder.CreateDefault(args); +internal class Program +{ + private static async Task Main(string[] args) + { + var builder = WebAssemblyHostBuilder.CreateDefault(args); -builder.AddBlazorAuth(); + builder.AddBlazorAuth(); -await builder.Build().RunAsync(); + await builder.Build().RunAsync(); + } +} \ No newline at end of file diff --git a/sample/SampleBlazorApp/SampleBlazorApp/Services/JwtService.cs b/sample/SampleBlazorApp/SampleBlazorApp/Services/JwtService.cs index 58d5f82..bdc2ee3 100644 --- a/sample/SampleBlazorApp/SampleBlazorApp/Services/JwtService.cs +++ b/sample/SampleBlazorApp/SampleBlazorApp/Services/JwtService.cs @@ -1,68 +1,21 @@ using BitzArt.Blazor.Auth; -using Microsoft.IdentityModel.Tokens; -using System.IdentityModel.Tokens.Jwt; -using System.Security.Claims; -using System.Security.Cryptography; namespace SampleBlazorApp.Services; public class JwtService { - private readonly JwtSecurityTokenHandler _tokenHandler; - private readonly SigningCredentials _signingCredentials; - private readonly TimeSpan _accessTokenDuration; - private readonly TimeSpan _refreshTokenDuration; - - public JwtService() - { - var options = new JwtOptions - { - PublicKey = "MIIBCgKCAQEA12zIJKpaIuNNk2yAdQ4e/EsT7al1hozyi/qFeTduf7BJFS4niFK7k9OL4VJFoUbpDt18y7Yqlz0nsEyinu/7wZJjf646yYymA8jBib/4kxQw6zH7C3qaam283k72pxb+aZOeJ6iU9KNkwTbfMHxKuTHoxySS6VH0vt3Sn0FYWryp8BVdPFlbuJp6K5otksTbdFOPgzgvwNreoI3TgA0e2clRKaEv+FGwhmY6WqR/hp/ebo0mflL2hPwJI1PLzjXdlx1sPHmYYfDTA02eJWkGYVti4oUZ9UTI5pZeRMNItSu1IyjHi45iLDQ+kRaPsx2bL/YZ7NXJu/g+dk7Lb4KdfQIDAQAB", - PrivateKey = "MIIEogIBAAKCAQEA12zIJKpaIuNNk2yAdQ4e/EsT7al1hozyi/qFeTduf7BJFS4niFK7k9OL4VJFoUbpDt18y7Yqlz0nsEyinu/7wZJjf646yYymA8jBib/4kxQw6zH7C3qaam283k72pxb+aZOeJ6iU9KNkwTbfMHxKuTHoxySS6VH0vt3Sn0FYWryp8BVdPFlbuJp6K5otksTbdFOPgzgvwNreoI3TgA0e2clRKaEv+FGwhmY6WqR/hp/ebo0mflL2hPwJI1PLzjXdlx1sPHmYYfDTA02eJWkGYVti4oUZ9UTI5pZeRMNItSu1IyjHi45iLDQ+kRaPsx2bL/YZ7NXJu/g+dk7Lb4KdfQIDAQABAoIBAE6nMQvyBqbmRtSksOIMHdQPtV74mChgHc5t0X3Id1e3jXdmOpjTXBlFC7VgzHtt4HnE9GOMR1Cgy3TbBiTxigHK6PkdK+maqKKJEeCxbpiErrewr/Ao+2gQWPzx56xqAMmbVAs2yevoHElPN34EY2PqjQrol5sIiUuGwffTa+b0f5fuhPuqsvjBRcHhZX8aCCpqrvoblwVVjWKZKsFv7oWH3+KMdm9iwmuuDdOqq51TNgqaJTSpdTl+luHq8bv7hjkx3omF8R8/JHIc2UTP5a6FA0wO77448WriyIjsUpAN4fROBVOS4a4tuHy8lEQr3udxQ1KWFj3sLBy7ls3+FiECgYEA4ZY5sJ2KmZ6xG/gwI96Skd6sc7jA+XZyo3gGeVHyh7yhooZ4JndyJZtjZ4kdiuRyuxrHOG7ZQo07DFJDDbjgRPqIcyiwbLTVkvV+FE7hPwhhN2OR2TUd4D7w9oY8eZ+C0ABXJwT2breS5XCcehAGgK+A31V3rLY4BdhIoOdNOOMCgYEA9HfVt0uz+6qZW22f4XihehELEBlwGJS8bDMoSjuDRHYDwxFErBVb+KlF0w6MLy1bv6Netdcr4aQtVzHDPpDI3MbG3/aNOq8+TXppIvDhMZTycoYzk3YGC8qbhppFH7/O4CX8Z5dd80Xni9sCVU7ePSFdk5BVi6XHTamq46WDfh8CgYAtWZz5Y4J0hZGHVOqgm2MNzh0PGoo43FYJhNyQUSgXn5VC7hODcCnTY5ylOMxmmqxx7t0z/BzTIz9Gp9bxEESNuWvq8rgc8nGpHI8fGAhyOoYIs4yjhOkfpqecd7n6nVWX6SmcH4RHF8KBO5VJeKVGA4I945mub+dtTWC0cCt3DwKBgAXplAif0xWGFblpWFGKqlUabmsQQm7FwhzXy+SntdAFDqg8Fa4XwiasaVzmYCuP7EUhPVwmfRAy+Um/kVpFBCaaxBqMivPdYyNaj4phywB4+rgcWMj7NMA6QTKrLnrLF8TCBm228nW8vhHa1R6dDrDpyqqT9g2vj7doIBLrYNe/AoGAObf5KfkjrpBBO5n//H1thZzR05g5U/hKDbwhCIzhVqP2RgJxtYkadsA3XA29RXOCD58MRsYH6nYpEw52JyP3tTp6jGSCxOM9ltYji2MEpd82yT3lOsVwJXwDL8uFFQNW2VV2xfFV41mk+5B2VqPjPZa7ewFUKluSkXy1oN8Wg1w=", - AccessTokenDuration = new TimeSpan(0, 1, 0), - RefreshTokenDuration = new TimeSpan(0, 2, 0) - }; - - _tokenHandler = new JwtSecurityTokenHandler(); - - var privateRsa = RSA.Create(); - var privateKey = Convert.FromBase64String(options.PrivateKey!); - privateRsa.ImportRSAPrivateKey(privateKey, out _); - - var privateSecurityKey = new RsaSecurityKey(privateRsa); - - _signingCredentials = new SigningCredentials(privateSecurityKey, SecurityAlgorithms.RsaSha256); - - _accessTokenDuration = options.AccessTokenDuration; - _refreshTokenDuration = options.RefreshTokenDuration; - } + private static readonly TimeSpan _accessTokenDuration = new(0, 1, 0); + private static readonly TimeSpan _refreshTokenDuration = new(1, 0, 0); public JwtPair BuildJwtPair() { - var issuedAt = DateTime.UtcNow; - var accessTokenExpiresAt = issuedAt + _accessTokenDuration; - - var accessToken = _tokenHandler.WriteToken(new JwtSecurityToken( - claims: new[] - { - new Claim("tt", "a") - }, - notBefore: issuedAt, - expires: accessTokenExpiresAt, - signingCredentials: _signingCredentials - )); + var now = DateTime.UtcNow; - var refreshTokenExpiresAt = issuedAt + _refreshTokenDuration; + var accessToken = "AccessToken"; + var accessTokenExpiresAt = now + _accessTokenDuration; - var refreshToken = _tokenHandler.WriteToken(new JwtSecurityToken( - claims: new[] - { - new Claim("tt", "r") - }, - notBefore: issuedAt, - expires: refreshTokenExpiresAt, - signingCredentials: _signingCredentials - )); + var refreshToken = "RefreshToken"; + var refreshTokenExpiresAt = now + _refreshTokenDuration; return new JwtPair { @@ -72,14 +25,4 @@ public JwtPair BuildJwtPair() RefreshTokenExpiresAt = refreshTokenExpiresAt }; } -} - -internal class JwtException(string errorMessage, Exception? innerException = null) : Exception(errorMessage, innerException); - -internal class JwtOptions -{ - public required string PublicKey { get; set; } - public required string PrivateKey { get; set; } - public TimeSpan AccessTokenDuration { get; set; } - public TimeSpan RefreshTokenDuration { get; set; } } \ No newline at end of file