Skip to content

Commit

Permalink
Merge pull request #29 from Ali-YousefiTelori/develop
Browse files Browse the repository at this point in the history
Add Support authorization
  • Loading branch information
Ali-YousefiTelori authored Nov 27, 2023
2 parents 253dc1b + f43ac44 commit b313269
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -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; }
Expand Down
Original file line number Diff line number Diff line change
@@ -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; }
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using EasyMicroservices.AuthenticationsMicroservice.Database.Entities;
using EasyMicroservices.Cores.Database.Schemas;
using Microsoft.EntityFrameworkCore;
using System;

namespace EasyMicroservices.AuthenticationsMicroservice.SeedData
{
Expand All @@ -9,7 +11,7 @@ public static void Seed(ModelBuilder modelBuilder)
{
modelBuilder
.Entity<RoleEntity>()
.HasData(
.HasData(FixDefaultValues(
new RoleEntity()
{
Id = 1,
Expand Down Expand Up @@ -59,10 +61,10 @@ public static void Seed(ModelBuilder modelBuilder)
{
Id = 10,
Name = "SoftReader"
});
}));
modelBuilder
.Entity<RoleParentChildEntity>()
.HasData(
.HasData(FixDefaultValues(
new RoleParentChildEntity()
{
ChildId = 1,
Expand Down Expand Up @@ -111,11 +113,11 @@ public static void Seed(ModelBuilder modelBuilder)
{
ChildId = 2,
ParentId = 9
});
}));

modelBuilder
.Entity<ServicePermissionEntity>()
.HasData(
.HasData(FixDefaultValues(
//owner full access
new ServicePermissionEntity()
{
Expand Down Expand Up @@ -270,11 +272,11 @@ public static void Seed(ModelBuilder modelBuilder)
//any microservice
MicroserviceName = null,
AccessType = DataTypes.AccessPermissionType.Granted
}
})
);
modelBuilder
.Entity<RoleServicePermissionEntity>()
.HasData(
.HasData(FixDefaultValues(
//owner full access
new RoleServicePermissionEntity()
{
Expand Down Expand Up @@ -363,8 +365,44 @@ public static void Seed(ModelBuilder modelBuilder)
Id = 14,
RoleId = 4,
ServicePermissionId = 14
}
);
}));

modelBuilder
.Entity<UserEntity>()
.HasData(FixDefaultValues(new UserEntity()
{
Id = 1,
UserName = "Owner",
IsVerified = true
}));

modelBuilder
.Entity<UserRoleEntity>()
.HasData(FixDefaultValues(new UserRoleEntity()
{
Id = 1,
UserId = 1,
RoleId = 1
}));

modelBuilder
.Entity<PersonalAccessTokenEntity>()
.HasData(FixDefaultValues(new PersonalAccessTokenEntity()
{
Id = 1,
UserId = 1,
Value = "ownerpat"
}));
}

static T[] FixDefaultValues<T>(params T[] values)
where T : FullAbilitySchema
{
foreach (var item in values)
{
item.CreationDateTime = DateTime.Now;
}
return values;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="EasyMicroservices.Cores.AspEntityFrameworkCoreApi" Version="0.0.0.51" />
<PackageReference Include="EasyMicroservices.Cores.AspEntityFrameworkCoreApi" Version="0.0.0.57" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -17,6 +18,7 @@ public RoleController(IBaseUnitOfWork unitOfWork) : base(unitOfWork)
}

[HttpPost]
[AllowAnonymous]
public async Task<ListMessageContract<RoleContract>> GetRolesByUserId(GetIdRequestContract<long> request)
{
var result = await UnitOfWork.GetLongLogic<UserRoleEntity>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -17,6 +18,7 @@ public ServicePermissionController(IBaseUnitOfWork unitOfWork) : base(unitOfWork
}

[HttpPost]
[AllowAnonymous]
public async Task<ListMessageContract<ServicePermissionContract>> GetAllPermissionsBy(ServicePermissionRequestContract request, CancellationToken cancellationToken)
{
request.RoleName.ThrowIfNullOrEmpty(nameof(request.RoleName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -31,6 +32,7 @@ public async Task<MessageContract<UserContract>> VerifyUserIdentity(UserSummaryC
}

[HttpPost]
[AllowAnonymous]
public async Task<MessageContract<UserContract>> GetUserByPersonalAccessToken(PersonalAccessTokenRequestContract request)
{
var result = await _unitOfWork.GetLongLogic<PersonalAccessTokenEntity>().GetBy(x => x.Value == request.Value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ public class Program
public static async Task Main(string[] args)
{
var app = CreateBuilder(args);
var build = await app.Build<AuthenticationsContext>(true, true);

//build.UseAuthentication();
var build = await app.Build<AuthenticationsContext>(true);
build.MapControllers();
build.UseCors(MyAllowSpecificOrigins);
build.Run();
Expand All @@ -30,15 +28,6 @@ static WebApplicationBuilder CreateBuilder(string[] args)
var app = StartUpExtensions.Create<AuthenticationsContext>(args);
app.Services.Builder<AuthenticationsContext>(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
{
{
Expand All @@ -56,12 +45,13 @@ static WebApplicationBuilder CreateBuilder(string[] args)
new List<string>()
}
});
});
}).UseDefaultSwaggerOptions();
app.Services.AddTransient((serviceProvider) => new UnitOfWork(serviceProvider));
app.Services.AddTransient(serviceProvider => new AuthenticationsContext(serviceProvider.GetService<IEntityFrameworkCoreDatabaseBuilder>()));
app.Services.AddTransient<IEntityFrameworkCoreDatabaseBuilder, DatabaseBuilder>();
app.Services.AddTransient<IBaseUnitOfWork, UnitOfWork>();
StartUpExtensions.AddWhiteLabel("Authentication", "RootAddresses:WhiteLabel");
StartUpExtensions.AddAuthentication("RootAddresses:Authentication");

app.Services.AddCors(options =>
{
Expand All @@ -71,32 +61,14 @@ static WebApplicationBuilder CreateBuilder(string[] args)
policy.AllowAnyOrigin();
});
});

//app.Services.AddScoped<IJWTManager, JWTManager>();

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;
}

public static async Task Run(string[] args, Action<IServiceCollection> use)
{
var app = CreateBuilder(args);
use?.Invoke(app.Services);
var build = await app.Build<AuthenticationsContext>(true, true);
var build = await app.Build<AuthenticationsContext>(true);
build.MapControllers();

build.Run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit b313269

Please sign in to comment.