diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Entities/RoleParentChildEntity.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Entities/RoleParentChildEntity.cs index cae45b7..5dab69d 100644 --- a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Entities/RoleParentChildEntity.cs +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Entities/RoleParentChildEntity.cs @@ -1,6 +1,8 @@ -namespace EasyMicroservices.AuthenticationsMicroservice.Database.Entities +using EasyMicroservices.Cores.Database.Schemas; + +namespace EasyMicroservices.AuthenticationsMicroservice.Database.Entities { - public class RoleParentChildEntity + public class RoleParentChildEntity : FullAbilitySchema { public long ChildId { get; set; } public long ParentId { get; set; } diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Schemas/UserSchema.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Schemas/UserSchema.cs index 2a0ada7..0897e50 100644 --- a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Schemas/UserSchema.cs +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/Database/Schemas/UserSchema.cs @@ -1,24 +1,14 @@ -using EasyMicroservices.Cores.Database.Interfaces; +using EasyMicroservices.Cores.Database.Schemas; using EasyMicroservices.Cores.Interfaces; using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace EasyMicroservices.AuthenticationsMicroservice.Database.Schemas { - public class UserSchema : IUniqueIdentitySchema, ISoftDeleteSchema, IDateTimeSchema + public class UserSchema : FullAbilitySchema { public string UserName { get; set; } public string Password { get; set; } - public string UniqueIdentity { get; set; } - public DateTime CreationDateTime { get; set; } - public DateTime? ModificationDateTime { get; set; } - public bool IsDeleted { get; set; } - public DateTime? DeletedDateTime { get; set; } - public bool IsUsernameVerified { get; set; } + public bool IsVerified { get; set; } } } diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/SeedData/AllSeedData.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/SeedData/AllSeedData.cs index 684e0aa..d98af6b 100644 --- a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/SeedData/AllSeedData.cs +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Database/SeedData/AllSeedData.cs @@ -1,5 +1,7 @@ using EasyMicroservices.AuthenticationsMicroservice.Database.Entities; +using EasyMicroservices.Cores.Database.Schemas; using Microsoft.EntityFrameworkCore; +using System; namespace EasyMicroservices.AuthenticationsMicroservice.SeedData { @@ -9,7 +11,7 @@ public static void Seed(ModelBuilder modelBuilder) { modelBuilder .Entity() - .HasData( + .HasData(FixDefaultValues( new RoleEntity() { Id = 1, @@ -59,10 +61,10 @@ public static void Seed(ModelBuilder modelBuilder) { Id = 10, Name = "SoftReader" - }); + })); modelBuilder .Entity() - .HasData( + .HasData(FixDefaultValues( new RoleParentChildEntity() { ChildId = 1, @@ -111,11 +113,11 @@ public static void Seed(ModelBuilder modelBuilder) { ChildId = 2, ParentId = 9 - }); + })); modelBuilder .Entity() - .HasData( + .HasData(FixDefaultValues( //owner full access new ServicePermissionEntity() { @@ -270,11 +272,11 @@ public static void Seed(ModelBuilder modelBuilder) //any microservice MicroserviceName = null, AccessType = DataTypes.AccessPermissionType.Granted - } + }) ); modelBuilder .Entity() - .HasData( + .HasData(FixDefaultValues( //owner full access new RoleServicePermissionEntity() { @@ -363,8 +365,44 @@ public static void Seed(ModelBuilder modelBuilder) Id = 14, RoleId = 4, ServicePermissionId = 14 - } - ); + })); + + modelBuilder + .Entity() + .HasData(FixDefaultValues(new UserEntity() + { + Id = 1, + UserName = "Owner", + IsVerified = true + })); + + modelBuilder + .Entity() + .HasData(FixDefaultValues(new UserRoleEntity() + { + Id = 1, + UserId = 1, + RoleId = 1 + })); + + modelBuilder + .Entity() + .HasData(FixDefaultValues(new PersonalAccessTokenEntity() + { + Id = 1, + UserId = 1, + Value = "ownerpat" + })); + } + + static T[] FixDefaultValues(params T[] values) + where T : FullAbilitySchema + { + foreach (var item in values) + { + item.CreationDateTime = DateTime.Now; + } + return values; } } } diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/EasyMicroservices.AuthenticationsMicroservice.Domain.csproj b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/EasyMicroservices.AuthenticationsMicroservice.Domain.csproj index 0c91af9..b37bc68 100644 --- a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/EasyMicroservices.AuthenticationsMicroservice.Domain.csproj +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/EasyMicroservices.AuthenticationsMicroservice.Domain.csproj @@ -10,7 +10,7 @@ - + diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Controllers/RoleController.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Controllers/RoleController.cs index 7ad9d0b..89a065b 100644 --- a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Controllers/RoleController.cs +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Controllers/RoleController.cs @@ -5,6 +5,7 @@ using EasyMicroservices.Cores.Contracts.Requests; using EasyMicroservices.Cores.Interfaces; using EasyMicroservices.ServiceContracts; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; @@ -17,6 +18,7 @@ public RoleController(IBaseUnitOfWork unitOfWork) : base(unitOfWork) } [HttpPost] + [AllowAnonymous] public async Task> GetRolesByUserId(GetIdRequestContract request) { var result = await UnitOfWork.GetLongLogic() diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Controllers/ServicePermissionController.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Controllers/ServicePermissionController.cs index 24b19fe..41977f5 100644 --- a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Controllers/ServicePermissionController.cs +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Controllers/ServicePermissionController.cs @@ -4,6 +4,7 @@ using EasyMicroservices.Cores.AspCoreApi; using EasyMicroservices.Cores.Interfaces; using EasyMicroservices.ServiceContracts; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; @@ -17,6 +18,7 @@ public ServicePermissionController(IBaseUnitOfWork unitOfWork) : base(unitOfWork } [HttpPost] + [AllowAnonymous] public async Task> GetAllPermissionsBy(ServicePermissionRequestContract request, CancellationToken cancellationToken) { request.RoleName.ThrowIfNullOrEmpty(nameof(request.RoleName)); diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Controllers/UserController.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Controllers/UserController.cs index ea0bb9e..49cf607 100644 --- a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Controllers/UserController.cs +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Controllers/UserController.cs @@ -4,6 +4,7 @@ using EasyMicroservices.Cores.AspCoreApi; using EasyMicroservices.Cores.AspEntityFrameworkCoreApi.Interfaces; using EasyMicroservices.ServiceContracts; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; @@ -31,6 +32,7 @@ public async Task> VerifyUserIdentity(UserSummaryC } [HttpPost] + [AllowAnonymous] public async Task> GetUserByPersonalAccessToken(PersonalAccessTokenRequestContract request) { var result = await _unitOfWork.GetLongLogic().GetBy(x => x.Value == request.Value diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Program.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Program.cs index 8fc17f5..0b93f22 100644 --- a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Program.cs +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/Program.cs @@ -15,9 +15,7 @@ public class Program public static async Task Main(string[] args) { var app = CreateBuilder(args); - var build = await app.Build(true, true); - - //build.UseAuthentication(); + var build = await app.Build(true); build.MapControllers(); build.UseCors(MyAllowSpecificOrigins); build.Run(); @@ -30,15 +28,6 @@ static WebApplicationBuilder CreateBuilder(string[] args) var app = StartUpExtensions.Create(args); app.Services.Builder(options => { - options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme - { - BearerFormat = "JWT", - Description = "JWT Authorization header using the Bearer scheme. \r\n\r\n Enter your token in the text input below.\r\n Example: \"Bearer {token}\"", - Name = "Authorization", - In = ParameterLocation.Header, - Type = SecuritySchemeType.ApiKey, - Scheme = "Bearer" - }); options.AddSecurityRequirement(new OpenApiSecurityRequirement { { @@ -56,12 +45,13 @@ static WebApplicationBuilder CreateBuilder(string[] args) new List() } }); - }); + }).UseDefaultSwaggerOptions(); app.Services.AddTransient((serviceProvider) => new UnitOfWork(serviceProvider)); app.Services.AddTransient(serviceProvider => new AuthenticationsContext(serviceProvider.GetService())); app.Services.AddTransient(); app.Services.AddTransient(); StartUpExtensions.AddWhiteLabel("Authentication", "RootAddresses:WhiteLabel"); + StartUpExtensions.AddAuthentication("RootAddresses:Authentication"); app.Services.AddCors(options => { @@ -71,24 +61,6 @@ static WebApplicationBuilder CreateBuilder(string[] args) policy.AllowAnyOrigin(); }); }); - - //app.Services.AddScoped(); - - app.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) - .AddJwtBearer(options => - { - options.TokenValidationParameters = new TokenValidationParameters - { - ValidateIssuer = true, - ValidateAudience = true, - ValidateLifetime = true, - ValidateIssuerSigningKey = true, - ValidIssuer = app.Configuration["JWT:Issuer"], - ValidAudience = app.Configuration["JWT:Audience"], - IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(app.Configuration["Jwt:Key"])) - }; - }); - return app; } @@ -96,7 +68,7 @@ public static async Task Run(string[] args, Action use) { var app = CreateBuilder(args); use?.Invoke(app.Services); - var build = await app.Build(true, true); + var build = await app.Build(true); build.MapControllers(); build.Run(); diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/appsettings.json b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/appsettings.json index c8fff97..f2e306d 100644 --- a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/appsettings.json +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.WebApi/appsettings.json @@ -12,14 +12,18 @@ "local": "conn" }, "RootAddresses": { - "whitelabel": "http://localhost:1041" + "whitelabel": "http://localhost:1041", + "Authentication": "http://localhost:1044" }, - "JWT": { - "Key": "VGhpc0lzQVNlY3JldEtleUZvckp3dEF1dGhlbnRpY2F0aW9u=", - "Issuer": "https://github.com/easymicroservices", - "Audience": "easymicroservices", + "Authorization": { + "Use": true, + "JWT": { + "Key": "VGhpc0lzGHGHGHlY3JldEtleUZvckp3dEF1dGhlbnRpY2F0aW9u=", + "Issuer": "https://github.com/easymicroservices", + "Audience": "easymicroservices", - "TokenExpireTimeInSeconds": 86400 + "TokenExpireTimeInSeconds": 86400 + } }, "Urls": "http://*:1044" }