From c7d72be2d1eed9b7fbfe719752ff800653d8c089 Mon Sep 17 00:00:00 2001 From: ivarlovlie Date: Sun, 2 Aug 2020 00:04:06 +0200 Subject: change db, set cookie --- src/server/.idea/.idea.Dough.dir/.idea/.gitignore | 13 ++ .../.idea.Dough.dir/.idea/.idea.Dough.dir.iml | 8 ++ src/server/.idea/.idea.Dough.dir/.idea/.name | 1 + .../.idea/.idea.Dough.dir/.idea/encodings.xml | 4 + .../.idea/.idea.Dough.dir/.idea/indexLayout.xml | 8 ++ src/server/.idea/.idea.Dough.dir/.idea/misc.xml | 6 + src/server/.idea/.idea.Dough.dir/.idea/vcs.xml | 6 + src/server/.idea/.idea.Dough.dir/riderModule.iml | 7 + src/server/Controllers/AccountController.cs | 16 ++- src/server/Controllers/BaseController.cs | 1 - src/server/Dough.csproj | 30 ++--- .../Migrations/20200729090558_Initial.Designer.cs | 143 -------------------- src/server/Migrations/20200729090558_Initial.cs | 97 -------------- .../20200801205356_INITIAL_MIGRATION.Designer.cs | 144 +++++++++++++++++++++ .../Migrations/20200801205356_INITIAL_MIGRATION.cs | 97 ++++++++++++++ .../Migrations/MainDbContextModelSnapshot.cs | 71 +++++----- src/server/Models/Constants.cs | 7 + src/server/Models/DbSetOverrides.cs | 5 +- src/server/Models/Payloads/LoginPayload.cs | 8 ++ src/server/Startup.cs | 25 +++- 20 files changed, 393 insertions(+), 304 deletions(-) create mode 100644 src/server/.idea/.idea.Dough.dir/.idea/.gitignore create mode 100644 src/server/.idea/.idea.Dough.dir/.idea/.idea.Dough.dir.iml create mode 100644 src/server/.idea/.idea.Dough.dir/.idea/.name create mode 100644 src/server/.idea/.idea.Dough.dir/.idea/encodings.xml create mode 100644 src/server/.idea/.idea.Dough.dir/.idea/indexLayout.xml create mode 100644 src/server/.idea/.idea.Dough.dir/.idea/misc.xml create mode 100644 src/server/.idea/.idea.Dough.dir/.idea/vcs.xml create mode 100644 src/server/.idea/.idea.Dough.dir/riderModule.iml delete mode 100644 src/server/Migrations/20200729090558_Initial.Designer.cs delete mode 100644 src/server/Migrations/20200729090558_Initial.cs create mode 100644 src/server/Migrations/20200801205356_INITIAL_MIGRATION.Designer.cs create mode 100644 src/server/Migrations/20200801205356_INITIAL_MIGRATION.cs create mode 100644 src/server/Models/Constants.cs create mode 100644 src/server/Models/Payloads/LoginPayload.cs (limited to 'src/server') diff --git a/src/server/.idea/.idea.Dough.dir/.idea/.gitignore b/src/server/.idea/.idea.Dough.dir/.idea/.gitignore new file mode 100644 index 0000000..f810591 --- /dev/null +++ b/src/server/.idea/.idea.Dough.dir/.idea/.gitignore @@ -0,0 +1,13 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Rider ignored files +/contentModel.xml +/modules.xml +/projectSettingsUpdater.xml +/.idea.Dough.iml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/src/server/.idea/.idea.Dough.dir/.idea/.idea.Dough.dir.iml b/src/server/.idea/.idea.Dough.dir/.idea/.idea.Dough.dir.iml new file mode 100644 index 0000000..e6bc2ca --- /dev/null +++ b/src/server/.idea/.idea.Dough.dir/.idea/.idea.Dough.dir.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/src/server/.idea/.idea.Dough.dir/.idea/.name b/src/server/.idea/.idea.Dough.dir/.idea/.name new file mode 100644 index 0000000..4b5d0cd --- /dev/null +++ b/src/server/.idea/.idea.Dough.dir/.idea/.name @@ -0,0 +1 @@ +Dough \ No newline at end of file diff --git a/src/server/.idea/.idea.Dough.dir/.idea/encodings.xml b/src/server/.idea/.idea.Dough.dir/.idea/encodings.xml new file mode 100644 index 0000000..df87cf9 --- /dev/null +++ b/src/server/.idea/.idea.Dough.dir/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/src/server/.idea/.idea.Dough.dir/.idea/indexLayout.xml b/src/server/.idea/.idea.Dough.dir/.idea/indexLayout.xml new file mode 100644 index 0000000..27ba142 --- /dev/null +++ b/src/server/.idea/.idea.Dough.dir/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/src/server/.idea/.idea.Dough.dir/.idea/misc.xml b/src/server/.idea/.idea.Dough.dir/.idea/misc.xml new file mode 100644 index 0000000..28a804d --- /dev/null +++ b/src/server/.idea/.idea.Dough.dir/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/src/server/.idea/.idea.Dough.dir/.idea/vcs.xml b/src/server/.idea/.idea.Dough.dir/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/src/server/.idea/.idea.Dough.dir/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/server/.idea/.idea.Dough.dir/riderModule.iml b/src/server/.idea/.idea.Dough.dir/riderModule.iml new file mode 100644 index 0000000..1a4e0d9 --- /dev/null +++ b/src/server/.idea/.idea.Dough.dir/riderModule.iml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/src/server/Controllers/AccountController.cs b/src/server/Controllers/AccountController.cs index 58bb7b6..ee87a68 100644 --- a/src/server/Controllers/AccountController.cs +++ b/src/server/Controllers/AccountController.cs @@ -8,6 +8,7 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Dough.Models; using Dough.Models.Database; +using Dough.Models.Payloads; using Dough.Models.Results; using Dough.Utilities; @@ -23,24 +24,25 @@ namespace Dough.Controllers } [HttpPost("login")] - public async Task Login(string username, string password) + public async Task Login(LoginPayload payload) { - var user = _context.Users.SingleByNameOrDefault(username); + var user = _context.Users.SingleByNameOrDefault(payload.Username); if (user == default) return BadRequest(new ErrorResult("Ugyldig brukernavn eller passord", "Verifiser at passord og brukernavn er riktig og prøv igjen")); - if (!user.VerifyPassword(password)) + if (!user.VerifyPassword(payload.Password)) return BadRequest(new ErrorResult("Ugyldig brukernavn eller passord", "Verifiser at passord og brukernavn er riktig")); var claims = new List { new Claim(ClaimTypes.Name, user.Username), - new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()) + new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()), + new Claim(ClaimTypes.AuthenticationInstant, DateTime.UtcNow.ToString("O")) }; - var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); + var claimsIdentity = new ClaimsIdentity(claims, Constants.AuthenticationScheme); var claimsPrincipal = new ClaimsPrincipal(claimsIdentity); var authenticationProperties = new AuthenticationProperties @@ -51,7 +53,7 @@ namespace Dough.Controllers ExpiresUtc = DateTime.UtcNow.AddDays(7), }; - await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, + await HttpContext.SignInAsync(Constants.AuthenticationScheme, claimsPrincipal, authenticationProperties); @@ -61,7 +63,7 @@ namespace Dough.Controllers [HttpGet("logout")] public async Task Logout(string continueTo = default) { - await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); + await HttpContext.SignOutAsync(Constants.AuthenticationScheme); if (continueTo.IsPresent() && continueTo.IsValidUrl()) return Redirect(continueTo); return Ok(); } diff --git a/src/server/Controllers/BaseController.cs b/src/server/Controllers/BaseController.cs index 33f1e4b..046c060 100644 --- a/src/server/Controllers/BaseController.cs +++ b/src/server/Controllers/BaseController.cs @@ -1,7 +1,6 @@ using System; using System.Security.Claims; using Microsoft.AspNetCore.Mvc; -using Dough.Models.Database; using Dough.Utilities; namespace Dough.Controllers diff --git a/src/server/Dough.csproj b/src/server/Dough.csproj index ae3d56a..65911ed 100644 --- a/src/server/Dough.csproj +++ b/src/server/Dough.csproj @@ -1,16 +1,14 @@ - - - - netcoreapp3.1 - - - - - - - - - - - - + + + + netcoreapp3.1 + f009efdc-3998-4ef3-8617-b0a501f192cb + + + + + + + + + \ No newline at end of file diff --git a/src/server/Migrations/20200729090558_Initial.Designer.cs b/src/server/Migrations/20200729090558_Initial.Designer.cs deleted file mode 100644 index 9657f66..0000000 --- a/src/server/Migrations/20200729090558_Initial.Designer.cs +++ /dev/null @@ -1,143 +0,0 @@ -// -using System; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; -using Dough.Models.Database; - -namespace Dough.Migrations -{ - [DbContext(typeof(MainDbContext))] - [Migration("20200729090558_Initial")] - partial class Initial - { - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .HasAnnotation("ProductVersion", "3.1.6"); - - modelBuilder.Entity("MoneyManager.Models.Database.Category", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("TEXT"); - - b.Property("Created") - .HasColumnType("TEXT"); - - b.Property("CreatedBy") - .HasColumnType("TEXT"); - - b.Property("Hidden") - .HasColumnType("INTEGER"); - - b.Property("Name") - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.ToTable("Categories"); - }); - - modelBuilder.Entity("MoneyManager.Models.Database.Payee", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("TEXT"); - - b.Property("Created") - .HasColumnType("TEXT"); - - b.Property("CreatedBy") - .HasColumnType("TEXT"); - - b.Property("Hidden") - .HasColumnType("INTEGER"); - - b.Property("Name") - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.ToTable("Payees"); - }); - - modelBuilder.Entity("MoneyManager.Models.Database.Transaction", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("TEXT"); - - b.Property("Amount") - .HasColumnType("REAL"); - - b.Property("CategoryId") - .HasColumnType("TEXT"); - - b.Property("Created") - .HasColumnType("TEXT"); - - b.Property("CreatedBy") - .HasColumnType("TEXT"); - - b.Property("Date") - .HasColumnType("TEXT"); - - b.Property("Hidden") - .HasColumnType("INTEGER"); - - b.Property("Note") - .HasColumnType("TEXT"); - - b.Property("PayeeId") - .HasColumnType("TEXT"); - - b.Property("Tags") - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.ToTable("Transactions"); - }); - - modelBuilder.Entity("MoneyManager.Models.Database.User", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("TEXT"); - - b.Property("Created") - .HasColumnType("TEXT"); - - b.Property("CreatedBy") - .HasColumnType("TEXT"); - - b.Property("Hidden") - .HasColumnType("INTEGER"); - - b.Property("Password") - .HasColumnType("TEXT"); - - b.Property("Username") - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.ToTable("Users"); - - b.HasData( - new - { - Id = new Guid("193053d0-4292-4dc5-baae-59a920b64891"), - Created = new DateTime(2020, 7, 29, 9, 5, 57, 914, DateTimeKind.Utc).AddTicks(3427), - Hidden = false, - Password = "$2b$10$RFdcYLeqporq94pUIOoJGOPnhUbpV7R4e.2Iz8ot02N2PqeCpDCA6", - Username = "ivar" - }); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/src/server/Migrations/20200729090558_Initial.cs b/src/server/Migrations/20200729090558_Initial.cs deleted file mode 100644 index 5dc0b40..0000000 --- a/src/server/Migrations/20200729090558_Initial.cs +++ /dev/null @@ -1,97 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore.Migrations; - -namespace Dough.Migrations -{ - public partial class Initial : Migration - { - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "Categories", - columns: table => new - { - Id = table.Column(nullable: false), - Created = table.Column(nullable: false), - CreatedBy = table.Column(nullable: true), - Hidden = table.Column(nullable: false), - Name = table.Column(nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_Categories", x => x.Id); - }); - - migrationBuilder.CreateTable( - name: "Payees", - columns: table => new - { - Id = table.Column(nullable: false), - Created = table.Column(nullable: false), - CreatedBy = table.Column(nullable: true), - Hidden = table.Column(nullable: false), - Name = table.Column(nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_Payees", x => x.Id); - }); - - migrationBuilder.CreateTable( - name: "Transactions", - columns: table => new - { - Id = table.Column(nullable: false), - Created = table.Column(nullable: false), - CreatedBy = table.Column(nullable: true), - Hidden = table.Column(nullable: false), - Tags = table.Column(nullable: true), - Note = table.Column(nullable: true), - Date = table.Column(nullable: false), - Amount = table.Column(nullable: false), - PayeeId = table.Column(nullable: false), - CategoryId = table.Column(nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Transactions", x => x.Id); - }); - - migrationBuilder.CreateTable( - name: "Users", - columns: table => new - { - Id = table.Column(nullable: false), - Created = table.Column(nullable: false), - CreatedBy = table.Column(nullable: true), - Hidden = table.Column(nullable: false), - Password = table.Column(nullable: true), - Username = table.Column(nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_Users", x => x.Id); - }); - - migrationBuilder.InsertData( - table: "Users", - columns: new[] { "Id", "Created", "CreatedBy", "Hidden", "Password", "Username" }, - values: new object[] { new Guid("193053d0-4292-4dc5-baae-59a920b64891"), new DateTime(2020, 7, 29, 9, 5, 57, 914, DateTimeKind.Utc).AddTicks(3427), null, false, "$2b$10$RFdcYLeqporq94pUIOoJGOPnhUbpV7R4e.2Iz8ot02N2PqeCpDCA6", "ivar" }); - } - - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "Categories"); - - migrationBuilder.DropTable( - name: "Payees"); - - migrationBuilder.DropTable( - name: "Transactions"); - - migrationBuilder.DropTable( - name: "Users"); - } - } -} diff --git a/src/server/Migrations/20200801205356_INITIAL_MIGRATION.Designer.cs b/src/server/Migrations/20200801205356_INITIAL_MIGRATION.Designer.cs new file mode 100644 index 0000000..b693783 --- /dev/null +++ b/src/server/Migrations/20200801205356_INITIAL_MIGRATION.Designer.cs @@ -0,0 +1,144 @@ +// +using System; +using Dough.Models.Database; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +namespace Dough.Migrations +{ + [DbContext(typeof(MainDbContext))] + [Migration("20200801205356_INITIAL_MIGRATION")] + partial class INITIAL_MIGRATION + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "3.1.6") + .HasAnnotation("Relational:MaxIdentifierLength", 64); + + modelBuilder.Entity("Dough.Models.Database.Category", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)"); + + b.Property("Created") + .HasColumnType("datetime(6)"); + + b.Property("CreatedBy") + .HasColumnType("char(36)"); + + b.Property("Hidden") + .HasColumnType("tinyint(1)"); + + b.Property("Name") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.HasKey("Id"); + + b.ToTable("Categories"); + }); + + modelBuilder.Entity("Dough.Models.Database.Payee", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)"); + + b.Property("Created") + .HasColumnType("datetime(6)"); + + b.Property("CreatedBy") + .HasColumnType("char(36)"); + + b.Property("Hidden") + .HasColumnType("tinyint(1)"); + + b.Property("Name") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.HasKey("Id"); + + b.ToTable("Payees"); + }); + + modelBuilder.Entity("Dough.Models.Database.Transaction", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)"); + + b.Property("Amount") + .HasColumnType("double"); + + b.Property("CategoryId") + .HasColumnType("char(36)"); + + b.Property("Created") + .HasColumnType("datetime(6)"); + + b.Property("CreatedBy") + .HasColumnType("char(36)"); + + b.Property("Date") + .HasColumnType("datetime(6)"); + + b.Property("Hidden") + .HasColumnType("tinyint(1)"); + + b.Property("Note") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("PayeeId") + .HasColumnType("char(36)"); + + b.Property("Tags") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.HasKey("Id"); + + b.ToTable("Transactions"); + }); + + modelBuilder.Entity("Dough.Models.Database.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)"); + + b.Property("Created") + .HasColumnType("datetime(6)"); + + b.Property("CreatedBy") + .HasColumnType("char(36)"); + + b.Property("Hidden") + .HasColumnType("tinyint(1)"); + + b.Property("Password") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.Property("Username") + .HasColumnType("longtext CHARACTER SET utf8mb4"); + + b.HasKey("Id"); + + b.ToTable("Users"); + + b.HasData( + new + { + Id = new Guid("4fb39ecf-9619-42be-8852-81c48a9a9f72"), + Created = new DateTime(2020, 8, 1, 20, 53, 56, 576, DateTimeKind.Utc).AddTicks(931), + Hidden = false, + Password = "$2b$10$v7e7AJc0atQvyvvwkvXAnuin0m75qRZSxcSAHdDiKcSHuByJpc0zy", + Username = "ivar" + }); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/server/Migrations/20200801205356_INITIAL_MIGRATION.cs b/src/server/Migrations/20200801205356_INITIAL_MIGRATION.cs new file mode 100644 index 0000000..f6495ae --- /dev/null +++ b/src/server/Migrations/20200801205356_INITIAL_MIGRATION.cs @@ -0,0 +1,97 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +namespace Dough.Migrations +{ + public partial class INITIAL_MIGRATION : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Categories", + columns: table => new + { + Id = table.Column(nullable: false), + Created = table.Column(nullable: false), + CreatedBy = table.Column(nullable: true), + Hidden = table.Column(nullable: false), + Name = table.Column(nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Categories", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Payees", + columns: table => new + { + Id = table.Column(nullable: false), + Created = table.Column(nullable: false), + CreatedBy = table.Column(nullable: true), + Hidden = table.Column(nullable: false), + Name = table.Column(nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Payees", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Transactions", + columns: table => new + { + Id = table.Column(nullable: false), + Created = table.Column(nullable: false), + CreatedBy = table.Column(nullable: true), + Hidden = table.Column(nullable: false), + Tags = table.Column(nullable: true), + Note = table.Column(nullable: true), + Date = table.Column(nullable: false), + Amount = table.Column(nullable: false), + PayeeId = table.Column(nullable: false), + CategoryId = table.Column(nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Transactions", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Users", + columns: table => new + { + Id = table.Column(nullable: false), + Created = table.Column(nullable: false), + CreatedBy = table.Column(nullable: true), + Hidden = table.Column(nullable: false), + Password = table.Column(nullable: true), + Username = table.Column(nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Users", x => x.Id); + }); + + migrationBuilder.InsertData( + table: "Users", + columns: new[] { "Id", "Created", "CreatedBy", "Hidden", "Password", "Username" }, + values: new object[] { new Guid("4fb39ecf-9619-42be-8852-81c48a9a9f72"), new DateTime(2020, 8, 1, 20, 53, 56, 576, DateTimeKind.Utc).AddTicks(931), null, false, "$2b$10$v7e7AJc0atQvyvvwkvXAnuin0m75qRZSxcSAHdDiKcSHuByJpc0zy", "ivar" }); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Categories"); + + migrationBuilder.DropTable( + name: "Payees"); + + migrationBuilder.DropTable( + name: "Transactions"); + + migrationBuilder.DropTable( + name: "Users"); + } + } +} diff --git a/src/server/Migrations/MainDbContextModelSnapshot.cs b/src/server/Migrations/MainDbContextModelSnapshot.cs index ad883c5..d4c6e7e 100644 --- a/src/server/Migrations/MainDbContextModelSnapshot.cs +++ b/src/server/Migrations/MainDbContextModelSnapshot.cs @@ -1,9 +1,9 @@ // using System; +using Dough.Models.Database; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; -using Dough.Models.Database; namespace Dough.Migrations { @@ -14,112 +14,113 @@ namespace Dough.Migrations { #pragma warning disable 612, 618 modelBuilder - .HasAnnotation("ProductVersion", "3.1.6"); + .HasAnnotation("ProductVersion", "3.1.6") + .HasAnnotation("Relational:MaxIdentifierLength", 64); - modelBuilder.Entity("MoneyManager.Models.Database.Category", b => + modelBuilder.Entity("Dough.Models.Database.Category", b => { b.Property("Id") .ValueGeneratedOnAdd() - .HasColumnType("TEXT"); + .HasColumnType("char(36)"); b.Property("Created") - .HasColumnType("TEXT"); + .HasColumnType("datetime(6)"); b.Property("CreatedBy") - .HasColumnType("TEXT"); + .HasColumnType("char(36)"); b.Property("Hidden") - .HasColumnType("INTEGER"); + .HasColumnType("tinyint(1)"); b.Property("Name") - .HasColumnType("TEXT"); + .HasColumnType("longtext CHARACTER SET utf8mb4"); b.HasKey("Id"); b.ToTable("Categories"); }); - modelBuilder.Entity("MoneyManager.Models.Database.Payee", b => + modelBuilder.Entity("Dough.Models.Database.Payee", b => { b.Property("Id") .ValueGeneratedOnAdd() - .HasColumnType("TEXT"); + .HasColumnType("char(36)"); b.Property("Created") - .HasColumnType("TEXT"); + .HasColumnType("datetime(6)"); b.Property("CreatedBy") - .HasColumnType("TEXT"); + .HasColumnType("char(36)"); b.Property("Hidden") - .HasColumnType("INTEGER"); + .HasColumnType("tinyint(1)"); b.Property("Name") - .HasColumnType("TEXT"); + .HasColumnType("longtext CHARACTER SET utf8mb4"); b.HasKey("Id"); b.ToTable("Payees"); }); - modelBuilder.Entity("MoneyManager.Models.Database.Transaction", b => + modelBuilder.Entity("Dough.Models.Database.Transaction", b => { b.Property("Id") .ValueGeneratedOnAdd() - .HasColumnType("TEXT"); + .HasColumnType("char(36)"); b.Property("Amount") - .HasColumnType("REAL"); + .HasColumnType("double"); b.Property("CategoryId") - .HasColumnType("TEXT"); + .HasColumnType("char(36)"); b.Property("Created") - .HasColumnType("TEXT"); + .HasColumnType("datetime(6)"); b.Property("CreatedBy") - .HasColumnType("TEXT"); + .HasColumnType("char(36)"); b.Property("Date") - .HasColumnType("TEXT"); + .HasColumnType("datetime(6)"); b.Property("Hidden") - .HasColumnType("INTEGER"); + .HasColumnType("tinyint(1)"); b.Property("Note") - .HasColumnType("TEXT"); + .HasColumnType("longtext CHARACTER SET utf8mb4"); b.Property("PayeeId") - .HasColumnType("TEXT"); + .HasColumnType("char(36)"); b.Property("Tags") - .HasColumnType("TEXT"); + .HasColumnType("longtext CHARACTER SET utf8mb4"); b.HasKey("Id"); b.ToTable("Transactions"); }); - modelBuilder.Entity("MoneyManager.Models.Database.User", b => + modelBuilder.Entity("Dough.Models.Database.User", b => { b.Property("Id") .ValueGeneratedOnAdd() - .HasColumnType("TEXT"); + .HasColumnType("char(36)"); b.Property("Created") - .HasColumnType("TEXT"); + .HasColumnType("datetime(6)"); b.Property("CreatedBy") - .HasColumnType("TEXT"); + .HasColumnType("char(36)"); b.Property("Hidden") - .HasColumnType("INTEGER"); + .HasColumnType("tinyint(1)"); b.Property("Password") - .HasColumnType("TEXT"); + .HasColumnType("longtext CHARACTER SET utf8mb4"); b.Property("Username") - .HasColumnType("TEXT"); + .HasColumnType("longtext CHARACTER SET utf8mb4"); b.HasKey("Id"); @@ -128,10 +129,10 @@ namespace Dough.Migrations b.HasData( new { - Id = new Guid("193053d0-4292-4dc5-baae-59a920b64891"), - Created = new DateTime(2020, 7, 29, 9, 5, 57, 914, DateTimeKind.Utc).AddTicks(3427), + Id = new Guid("4fb39ecf-9619-42be-8852-81c48a9a9f72"), + Created = new DateTime(2020, 8, 1, 20, 53, 56, 576, DateTimeKind.Utc).AddTicks(931), Hidden = false, - Password = "$2b$10$RFdcYLeqporq94pUIOoJGOPnhUbpV7R4e.2Iz8ot02N2PqeCpDCA6", + Password = "$2b$10$v7e7AJc0atQvyvvwkvXAnuin0m75qRZSxcSAHdDiKcSHuByJpc0zy", Username = "ivar" }); }); diff --git a/src/server/Models/Constants.cs b/src/server/Models/Constants.cs new file mode 100644 index 0000000..759030a --- /dev/null +++ b/src/server/Models/Constants.cs @@ -0,0 +1,7 @@ +namespace Dough.Models +{ + public class Constants + { + public const string AuthenticationScheme = "Cookies"; + } +} \ No newline at end of file diff --git a/src/server/Models/DbSetOverrides.cs b/src/server/Models/DbSetOverrides.cs index 24622bc..fbab76d 100644 --- a/src/server/Models/DbSetOverrides.cs +++ b/src/server/Models/DbSetOverrides.cs @@ -18,8 +18,9 @@ namespace Dough.Models public static User SingleByNameOrDefault(this DbSet users, string username, bool includeHidden = false) { if (includeHidden) - return users.SingleOrDefault(c => c.Username == username); - return users.SingleOrDefault(c => c.Username == username && !c.Hidden); + return users.SingleOrDefault(c => string.Equals(c.Username, username, StringComparison.InvariantCultureIgnoreCase)); + return users.SingleOrDefault(c => string.Equals(c.Username, username, StringComparison.InvariantCultureIgnoreCase) + && !c.Hidden); } } } diff --git a/src/server/Models/Payloads/LoginPayload.cs b/src/server/Models/Payloads/LoginPayload.cs new file mode 100644 index 0000000..d7bc50b --- /dev/null +++ b/src/server/Models/Payloads/LoginPayload.cs @@ -0,0 +1,8 @@ +namespace Dough.Models.Payloads +{ + public class LoginPayload + { + public string Username { get; set; } + public string Password { get; set; } + } +} \ No newline at end of file diff --git a/src/server/Startup.cs b/src/server/Startup.cs index ad98370..7c7adf0 100644 --- a/src/server/Startup.cs +++ b/src/server/Startup.cs @@ -8,6 +8,8 @@ using Microsoft.Extensions.Hosting; using Dough.Models; using Dough.Utilities; using Dough.Models.Database; +using Microsoft.AspNetCore.CookiePolicy; +using Microsoft.AspNetCore.Http; namespace Dough { @@ -21,6 +23,16 @@ namespace Dough public IConfiguration Configuration { get; } private const string MainCorsPolicy = "MainCorsPolicy"; + private string GetConnectionStringFromEnvironment() + { + var host = Configuration.GetValue("DB_HOST"); + var port = Configuration.GetValue("DB_PORT", "3306"); + var user = Configuration.GetValue("DB_USER"); + var password = Configuration.GetValue("DB_PASSWORD"); + var name = Configuration.GetValue("DB_NAME"); + return $"Server={host},{port};Database={name};User={user};Password={password}"; + } + public void ConfigureServices(IServiceCollection services) { @@ -37,14 +49,21 @@ namespace Dough }); services.AddDbContext(options => { - options.UseSqlite("Data Source=database.sqlite"); + options.UseMySql(GetConnectionStringFromEnvironment(), settings => + { + settings.EnableRetryOnFailure(3); + }); }); services.AddControllers(); - services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) - .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options => + services.AddAuthentication(Constants.AuthenticationScheme) + .AddCookie(Constants.AuthenticationScheme, options => { + options.Cookie.Name = "dough_session"; + options.Cookie.HttpOnly = true; + options.Cookie.SameSite = SameSiteMode.Strict; + options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest; options.LoginPath = "/api/account/login"; options.SlidingExpiration = true; options.LogoutPath = "/api/account/logout"; -- cgit v1.3