diff options
| -rw-r--r-- | API/Controllers/UserController.cs | 4 | ||||
| -rw-r--r-- | API/Extensions/ConfigureJWT.cs | 47 | ||||
| -rw-r--r-- | API/Migrations/20201212135453_JWT_Authorization_Added.Designer.cs | 314 | ||||
| -rw-r--r-- | API/Migrations/20201212135453_JWT_Authorization_Added.cs | 17 | ||||
| -rw-r--r-- | API/Startup.cs | 45 | ||||
| -rw-r--r-- | Data/Models/Classes/User.cs | 2 |
6 files changed, 372 insertions, 57 deletions
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<IActionResult> Login([FromBody] LoginDTO loginDTO) { return await this._service.LoginUser(loginDTO); } [HttpPost] - [Route("register")] + [Route("Register")] public async Task<IActionResult> 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<JWTOptions>( + 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 @@ +// <auto-generated /> +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<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.HasKey("Id"); + + b.ToTable("Languages"); + }); + + modelBuilder.Entity("Data.Models.Classes.Technology", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.HasKey("Id"); + + b.ToTable("Technologies"); + }); + + modelBuilder.Entity("Data.Models.Classes.User", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.Property<int>("AccessFailedCount") + .HasColumnType("integer"); + + b.Property<string>("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("text"); + + b.Property<string>("Email") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property<bool>("EmailConfirmed") + .HasColumnType("boolean"); + + b.Property<string>("FirstName") + .IsRequired() + .HasColumnType("text"); + + b.Property<string>("LastName") + .IsRequired() + .HasColumnType("text"); + + b.Property<bool>("LockoutEnabled") + .HasColumnType("boolean"); + + b.Property<DateTimeOffset?>("LockoutEnd") + .HasColumnType("timestamp with time zone"); + + b.Property<string>("NormalizedEmail") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property<string>("NormalizedUserName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property<string>("PasswordHash") + .HasColumnType("text"); + + b.Property<string>("PhoneNumber") + .HasColumnType("text"); + + b.Property<bool>("PhoneNumberConfirmed") + .HasColumnType("boolean"); + + b.Property<string>("ProfilePicture") + .HasColumnType("text"); + + b.Property<string>("Role") + .HasColumnType("text"); + + b.Property<string>("SecurityStamp") + .HasColumnType("text"); + + b.Property<bool>("TwoFactorEnabled") + .HasColumnType("boolean"); + + b.Property<string>("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<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.Property<string>("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("text"); + + b.Property<string>("Name") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property<string>("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<int>", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.Property<string>("ClaimType") + .HasColumnType("text"); + + b.Property<string>("ClaimValue") + .HasColumnType("text"); + + b.Property<int>("RoleId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetRoleClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<int>", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.Property<string>("ClaimType") + .HasColumnType("text"); + + b.Property<string>("ClaimValue") + .HasColumnType("text"); + + b.Property<int>("UserId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<int>", b => + { + b.Property<string>("LoginProvider") + .HasColumnType("text"); + + b.Property<string>("ProviderKey") + .HasColumnType("text"); + + b.Property<string>("ProviderDisplayName") + .HasColumnType("text"); + + b.Property<int>("UserId") + .HasColumnType("integer"); + + b.HasKey("LoginProvider", "ProviderKey"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserLogins"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<int>", b => + { + b.Property<int>("UserId") + .HasColumnType("integer"); + + b.Property<int>("RoleId") + .HasColumnType("integer"); + + b.HasKey("UserId", "RoleId"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetUserRoles"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<int>", b => + { + b.Property<int>("UserId") + .HasColumnType("integer"); + + b.Property<string>("LoginProvider") + .HasColumnType("text"); + + b.Property<string>("Name") + .HasColumnType("text"); + + b.Property<string>("Value") + .HasColumnType("text"); + + b.HasKey("UserId", "LoginProvider", "Name"); + + b.ToTable("AspNetUserTokens"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<int>", b => + { + b.HasOne("Data.Models.Classes.UserRoles", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<int>", b => + { + b.HasOne("Data.Models.Classes.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<int>", b => + { + b.HasOne("Data.Models.Classes.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<int>", 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<int>", 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<JWTOptions>(
- 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 =>
{
diff --git a/Data/Models/Classes/User.cs b/Data/Models/Classes/User.cs index 50ceb1e..44d2a60 100644 --- a/Data/Models/Classes/User.cs +++ b/Data/Models/Classes/User.cs @@ -60,7 +60,7 @@ namespace Data.Models.Classes } } - public string Role { get; set; } + public Roles Role { get; set; } // public List<User> Friends { get; set; } |
