From e7cbd32a39ae873276bbd9b9da35a62684eb3c72 Mon Sep 17 00:00:00 2001 From: hello Date: Thu, 7 Mar 2024 20:10:52 +0800 Subject: [PATCH] =?UTF-8?q?Identity=20=E8=BA=AB=E4=BB=BD=E8=AE=A4=E8=AF=81?= =?UTF-8?q?=E5=92=8C=E6=8E=88=E6=9D=83=E7=BB=88=E7=BB=93=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/TestsController.cs | 34 ++ .../Controllers/UsersController.cs | 2 - .../DataSeeding/User2DataSeedingProvider.cs | 27 -- .../DataSeeding/UserDataSeedingProvider.cs | 54 ++- .../Entities/Role.cs | 12 + .../Entities/User.cs | 17 +- .../RoleClaimEntityTypeConfiguration.cs | 19 ++ .../RoleEntityTypeConfiguration.cs | 23 ++ .../UserClaimEntityTypeConfiguration.cs | 19 ++ .../UserEntityTypeConfiguration.cs | 19 +- .../UserLoginEntityTypeConfiguration.cs | 20 ++ .../UserRoleEntityTypeConfiguration.cs | 16 + .../UserTokenEntityTypeConfiguration.cs | 20 ++ .../IdentityServiceDbContext.cs | 6 +- .../20240302004354_InitialCreate.Designer.cs | 59 ---- .../20240302004354_InitialCreate.cs | 38 --- .../20240307112633_InitialCreate.Designer.cs | 318 ++++++++++++++++++ .../20240307112633_InitialCreate.cs | 227 +++++++++++++ .../IdentityServiceDbContextModelSnapshot.cs | 277 ++++++++++++++- .../HelloShop.IdentityService.csproj | 1 + src/HelloShop.IdentityService/Program.cs | 23 +- .../Extensions/OpenApiExtensions.cs | 23 ++ 22 files changed, 1087 insertions(+), 167 deletions(-) create mode 100644 src/HelloShop.IdentityService/Controllers/TestsController.cs delete mode 100644 src/HelloShop.IdentityService/DataSeeding/User2DataSeedingProvider.cs create mode 100644 src/HelloShop.IdentityService/Entities/Role.cs create mode 100644 src/HelloShop.IdentityService/EntityFrameworks/EntityConfigurations/RoleClaimEntityTypeConfiguration.cs create mode 100644 src/HelloShop.IdentityService/EntityFrameworks/EntityConfigurations/RoleEntityTypeConfiguration.cs create mode 100644 src/HelloShop.IdentityService/EntityFrameworks/EntityConfigurations/UserClaimEntityTypeConfiguration.cs create mode 100644 src/HelloShop.IdentityService/EntityFrameworks/EntityConfigurations/UserLoginEntityTypeConfiguration.cs create mode 100644 src/HelloShop.IdentityService/EntityFrameworks/EntityConfigurations/UserRoleEntityTypeConfiguration.cs create mode 100644 src/HelloShop.IdentityService/EntityFrameworks/EntityConfigurations/UserTokenEntityTypeConfiguration.cs delete mode 100644 src/HelloShop.IdentityService/EntityFrameworks/Migrations/20240302004354_InitialCreate.Designer.cs delete mode 100644 src/HelloShop.IdentityService/EntityFrameworks/Migrations/20240302004354_InitialCreate.cs create mode 100644 src/HelloShop.IdentityService/EntityFrameworks/Migrations/20240307112633_InitialCreate.Designer.cs create mode 100644 src/HelloShop.IdentityService/EntityFrameworks/Migrations/20240307112633_InitialCreate.cs diff --git a/src/HelloShop.IdentityService/Controllers/TestsController.cs b/src/HelloShop.IdentityService/Controllers/TestsController.cs new file mode 100644 index 0000000..baf5a53 --- /dev/null +++ b/src/HelloShop.IdentityService/Controllers/TestsController.cs @@ -0,0 +1,34 @@ +// Copyright (c) HelloShop Corporation. All rights reserved. +// See the license file in the project root for more information. + +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; + +namespace HelloShop.IdentityService.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class TestsController : ControllerBase + { + [HttpGet(nameof(Foo))] + [Authorize(Roles = "AdminRole")] + public IActionResult Foo() + { + return Ok("Hello, World!"); + } + + [HttpGet(nameof(Bar))] + [Authorize(Roles = "GuestRole")] + public IActionResult Bar() + { + return Ok("Hello, World!"); + } + + [HttpGet(nameof(Baz))] + public IActionResult Baz() + { + return Ok("Hello, World!"); + } + } +} diff --git a/src/HelloShop.IdentityService/Controllers/UsersController.cs b/src/HelloShop.IdentityService/Controllers/UsersController.cs index b3c67c2..3a49b07 100644 --- a/src/HelloShop.IdentityService/Controllers/UsersController.cs +++ b/src/HelloShop.IdentityService/Controllers/UsersController.cs @@ -27,8 +27,6 @@ namespace HelloShop.IdentityService.Controllers [HttpPost] public void Post([FromBody] User value) { - value.CreationTime = DateTimeOffset.Now; - dbContext.Add(value); dbContext.SaveChanges(); } diff --git a/src/HelloShop.IdentityService/DataSeeding/User2DataSeedingProvider.cs b/src/HelloShop.IdentityService/DataSeeding/User2DataSeedingProvider.cs deleted file mode 100644 index f615bf8..0000000 --- a/src/HelloShop.IdentityService/DataSeeding/User2DataSeedingProvider.cs +++ /dev/null @@ -1,27 +0,0 @@ -using HelloShop.IdentityService.Entities; -using HelloShop.IdentityService.EntityFrameworks; -using HelloShop.ServiceDefaults.Infrastructure; - -namespace HelloShop.IdentityService.DataSeeding -{ - public class User2DataSeedingProvider : IDataSeedingProvider - { - public async Task SeedingAsync(IServiceProvider serviceProvider) - { - var dbContext = serviceProvider.GetRequiredService(); - - var guestUser = dbContext.Set().SingleOrDefault(x => x.UserName == "guest2"); - - if (guestUser is null) - { - await dbContext.Set().AddAsync(new User - { - UserName = "guest2", - PasswordHash = "AQAAAAEAACcQAAAAEJ" - }); - - await dbContext.SaveChangesAsync(); - } - } - } -} diff --git a/src/HelloShop.IdentityService/DataSeeding/UserDataSeedingProvider.cs b/src/HelloShop.IdentityService/DataSeeding/UserDataSeedingProvider.cs index 0bc0e4c..5f2a703 100644 --- a/src/HelloShop.IdentityService/DataSeeding/UserDataSeedingProvider.cs +++ b/src/HelloShop.IdentityService/DataSeeding/UserDataSeedingProvider.cs @@ -1,24 +1,64 @@ using HelloShop.IdentityService.Entities; using HelloShop.IdentityService.EntityFrameworks; using HelloShop.ServiceDefaults.Infrastructure; +using Microsoft.AspNetCore.Identity; namespace HelloShop.IdentityService.DataSeeding { - public class UserDataSeedingProvider(IdentityServiceDbContext dbContext) : IDataSeedingProvider + public class UserDataSeedingProvider(UserManager userManager, RoleManager roleManager) : IDataSeedingProvider { public async Task SeedingAsync(IServiceProvider serviceProvider) { - var guestUser = dbContext.Set().SingleOrDefault(x => x.UserName == "guest"); + var adminRole = await roleManager.FindByNameAsync("AdminRole"); - if (guestUser is null) + if (adminRole == null) { - await dbContext.Set().AddAsync(new User + await roleManager.CreateAsync(new Role + { + Name = "AdminRole" + }); + } + + var guestRole = await roleManager.FindByNameAsync("GuestRole"); + + if (guestRole == null) + { + await roleManager.CreateAsync(new Role + { + Name = "GuestRole" + }); + } + + var adminUser = await userManager.FindByNameAsync("admin"); + + if (adminUser == null) + { + await userManager.CreateAsync(new User + { + UserName = "admin", + Email = "admin@test.com" + },"admin"); + } + + if (adminUser!=null) + { + await userManager.AddToRolesAsync(adminUser, ["AdminRole", "GuestRole"]); + } + + var guestUser = await userManager.FindByNameAsync("guest"); + + if (guestUser == null) + { + await userManager.CreateAsync(new User { UserName = "guest", - PasswordHash = "AQAAAAEAACcQAAAAEJ" - }); + Email = "guest@test.com" + },"guest"); + } - await dbContext.SaveChangesAsync(); + if (guestUser!=null) + { + await userManager.AddToRoleAsync(guestUser, "GuestRole"); } } } diff --git a/src/HelloShop.IdentityService/Entities/Role.cs b/src/HelloShop.IdentityService/Entities/Role.cs new file mode 100644 index 0000000..b1cb738 --- /dev/null +++ b/src/HelloShop.IdentityService/Entities/Role.cs @@ -0,0 +1,12 @@ +// Copyright (c) HelloShop Corporation. All rights reserved. +// See the license file in the project root for more information. + +using Microsoft.AspNetCore.Identity; + +namespace HelloShop.IdentityService.Entities +{ + public class Role: IdentityRole + { + public DateTimeOffset CreationTime { get; set; }=TimeProvider.System.GetUtcNow(); + } +} diff --git a/src/HelloShop.IdentityService/Entities/User.cs b/src/HelloShop.IdentityService/Entities/User.cs index 4577e53..cd5952d 100644 --- a/src/HelloShop.IdentityService/Entities/User.cs +++ b/src/HelloShop.IdentityService/Entities/User.cs @@ -1,13 +1,12 @@ -namespace HelloShop.IdentityService.Entities +// Copyright (c) HelloShop Corporation. All rights reserved. +// See the license file in the project root for more information. + +using Microsoft.AspNetCore.Identity; + +namespace HelloShop.IdentityService.Entities { - public class User + public class User: IdentityUser { - public int Id { get; set; } - - public string UserName { get; set; } = default!; - - public string PasswordHash { get; set; } = default!; - - public DateTimeOffset CreationTime { get; set; } = DateTimeOffset.UtcNow; + public DateTimeOffset CreationTime { get; init; } = TimeProvider.System.GetUtcNow(); } } diff --git a/src/HelloShop.IdentityService/EntityFrameworks/EntityConfigurations/RoleClaimEntityTypeConfiguration.cs b/src/HelloShop.IdentityService/EntityFrameworks/EntityConfigurations/RoleClaimEntityTypeConfiguration.cs new file mode 100644 index 0000000..646ab4b --- /dev/null +++ b/src/HelloShop.IdentityService/EntityFrameworks/EntityConfigurations/RoleClaimEntityTypeConfiguration.cs @@ -0,0 +1,19 @@ +// Copyright (c) HelloShop Corporation. All rights reserved. +// See the license file in the project root for more information. + +using Microsoft.AspNetCore.Identity; +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using Microsoft.EntityFrameworkCore; + +namespace HelloShop.IdentityService.EntityFrameworks.EntityConfigurations +{ + public class RoleClaimEntityTypeConfiguration : IEntityTypeConfiguration> + { + public void Configure(EntityTypeBuilder> builder) + { + builder.ToTable("RoleClaims"); + + builder.Property(rc => rc.ClaimType).HasMaxLength(128); + } + } +} diff --git a/src/HelloShop.IdentityService/EntityFrameworks/EntityConfigurations/RoleEntityTypeConfiguration.cs b/src/HelloShop.IdentityService/EntityFrameworks/EntityConfigurations/RoleEntityTypeConfiguration.cs new file mode 100644 index 0000000..b042731 --- /dev/null +++ b/src/HelloShop.IdentityService/EntityFrameworks/EntityConfigurations/RoleEntityTypeConfiguration.cs @@ -0,0 +1,23 @@ +// Copyright (c) HelloShop Corporation. All rights reserved. +// See the license file in the project root for more information. + +using HelloShop.IdentityService.Entities; +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using Microsoft.EntityFrameworkCore; + +namespace HelloShop.IdentityService.EntityFrameworks.EntityConfigurations +{ + public class RoleEntityTypeConfiguration : IEntityTypeConfiguration + { + public void Configure(EntityTypeBuilder builder) + { + builder.ToTable("Roles"); + + builder.Property(r => r.Id).HasColumnOrder(1); + builder.Property(r => r.Name).HasMaxLength(16).HasColumnOrder(2); + builder.Property(r => r.NormalizedName).HasMaxLength(16).HasColumnOrder(3); + builder.Property(r => r.ConcurrencyStamp).HasMaxLength(64).HasColumnOrder(4); + builder.Property(r => r.CreationTime).HasColumnOrder(5); + } + } +} diff --git a/src/HelloShop.IdentityService/EntityFrameworks/EntityConfigurations/UserClaimEntityTypeConfiguration.cs b/src/HelloShop.IdentityService/EntityFrameworks/EntityConfigurations/UserClaimEntityTypeConfiguration.cs new file mode 100644 index 0000000..b6642cd --- /dev/null +++ b/src/HelloShop.IdentityService/EntityFrameworks/EntityConfigurations/UserClaimEntityTypeConfiguration.cs @@ -0,0 +1,19 @@ +// Copyright (c) HelloShop Corporation. All rights reserved. +// See the license file in the project root for more information. + +using Microsoft.AspNetCore.Identity; +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using Microsoft.EntityFrameworkCore; + +namespace HelloShop.IdentityService.EntityFrameworks.EntityConfigurations +{ + public class UserClaimEntityTypeConfiguration : IEntityTypeConfiguration> + { + public void Configure(EntityTypeBuilder> builder) + { + builder.ToTable("UserClaims"); + + builder.Property(uc => uc.ClaimType).HasMaxLength(128); + } + } +} diff --git a/src/HelloShop.IdentityService/EntityFrameworks/EntityConfigurations/UserEntityTypeConfiguration.cs b/src/HelloShop.IdentityService/EntityFrameworks/EntityConfigurations/UserEntityTypeConfiguration.cs index 3798553..7593740 100644 --- a/src/HelloShop.IdentityService/EntityFrameworks/EntityConfigurations/UserEntityTypeConfiguration.cs +++ b/src/HelloShop.IdentityService/EntityFrameworks/EntityConfigurations/UserEntityTypeConfiguration.cs @@ -10,11 +10,22 @@ namespace HelloShop.IdentityService.EntityFrameworks.EntityConfigurations { builder.ToTable("Users"); - builder.HasKey(u => u.Id); builder.Property(u => u.Id).HasColumnOrder(1); - - builder.Property(u => u.UserName).HasMaxLength(50).HasColumnOrder(3); - builder.Property(u => u.PasswordHash).HasMaxLength(100).HasColumnOrder(2); + builder.Property(u => u.UserName).HasMaxLength(16).HasColumnOrder(2); + builder.Property(u => u.NormalizedUserName).HasMaxLength(16).HasColumnOrder(3); + builder.Property(u => u.Email).HasMaxLength(32).HasColumnOrder(4); + builder.Property(u => u.NormalizedEmail).HasMaxLength(32).HasColumnOrder(5); + builder.Property(u => u.EmailConfirmed).HasColumnOrder(6); + builder.Property(u => u.PasswordHash).HasMaxLength(512).HasColumnOrder(7); + builder.Property(u => u.SecurityStamp).HasMaxLength(32).HasColumnOrder(8); + builder.Property(u => u.ConcurrencyStamp).HasMaxLength(64).HasColumnOrder(9); + builder.Property(u => u.PhoneNumber).HasMaxLength(16).HasColumnOrder(10); + builder.Property(u => u.PhoneNumberConfirmed).HasColumnOrder(11); + builder.Property(u => u.TwoFactorEnabled).HasColumnOrder(12); + builder.Property(u => u.LockoutEnd).HasColumnOrder(13); + builder.Property(u => u.LockoutEnabled).HasColumnOrder(14); + builder.Property(u => u.AccessFailedCount).HasColumnOrder(15); + builder.Property(u => u.CreationTime).HasColumnOrder(16); } } } diff --git a/src/HelloShop.IdentityService/EntityFrameworks/EntityConfigurations/UserLoginEntityTypeConfiguration.cs b/src/HelloShop.IdentityService/EntityFrameworks/EntityConfigurations/UserLoginEntityTypeConfiguration.cs new file mode 100644 index 0000000..7d4b6ea --- /dev/null +++ b/src/HelloShop.IdentityService/EntityFrameworks/EntityConfigurations/UserLoginEntityTypeConfiguration.cs @@ -0,0 +1,20 @@ +// Copyright (c) HelloShop Corporation. All rights reserved. +// See the license file in the project root for more information. + +using Microsoft.AspNetCore.Identity; +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using Microsoft.EntityFrameworkCore; + +namespace HelloShop.IdentityService.EntityFrameworks.EntityConfigurations +{ + public class UserLoginEntityTypeConfiguration : IEntityTypeConfiguration> + { + public void Configure(EntityTypeBuilder> builder) + { + builder.ToTable("UserLogins"); + + builder.Property(ul => ul.LoginProvider).HasMaxLength(16); + builder.Property(ul => ul.ProviderDisplayName).HasMaxLength(16); + } + } +} diff --git a/src/HelloShop.IdentityService/EntityFrameworks/EntityConfigurations/UserRoleEntityTypeConfiguration.cs b/src/HelloShop.IdentityService/EntityFrameworks/EntityConfigurations/UserRoleEntityTypeConfiguration.cs new file mode 100644 index 0000000..16ad132 --- /dev/null +++ b/src/HelloShop.IdentityService/EntityFrameworks/EntityConfigurations/UserRoleEntityTypeConfiguration.cs @@ -0,0 +1,16 @@ +// Copyright (c) HelloShop Corporation. All rights reserved. +// See the license file in the project root for more information. + +using Microsoft.AspNetCore.Identity; +using Microsoft.EntityFrameworkCore; + +namespace HelloShop.IdentityService.EntityFrameworks.EntityConfigurations +{ + public class UserRoleEntityTypeConfiguration : IEntityTypeConfiguration> + { + public void Configure(Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder> builder) + { + builder.ToTable("UserRoles"); + } + } +} diff --git a/src/HelloShop.IdentityService/EntityFrameworks/EntityConfigurations/UserTokenEntityTypeConfiguration.cs b/src/HelloShop.IdentityService/EntityFrameworks/EntityConfigurations/UserTokenEntityTypeConfiguration.cs new file mode 100644 index 0000000..0c45fff --- /dev/null +++ b/src/HelloShop.IdentityService/EntityFrameworks/EntityConfigurations/UserTokenEntityTypeConfiguration.cs @@ -0,0 +1,20 @@ +// Copyright (c) HelloShop Corporation. All rights reserved. +// See the license file in the project root for more information. + +using Microsoft.AspNetCore.Identity; +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using Microsoft.EntityFrameworkCore; + +namespace HelloShop.IdentityService.EntityFrameworks.EntityConfigurations +{ + public class UserTokenEntityTypeConfiguration : IEntityTypeConfiguration> + { + public void Configure(EntityTypeBuilder> builder) + { + builder.ToTable("UserTokens"); + + builder.Property(ut => ut.Name).HasMaxLength(16); + builder.Property(ut => ut.LoginProvider).HasMaxLength(16); + } + } +} diff --git a/src/HelloShop.IdentityService/EntityFrameworks/IdentityServiceDbContext.cs b/src/HelloShop.IdentityService/EntityFrameworks/IdentityServiceDbContext.cs index ee5925c..0882521 100644 --- a/src/HelloShop.IdentityService/EntityFrameworks/IdentityServiceDbContext.cs +++ b/src/HelloShop.IdentityService/EntityFrameworks/IdentityServiceDbContext.cs @@ -1,9 +1,11 @@ -using Microsoft.EntityFrameworkCore; +using HelloShop.IdentityService.Entities; +using Microsoft.AspNetCore.Identity.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; using System.Reflection; namespace HelloShop.IdentityService.EntityFrameworks { - public class IdentityServiceDbContext(DbContextOptions options) : DbContext(options) + public class IdentityServiceDbContext(DbContextOptions options) : IdentityDbContext(options) { protected override void OnModelCreating(ModelBuilder modelBuilder) { diff --git a/src/HelloShop.IdentityService/EntityFrameworks/Migrations/20240302004354_InitialCreate.Designer.cs b/src/HelloShop.IdentityService/EntityFrameworks/Migrations/20240302004354_InitialCreate.Designer.cs deleted file mode 100644 index d62939c..0000000 --- a/src/HelloShop.IdentityService/EntityFrameworks/Migrations/20240302004354_InitialCreate.Designer.cs +++ /dev/null @@ -1,59 +0,0 @@ -// -using System; -using HelloShop.IdentityService.EntityFrameworks; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; -using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; - -#nullable disable - -namespace HelloShop.IdentityService.EntityFrameworks.Migrations -{ - [DbContext(typeof(IdentityServiceDbContext))] - [Migration("20240302004354_InitialCreate")] - partial class InitialCreate - { - /// - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .HasAnnotation("ProductVersion", "8.0.2") - .HasAnnotation("Relational:MaxIdentifierLength", 63); - - NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); - - modelBuilder.Entity("HelloShop.IdentityService.Entities.User", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("integer") - .HasColumnOrder(1); - - NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); - - b.Property("CreationTime") - .HasColumnType("timestamp with time zone"); - - b.Property("PasswordHash") - .IsRequired() - .HasMaxLength(100) - .HasColumnType("character varying(100)") - .HasColumnOrder(2); - - b.Property("UserName") - .IsRequired() - .HasMaxLength(50) - .HasColumnType("character varying(50)") - .HasColumnOrder(3); - - b.HasKey("Id"); - - b.ToTable("Users", (string)null); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/src/HelloShop.IdentityService/EntityFrameworks/Migrations/20240302004354_InitialCreate.cs b/src/HelloShop.IdentityService/EntityFrameworks/Migrations/20240302004354_InitialCreate.cs deleted file mode 100644 index 8186ecb..0000000 --- a/src/HelloShop.IdentityService/EntityFrameworks/Migrations/20240302004354_InitialCreate.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore.Migrations; -using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; - -#nullable disable - -namespace HelloShop.IdentityService.EntityFrameworks.Migrations -{ - /// - public partial class InitialCreate : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "Users", - columns: table => new - { - Id = table.Column(type: "integer", nullable: false) - .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), - PasswordHash = table.Column(type: "character varying(100)", maxLength: 100, nullable: false), - UserName = table.Column(type: "character varying(50)", maxLength: 50, nullable: false), - CreationTime = table.Column(type: "timestamp with time zone", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Users", x => x.Id); - }); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "Users"); - } - } -} diff --git a/src/HelloShop.IdentityService/EntityFrameworks/Migrations/20240307112633_InitialCreate.Designer.cs b/src/HelloShop.IdentityService/EntityFrameworks/Migrations/20240307112633_InitialCreate.Designer.cs new file mode 100644 index 0000000..5b72bdd --- /dev/null +++ b/src/HelloShop.IdentityService/EntityFrameworks/Migrations/20240307112633_InitialCreate.Designer.cs @@ -0,0 +1,318 @@ +// +using System; +using HelloShop.IdentityService.EntityFrameworks; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace HelloShop.IdentityService.EntityFrameworks.Migrations +{ + [DbContext(typeof(IdentityServiceDbContext))] + [Migration("20240307112633_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.2") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("HelloShop.IdentityService.Entities.Role", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasColumnOrder(1); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasMaxLength(64) + .HasColumnType("character varying(64)") + .HasColumnOrder(4); + + b.Property("CreationTime") + .HasColumnType("timestamp with time zone") + .HasColumnOrder(5); + + b.Property("Name") + .HasMaxLength(16) + .HasColumnType("character varying(16)") + .HasColumnOrder(2); + + b.Property("NormalizedName") + .HasMaxLength(16) + .HasColumnType("character varying(16)") + .HasColumnOrder(3); + + b.HasKey("Id"); + + b.HasIndex("NormalizedName") + .IsUnique() + .HasDatabaseName("RoleNameIndex"); + + b.ToTable("Roles", (string)null); + }); + + modelBuilder.Entity("HelloShop.IdentityService.Entities.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasColumnOrder(1); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("AccessFailedCount") + .HasColumnType("integer") + .HasColumnOrder(15); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasMaxLength(64) + .HasColumnType("character varying(64)") + .HasColumnOrder(9); + + b.Property("CreationTime") + .HasColumnType("timestamp with time zone") + .HasColumnOrder(16); + + b.Property("Email") + .HasMaxLength(32) + .HasColumnType("character varying(32)") + .HasColumnOrder(4); + + b.Property("EmailConfirmed") + .HasColumnType("boolean") + .HasColumnOrder(6); + + b.Property("LockoutEnabled") + .HasColumnType("boolean") + .HasColumnOrder(14); + + b.Property("LockoutEnd") + .HasColumnType("timestamp with time zone") + .HasColumnOrder(13); + + b.Property("NormalizedEmail") + .HasMaxLength(32) + .HasColumnType("character varying(32)") + .HasColumnOrder(5); + + b.Property("NormalizedUserName") + .HasMaxLength(16) + .HasColumnType("character varying(16)") + .HasColumnOrder(3); + + b.Property("PasswordHash") + .HasMaxLength(512) + .HasColumnType("character varying(512)") + .HasColumnOrder(7); + + b.Property("PhoneNumber") + .HasMaxLength(16) + .HasColumnType("character varying(16)") + .HasColumnOrder(10); + + b.Property("PhoneNumberConfirmed") + .HasColumnType("boolean") + .HasColumnOrder(11); + + b.Property("SecurityStamp") + .HasMaxLength(32) + .HasColumnType("character varying(32)") + .HasColumnOrder(8); + + b.Property("TwoFactorEnabled") + .HasColumnType("boolean") + .HasColumnOrder(12); + + b.Property("UserName") + .HasMaxLength(16) + .HasColumnType("character varying(16)") + .HasColumnOrder(2); + + b.HasKey("Id"); + + b.HasIndex("NormalizedEmail") + .HasDatabaseName("EmailIndex"); + + b.HasIndex("NormalizedUserName") + .IsUnique() + .HasDatabaseName("UserNameIndex"); + + b.ToTable("Users", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ClaimType") + .HasMaxLength(128) + .HasColumnType("character varying(128)"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("RoleId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.ToTable("RoleClaims", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ClaimType") + .HasMaxLength(128) + .HasColumnType("character varying(128)"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("UserClaims", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.Property("LoginProvider") + .HasMaxLength(16) + .HasColumnType("character varying(16)"); + + b.Property("ProviderKey") + .HasColumnType("text"); + + b.Property("ProviderDisplayName") + .HasMaxLength(16) + .HasColumnType("character varying(16)"); + + b.Property("UserId") + .HasColumnType("integer"); + + b.HasKey("LoginProvider", "ProviderKey"); + + b.HasIndex("UserId"); + + b.ToTable("UserLogins", (string)null); + }); + + 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("UserRoles", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.Property("UserId") + .HasColumnType("integer"); + + b.Property("LoginProvider") + .HasMaxLength(16) + .HasColumnType("character varying(16)"); + + b.Property("Name") + .HasMaxLength(16) + .HasColumnType("character varying(16)"); + + b.Property("Value") + .HasColumnType("text"); + + b.HasKey("UserId", "LoginProvider", "Name"); + + b.ToTable("UserTokens", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.HasOne("HelloShop.IdentityService.Entities.Role", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.HasOne("HelloShop.IdentityService.Entities.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.HasOne("HelloShop.IdentityService.Entities.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.HasOne("HelloShop.IdentityService.Entities.Role", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("HelloShop.IdentityService.Entities.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.HasOne("HelloShop.IdentityService.Entities.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/HelloShop.IdentityService/EntityFrameworks/Migrations/20240307112633_InitialCreate.cs b/src/HelloShop.IdentityService/EntityFrameworks/Migrations/20240307112633_InitialCreate.cs new file mode 100644 index 0000000..2e55a16 --- /dev/null +++ b/src/HelloShop.IdentityService/EntityFrameworks/Migrations/20240307112633_InitialCreate.cs @@ -0,0 +1,227 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace HelloShop.IdentityService.EntityFrameworks.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Roles", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + Name = table.Column(type: "character varying(16)", maxLength: 16, nullable: true), + NormalizedName = table.Column(type: "character varying(16)", maxLength: 16, nullable: true), + ConcurrencyStamp = table.Column(type: "character varying(64)", maxLength: 64, nullable: true), + CreationTime = table.Column(type: "timestamp with time zone", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Roles", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Users", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + UserName = table.Column(type: "character varying(16)", maxLength: 16, nullable: true), + NormalizedUserName = table.Column(type: "character varying(16)", maxLength: 16, nullable: true), + Email = table.Column(type: "character varying(32)", maxLength: 32, nullable: true), + NormalizedEmail = table.Column(type: "character varying(32)", maxLength: 32, nullable: true), + EmailConfirmed = table.Column(type: "boolean", nullable: false), + PasswordHash = table.Column(type: "character varying(512)", maxLength: 512, nullable: true), + SecurityStamp = table.Column(type: "character varying(32)", maxLength: 32, nullable: true), + ConcurrencyStamp = table.Column(type: "character varying(64)", maxLength: 64, nullable: true), + PhoneNumber = table.Column(type: "character varying(16)", maxLength: 16, nullable: true), + PhoneNumberConfirmed = table.Column(type: "boolean", nullable: false), + TwoFactorEnabled = table.Column(type: "boolean", nullable: false), + LockoutEnd = table.Column(type: "timestamp with time zone", nullable: true), + LockoutEnabled = table.Column(type: "boolean", nullable: false), + AccessFailedCount = table.Column(type: "integer", nullable: false), + CreationTime = table.Column(type: "timestamp with time zone", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Users", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "RoleClaims", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + RoleId = table.Column(type: "integer", nullable: false), + ClaimType = table.Column(type: "character varying(128)", maxLength: 128, nullable: true), + ClaimValue = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_RoleClaims", x => x.Id); + table.ForeignKey( + name: "FK_RoleClaims_Roles_RoleId", + column: x => x.RoleId, + principalTable: "Roles", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "UserClaims", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + UserId = table.Column(type: "integer", nullable: false), + ClaimType = table.Column(type: "character varying(128)", maxLength: 128, nullable: true), + ClaimValue = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_UserClaims", x => x.Id); + table.ForeignKey( + name: "FK_UserClaims_Users_UserId", + column: x => x.UserId, + principalTable: "Users", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "UserLogins", + columns: table => new + { + LoginProvider = table.Column(type: "character varying(16)", maxLength: 16, nullable: false), + ProviderKey = table.Column(type: "text", nullable: false), + ProviderDisplayName = table.Column(type: "character varying(16)", maxLength: 16, nullable: true), + UserId = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_UserLogins", x => new { x.LoginProvider, x.ProviderKey }); + table.ForeignKey( + name: "FK_UserLogins_Users_UserId", + column: x => x.UserId, + principalTable: "Users", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "UserRoles", + columns: table => new + { + UserId = table.Column(type: "integer", nullable: false), + RoleId = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_UserRoles", x => new { x.UserId, x.RoleId }); + table.ForeignKey( + name: "FK_UserRoles_Roles_RoleId", + column: x => x.RoleId, + principalTable: "Roles", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_UserRoles_Users_UserId", + column: x => x.UserId, + principalTable: "Users", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "UserTokens", + columns: table => new + { + UserId = table.Column(type: "integer", nullable: false), + LoginProvider = table.Column(type: "character varying(16)", maxLength: 16, nullable: false), + Name = table.Column(type: "character varying(16)", maxLength: 16, nullable: false), + Value = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_UserTokens", x => new { x.UserId, x.LoginProvider, x.Name }); + table.ForeignKey( + name: "FK_UserTokens_Users_UserId", + column: x => x.UserId, + principalTable: "Users", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_RoleClaims_RoleId", + table: "RoleClaims", + column: "RoleId"); + + migrationBuilder.CreateIndex( + name: "RoleNameIndex", + table: "Roles", + column: "NormalizedName", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_UserClaims_UserId", + table: "UserClaims", + column: "UserId"); + + migrationBuilder.CreateIndex( + name: "IX_UserLogins_UserId", + table: "UserLogins", + column: "UserId"); + + migrationBuilder.CreateIndex( + name: "IX_UserRoles_RoleId", + table: "UserRoles", + column: "RoleId"); + + migrationBuilder.CreateIndex( + name: "EmailIndex", + table: "Users", + column: "NormalizedEmail"); + + migrationBuilder.CreateIndex( + name: "UserNameIndex", + table: "Users", + column: "NormalizedUserName", + unique: true); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "RoleClaims"); + + migrationBuilder.DropTable( + name: "UserClaims"); + + migrationBuilder.DropTable( + name: "UserLogins"); + + migrationBuilder.DropTable( + name: "UserRoles"); + + migrationBuilder.DropTable( + name: "UserTokens"); + + migrationBuilder.DropTable( + name: "Roles"); + + migrationBuilder.DropTable( + name: "Users"); + } + } +} diff --git a/src/HelloShop.IdentityService/EntityFrameworks/Migrations/IdentityServiceDbContextModelSnapshot.cs b/src/HelloShop.IdentityService/EntityFrameworks/Migrations/IdentityServiceDbContextModelSnapshot.cs index d28c2ee..b7c373d 100644 --- a/src/HelloShop.IdentityService/EntityFrameworks/Migrations/IdentityServiceDbContextModelSnapshot.cs +++ b/src/HelloShop.IdentityService/EntityFrameworks/Migrations/IdentityServiceDbContextModelSnapshot.cs @@ -22,6 +22,44 @@ namespace HelloShop.IdentityService.EntityFrameworks.Migrations NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + modelBuilder.Entity("HelloShop.IdentityService.Entities.Role", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasColumnOrder(1); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasMaxLength(64) + .HasColumnType("character varying(64)") + .HasColumnOrder(4); + + b.Property("CreationTime") + .HasColumnType("timestamp with time zone") + .HasColumnOrder(5); + + b.Property("Name") + .HasMaxLength(16) + .HasColumnType("character varying(16)") + .HasColumnOrder(2); + + b.Property("NormalizedName") + .HasMaxLength(16) + .HasColumnType("character varying(16)") + .HasColumnOrder(3); + + b.HasKey("Id"); + + b.HasIndex("NormalizedName") + .IsUnique() + .HasDatabaseName("RoleNameIndex"); + + b.ToTable("Roles", (string)null); + }); + modelBuilder.Entity("HelloShop.IdentityService.Entities.User", b => { b.Property("Id") @@ -31,25 +69,246 @@ namespace HelloShop.IdentityService.EntityFrameworks.Migrations NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + b.Property("AccessFailedCount") + .HasColumnType("integer") + .HasColumnOrder(15); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasMaxLength(64) + .HasColumnType("character varying(64)") + .HasColumnOrder(9); + b.Property("CreationTime") - .HasColumnType("timestamp with time zone"); + .HasColumnType("timestamp with time zone") + .HasColumnOrder(16); + + b.Property("Email") + .HasMaxLength(32) + .HasColumnType("character varying(32)") + .HasColumnOrder(4); + + b.Property("EmailConfirmed") + .HasColumnType("boolean") + .HasColumnOrder(6); + + b.Property("LockoutEnabled") + .HasColumnType("boolean") + .HasColumnOrder(14); + + b.Property("LockoutEnd") + .HasColumnType("timestamp with time zone") + .HasColumnOrder(13); + + b.Property("NormalizedEmail") + .HasMaxLength(32) + .HasColumnType("character varying(32)") + .HasColumnOrder(5); + + b.Property("NormalizedUserName") + .HasMaxLength(16) + .HasColumnType("character varying(16)") + .HasColumnOrder(3); b.Property("PasswordHash") - .IsRequired() - .HasMaxLength(100) - .HasColumnType("character varying(100)") - .HasColumnOrder(2); + .HasMaxLength(512) + .HasColumnType("character varying(512)") + .HasColumnOrder(7); + + b.Property("PhoneNumber") + .HasMaxLength(16) + .HasColumnType("character varying(16)") + .HasColumnOrder(10); + + b.Property("PhoneNumberConfirmed") + .HasColumnType("boolean") + .HasColumnOrder(11); + + b.Property("SecurityStamp") + .HasMaxLength(32) + .HasColumnType("character varying(32)") + .HasColumnOrder(8); + + b.Property("TwoFactorEnabled") + .HasColumnType("boolean") + .HasColumnOrder(12); b.Property("UserName") - .IsRequired() - .HasMaxLength(50) - .HasColumnType("character varying(50)") - .HasColumnOrder(3); + .HasMaxLength(16) + .HasColumnType("character varying(16)") + .HasColumnOrder(2); b.HasKey("Id"); + b.HasIndex("NormalizedEmail") + .HasDatabaseName("EmailIndex"); + + b.HasIndex("NormalizedUserName") + .IsUnique() + .HasDatabaseName("UserNameIndex"); + b.ToTable("Users", (string)null); }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ClaimType") + .HasMaxLength(128) + .HasColumnType("character varying(128)"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("RoleId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.ToTable("RoleClaims", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ClaimType") + .HasMaxLength(128) + .HasColumnType("character varying(128)"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("UserClaims", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.Property("LoginProvider") + .HasMaxLength(16) + .HasColumnType("character varying(16)"); + + b.Property("ProviderKey") + .HasColumnType("text"); + + b.Property("ProviderDisplayName") + .HasMaxLength(16) + .HasColumnType("character varying(16)"); + + b.Property("UserId") + .HasColumnType("integer"); + + b.HasKey("LoginProvider", "ProviderKey"); + + b.HasIndex("UserId"); + + b.ToTable("UserLogins", (string)null); + }); + + 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("UserRoles", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.Property("UserId") + .HasColumnType("integer"); + + b.Property("LoginProvider") + .HasMaxLength(16) + .HasColumnType("character varying(16)"); + + b.Property("Name") + .HasMaxLength(16) + .HasColumnType("character varying(16)"); + + b.Property("Value") + .HasColumnType("text"); + + b.HasKey("UserId", "LoginProvider", "Name"); + + b.ToTable("UserTokens", (string)null); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.HasOne("HelloShop.IdentityService.Entities.Role", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.HasOne("HelloShop.IdentityService.Entities.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.HasOne("HelloShop.IdentityService.Entities.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.HasOne("HelloShop.IdentityService.Entities.Role", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("HelloShop.IdentityService.Entities.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.HasOne("HelloShop.IdentityService.Entities.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); #pragma warning restore 612, 618 } } diff --git a/src/HelloShop.IdentityService/HelloShop.IdentityService.csproj b/src/HelloShop.IdentityService/HelloShop.IdentityService.csproj index 43ed4c7..e689458 100644 --- a/src/HelloShop.IdentityService/HelloShop.IdentityService.csproj +++ b/src/HelloShop.IdentityService/HelloShop.IdentityService.csproj @@ -6,6 +6,7 @@ true + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/src/HelloShop.IdentityService/Program.cs b/src/HelloShop.IdentityService/Program.cs index 57e6b7f..5bb9bad 100644 --- a/src/HelloShop.IdentityService/Program.cs +++ b/src/HelloShop.IdentityService/Program.cs @@ -1,5 +1,6 @@ using HelloShop.IdentityService.Constants; using HelloShop.IdentityService.DataSeeding; +using HelloShop.IdentityService.Entities; using HelloShop.IdentityService.EntityFrameworks; using HelloShop.ServiceDefaults.Extensions; using Microsoft.EntityFrameworkCore; @@ -11,28 +12,30 @@ builder.AddServiceDefaults(); // Add services to the container. builder.Services.AddControllers(); -// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle -builder.Services.AddEndpointsApiExplorer(); -builder.Services.AddSwaggerGen(); builder.Services.AddDbContext(options => { options.UseNpgsql(builder.Configuration.GetConnectionString(DbConstants.ConnectionStringName)); }); +builder.Services.AddIdentityApiEndpoints(options => +{ + options.Password.RequireDigit = false; + options.Password.RequireLowercase = false; + options.Password.RequireUppercase = false; + options.Password.RequireNonAlphanumeric = false; + options.Password.RequiredLength = 5; + options.SignIn.RequireConfirmedAccount = false; +}).AddRoles().AddEntityFrameworkStores(); + builder.Services.AddDataSeedingProviders(); builder.Services.AddOpenApi(); var app = builder.Build(); -app.MapDefaultEndpoints(); +app.MapGroup("identity").MapIdentityApi(); -// Configure the HTTP request pipeline. -if (app.Environment.IsDevelopment()) -{ - app.UseSwagger(); - app.UseSwaggerUI(); -} +app.MapDefaultEndpoints(); app.UseHttpsRedirection(); diff --git a/src/HelloShop.ServiceDefaults/Extensions/OpenApiExtensions.cs b/src/HelloShop.ServiceDefaults/Extensions/OpenApiExtensions.cs index 531ed9a..9a54dde 100644 --- a/src/HelloShop.ServiceDefaults/Extensions/OpenApiExtensions.cs +++ b/src/HelloShop.ServiceDefaults/Extensions/OpenApiExtensions.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; +using Microsoft.OpenApi.Models; using Swashbuckle.AspNetCore.Swagger; using Swashbuckle.AspNetCore.SwaggerGen; using Swashbuckle.AspNetCore.SwaggerUI; @@ -22,6 +23,28 @@ namespace HelloShop.ServiceDefaults.Extensions services.AddEndpointsApiExplorer(); services.AddSwaggerGen(configureOptions); + services.Configure(options => + { + options.AddSecurityDefinition("bearerAuth", new OpenApiSecurityScheme + { + Type = SecuritySchemeType.Http, + Scheme = "bearer", + BearerFormat = "JWT", + Description = "JWT Authorization header using the Bearer scheme." + }); + + options.AddSecurityRequirement(new OpenApiSecurityRequirement + { + { + new OpenApiSecurityScheme + { + Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "bearerAuth" } + }, + Array.Empty() + } + }); + }); + return services; }