From 9fe564d452af7d5023cc410cd26761ba05730dc3 Mon Sep 17 00:00:00 2001 From: transtrike Date: Sat, 12 Dec 2020 15:58:55 +0200 Subject: Last push cleanup --- API/Controllers/UserController.cs | 4 +- API/Extensions/ConfigureJWT.cs | 47 ++- ...01212135453_JWT_Authorization_Added.Designer.cs | 314 +++++++++++++++++++++ .../20201212135453_JWT_Authorization_Added.cs | 17 ++ API/Startup.cs | 45 +-- 5 files changed, 371 insertions(+), 56 deletions(-) create mode 100644 API/Migrations/20201212135453_JWT_Authorization_Added.Designer.cs create mode 100644 API/Migrations/20201212135453_JWT_Authorization_Added.cs (limited to 'API') diff --git a/API/Controllers/UserController.cs b/API/Controllers/UserController.cs index ceeee33..6cb1f54 100644 --- a/API/Controllers/UserController.cs +++ b/API/Controllers/UserController.cs @@ -22,14 +22,14 @@ namespace API.Controllers } [HttpPost] - [Route("login")] + [Route("Login")] public async Task Login([FromBody] LoginDTO loginDTO) { return await this._service.LoginUser(loginDTO); } [HttpPost] - [Route("register")] + [Route("Register")] public async Task Register([FromBody] RegisterDTO registerDto) { return await this._service.RegisterUser(registerDto); diff --git a/API/Extensions/ConfigureJWT.cs b/API/Extensions/ConfigureJWT.cs index f5862f5..1de2aa5 100644 --- a/API/Extensions/ConfigureJWT.cs +++ b/API/Extensions/ConfigureJWT.cs @@ -1,21 +1,48 @@ -using Microsoft.AspNetCore.Builder; +using System.Text; +using System.Threading.Tasks; +using Data.Models.Options; +using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Microsoft.OpenApi.Models; +using Microsoft.IdentityModel.Tokens; namespace API.Extensions { public static class JWTExtensions { - public static void JWTConfiguration(this IServiceCollection services) + public static void JWTConfiguration(this IServiceCollection services, IConfiguration configuration) { - - } - - public static void UseJWTConfiguration(this IApplicationBuilder app) - { - - + 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/API/Migrations/20201212135453_JWT_Authorization_Added.Designer.cs b/API/Migrations/20201212135453_JWT_Authorization_Added.Designer.cs new file mode 100644 index 0000000..aa90eb2 --- /dev/null +++ b/API/Migrations/20201212135453_JWT_Authorization_Added.Designer.cs @@ -0,0 +1,314 @@ +// +using System; +using API.Database; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +namespace API.Migrations +{ + [DbContext(typeof(DevHiveContext))] + [Migration("20201212135453_JWT_Authorization_Added")] + partial class JWT_Authorization_Added + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .UseIdentityByDefaultColumns() + .HasAnnotation("Relational:MaxIdentifierLength", 63) + .HasAnnotation("ProductVersion", "5.0.1"); + + modelBuilder.Entity("Data.Models.Classes.Language", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.HasKey("Id"); + + b.ToTable("Languages"); + }); + + modelBuilder.Entity("Data.Models.Classes.Technology", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.HasKey("Id"); + + b.ToTable("Technologies"); + }); + + modelBuilder.Entity("Data.Models.Classes.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.Property("AccessFailedCount") + .HasColumnType("integer"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("text"); + + b.Property("Email") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("EmailConfirmed") + .HasColumnType("boolean"); + + b.Property("FirstName") + .IsRequired() + .HasColumnType("text"); + + b.Property("LastName") + .IsRequired() + .HasColumnType("text"); + + b.Property("LockoutEnabled") + .HasColumnType("boolean"); + + b.Property("LockoutEnd") + .HasColumnType("timestamp with time zone"); + + b.Property("NormalizedEmail") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("NormalizedUserName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("PasswordHash") + .HasColumnType("text"); + + b.Property("PhoneNumber") + .HasColumnType("text"); + + b.Property("PhoneNumberConfirmed") + .HasColumnType("boolean"); + + b.Property("ProfilePicture") + .HasColumnType("text"); + + b.Property("Role") + .HasColumnType("text"); + + b.Property("SecurityStamp") + .HasColumnType("text"); + + b.Property("TwoFactorEnabled") + .HasColumnType("boolean"); + + b.Property("UserName") + .IsRequired() + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedEmail") + .HasDatabaseName("EmailIndex"); + + b.HasIndex("NormalizedUserName") + .IsUnique() + .HasDatabaseName("UserNameIndex"); + + b.HasIndex("UserName") + .IsUnique(); + + b.ToTable("AspNetUsers"); + }); + + modelBuilder.Entity("Data.Models.Classes.UserRoles", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("text"); + + b.Property("Name") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("NormalizedName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedName") + .IsUnique() + .HasDatabaseName("RoleNameIndex"); + + b.ToTable("AspNetRoles"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("RoleId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetRoleClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.Property("LoginProvider") + .HasColumnType("text"); + + b.Property("ProviderKey") + .HasColumnType("text"); + + b.Property("ProviderDisplayName") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("integer"); + + b.HasKey("LoginProvider", "ProviderKey"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserLogins"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.Property("UserId") + .HasColumnType("integer"); + + b.Property("RoleId") + .HasColumnType("integer"); + + b.HasKey("UserId", "RoleId"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetUserRoles"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.Property("UserId") + .HasColumnType("integer"); + + b.Property("LoginProvider") + .HasColumnType("text"); + + b.Property("Name") + .HasColumnType("text"); + + b.Property("Value") + .HasColumnType("text"); + + b.HasKey("UserId", "LoginProvider", "Name"); + + b.ToTable("AspNetUserTokens"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.HasOne("Data.Models.Classes.UserRoles", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.HasOne("Data.Models.Classes.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.HasOne("Data.Models.Classes.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.HasOne("Data.Models.Classes.UserRoles", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Data.Models.Classes.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.HasOne("Data.Models.Classes.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/API/Migrations/20201212135453_JWT_Authorization_Added.cs b/API/Migrations/20201212135453_JWT_Authorization_Added.cs new file mode 100644 index 0000000..b55418a --- /dev/null +++ b/API/Migrations/20201212135453_JWT_Authorization_Added.cs @@ -0,0 +1,17 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +namespace API.Migrations +{ + public partial class JWT_Authorization_Added : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + + } + } +} diff --git a/API/Startup.cs b/API/Startup.cs index de108a5..100e2e2 100644 --- a/API/Startup.cs +++ b/API/Startup.cs @@ -5,17 +5,7 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -using Microsoft.OpenApi.Models; -using Data.Models.Classes; -using Data.Models.Options; -using Microsoft.IdentityModel.Tokens; -using Microsoft.AspNetCore.Authentication.JwtBearer; -using System.Text; -using System.Threading.Tasks; using API.Extensions; -using API.Database; -using Microsoft.EntityFrameworkCore; -using Microsoft.AspNetCore.Identity; namespace API { @@ -33,41 +23,9 @@ namespace API { services.AddControllers(); - 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 - }; - }); - services.DatabaseConfiguration(Configuration); services.SwaggerConfiguration(); - services.JWTConfiguration(); + services.JWTConfiguration(Configuration); services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); } @@ -88,7 +46,6 @@ namespace API } app.UseDatabaseConfiguration(); - app.UseJWTConfiguration(); app.UseEndpoints(endpoints => { -- cgit v1.2.3