From dee2e37a4a8759108390c664e06bf147b8385cbf Mon Sep 17 00:00:00 2001 From: transtrike Date: Mon, 14 Dec 2020 23:29:14 +0200 Subject: Stabalized project for compilation. Next step after init architecture --- .../Configurations/Extensions/ConfigureDatabase.cs | 39 +++++++++ .../Configurations/Extensions/ConfigureJWT.cs | 54 ++++++++++++ .../Configurations/Extensions/ConfigureSwagger.cs | 23 +++++ .../Configurations/Mapping/UserMappings.cs | 16 ++++ src/DevHive.Web/Controllers/ErrorController.cs | 20 +++++ src/DevHive.Web/Controllers/RoleController.cs | 34 ++++++++ src/DevHive.Web/Controllers/UserController.cs | 66 +++++++++++++++ src/DevHive.Web/DevHive.Web.csproj | 33 +++++--- src/DevHive.Web/Program.cs | 35 ++++---- src/DevHive.Web/Startup.cs | 99 +++++++++++----------- src/DevHive.Web/appsettings.json | 6 ++ 11 files changed, 344 insertions(+), 81 deletions(-) create mode 100644 src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs create mode 100644 src/DevHive.Web/Configurations/Extensions/ConfigureJWT.cs create mode 100644 src/DevHive.Web/Configurations/Extensions/ConfigureSwagger.cs create mode 100644 src/DevHive.Web/Configurations/Mapping/UserMappings.cs create mode 100644 src/DevHive.Web/Controllers/ErrorController.cs create mode 100644 src/DevHive.Web/Controllers/RoleController.cs create mode 100644 src/DevHive.Web/Controllers/UserController.cs (limited to 'src/DevHive.Web') diff --git a/src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs b/src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs new file mode 100644 index 0000000..178d345 --- /dev/null +++ b/src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs @@ -0,0 +1,39 @@ +using Microsoft.Extensions.DependencyInjection; +using DevHive.Data.Repositories; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using DevHive.Data.Models; +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Builder; + +namespace DevHive.Web.Configurations.Extensions +{ + public static class DatabaseExtensions + { + public static void DatabaseConfiguration(this IServiceCollection services, IConfiguration configuration) + { + services.AddDbContext(options => + options.UseNpgsql(configuration.GetConnectionString("DEV"))); + + services.AddIdentity() + .AddEntityFrameworkStores(); + + services.Configure(options => + { + //TODO: Add more validations + options.User.RequireUniqueEmail = true; + + options.Password.RequiredLength = 5; + }); + } + + public static void UseDatabaseConfiguration(this IApplicationBuilder app) + { + app.UseHttpsRedirection(); + app.UseRouting(); + + app.UseAuthentication(); + app.UseAuthorization(); + } + } +} diff --git a/src/DevHive.Web/Configurations/Extensions/ConfigureJWT.cs b/src/DevHive.Web/Configurations/Extensions/ConfigureJWT.cs new file mode 100644 index 0000000..bc5ac15 --- /dev/null +++ b/src/DevHive.Web/Configurations/Extensions/ConfigureJWT.cs @@ -0,0 +1,54 @@ +using System.Text; +using System.Threading.Tasks; +using DevHive.Services.Options; +using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.IdentityModel.Tokens; + +namespace DevHive.Web.Configurations.Extensions +{ + public static class JWTExtensions + { + public static void JWTConfiguration(this IServiceCollection services, IConfiguration configuration) + { + services.AddSingleton(new JWTOptions(configuration + .GetSection("AppSettings") + .GetSection("Secret") + .Value)); + + // Get key from appsettings.json + var key = Encoding.ASCII.GetBytes(configuration + .GetSection("AppSettings") + .GetSection("Secret") + .Value); + + // Setup Jwt Authentication + services.AddAuthentication(x => + { + x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; + x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; + }) + .AddJwtBearer(x => + { + x.Events = new JwtBearerEvents + { + OnTokenValidated = context => + { + // TODO: add more authentication + return Task.CompletedTask; + } + }; + x.RequireHttpsMetadata = false; + x.SaveToken = true; + x.TokenValidationParameters = new TokenValidationParameters + { + ValidateIssuerSigningKey = true, + IssuerSigningKey = new SymmetricSecurityKey(key), + ValidateIssuer = false, + ValidateAudience = false + }; + }); + } + } +} \ No newline at end of file diff --git a/src/DevHive.Web/Configurations/Extensions/ConfigureSwagger.cs b/src/DevHive.Web/Configurations/Extensions/ConfigureSwagger.cs new file mode 100644 index 0000000..a0641ab --- /dev/null +++ b/src/DevHive.Web/Configurations/Extensions/ConfigureSwagger.cs @@ -0,0 +1,23 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.OpenApi.Models; + +namespace DevHive.Web.Configurations.Extensions +{ + public static class SwaggerExtensions + { + public static void SwaggerConfiguration(this IServiceCollection services) + { + services.AddSwaggerGen(c => + { + c.SwaggerDoc("v1", new OpenApiInfo { Title = "API", Version = "v1" }); + }); + } + + public static void UseSwaggerConfiguration(this IApplicationBuilder app) + { + app.UseSwagger(); + app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "API v1")); + } + } +} \ No newline at end of file diff --git a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs new file mode 100644 index 0000000..f3daf5a --- /dev/null +++ b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs @@ -0,0 +1,16 @@ +using DevHive.Data.Models; +using AutoMapper; +using DevHive.Services.Models.Identity; + +namespace DevHive.Web.Configurations.Mapping +{ + public class UserMappings : Profile + { + public UserMappings() + { + CreateMap(); + CreateMap(); + CreateMap(); + } + } +} diff --git a/src/DevHive.Web/Controllers/ErrorController.cs b/src/DevHive.Web/Controllers/ErrorController.cs new file mode 100644 index 0000000..560b0da --- /dev/null +++ b/src/DevHive.Web/Controllers/ErrorController.cs @@ -0,0 +1,20 @@ +using System; +using System.Net; +using System.Net.Http; +using Microsoft.AspNetCore.Mvc; + +namespace DevHive.Web.Controllers +{ + [ApiController] + [Route("/api/[controller]")] + public class ErrorController + { + [HttpGet] + public HttpStatusCode HttpError(HttpRequestException exception) + { + Console.WriteLine("WE HERE, BOIIIIIII"); + + return HttpStatusCode.OK; + } + } +} diff --git a/src/DevHive.Web/Controllers/RoleController.cs b/src/DevHive.Web/Controllers/RoleController.cs new file mode 100644 index 0000000..fda8bb2 --- /dev/null +++ b/src/DevHive.Web/Controllers/RoleController.cs @@ -0,0 +1,34 @@ +using System; +using System.Threading.Tasks; +using DevHive.Data.Repositories; +using DevHive.Services.Services; +using Microsoft.AspNetCore.Mvc; + +namespace DevHive.Web.Controllers +{ + [ApiController] + [Route("/api/[controller]")] + public class RoleController + { + private readonly RoleService _service; + + public RoleController(DevHiveContext context) + { + //this._service = new RoleService(context); + } + + [HttpPost] + public Task Create(string name) + { + //return this._service.CreatePost(name); + throw new NotImplementedException(); + } + + [HttpGet] + public Task ShowPost(uint postId) + { + //return this._service.GetPostById(postId); + throw new NotImplementedException(); + } + } +} diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs new file mode 100644 index 0000000..84231d1 --- /dev/null +++ b/src/DevHive.Web/Controllers/UserController.cs @@ -0,0 +1,66 @@ +using System; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Data.Repositories; +using DevHive.Services.Models.Identity; +using DevHive.Services.Options; +using DevHive.Services.Services; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace DevHive.Web.Controllers +{ + [ApiController] + [Route("/api/[controller]")] + public class UserController: ControllerBase + { + private readonly UserService _service; + + public UserController(DevHiveContext context, IMapper mapper, JWTOptions jwtOptions) + { + //this._service = new UserService(context, mapper, jwtOptions); + } + + [HttpPost] + [Route("Login")] + public async Task Login([FromBody] LoginServiceModel loginServiceModel) + { + //return await this._service.LoginUser(loginDTO); + throw new NotImplementedException(); + } + + [HttpPost] + [Route("Register")] + public async Task Register([FromBody] RegisterServiceModel registerServiceModel) + { + //return await this._service.RegisterUser(registerDto); + throw new NotImplementedException(); + } + + //Read + [HttpGet] + public async Task GetById(Guid id) + { + //return await this._service.GetUserById(id); + throw new NotImplementedException(); + } + + //Update + [HttpPut] + [Authorize] + public async Task Update(Guid id, [FromBody] UpdateUserServiceModel updateUserServiceModel) + { + //return await this._service.UpdateUser(id, userDTO); + throw new NotImplementedException(); + } + + //Delete + [HttpDelete] + [Authorize] + public async Task Delete(Guid id) + { + //return await this._service.DeleteUser(id); + throw new NotImplementedException(); + } + } +} diff --git a/src/DevHive.Web/DevHive.Web.csproj b/src/DevHive.Web/DevHive.Web.csproj index 218b809..5b5478d 100644 --- a/src/DevHive.Web/DevHive.Web.csproj +++ b/src/DevHive.Web/DevHive.Web.csproj @@ -1,13 +1,20 @@ - - - - net5.0 - - - - - - - - - + + + net5.0 + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/DevHive.Web/Program.cs b/src/DevHive.Web/Program.cs index 3967a71..9cbe12a 100644 --- a/src/DevHive.Web/Program.cs +++ b/src/DevHive.Web/Program.cs @@ -1,26 +1,23 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; namespace DevHive.Web { - public class Program - { - public static void Main(string[] args) - { - CreateHostBuilder(args).Build().Run(); - } + public class Program + { + private const int HTTPS_PORT = 5000; - public static IHostBuilder CreateHostBuilder(string[] args) => - Host.CreateDefaultBuilder(args) - .ConfigureWebHostDefaults(webBuilder => - { - webBuilder.UseStartup(); - }); - } + public static void Main(string[] args) + { + CreateHostBuilder(args).Build().Run(); + } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.ConfigureKestrel(opt => opt.ListenLocalhost(HTTPS_PORT)); + webBuilder.UseStartup(); + }); + } } diff --git a/src/DevHive.Web/Startup.cs b/src/DevHive.Web/Startup.cs index 303c080..62d9d2c 100644 --- a/src/DevHive.Web/Startup.cs +++ b/src/DevHive.Web/Startup.cs @@ -1,59 +1,60 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.HttpsPolicy; -using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; -using Microsoft.OpenApi.Models; +using DevHive.Web.Configurations.Extensions; +using AutoMapper; namespace DevHive.Web { - public class Startup - { - public Startup(IConfiguration configuration) - { - Configuration = configuration; - } - - public IConfiguration Configuration { get; } - - // This method gets called by the runtime. Use this method to add services to the container. - public void ConfigureServices(IServiceCollection services) - { - - services.AddControllers(); - services.AddSwaggerGen(c => - { - c.SwaggerDoc("v1", new OpenApiInfo { Title = "DevHive.Web", Version = "v1" }); - }); - } - - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) - { - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - app.UseSwagger(); - app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "DevHive.Web v1")); - } - - app.UseHttpsRedirection(); - - app.UseRouting(); - - app.UseAuthorization(); - - app.UseEndpoints(endpoints => - { - endpoints.MapControllers(); - }); - } - } + public class Startup + { + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + public IConfiguration Configuration { get; } + + // This method gets called by the runtime. Use this method to add services to the container. + public void ConfigureServices(IServiceCollection services) + { + services.AddControllers(); + + services.DatabaseConfiguration(Configuration); + services.SwaggerConfiguration(); + services.JWTConfiguration(Configuration); + //services.AutoMapperConfiguration(); + + services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + //app.UseExceptionHandler("/api/HttpError"); + app.UseSwaggerConfiguration(); + } + else + { + app.UseExceptionHandler("/api/HttpError"); + app.UseHsts(); + } + + app.UseDatabaseConfiguration(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllerRoute( + name: "default", + pattern: "api/{controller}/{action}" + ); + }); + } + } } diff --git a/src/DevHive.Web/appsettings.json b/src/DevHive.Web/appsettings.json index 81ff877..1784183 100644 --- a/src/DevHive.Web/appsettings.json +++ b/src/DevHive.Web/appsettings.json @@ -1,4 +1,10 @@ { + "AppSettings": { + "Secret": "ADD_ANY_STRING_WITH_32_OR_MORE_CHARACTERS" + }, + "ConnectionStrings" : { + "DEV": "Server=localhost;Port=5432;Database=API;User Id=postgres;Password=;" + }, "Logging": { "LogLevel": { "Default": "Information", -- cgit v1.2.3