From 7091849e38206a4057c15901d2951f11dffa99ec Mon Sep 17 00:00:00 2001 From: code liturgy Date: Fri, 18 Nov 2022 00:33:03 +0000 Subject: [PATCH] strip jwt auth, no longer needed --- .../Application/Users/Auth/AuthManager.cs | 16 +- .../Users/Auth/Crypto/IJwtFactory.cs | 9 - .../Users/Auth/Crypto/IJwtTokenHandler.cs | 12 - .../Users/Auth/Crypto/JwtFactory.cs | 85 ---- .../Users/Auth/Crypto/JwtIssuerOptions.cs | 54 --- .../Users/Auth/Crypto/JwtTokenHandler.cs | 53 --- CodeLiturgy.Views/CodeLiturgy.Views.csproj | 20 +- .../Controllers/HomeController.cs | 3 - .../20221118003233_first.Designer.cs | 401 ++++++++++++++++++ .../Migrations/20221118003233_first.cs | 324 ++++++++++++++ .../ApplicationUserDbContextModelSnapshot.cs | 398 +++++++++++++++++ .../Models/ModelBuilderExtensions.cs | 42 -- CodeLiturgy.Views/StartupExtensions.cs | 60 +-- .../Views/Shared/_HeaderMenu.cshtml | 2 +- 14 files changed, 1143 insertions(+), 336 deletions(-) delete mode 100644 CodeLiturgy.Views/Application/Users/Auth/Crypto/IJwtFactory.cs delete mode 100644 CodeLiturgy.Views/Application/Users/Auth/Crypto/IJwtTokenHandler.cs delete mode 100644 CodeLiturgy.Views/Application/Users/Auth/Crypto/JwtFactory.cs delete mode 100644 CodeLiturgy.Views/Application/Users/Auth/Crypto/JwtIssuerOptions.cs delete mode 100644 CodeLiturgy.Views/Application/Users/Auth/Crypto/JwtTokenHandler.cs create mode 100644 CodeLiturgy.Views/Migrations/20221118003233_first.Designer.cs create mode 100644 CodeLiturgy.Views/Migrations/20221118003233_first.cs create mode 100644 CodeLiturgy.Views/Migrations/ApplicationUserDbContextModelSnapshot.cs diff --git a/CodeLiturgy.Views/Application/Users/Auth/AuthManager.cs b/CodeLiturgy.Views/Application/Users/Auth/AuthManager.cs index ad30fde..c60ee92 100644 --- a/CodeLiturgy.Views/Application/Users/Auth/AuthManager.cs +++ b/CodeLiturgy.Views/Application/Users/Auth/AuthManager.cs @@ -14,7 +14,6 @@ namespace CodeLiturgy.Data.Auth.Context.Users { private readonly ApplicationUserManager _userManager; private readonly IHasher _hasher; - private readonly IJwtFactory _jwtFactory; /// /// Auth manager constructor @@ -25,12 +24,10 @@ namespace CodeLiturgy.Data.Auth.Context.Users /// public AuthManager( ApplicationUserManager userManager, - IHasher hasher, - IJwtFactory jwtFactory) + IHasher hasher) { _userManager = userManager; _hasher = hasher; - _jwtFactory = jwtFactory; } @@ -41,17 +38,6 @@ namespace CodeLiturgy.Data.Auth.Context.Users } - - - private async Task<(bool, string)> GenerateBearerToken(ClaimsIdentity identity, ApplicationUser user) - { - var jwtToken = await _jwtFactory.GenerateEncodedToken(user.Id, user.UserName); - var completed = await _userManager.SetAuthenticationTokenAsync(user, SessionConstants.ApiNamePolicy, - SessionConstants.ApiNamePolicy, jwtToken.Token); - return (completed == IdentityResult.Success, jwtToken.Token); - } - - /// /// Verify Password /// diff --git a/CodeLiturgy.Views/Application/Users/Auth/Crypto/IJwtFactory.cs b/CodeLiturgy.Views/Application/Users/Auth/Crypto/IJwtFactory.cs deleted file mode 100644 index afefcab..0000000 --- a/CodeLiturgy.Views/Application/Users/Auth/Crypto/IJwtFactory.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System.Threading.Tasks; -using CodeLiturgy.Data.Application.Users; - -namespace CodeLiturgy.Data.Auth.Context.Users; - -public interface IJwtFactory -{ - Task GenerateEncodedToken(string id, string userName); -} diff --git a/CodeLiturgy.Views/Application/Users/Auth/Crypto/IJwtTokenHandler.cs b/CodeLiturgy.Views/Application/Users/Auth/Crypto/IJwtTokenHandler.cs deleted file mode 100644 index 9b63b1e..0000000 --- a/CodeLiturgy.Views/Application/Users/Auth/Crypto/IJwtTokenHandler.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System.IdentityModel.Tokens.Jwt; -using System.Security.Claims; -using Microsoft.IdentityModel.Tokens; - -namespace CodeLiturgy.Data.Auth.Context.Users -{ - public interface IJwtTokenHandler - { - string WriteToken(JwtSecurityToken jwt); - ClaimsPrincipal ValidateToken(string token, TokenValidationParameters tokenValidationParameters); - } -} \ No newline at end of file diff --git a/CodeLiturgy.Views/Application/Users/Auth/Crypto/JwtFactory.cs b/CodeLiturgy.Views/Application/Users/Auth/Crypto/JwtFactory.cs deleted file mode 100644 index 735bb7e..0000000 --- a/CodeLiturgy.Views/Application/Users/Auth/Crypto/JwtFactory.cs +++ /dev/null @@ -1,85 +0,0 @@ -using System; -using System.IdentityModel.Tokens.Jwt; -using System.Security.Claims; -using System.Security.Principal; -using CodeLiturgy.Data.Application.Users; -using Microsoft.Extensions.Options; -using static CodeLiturgy.Data.Auth.Context.Users.Constants; - -namespace CodeLiturgy.Data.Auth.Context.Users; - -public class JwtFactory : IJwtFactory -{ - private readonly IJwtTokenHandler _jwtTokenHandler; - private readonly JwtIssuerOptions _jwtOptions; - - public JwtFactory(IJwtTokenHandler jwtTokenHandler, IOptions jwtOptions) - { - _jwtTokenHandler = jwtTokenHandler; - _jwtOptions = jwtOptions.Value; - ThrowIfInvalidOptions(_jwtOptions); - } - - public async Task GenerateEncodedToken(string id, string userName) - { - var identity = GenerateClaimsIdentity(id, userName); - - var claims = new[] - { - new Claim(JwtRegisteredClaimNames.Sub, userName), - new Claim(JwtRegisteredClaimNames.Jti, await _jwtOptions.JtiGenerator()), - new Claim(JwtRegisteredClaimNames.Aud, _jwtOptions.Audience), - - new Claim(JwtRegisteredClaimNames.Iat, ToUnixEpochDate(_jwtOptions.IssuedAt).ToString(), - ClaimValueTypes.Integer64), - identity.FindFirst(JwtClaimIdentifiers.Rol), - identity.FindFirst(JwtClaimIdentifiers.Id) - }; - - // Create the JWT security token and encode it. - var jwt = new JwtSecurityToken( - _jwtOptions.Issuer, - _jwtOptions.Audience, - claims, - _jwtOptions.NotBefore, - _jwtOptions.Expiration, - _jwtOptions.SigningCredentials); - - return new AccessToken(_jwtTokenHandler.WriteToken(jwt), (int)_jwtOptions.ValidFor.TotalSeconds); - } - - private static ClaimsIdentity GenerateClaimsIdentity(string id, string userName) - { - return new ClaimsIdentity(new GenericIdentity(userName, "Token"), new[] - { - new Claim(JwtClaimIdentifiers.Id, id), - new Claim(JwtClaimIdentifiers.Rol, JwtClaims.ApiAccess) - }); - } - - /// Date converted to seconds since Unix epoch (Jan 1, 1970, midnight UTC). - private static long ToUnixEpochDate(DateTime date) - => (long)Math.Round((date.ToUniversalTime() - - new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero)) - .TotalSeconds); - - private static void ThrowIfInvalidOptions(JwtIssuerOptions options) - { - if (options == null) throw new ArgumentNullException(nameof(options)); - - if (options.ValidFor <= TimeSpan.Zero) - { - throw new ArgumentException("Must be a non-zero TimeSpan.", nameof(JwtIssuerOptions.ValidFor)); - } - - if (options.SigningCredentials == null) - { - throw new ArgumentNullException(nameof(JwtIssuerOptions.SigningCredentials)); - } - - if (options.JtiGenerator == null) - { - throw new ArgumentNullException(nameof(JwtIssuerOptions.JtiGenerator)); - } - } -} \ No newline at end of file diff --git a/CodeLiturgy.Views/Application/Users/Auth/Crypto/JwtIssuerOptions.cs b/CodeLiturgy.Views/Application/Users/Auth/Crypto/JwtIssuerOptions.cs deleted file mode 100644 index 16f3b89..0000000 --- a/CodeLiturgy.Views/Application/Users/Auth/Crypto/JwtIssuerOptions.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; -using System.Threading.Tasks; -using Microsoft.IdentityModel.Tokens; - -namespace CodeLiturgy.Data.Auth.Context.Users; - -public class JwtIssuerOptions -{ - /// - /// 4.1.1. "iss" (Issuer) Claim - The "iss" (issuer) claim identifies the principal that issued the JWT. - /// - public string Issuer { get; set; } - - /// - /// 4.1.2. "sub" (Subject) Claim - The "sub" (subject) claim identifies the principal that is the subject of the JWT. - /// - public string Subject { get; set; } - - /// - /// 4.1.3. "aud" (Audience) Claim - The "aud" (audience) claim identifies the recipients that the JWT is intended for. - /// - public string Audience { get; set; } - - /// - /// 4.1.4. "exp" (Expiration Time) Claim - The "exp" (expiration time) claim identifies the expiration time on or after which the JWT MUST NOT be accepted for processing. - /// - public DateTime Expiration => IssuedAt.Add(ValidFor); - - /// - /// 4.1.5. "nbf" (Not Before) Claim - The "nbf" (not before) claim identifies the time before which the JWT MUST NOT be accepted for processing. - /// - public DateTime NotBefore => DateTime.UtcNow; - - /// - /// 4.1.6. "iat" (Issued At) Claim - The "iat" (issued at) claim identifies the time at which the JWT was issued. - /// - public DateTime IssuedAt => DateTime.UtcNow; - - /// - /// Set the timespan the token will be valid for (default is 120 min) - /// - public TimeSpan ValidFor { get; set; } = SessionConstants.DefaultSessionMaxAge; - - /// - /// "jti" (JWT ID) Claim (default ID is a GUID) - /// - public Func> JtiGenerator => - () => Task.FromResult(Guid.NewGuid().ToString()); - - /// - /// The signing key to use when generating tokens. - /// - public SigningCredentials SigningCredentials { get; set; } -} \ No newline at end of file diff --git a/CodeLiturgy.Views/Application/Users/Auth/Crypto/JwtTokenHandler.cs b/CodeLiturgy.Views/Application/Users/Auth/Crypto/JwtTokenHandler.cs deleted file mode 100644 index e8e5adb..0000000 --- a/CodeLiturgy.Views/Application/Users/Auth/Crypto/JwtTokenHandler.cs +++ /dev/null @@ -1,53 +0,0 @@ -using System; -using System.IdentityModel.Tokens.Jwt; -using System.Security.Claims; -using Microsoft.IdentityModel.Tokens; - -namespace CodeLiturgy.Data.Auth.Context.Users; - -public class JwtTokenHandler : IJwtTokenHandler -{ - private readonly JwtSecurityTokenHandler _jwtSecurityTokenHandler; - - /// - /// JwtTokenHandler - /// - public JwtTokenHandler() - { - _jwtSecurityTokenHandler = new JwtSecurityTokenHandler(); - } - - /// - /// Write token - /// - /// - /// - public string WriteToken(JwtSecurityToken jwt) - { - return _jwtSecurityTokenHandler.WriteToken(jwt); - } - - /// - /// Validate Token - /// - /// - /// - /// - /// - public ClaimsPrincipal ValidateToken(string token, TokenValidationParameters tokenValidationParameters) - { - try - { - var principal = _jwtSecurityTokenHandler.ValidateToken(token, tokenValidationParameters, out var securityToken); - - if (!(securityToken is JwtSecurityToken jwtSecurityToken) || !jwtSecurityToken.Header.Alg.Equals(SecurityAlgorithms.HmacSha256, StringComparison.InvariantCultureIgnoreCase)) - throw new SecurityTokenException("Invalid token"); - - return principal; - } - catch (Exception e) - { - return null; - } - } -} \ No newline at end of file diff --git a/CodeLiturgy.Views/CodeLiturgy.Views.csproj b/CodeLiturgy.Views/CodeLiturgy.Views.csproj index dcba0e7..dc811ed 100644 --- a/CodeLiturgy.Views/CodeLiturgy.Views.csproj +++ b/CodeLiturgy.Views/CodeLiturgy.Views.csproj @@ -11,17 +11,18 @@ + + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive + - - - - - <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\css\bootstrap-grid.css" /> <_ContentIncludedByDefault Remove="wwwroot\lib\bootstrap\dist\css\bootstrap-grid.css.map" /> @@ -112,7 +113,6 @@ - @@ -134,6 +134,14 @@ + + + ..\..\..\..\..\.nuget\packages\duende.identityserver\6.0.4\lib\net6.0\Duende.IdentityServer.dll + + + ..\..\..\..\..\.nuget\packages\microsoft.aspnetcore.identity.entityframeworkcore\7.0.0\lib\net7.0\Microsoft.AspNetCore.Identity.EntityFrameworkCore.dll + + diff --git a/CodeLiturgy.Views/Controllers/HomeController.cs b/CodeLiturgy.Views/Controllers/HomeController.cs index c082f67..cf7efcb 100644 --- a/CodeLiturgy.Views/Controllers/HomeController.cs +++ b/CodeLiturgy.Views/Controllers/HomeController.cs @@ -3,9 +3,6 @@ using Microsoft.AspNetCore.Mvc; using CodeLiturgy.Views.Utils; using Duende.IdentityServer.Extensions; using Microsoft.AspNetCore.Authorization; -using Microsoft.Extensions.Options; -using Controller = Microsoft.AspNetCore.Mvc.Controller; - namespace CodeLiturgy.Views.Controllers; [System.Web.Mvc.Route("/")] diff --git a/CodeLiturgy.Views/Migrations/20221118003233_first.Designer.cs b/CodeLiturgy.Views/Migrations/20221118003233_first.Designer.cs new file mode 100644 index 0000000..861caf1 --- /dev/null +++ b/CodeLiturgy.Views/Migrations/20221118003233_first.Designer.cs @@ -0,0 +1,401 @@ +// +using System; +using CodeLiturgy.Domain; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace CodeLiturgy.Views.Migrations +{ + [DbContext(typeof(ApplicationUserDbContext))] + [Migration("20221118003233_first")] + partial class first + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.0") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("CodeLiturgy.Data.Application.Site", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("text"); + + b.Property("CreatedDate") + .HasColumnType("timestamp with time zone"); + + b.Property("Domain") + .HasColumnType("text"); + + b.Property("EnvironmentId") + .HasColumnType("text"); + + b.Property("EnvironmentType") + .HasColumnType("integer"); + + b.Property("LastChanged") + .HasColumnType("timestamp with time zone"); + + b.Property("UrlAddress") + .HasColumnType("text"); + + b.HasKey("Id"); + + b.HasIndex("EnvironmentId"); + + b.ToTable("Site"); + }); + + modelBuilder.Entity("CodeLiturgy.Data.Application.SiteEnvironment", b => + { + b.Property("Id") + .HasColumnType("text"); + + b.Property("Name") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("text"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("site_env", (string)null); + }); + + modelBuilder.Entity("CodeLiturgy.Data.Application.Users.ApplicationRole", b => + { + b.Property("Id") + .HasColumnType("text"); + + 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", (string)null); + }); + + modelBuilder.Entity("CodeLiturgy.Data.Application.Users.ApplicationRoleClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ApplicationRoleId") + .HasColumnType("text"); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("RoleId") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.HasIndex("ApplicationRoleId"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetRoleClaims", (string)null); + }); + + modelBuilder.Entity("CodeLiturgy.Data.Application.Users.ApplicationUser", b => + { + b.Property("Id") + .HasColumnType("text"); + + 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("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("SecurityStamp") + .HasColumnType("text"); + + b.Property("TwoFactorEnabled") + .HasColumnType("boolean"); + + b.Property("UserName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedEmail") + .HasDatabaseName("EmailIndex"); + + b.HasIndex("NormalizedUserName") + .IsUnique() + .HasDatabaseName("UserNameIndex"); + + b.ToTable("AspNetUsers", (string)null); + }); + + modelBuilder.Entity("CodeLiturgy.Data.Application.Users.ApplicationUserClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ApplicationUserId") + .HasColumnType("text"); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("UserId") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.HasIndex("ApplicationUserId"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserClaims", (string)null); + }); + + modelBuilder.Entity("CodeLiturgy.Data.Application.Users.ApplicationUserLogin", b => + { + b.Property("LoginProvider") + .HasColumnType("text"); + + b.Property("ProviderKey") + .HasColumnType("text"); + + b.Property("ProviderDisplayName") + .HasColumnType("text"); + + b.Property("UserId") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("LoginProvider", "ProviderKey"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserLogins", (string)null); + }); + + modelBuilder.Entity("CodeLiturgy.Data.Application.Users.ApplicationUserRole", b => + { + b.Property("UserId") + .HasColumnType("text"); + + b.Property("RoleId") + .HasColumnType("text"); + + b.Property("ApplicationRoleId") + .HasColumnType("text"); + + b.Property("UserId1") + .HasColumnType("text"); + + b.HasKey("UserId", "RoleId"); + + b.HasIndex("ApplicationRoleId"); + + b.HasIndex("RoleId"); + + b.HasIndex("UserId1"); + + b.ToTable("AspNetUserRoles", (string)null); + }); + + modelBuilder.Entity("CodeLiturgy.Data.Application.Users.ApplicationUserToken", b => + { + b.Property("UserId") + .HasColumnType("text"); + + b.Property("LoginProvider") + .HasColumnType("text"); + + b.Property("Name") + .HasColumnType("text"); + + b.Property("Id") + .HasColumnType("text"); + + b.Property("Value") + .HasColumnType("text"); + + b.HasKey("UserId", "LoginProvider", "Name"); + + b.ToTable("AspNetUserTokens", (string)null); + }); + + modelBuilder.Entity("CodeLiturgy.Data.Application.Site", b => + { + b.HasOne("CodeLiturgy.Data.Application.SiteEnvironment", "Environment") + .WithMany("Sites") + .HasForeignKey("EnvironmentId"); + + b.Navigation("Environment"); + }); + + modelBuilder.Entity("CodeLiturgy.Data.Application.SiteEnvironment", b => + { + b.HasOne("CodeLiturgy.Data.Application.Users.ApplicationUser", "User") + .WithMany("Environments") + .HasForeignKey("UserId"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("CodeLiturgy.Data.Application.Users.ApplicationRoleClaim", b => + { + b.HasOne("CodeLiturgy.Data.Application.Users.ApplicationRole", "ApplicationRole") + .WithMany() + .HasForeignKey("ApplicationRoleId"); + + b.HasOne("CodeLiturgy.Data.Application.Users.ApplicationRole", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("ApplicationRole"); + }); + + modelBuilder.Entity("CodeLiturgy.Data.Application.Users.ApplicationUserClaim", b => + { + b.HasOne("CodeLiturgy.Data.Application.Users.ApplicationUser", "ApplicationUser") + .WithMany() + .HasForeignKey("ApplicationUserId"); + + b.HasOne("CodeLiturgy.Data.Application.Users.ApplicationUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("ApplicationUser"); + }); + + modelBuilder.Entity("CodeLiturgy.Data.Application.Users.ApplicationUserLogin", b => + { + b.HasOne("CodeLiturgy.Data.Application.Users.ApplicationUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("CodeLiturgy.Data.Application.Users.ApplicationUserRole", b => + { + b.HasOne("CodeLiturgy.Data.Application.Users.ApplicationRole", "ApplicationRole") + .WithMany() + .HasForeignKey("ApplicationRoleId"); + + b.HasOne("CodeLiturgy.Data.Application.Users.ApplicationRole", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("CodeLiturgy.Data.Application.Users.ApplicationUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("CodeLiturgy.Data.Application.Users.ApplicationUser", "User") + .WithMany() + .HasForeignKey("UserId1"); + + b.Navigation("ApplicationRole"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("CodeLiturgy.Data.Application.Users.ApplicationUserToken", b => + { + b.HasOne("CodeLiturgy.Data.Application.Users.ApplicationUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("CodeLiturgy.Data.Application.SiteEnvironment", b => + { + b.Navigation("Sites"); + }); + + modelBuilder.Entity("CodeLiturgy.Data.Application.Users.ApplicationUser", b => + { + b.Navigation("Environments"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/CodeLiturgy.Views/Migrations/20221118003233_first.cs b/CodeLiturgy.Views/Migrations/20221118003233_first.cs new file mode 100644 index 0000000..e4af06c --- /dev/null +++ b/CodeLiturgy.Views/Migrations/20221118003233_first.cs @@ -0,0 +1,324 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace CodeLiturgy.Views.Migrations +{ + /// + public partial class first : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "AspNetRoles", + columns: table => new + { + Id = table.Column(type: "text", nullable: false), + Name = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), + NormalizedName = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), + ConcurrencyStamp = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetRoles", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "AspNetUsers", + columns: table => new + { + Id = table.Column(type: "text", nullable: false), + UserName = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), + NormalizedUserName = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), + Email = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), + NormalizedEmail = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), + EmailConfirmed = table.Column(type: "boolean", nullable: false), + PasswordHash = table.Column(type: "text", nullable: true), + SecurityStamp = table.Column(type: "text", nullable: true), + ConcurrencyStamp = table.Column(type: "text", nullable: true), + PhoneNumber = table.Column(type: "text", 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) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetUsers", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "AspNetRoleClaims", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + RoleId = table.Column(type: "text", nullable: false), + ApplicationRoleId = table.Column(type: "text", nullable: true), + ClaimType = table.Column(type: "text", nullable: true), + ClaimValue = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetRoleClaims", x => x.Id); + table.ForeignKey( + name: "FK_AspNetRoleClaims_AspNetRoles_ApplicationRoleId", + column: x => x.ApplicationRoleId, + principalTable: "AspNetRoles", + principalColumn: "Id"); + table.ForeignKey( + name: "FK_AspNetRoleClaims_AspNetRoles_RoleId", + column: x => x.RoleId, + principalTable: "AspNetRoles", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "AspNetUserClaims", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + UserId = table.Column(type: "text", nullable: false), + ApplicationUserId = table.Column(type: "text", nullable: true), + ClaimType = table.Column(type: "text", nullable: true), + ClaimValue = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetUserClaims", x => x.Id); + table.ForeignKey( + name: "FK_AspNetUserClaims_AspNetUsers_ApplicationUserId", + column: x => x.ApplicationUserId, + principalTable: "AspNetUsers", + principalColumn: "Id"); + table.ForeignKey( + name: "FK_AspNetUserClaims_AspNetUsers_UserId", + column: x => x.UserId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "AspNetUserLogins", + columns: table => new + { + LoginProvider = table.Column(type: "text", nullable: false), + ProviderKey = table.Column(type: "text", nullable: false), + ProviderDisplayName = table.Column(type: "text", nullable: true), + UserId = table.Column(type: "text", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetUserLogins", x => new { x.LoginProvider, x.ProviderKey }); + table.ForeignKey( + name: "FK_AspNetUserLogins_AspNetUsers_UserId", + column: x => x.UserId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "AspNetUserRoles", + columns: table => new + { + UserId = table.Column(type: "text", nullable: false), + RoleId = table.Column(type: "text", nullable: false), + UserId1 = table.Column(type: "text", nullable: true), + ApplicationRoleId = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetUserRoles", x => new { x.UserId, x.RoleId }); + table.ForeignKey( + name: "FK_AspNetUserRoles_AspNetRoles_ApplicationRoleId", + column: x => x.ApplicationRoleId, + principalTable: "AspNetRoles", + principalColumn: "Id"); + table.ForeignKey( + name: "FK_AspNetUserRoles_AspNetRoles_RoleId", + column: x => x.RoleId, + principalTable: "AspNetRoles", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_AspNetUserRoles_AspNetUsers_UserId", + column: x => x.UserId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_AspNetUserRoles_AspNetUsers_UserId1", + column: x => x.UserId1, + principalTable: "AspNetUsers", + principalColumn: "Id"); + }); + + migrationBuilder.CreateTable( + name: "AspNetUserTokens", + columns: table => new + { + UserId = table.Column(type: "text", nullable: false), + LoginProvider = table.Column(type: "text", nullable: false), + Name = table.Column(type: "text", nullable: false), + Id = table.Column(type: "text", nullable: true), + Value = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetUserTokens", x => new { x.UserId, x.LoginProvider, x.Name }); + table.ForeignKey( + name: "FK_AspNetUserTokens_AspNetUsers_UserId", + column: x => x.UserId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "site_env", + columns: table => new + { + Id = table.Column(type: "text", nullable: false), + Name = table.Column(type: "text", nullable: true), + UserId = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_site_env", x => x.Id); + table.ForeignKey( + name: "FK_site_env_AspNetUsers_UserId", + column: x => x.UserId, + principalTable: "AspNetUsers", + principalColumn: "Id"); + }); + + migrationBuilder.CreateTable( + name: "Site", + columns: table => new + { + Id = table.Column(type: "text", nullable: false), + EnvironmentId = table.Column(type: "text", nullable: true), + Domain = table.Column(type: "text", nullable: true), + UrlAddress = table.Column(type: "text", nullable: true), + EnvironmentType = table.Column(type: "integer", nullable: false), + CreatedDate = table.Column(type: "timestamp with time zone", nullable: false), + LastChanged = table.Column(type: "timestamp with time zone", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Site", x => x.Id); + table.ForeignKey( + name: "FK_Site_site_env_EnvironmentId", + column: x => x.EnvironmentId, + principalTable: "site_env", + principalColumn: "Id"); + }); + + migrationBuilder.CreateIndex( + name: "IX_AspNetRoleClaims_ApplicationRoleId", + table: "AspNetRoleClaims", + column: "ApplicationRoleId"); + + migrationBuilder.CreateIndex( + name: "IX_AspNetRoleClaims_RoleId", + table: "AspNetRoleClaims", + column: "RoleId"); + + migrationBuilder.CreateIndex( + name: "RoleNameIndex", + table: "AspNetRoles", + column: "NormalizedName", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_AspNetUserClaims_ApplicationUserId", + table: "AspNetUserClaims", + column: "ApplicationUserId"); + + migrationBuilder.CreateIndex( + name: "IX_AspNetUserClaims_UserId", + table: "AspNetUserClaims", + column: "UserId"); + + migrationBuilder.CreateIndex( + name: "IX_AspNetUserLogins_UserId", + table: "AspNetUserLogins", + column: "UserId"); + + migrationBuilder.CreateIndex( + name: "IX_AspNetUserRoles_ApplicationRoleId", + table: "AspNetUserRoles", + column: "ApplicationRoleId"); + + migrationBuilder.CreateIndex( + name: "IX_AspNetUserRoles_RoleId", + table: "AspNetUserRoles", + column: "RoleId"); + + migrationBuilder.CreateIndex( + name: "IX_AspNetUserRoles_UserId1", + table: "AspNetUserRoles", + column: "UserId1"); + + migrationBuilder.CreateIndex( + name: "EmailIndex", + table: "AspNetUsers", + column: "NormalizedEmail"); + + migrationBuilder.CreateIndex( + name: "UserNameIndex", + table: "AspNetUsers", + column: "NormalizedUserName", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_Site_EnvironmentId", + table: "Site", + column: "EnvironmentId"); + + migrationBuilder.CreateIndex( + name: "IX_site_env_UserId", + table: "site_env", + column: "UserId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "AspNetRoleClaims"); + + migrationBuilder.DropTable( + name: "AspNetUserClaims"); + + migrationBuilder.DropTable( + name: "AspNetUserLogins"); + + migrationBuilder.DropTable( + name: "AspNetUserRoles"); + + migrationBuilder.DropTable( + name: "AspNetUserTokens"); + + migrationBuilder.DropTable( + name: "Site"); + + migrationBuilder.DropTable( + name: "AspNetRoles"); + + migrationBuilder.DropTable( + name: "site_env"); + + migrationBuilder.DropTable( + name: "AspNetUsers"); + } + } +} diff --git a/CodeLiturgy.Views/Migrations/ApplicationUserDbContextModelSnapshot.cs b/CodeLiturgy.Views/Migrations/ApplicationUserDbContextModelSnapshot.cs new file mode 100644 index 0000000..257d26c --- /dev/null +++ b/CodeLiturgy.Views/Migrations/ApplicationUserDbContextModelSnapshot.cs @@ -0,0 +1,398 @@ +// +using System; +using CodeLiturgy.Domain; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace CodeLiturgy.Views.Migrations +{ + [DbContext(typeof(ApplicationUserDbContext))] + partial class ApplicationUserDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.0") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("CodeLiturgy.Data.Application.Site", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("text"); + + b.Property("CreatedDate") + .HasColumnType("timestamp with time zone"); + + b.Property("Domain") + .HasColumnType("text"); + + b.Property("EnvironmentId") + .HasColumnType("text"); + + b.Property("EnvironmentType") + .HasColumnType("integer"); + + b.Property("LastChanged") + .HasColumnType("timestamp with time zone"); + + b.Property("UrlAddress") + .HasColumnType("text"); + + b.HasKey("Id"); + + b.HasIndex("EnvironmentId"); + + b.ToTable("Site"); + }); + + modelBuilder.Entity("CodeLiturgy.Data.Application.SiteEnvironment", b => + { + b.Property("Id") + .HasColumnType("text"); + + b.Property("Name") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("text"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("site_env", (string)null); + }); + + modelBuilder.Entity("CodeLiturgy.Data.Application.Users.ApplicationRole", b => + { + b.Property("Id") + .HasColumnType("text"); + + 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", (string)null); + }); + + modelBuilder.Entity("CodeLiturgy.Data.Application.Users.ApplicationRoleClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ApplicationRoleId") + .HasColumnType("text"); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("RoleId") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.HasIndex("ApplicationRoleId"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetRoleClaims", (string)null); + }); + + modelBuilder.Entity("CodeLiturgy.Data.Application.Users.ApplicationUser", b => + { + b.Property("Id") + .HasColumnType("text"); + + 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("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("SecurityStamp") + .HasColumnType("text"); + + b.Property("TwoFactorEnabled") + .HasColumnType("boolean"); + + b.Property("UserName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedEmail") + .HasDatabaseName("EmailIndex"); + + b.HasIndex("NormalizedUserName") + .IsUnique() + .HasDatabaseName("UserNameIndex"); + + b.ToTable("AspNetUsers", (string)null); + }); + + modelBuilder.Entity("CodeLiturgy.Data.Application.Users.ApplicationUserClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ApplicationUserId") + .HasColumnType("text"); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("UserId") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.HasIndex("ApplicationUserId"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserClaims", (string)null); + }); + + modelBuilder.Entity("CodeLiturgy.Data.Application.Users.ApplicationUserLogin", b => + { + b.Property("LoginProvider") + .HasColumnType("text"); + + b.Property("ProviderKey") + .HasColumnType("text"); + + b.Property("ProviderDisplayName") + .HasColumnType("text"); + + b.Property("UserId") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("LoginProvider", "ProviderKey"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserLogins", (string)null); + }); + + modelBuilder.Entity("CodeLiturgy.Data.Application.Users.ApplicationUserRole", b => + { + b.Property("UserId") + .HasColumnType("text"); + + b.Property("RoleId") + .HasColumnType("text"); + + b.Property("ApplicationRoleId") + .HasColumnType("text"); + + b.Property("UserId1") + .HasColumnType("text"); + + b.HasKey("UserId", "RoleId"); + + b.HasIndex("ApplicationRoleId"); + + b.HasIndex("RoleId"); + + b.HasIndex("UserId1"); + + b.ToTable("AspNetUserRoles", (string)null); + }); + + modelBuilder.Entity("CodeLiturgy.Data.Application.Users.ApplicationUserToken", b => + { + b.Property("UserId") + .HasColumnType("text"); + + b.Property("LoginProvider") + .HasColumnType("text"); + + b.Property("Name") + .HasColumnType("text"); + + b.Property("Id") + .HasColumnType("text"); + + b.Property("Value") + .HasColumnType("text"); + + b.HasKey("UserId", "LoginProvider", "Name"); + + b.ToTable("AspNetUserTokens", (string)null); + }); + + modelBuilder.Entity("CodeLiturgy.Data.Application.Site", b => + { + b.HasOne("CodeLiturgy.Data.Application.SiteEnvironment", "Environment") + .WithMany("Sites") + .HasForeignKey("EnvironmentId"); + + b.Navigation("Environment"); + }); + + modelBuilder.Entity("CodeLiturgy.Data.Application.SiteEnvironment", b => + { + b.HasOne("CodeLiturgy.Data.Application.Users.ApplicationUser", "User") + .WithMany("Environments") + .HasForeignKey("UserId"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("CodeLiturgy.Data.Application.Users.ApplicationRoleClaim", b => + { + b.HasOne("CodeLiturgy.Data.Application.Users.ApplicationRole", "ApplicationRole") + .WithMany() + .HasForeignKey("ApplicationRoleId"); + + b.HasOne("CodeLiturgy.Data.Application.Users.ApplicationRole", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("ApplicationRole"); + }); + + modelBuilder.Entity("CodeLiturgy.Data.Application.Users.ApplicationUserClaim", b => + { + b.HasOne("CodeLiturgy.Data.Application.Users.ApplicationUser", "ApplicationUser") + .WithMany() + .HasForeignKey("ApplicationUserId"); + + b.HasOne("CodeLiturgy.Data.Application.Users.ApplicationUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("ApplicationUser"); + }); + + modelBuilder.Entity("CodeLiturgy.Data.Application.Users.ApplicationUserLogin", b => + { + b.HasOne("CodeLiturgy.Data.Application.Users.ApplicationUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("CodeLiturgy.Data.Application.Users.ApplicationUserRole", b => + { + b.HasOne("CodeLiturgy.Data.Application.Users.ApplicationRole", "ApplicationRole") + .WithMany() + .HasForeignKey("ApplicationRoleId"); + + b.HasOne("CodeLiturgy.Data.Application.Users.ApplicationRole", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("CodeLiturgy.Data.Application.Users.ApplicationUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("CodeLiturgy.Data.Application.Users.ApplicationUser", "User") + .WithMany() + .HasForeignKey("UserId1"); + + b.Navigation("ApplicationRole"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("CodeLiturgy.Data.Application.Users.ApplicationUserToken", b => + { + b.HasOne("CodeLiturgy.Data.Application.Users.ApplicationUser", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("CodeLiturgy.Data.Application.SiteEnvironment", b => + { + b.Navigation("Sites"); + }); + + modelBuilder.Entity("CodeLiturgy.Data.Application.Users.ApplicationUser", b => + { + b.Navigation("Environments"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/CodeLiturgy.Views/Models/ModelBuilderExtensions.cs b/CodeLiturgy.Views/Models/ModelBuilderExtensions.cs index 29d4396..1782c7c 100644 --- a/CodeLiturgy.Views/Models/ModelBuilderExtensions.cs +++ b/CodeLiturgy.Views/Models/ModelBuilderExtensions.cs @@ -41,48 +41,6 @@ namespace CodeLiturgy.Domain.Model /// private static ModelBuilder ConfigureAppContextModel(this ModelBuilder builder) { - builder.Entity(b => - { - b.HasMany() - .WithOne(b => b.User) - .HasForeignKey(ur => ur.UserId).IsRequired(); - }); - - - builder.Entity() - .HasKey(x => x.Id); - - - builder.Entity(b => - { - b.HasKey(r => r.Id); - b.HasIndex(r => r.NormalizedName).HasDatabaseName("RoleNameIndex").IsUnique(); - b.ToTable("roles"); - b.Property(r => r.ConcurrencyStamp).IsConcurrencyToken(); - - b.Property(u => u.Name).HasMaxLength(256); - b.Property(u => u.NormalizedName).HasMaxLength(256); - - b.HasMany().WithOne().HasForeignKey(ur => ur.RoleId).IsRequired(); - b.HasMany().WithOne().HasForeignKey(rc => rc.RoleId).IsRequired(); - }); - - builder.Entity().HasOne(x => x.ApplicationRole); - builder.Entity().HasOne(x => x.ApplicationRole); - builder.Entity().HasOne(x => x.ApplicationUser); - - - builder.Entity(b => - { - b.HasKey(rc => rc.Id); - }); - - builder.Entity(b => - { - b.HasKey(r => new {r.UserId, r.RoleId}); - }); - - return builder; //.ConfigureIdentityModel(); diff --git a/CodeLiturgy.Views/StartupExtensions.cs b/CodeLiturgy.Views/StartupExtensions.cs index c0eedf3..200c527 100644 --- a/CodeLiturgy.Views/StartupExtensions.cs +++ b/CodeLiturgy.Views/StartupExtensions.cs @@ -7,10 +7,8 @@ using CodeLiturgy.Domain; using BlueWest.WebApi.Context.Users; using CodeLiturgy.Views.Utils; using Microsoft.AspNetCore.Authentication.Cookies; -using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; -using Microsoft.IdentityModel.Tokens; namespace CodeLiturgy.Views; @@ -47,8 +45,6 @@ public static class StartupExtensions }); services - .AddScoped() - .AddScoped() .AddScoped() .AddScoped() .AddScoped() @@ -60,70 +56,22 @@ public static class StartupExtensions // Register the ConfigurationBuilder instance of AuthSettings var authSettings = configuration.GetSection(nameof(AuthSettings)); services.Configure(authSettings); - var signingKey = new SymmetricSecurityKey - (Encoding.ASCII.GetBytes(authSettings[nameof(AuthSettings.SecretKey)])); - // jwt wire up - // Get options from app settings - var jwtAppSettingOptions = configuration - .GetSection(nameof(JwtIssuerOptions)); - - // Configure JwtIssuerOptions - services.Configure(options => - { - options.Issuer = jwtAppSettingOptions[nameof(JwtIssuerOptions.Issuer)]; - options.Audience = jwtAppSettingOptions[nameof(JwtIssuerOptions.Audience)]; - options.SigningCredentials = new SigningCredentials - (signingKey, SecurityAlgorithms.HmacSha256); - }); - - var tokenValidationParameters = new TokenValidationParameters - { - ValidateIssuer = true, - ValidIssuer = jwtAppSettingOptions[nameof(JwtIssuerOptions.Issuer)], - - ValidateAudience = true, - ValidAudience = jwtAppSettingOptions[nameof(JwtIssuerOptions.Audience)], - - ValidateIssuerSigningKey = true, - IssuerSigningKey = signingKey, - - RequireExpirationTime = false, - ValidateLifetime = true, - ClockSkew = TimeSpan.Zero - }; services.AddAuthentication(options => { options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme; - options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; + options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme; options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; - options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; - options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; + options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme; + options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; }) .AddCookie(options => { options.LoginPath = Routes.AuthLoginRoute; options.LogoutPath = Routes.AuthLogoutRoute; - }) - .AddJwtBearer(configureOptions => - { - configureOptions.ClaimsIssuer = jwtAppSettingOptions[nameof(JwtIssuerOptions.Issuer)]; - configureOptions.TokenValidationParameters = tokenValidationParameters; - configureOptions.SaveToken = true; - configureOptions.Events = new JwtBearerEvents - { - OnAuthenticationFailed = context => - { - if (context.Exception.GetType() == typeof(SecurityTokenExpiredException)) - { - context.Response.Headers.Add("Token-Expired", "true"); - } - - return Task.CompletedTask; - }, - }; }); + // api user claim policy diff --git a/CodeLiturgy.Views/Views/Shared/_HeaderMenu.cshtml b/CodeLiturgy.Views/Views/Shared/_HeaderMenu.cshtml index 7edf133..149eb7b 100644 --- a/CodeLiturgy.Views/Views/Shared/_HeaderMenu.cshtml +++ b/CodeLiturgy.Views/Views/Shared/_HeaderMenu.cshtml @@ -1,6 +1,6 @@ @using CodeLiturgy.Views.Utils -@using Duende.IdentityServer.Extensions @using CodeLiturgy.Data.Auth +@using Duende.IdentityServer.Extensions @{ Layout = null; var userAuthenticated = User.IsAuthenticated();