Add IsParent to user, add authentication middleware

This commit is contained in:
ReiMerc 2023-12-18 20:11:06 +01:00
parent c104ef8a53
commit f0c1fe2457
8 changed files with 155 additions and 16 deletions

View File

@ -1,11 +1,10 @@
using MQTTnet.Client;
using Microsoft.EntityFrameworkCore;
namespace backend.Application
namespace backend.Application;
public static class ApplicationState
{
public static class ApplicationState
{
public static IMqttClient? MqttClient { get; set; }
public static DispenserContext? DbContext { get; set; }
}
}

View File

@ -4,6 +4,7 @@ using backend.Models;
using System.Text.Json.Nodes;
using Microsoft.AspNetCore.Identity;
using System.Web;
using backend.Middleware;
namespace backend.Controllers;
@ -37,6 +38,7 @@ public class UserController : ControllerBase
Username = input["username"]!.ToString(),
Password = hashedPassword,
TouchCode = touchCode,
IsParent = false,
};
// Save user
@ -87,13 +89,9 @@ public class UserController : ControllerBase
}
[HttpPost("Logout")]
[MiddlewareFilter(typeof(AuthenticationMiddlewareBuilder))]
public IActionResult LogOut()
{
// Validate
if (Request.Cookies["session"] == null) {
return BadRequest("You are not logged in");
}
// Get user
var user = ApplicationState.DbContext!.Users.FirstOrDefault(user => user.SessionToken == Request.Cookies["session"]!.ToString());
if (user == null) {
@ -110,13 +108,9 @@ public class UserController : ControllerBase
}
[HttpGet("UserInfo")]
[MiddlewareFilter(typeof(AuthenticationMiddlewareBuilder))]
public IActionResult UserInfo()
{
// Validate
if (Request.Cookies["session"] == null) {
return BadRequest("You are not logged in");
}
// Get user
var user = ApplicationState.DbContext!.Users.FirstOrDefault(user => user.SessionToken == Request.Cookies["session"]!.ToString());
if (user == null) {
@ -126,6 +120,7 @@ public class UserController : ControllerBase
var data = new {
username = user.Username,
touchCode = user.TouchCode,
isParent = user.IsParent,
};
return new JsonResult(data);

View File

@ -0,0 +1,42 @@
using backend.Application;
namespace backend.Middleware;
public class AuthenticationMiddleware
{
private readonly RequestDelegate _next;
public AuthenticationMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context)
{
if (context.Request.Cookies["session"] == null) {
context.Response.Clear();
context.Response.StatusCode = 401;
await context.Response.WriteAsync("You are not logged in");
return;
}
var user = ApplicationState.DbContext!.Users.FirstOrDefault(user => user.SessionToken == context.Request.Cookies["session"]!.ToString());
if (user == null) {
context.Response.Clear();
context.Response.StatusCode = 401;
await context.Response.WriteAsync("Invalid session token");
return;
}
await _next(context);
}
}
public class AuthenticationMiddlewareBuilder
{
public void Configure(IApplicationBuilder app)
{
app.UseMiddleware<AuthenticationMiddleware>();
}
}

View File

@ -0,0 +1,67 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace backend.Migrations
{
[DbContext(typeof(DispenserContext))]
[Migration("20231218184746_AddIsParentToUser")]
partial class AddIsParentToUser
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "8.0.0");
modelBuilder.Entity("backend.Models.DispenserLog", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<DateTime>("Timestamp")
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("DispenserLogs");
});
modelBuilder.Entity("backend.Models.User", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<bool>("IsParent")
.HasColumnType("INTEGER");
b.Property<string>("Password")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("SessionToken")
.HasColumnType("TEXT");
b.Property<string>("TouchCode")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Username")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Users");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,29 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace backend.Migrations
{
/// <inheritdoc />
public partial class AddIsParentToUser : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<bool>(
name: "IsParent",
table: "Users",
type: "INTEGER",
nullable: false,
defaultValue: false);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "IsParent",
table: "Users");
}
}
}

View File

@ -36,6 +36,9 @@ namespace backend.Migrations
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<bool>("IsParent")
.HasColumnType("INTEGER");
b.Property<string>("Password")
.IsRequired()
.HasColumnType("TEXT");

View File

@ -13,4 +13,5 @@ public class User
public string Password { get; set; }
public string TouchCode { get; set; }
public string? SessionToken { get; set; }
public bool IsParent { get; set; }
}

View File

@ -15,6 +15,9 @@ async function dispense() {
<h2>Welcome back, {{ userStore.state.userInfo.username }}</h2>
<br>
<button @click="dispense">Dispense the m&m</button>
<br><br>
<h3>Your touch code is: {{ userStore.state.userInfo.touchCode }}</h3>
<p>Use this code to login on the dispenser</p>
</template>
<template v-else>