From 2361e12847bf682ada1fc42c5e384527d4ced0fb Mon Sep 17 00:00:00 2001 From: max Date: Sat, 21 Dec 2024 15:30:17 +0100 Subject: [PATCH] [ADD] Base Authority initial commit --- .../DotBasedAuthDependencyInjection.cs | 2 +- DotBased.AspNet.Auth/BasedAuthExtensions.cs | 11 ------- .../Attributes/ProtectAttribute.cs | 10 ++++++ DotBased.AspNet.Authority/AuthorityBuilder.cs | 13 ++++++++ .../AuthorityDefaults.cs | 11 +++++++ .../AuthorityProviderExtensions.cs | 17 ++++++++++ .../DotBased.AspNet.Authority.csproj | 18 ++++------- .../Interfaces/IAttributeRepository.cs | 6 ++++ .../Interfaces/IAuthorityRepository.cs | 6 ++++ .../Interfaces/IRoleRepository.cs | 6 ++++ .../Interfaces/IUserRepository.cs | 6 ++++ .../Models/Authority/AuthorityUser.cs | 10 ++++++ .../Models/Authority/AuthorityUserBase.cs | 32 +++++++++++++++++++ .../Services/AuthorityService.cs | 6 ++++ DotBased.sln | 12 +++---- TestWebApi/Program.cs | 9 ++++++ TestWebApi/SeedAuthorityData.cs | 9 ++++++ TestWebApi/TestWebApi.csproj | 1 + 18 files changed, 155 insertions(+), 30 deletions(-) delete mode 100644 DotBased.AspNet.Auth/BasedAuthExtensions.cs create mode 100644 DotBased.AspNet.Authority/Attributes/ProtectAttribute.cs create mode 100644 DotBased.AspNet.Authority/AuthorityBuilder.cs create mode 100644 DotBased.AspNet.Authority/AuthorityDefaults.cs create mode 100644 DotBased.AspNet.Authority/AuthorityProviderExtensions.cs rename DotBased.AspNet.Auth/DotBased.AspNet.Auth.csproj => DotBased.AspNet.Authority/DotBased.AspNet.Authority.csproj (64%) create mode 100644 DotBased.AspNet.Authority/Interfaces/IAttributeRepository.cs create mode 100644 DotBased.AspNet.Authority/Interfaces/IAuthorityRepository.cs create mode 100644 DotBased.AspNet.Authority/Interfaces/IRoleRepository.cs create mode 100644 DotBased.AspNet.Authority/Interfaces/IUserRepository.cs create mode 100644 DotBased.AspNet.Authority/Models/Authority/AuthorityUser.cs create mode 100644 DotBased.AspNet.Authority/Models/Authority/AuthorityUserBase.cs create mode 100644 DotBased.AspNet.Authority/Services/AuthorityService.cs create mode 100644 TestWebApi/SeedAuthorityData.cs diff --git a/DotBased.ASP.Auth/DotBasedAuthDependencyInjection.cs b/DotBased.ASP.Auth/DotBasedAuthDependencyInjection.cs index 59fa9e0..b060926 100644 --- a/DotBased.ASP.Auth/DotBasedAuthDependencyInjection.cs +++ b/DotBased.ASP.Auth/DotBasedAuthDependencyInjection.cs @@ -30,7 +30,7 @@ public static class DotBasedAuthDependencyInjection services.AddAuthentication(options => { options.DefaultScheme = BasedAuthDefaults.AuthenticationScheme; - });/*.AddScheme(BasedAuthDefaults.AuthenticationScheme, null);*/ + }); services.AddAuthorization(); services.AddCascadingAuthenticationState(); return services; diff --git a/DotBased.AspNet.Auth/BasedAuthExtensions.cs b/DotBased.AspNet.Auth/BasedAuthExtensions.cs deleted file mode 100644 index 023a35f..0000000 --- a/DotBased.AspNet.Auth/BasedAuthExtensions.cs +++ /dev/null @@ -1,11 +0,0 @@ -using Microsoft.Extensions.DependencyInjection; - -namespace DotBased.AspNet.Auth; - -public static class BasedAuthExtensions -{ - public static IServiceCollection AddBasedAuthentication(this IServiceCollection services) - { - return services; - } -} \ No newline at end of file diff --git a/DotBased.AspNet.Authority/Attributes/ProtectAttribute.cs b/DotBased.AspNet.Authority/Attributes/ProtectAttribute.cs new file mode 100644 index 0000000..13394b8 --- /dev/null +++ b/DotBased.AspNet.Authority/Attributes/ProtectAttribute.cs @@ -0,0 +1,10 @@ +namespace DotBased.AspNet.Authority.Attributes; + +/// +/// Indicates that the property should be protected. +/// +[AttributeUsage(AttributeTargets.Property)] +public class ProtectAttribute : Attribute +{ + +} \ No newline at end of file diff --git a/DotBased.AspNet.Authority/AuthorityBuilder.cs b/DotBased.AspNet.Authority/AuthorityBuilder.cs new file mode 100644 index 0000000..e778248 --- /dev/null +++ b/DotBased.AspNet.Authority/AuthorityBuilder.cs @@ -0,0 +1,13 @@ +using Microsoft.Extensions.DependencyInjection; + +namespace DotBased.AspNet.Authority; + +public class AuthorityBuilder +{ + public AuthorityBuilder(IServiceCollection services) + { + Services = services; + } + + public IServiceCollection Services { get; } +} \ No newline at end of file diff --git a/DotBased.AspNet.Authority/AuthorityDefaults.cs b/DotBased.AspNet.Authority/AuthorityDefaults.cs new file mode 100644 index 0000000..a0b4726 --- /dev/null +++ b/DotBased.AspNet.Authority/AuthorityDefaults.cs @@ -0,0 +1,11 @@ +namespace DotBased.AspNet.Authority; + +public static class AuthorityDefaults +{ + public static class Scheme + { + public const string AuthenticationScheme = "Authority.Scheme.Authentication"; + public const string ExternalScheme = "Authority.Scheme.External"; + } + +} \ No newline at end of file diff --git a/DotBased.AspNet.Authority/AuthorityProviderExtensions.cs b/DotBased.AspNet.Authority/AuthorityProviderExtensions.cs new file mode 100644 index 0000000..71f7730 --- /dev/null +++ b/DotBased.AspNet.Authority/AuthorityProviderExtensions.cs @@ -0,0 +1,17 @@ +using DotBased.AspNet.Authority.Interfaces; +using Microsoft.Extensions.DependencyInjection; + +namespace DotBased.AspNet.Authority; + +public static class AuthorityProviderExtensions +{ + public static AuthorityBuilder AddAuthorityProvider(this IServiceCollection services) where TModel : class + { + return new AuthorityBuilder(services); + } + + public static AuthorityBuilder AddAuthorityStore(this AuthorityBuilder authorityBuilder) where TStore : IAuthorityRepository + { + return authorityBuilder; + } +} \ No newline at end of file diff --git a/DotBased.AspNet.Auth/DotBased.AspNet.Auth.csproj b/DotBased.AspNet.Authority/DotBased.AspNet.Authority.csproj similarity index 64% rename from DotBased.AspNet.Auth/DotBased.AspNet.Auth.csproj rename to DotBased.AspNet.Authority/DotBased.AspNet.Authority.csproj index 3af943e..14ddfd0 100644 --- a/DotBased.AspNet.Auth/DotBased.AspNet.Auth.csproj +++ b/DotBased.AspNet.Authority/DotBased.AspNet.Authority.csproj @@ -7,8 +7,9 @@ - - + + ..\..\..\.nuget\packages\microsoft.extensions.dependencyinjection.abstractions\8.0.2\lib\net8.0\Microsoft.Extensions.DependencyInjection.Abstractions.dll + @@ -16,16 +17,9 @@ - - ..\..\..\..\..\usr\lib64\dotnet\shared\Microsoft.AspNetCore.App\8.0.11\Microsoft.AspNetCore.Authentication.dll - - - ..\..\..\.nuget\packages\microsoft.extensions.dependencyinjection.abstractions\8.0.2\lib\net8.0\Microsoft.Extensions.DependencyInjection.Abstractions.dll - - - - - + + + diff --git a/DotBased.AspNet.Authority/Interfaces/IAttributeRepository.cs b/DotBased.AspNet.Authority/Interfaces/IAttributeRepository.cs new file mode 100644 index 0000000..d0f90d1 --- /dev/null +++ b/DotBased.AspNet.Authority/Interfaces/IAttributeRepository.cs @@ -0,0 +1,6 @@ +namespace DotBased.AspNet.Authority.Interfaces; + +public interface IAttributeRepository +{ + +} \ No newline at end of file diff --git a/DotBased.AspNet.Authority/Interfaces/IAuthorityRepository.cs b/DotBased.AspNet.Authority/Interfaces/IAuthorityRepository.cs new file mode 100644 index 0000000..3f09635 --- /dev/null +++ b/DotBased.AspNet.Authority/Interfaces/IAuthorityRepository.cs @@ -0,0 +1,6 @@ +namespace DotBased.AspNet.Authority.Interfaces; + +public interface IAuthorityRepository +{ + +} \ No newline at end of file diff --git a/DotBased.AspNet.Authority/Interfaces/IRoleRepository.cs b/DotBased.AspNet.Authority/Interfaces/IRoleRepository.cs new file mode 100644 index 0000000..9ad9dc8 --- /dev/null +++ b/DotBased.AspNet.Authority/Interfaces/IRoleRepository.cs @@ -0,0 +1,6 @@ +namespace DotBased.AspNet.Authority.Interfaces; + +public interface IRoleRepository +{ + +} \ No newline at end of file diff --git a/DotBased.AspNet.Authority/Interfaces/IUserRepository.cs b/DotBased.AspNet.Authority/Interfaces/IUserRepository.cs new file mode 100644 index 0000000..0bc7ad3 --- /dev/null +++ b/DotBased.AspNet.Authority/Interfaces/IUserRepository.cs @@ -0,0 +1,6 @@ +namespace DotBased.AspNet.Authority.Interfaces; + +public interface IUserRepository +{ + +} \ No newline at end of file diff --git a/DotBased.AspNet.Authority/Models/Authority/AuthorityUser.cs b/DotBased.AspNet.Authority/Models/Authority/AuthorityUser.cs new file mode 100644 index 0000000..6c4f8a5 --- /dev/null +++ b/DotBased.AspNet.Authority/Models/Authority/AuthorityUser.cs @@ -0,0 +1,10 @@ +namespace DotBased.AspNet.Authority.Models.Authority; + +public class AuthorityUser : AuthorityUserBase +{ + public AuthorityUser() + { + Id = Guid.NewGuid(); + CreatedDate = DateTime.Now; + } +} \ No newline at end of file diff --git a/DotBased.AspNet.Authority/Models/Authority/AuthorityUserBase.cs b/DotBased.AspNet.Authority/Models/Authority/AuthorityUserBase.cs new file mode 100644 index 0000000..184bd7f --- /dev/null +++ b/DotBased.AspNet.Authority/Models/Authority/AuthorityUserBase.cs @@ -0,0 +1,32 @@ +using DotBased.AspNet.Authority.Attributes; + +namespace DotBased.AspNet.Authority.Models.Authority; + +public abstract class AuthorityUserBase where TKey : IEquatable +{ + public TKey Id { get; set; } + + public bool Enabled { get; set; } + + public bool Locked { get; set; } + + public string UserName { get; set; } + + public string PasswordHash { get; set; } + + public DateTime CreatedDate { get; set; } + + public bool TwoFactorEnabled { get; set; } + + + [Protect] + public string EmailAddress { get; set; } + + public bool EmailConfirmed { get; set; } + + [Protect] + public string PhoneNumber { get; set; } + + public bool PhoneNumberConfirmed { get; set; } + +} \ No newline at end of file diff --git a/DotBased.AspNet.Authority/Services/AuthorityService.cs b/DotBased.AspNet.Authority/Services/AuthorityService.cs new file mode 100644 index 0000000..9f90f61 --- /dev/null +++ b/DotBased.AspNet.Authority/Services/AuthorityService.cs @@ -0,0 +1,6 @@ +namespace DotBased.AspNet.Authority.Services; + +public class AuthorityService +{ + +} \ No newline at end of file diff --git a/DotBased.sln b/DotBased.sln index 4221777..0275d48 100755 --- a/DotBased.sln +++ b/DotBased.sln @@ -20,7 +20,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Blazor.Wasm", "Blazor.Wasm\ EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "AspNet", "AspNet", "{624E7B11-8A18-46E5-AB1F-6AF6097F9D4D}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotBased.AspNet.Auth", "DotBased.AspNet.Auth\DotBased.AspNet.Auth.csproj", "{6F407D81-DFAC-4936-ACDD-D75E9FDE2E7B}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotBased.AspNet.Authority", "DotBased.AspNet.Authority\DotBased.AspNet.Authority.csproj", "{A3ADC9AF-39B7-4EC4-8022-946118A8C322}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -56,10 +56,10 @@ Global {AC8343A5-7953-4E1D-A926-406BE4D7E819}.Debug|Any CPU.Build.0 = Debug|Any CPU {AC8343A5-7953-4E1D-A926-406BE4D7E819}.Release|Any CPU.ActiveCfg = Release|Any CPU {AC8343A5-7953-4E1D-A926-406BE4D7E819}.Release|Any CPU.Build.0 = Release|Any CPU - {6F407D81-DFAC-4936-ACDD-D75E9FDE2E7B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {6F407D81-DFAC-4936-ACDD-D75E9FDE2E7B}.Debug|Any CPU.Build.0 = Debug|Any CPU - {6F407D81-DFAC-4936-ACDD-D75E9FDE2E7B}.Release|Any CPU.ActiveCfg = Release|Any CPU - {6F407D81-DFAC-4936-ACDD-D75E9FDE2E7B}.Release|Any CPU.Build.0 = Release|Any CPU + {A3ADC9AF-39B7-4EC4-8022-946118A8C322}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A3ADC9AF-39B7-4EC4-8022-946118A8C322}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A3ADC9AF-39B7-4EC4-8022-946118A8C322}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A3ADC9AF-39B7-4EC4-8022-946118A8C322}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {EBBDAF9A-BFC7-4BDC-8C51-0501B59A1DDC} = {2156FB93-C252-4B33-8A0C-73C82FABB163} @@ -68,6 +68,6 @@ Global {BADA4BAF-142B-47A8-95FC-B25E1D3D0020} = {DBDB4538-85D4-45AC-9E0A-A684467AEABA} {AC8343A5-7953-4E1D-A926-406BE4D7E819} = {DBDB4538-85D4-45AC-9E0A-A684467AEABA} {624E7B11-8A18-46E5-AB1F-6AF6097F9D4D} = {2156FB93-C252-4B33-8A0C-73C82FABB163} - {6F407D81-DFAC-4936-ACDD-D75E9FDE2E7B} = {624E7B11-8A18-46E5-AB1F-6AF6097F9D4D} + {A3ADC9AF-39B7-4EC4-8022-946118A8C322} = {624E7B11-8A18-46E5-AB1F-6AF6097F9D4D} EndGlobalSection EndGlobal diff --git a/TestWebApi/Program.cs b/TestWebApi/Program.cs index 32c7f05..f18c0ae 100644 --- a/TestWebApi/Program.cs +++ b/TestWebApi/Program.cs @@ -2,6 +2,7 @@ using DotBased.Logging; using DotBased.Logging.MEL; using DotBased.Logging.Serilog; using Serilog; +using TestWebApi; using ILogger = Serilog.ILogger; var builder = WebApplication.CreateBuilder(args); @@ -18,6 +19,12 @@ LogService.AddLogAdapter(new BasedSerilogAdapter(serilogLogger)); builder.Logging.ClearProviders(); builder.Logging.AddDotBasedLoggerProvider(LogService.Options); +/*builder.Services.AddAuthentication(options => +{ + options.DefaultScheme = BasedAuthenticationDefaults.BasedAuthenticationScheme; + options.DefaultChallengeScheme = BasedAuthenticationDefaults.BasedAuthenticationScheme; +}).AddCookie();*/ + // Add services to the container. // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); @@ -25,6 +32,8 @@ builder.Services.AddSwaggerGen(); var app = builder.Build(); +await SeedAuthorityData.InitializeData(app.Services); + // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { diff --git a/TestWebApi/SeedAuthorityData.cs b/TestWebApi/SeedAuthorityData.cs new file mode 100644 index 0000000..5233886 --- /dev/null +++ b/TestWebApi/SeedAuthorityData.cs @@ -0,0 +1,9 @@ +namespace TestWebApi; + +public class SeedAuthorityData +{ + public static async Task InitializeData(IServiceProvider serviceProvider) + { + // Get the needed services and create users, roles, attributes. etc. + } +} \ No newline at end of file diff --git a/TestWebApi/TestWebApi.csproj b/TestWebApi/TestWebApi.csproj index c3c9828..ef97892 100644 --- a/TestWebApi/TestWebApi.csproj +++ b/TestWebApi/TestWebApi.csproj @@ -13,6 +13,7 @@ +