Initial commit - Auth
This commit is contained in:
Executable
+63
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using Auth.Contracts.Permissions;
|
||||
|
||||
namespace Auth.API.Config;
|
||||
|
||||
public static class Authentication
|
||||
{
|
||||
public static IServiceCollection Authenticate(this IServiceCollection services, ConfigurationManager configuration)
|
||||
{
|
||||
services.AddAuthentication(x =>
|
||||
{
|
||||
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
}).AddJwtBearer(o =>
|
||||
{
|
||||
|
||||
var Key = Encoding.UTF8.GetBytes(configuration["JWT:Key"]);
|
||||
o.SaveToken = true;
|
||||
o.TokenValidationParameters = new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuer = false, // on production make it true
|
||||
ValidateAudience = false, // on production make it true
|
||||
ValidateLifetime = true,
|
||||
ValidateIssuerSigningKey = true,
|
||||
ValidIssuer = configuration["JWT:Issuer"],
|
||||
ValidAudience = configuration["JWT:Audience"],
|
||||
IssuerSigningKey = new SymmetricSecurityKey(Key),
|
||||
ClockSkew = TimeSpan.Zero
|
||||
};
|
||||
o.Events = new JwtBearerEvents
|
||||
{
|
||||
OnAuthenticationFailed = context =>
|
||||
{
|
||||
if (context.Exception.GetType() == typeof(SecurityTokenExpiredException))
|
||||
{
|
||||
context.Response.Headers.Add("IS-TOKEN-EXPIRED", "true");
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddPermissionPolicies(this IServiceCollection services)
|
||||
{
|
||||
services.AddAuthorization(options =>
|
||||
{
|
||||
foreach (var permission in AuthPermissions.GetAllPermissions())
|
||||
{
|
||||
options.AddPolicy(permission, policy => policy.RequireClaim("Permission", permission));
|
||||
}
|
||||
});
|
||||
return services;
|
||||
}
|
||||
}
|
||||
Executable
+25
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
namespace Auth.API.Config;
|
||||
|
||||
public static class Authorization
|
||||
{
|
||||
public static IServiceCollection Authorize(this IServiceCollection services)
|
||||
{
|
||||
services.AddAuthorization(options =>
|
||||
{
|
||||
options.AddPolicy("Admin", policy => policy.RequireRole("Admin"));
|
||||
options.AddPolicy("Moderator", policy => policy.RequireRole("Moderator"));
|
||||
options.AddPolicy("Seller", policy => policy.RequireRole("Seller"));
|
||||
// options.AddPolicy("OrderOwnerOrAdmin", policy =>
|
||||
// policy.Requirements.Add(new OrderOwnerOrAdminRequirement()));
|
||||
});
|
||||
|
||||
return services;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
// using System;
|
||||
// using System.Collections.Generic;
|
||||
// using System.Linq;
|
||||
// using System.Threading.Tasks;
|
||||
// using Microsoft.AspNetCore.Authorization;
|
||||
// using Core.Services;
|
||||
// using System.Security.Claims;
|
||||
// using Microsoft.AspNetCore.Identity;
|
||||
// using System.Threading.Tasks;
|
||||
|
||||
// namespace ECommerce_API.Config.AuthorizeHandlers
|
||||
// {
|
||||
// public class OrderOwnerOrAdminHandler : AuthorizationHandler<OrderOwnerOrAdminRequirement, Guid>
|
||||
// {
|
||||
// // private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
// // public OrderOwnerOrAdminHandler(IServiceProvider serviceProvider)
|
||||
// // {
|
||||
// // _serviceProvider = serviceProvider;
|
||||
// // }
|
||||
// private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
// public OrderOwnerOrAdminHandler(IServiceProvider serviceProvider)
|
||||
// {
|
||||
// _serviceProvider = serviceProvider;
|
||||
// }
|
||||
|
||||
// // protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context,
|
||||
// // OrderOwnerOrAdminRequirement requirement, Guid orderId)
|
||||
// // {
|
||||
|
||||
// // using var scope = _serviceProvider.CreateScope();
|
||||
// // var orderService = scope.ServiceProvider.GetRequiredService<OrderService>();
|
||||
|
||||
// // var user = context.User;
|
||||
// // var userId = user.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
||||
// // var isAdmin = user.IsInRole("Admin");
|
||||
|
||||
// // if (isAdmin)
|
||||
// // {
|
||||
// // context.Succeed(requirement);
|
||||
// // return;
|
||||
// // }
|
||||
|
||||
// // // // var order = await orderService.GetOrderById(orderId.ToString());
|
||||
// // // if (order != null && order.CustomerId == userId)
|
||||
// // // {
|
||||
// // // context.Succeed(requirement);
|
||||
// // // }
|
||||
// // }
|
||||
// }
|
||||
|
||||
// public class OrderOwnerOrAdminRequirement : IAuthorizationRequirement { }
|
||||
// }
|
||||
|
||||
|
||||
Executable
+40
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Auth.API.Config;
|
||||
|
||||
public static class Cors
|
||||
{
|
||||
public static IServiceCollection AddCustomCors(this IServiceCollection services)
|
||||
{
|
||||
services.AddCors(options =>
|
||||
{
|
||||
options.AddDefaultPolicy(builder =>
|
||||
{
|
||||
builder
|
||||
.WithOrigins("http://localhost:5211", "http://localhost:5055")
|
||||
.AllowAnyHeader()
|
||||
.AllowAnyMethod().AllowCredentials()
|
||||
.WithExposedHeaders("*");
|
||||
;
|
||||
});
|
||||
|
||||
options.AddPolicy(
|
||||
"default",
|
||||
builder =>
|
||||
{
|
||||
builder
|
||||
.WithOrigins("http://localhost:5211" , "http://localhost:5055")
|
||||
.AllowAnyHeader()
|
||||
.AllowAnyMethod().AllowCredentials()
|
||||
.WithExposedHeaders("*");
|
||||
;
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
Executable
+27
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Auth.API.Config;
|
||||
|
||||
public static class FluentEmailExtensions
|
||||
{
|
||||
public static void RegFluentEmail(
|
||||
this IServiceCollection services,
|
||||
ConfigurationManager configuration
|
||||
)
|
||||
{
|
||||
var emailSettings = configuration.GetSection("MailSettings");
|
||||
var defaultFromEmail = emailSettings["Mail"];
|
||||
var host = emailSettings["Host"];
|
||||
var port = emailSettings.GetValue<int>("Port");
|
||||
var userName = emailSettings["UserName"];
|
||||
var password = emailSettings["Password"];
|
||||
|
||||
services
|
||||
.AddFluentEmail(defaultFromEmail)
|
||||
.AddSmtpSender(host, port, userName, password)
|
||||
.AddRazorRenderer();
|
||||
}
|
||||
}
|
||||
Executable
+28
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Auth.Core;
|
||||
using Auth.Core.Services;
|
||||
using Auth.Core.Services.Auth;
|
||||
|
||||
using Auth.Core.Services.HR.Users;
|
||||
using Auth.Domain.Entities;
|
||||
// using ECommerce_API.Config.AuthorizeHandlers;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
|
||||
namespace Auth.API.Config;
|
||||
|
||||
public static class RegisteringServices
|
||||
{
|
||||
public static IServiceCollection RegisterServices(this IServiceCollection services)
|
||||
{
|
||||
// services.AddScoped<EmailService, EmailService>();
|
||||
services.AddScoped<TokenGenerator>();
|
||||
services.AddScoped<UserService>();
|
||||
services.AddScoped<AuthService>();
|
||||
services.AddScoped<EmailService>();
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
Executable
+28
@@ -0,0 +1,28 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Scalar.AspNetCore;
|
||||
|
||||
namespace Auth.API.Config;
|
||||
|
||||
public static class Swagger
|
||||
{
|
||||
public static IServiceCollection AddSwagger(this IServiceCollection services)
|
||||
{
|
||||
services.AddOpenApi(options =>
|
||||
{
|
||||
options.AddDocumentTransformer((document, context, cancellationToken) =>
|
||||
{
|
||||
document.Info.Title = "Auth API";
|
||||
document.Info.Version = "v1";
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
});
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IEndpointRouteBuilder UseSwag(this IEndpointRouteBuilder app)
|
||||
{
|
||||
app.MapScalarApiReference();
|
||||
return app;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user