Finished database management and user authentication

This commit is contained in:
2024-07-13 16:37:36 +02:00
parent fe5b5d28eb
commit c1ac7f9972
47 changed files with 1620 additions and 0 deletions

3
DatabaseTest/.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
obj
bin
appsettings.Development.json

View File

@@ -0,0 +1,9 @@
using HopFrame.Api.Controller;
using Microsoft.AspNetCore.Mvc;
namespace DatabaseTest.Controllers;
[ApiController]
public class TestController(DatabaseContext context) : SecurityController<DatabaseContext>(context) {
}

View File

@@ -0,0 +1,12 @@
using HopFrame.Database;
using Microsoft.EntityFrameworkCore;
namespace DatabaseTest;
public class DatabaseContext : HopDbContextBase {
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
base.OnConfiguring(optionsBuilder);
optionsBuilder.UseSqlite("Data Source=C:\\Users\\Remote\\Documents\\Projekte\\HopFrame\\DatabaseTest\\bin\\Debug\\net7.0\\test.db;Mode=ReadWrite;");
}
}

View File

@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.7">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.7" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\HopFrame.Api\HopFrame.Api.csproj" />
<ProjectReference Include="..\HopFrame.Security\HopFrame.Security.csproj" />
<ProjectReference Include="..\HopFrame.Database\HopFrame.Database.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,75 @@
// <auto-generated />
using System;
using DatabaseTest;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace DatabaseTest.Migrations
{
[DbContext(typeof(DatabaseContext))]
[Migration("20240712130909_Initial")]
partial class Initial
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "7.0.20");
modelBuilder.Entity("HopFrame.Database.Models.PermissionEntry", b =>
{
b.Property<long>("RecordId")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<DateTime>("GrantedAt")
.HasColumnType("TEXT");
b.Property<long>("OwnerId")
.HasColumnType("INTEGER");
b.Property<string>("PermissionText")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("TEXT");
b.HasKey("RecordId");
b.ToTable("Permissions");
});
modelBuilder.Entity("HopFrame.Database.Models.UserEntry", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<DateTime>("CreatedAt")
.HasColumnType("TEXT");
b.Property<string>("Email")
.IsRequired()
.HasMaxLength(50)
.HasColumnType("TEXT");
b.Property<string>("Password")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("TEXT");
b.Property<string>("Username")
.HasMaxLength(50)
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Users");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,56 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace DatabaseTest.Migrations
{
/// <inheritdoc />
public partial class Initial : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Permissions",
columns: table => new
{
RecordId = table.Column<long>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
PermissionText = table.Column<string>(type: "TEXT", maxLength: 255, nullable: false),
OwnerId = table.Column<long>(type: "INTEGER", nullable: false),
GrantedAt = table.Column<DateTime>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Permissions", x => x.RecordId);
});
migrationBuilder.CreateTable(
name: "Users",
columns: table => new
{
Id = table.Column<long>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Username = table.Column<string>(type: "TEXT", maxLength: 50, nullable: true),
Email = table.Column<string>(type: "TEXT", maxLength: 50, nullable: false),
Password = table.Column<string>(type: "TEXT", maxLength: 255, nullable: false),
CreatedAt = table.Column<DateTime>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Users", x => x.Id);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Permissions");
migrationBuilder.DropTable(
name: "Users");
}
}
}

View File

@@ -0,0 +1,101 @@
// <auto-generated />
using System;
using DatabaseTest;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace DatabaseTest.Migrations
{
[DbContext(typeof(DatabaseContext))]
[Migration("20240712143345_Tokens")]
partial class Tokens
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "7.0.20");
modelBuilder.Entity("HopFrame.Database.Models.Entries.PermissionEntry", b =>
{
b.Property<long>("RecordId")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<DateTime>("GrantedAt")
.HasColumnType("TEXT");
b.Property<long>("OwnerId")
.HasColumnType("INTEGER");
b.Property<string>("PermissionText")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("TEXT");
b.HasKey("RecordId");
b.ToTable("Permissions");
});
modelBuilder.Entity("HopFrame.Database.Models.Entries.UserEntry", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<DateTime>("CreatedAt")
.HasColumnType("TEXT");
b.Property<string>("Email")
.IsRequired()
.HasMaxLength(50)
.HasColumnType("TEXT");
b.Property<string>("Password")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("TEXT");
b.Property<string>("Username")
.HasMaxLength(50)
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Users");
});
modelBuilder.Entity("HopFrame.Security.Models.TokenEntry", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<DateTime>("CreatedAt")
.HasColumnType("TEXT");
b.Property<string>("Token")
.IsRequired()
.HasMaxLength(36)
.HasColumnType("TEXT");
b.Property<int>("Type")
.HasMaxLength(1)
.HasColumnType("INTEGER");
b.Property<long>("UserId")
.HasColumnType("INTEGER");
b.HasKey("Id");
b.ToTable("Tokens");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,38 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace DatabaseTest.Migrations
{
/// <inheritdoc />
public partial class Tokens : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Tokens",
columns: table => new
{
Id = table.Column<long>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Type = table.Column<int>(type: "INTEGER", maxLength: 1, nullable: false),
Token = table.Column<string>(type: "TEXT", maxLength: 36, nullable: false),
UserId = table.Column<long>(type: "INTEGER", nullable: false),
CreatedAt = table.Column<DateTime>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Tokens", x => x.Id);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Tokens");
}
}
}

View File

@@ -0,0 +1,100 @@
// <auto-generated />
using System;
using DatabaseTest;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace DatabaseTest.Migrations
{
[DbContext(typeof(DatabaseContext))]
[Migration("20240713083821_Security")]
partial class Security
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "8.0.7");
modelBuilder.Entity("HopFrame.Database.Models.Entries.PermissionEntry", b =>
{
b.Property<long>("RecordId")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<DateTime>("GrantedAt")
.HasColumnType("TEXT");
b.Property<string>("PermissionText")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("TEXT");
b.Property<string>("UserId")
.IsRequired()
.HasMaxLength(36)
.HasColumnType("TEXT");
b.HasKey("RecordId");
b.ToTable("Permissions");
});
modelBuilder.Entity("HopFrame.Database.Models.Entries.TokenEntry", b =>
{
b.Property<string>("Token")
.HasMaxLength(36)
.HasColumnType("TEXT");
b.Property<DateTime>("CreatedAt")
.HasColumnType("TEXT");
b.Property<int>("Type")
.HasMaxLength(1)
.HasColumnType("INTEGER");
b.Property<string>("UserId")
.IsRequired()
.HasMaxLength(36)
.HasColumnType("TEXT");
b.HasKey("Token");
b.ToTable("Tokens");
});
modelBuilder.Entity("HopFrame.Database.Models.Entries.UserEntry", b =>
{
b.Property<string>("Id")
.HasMaxLength(36)
.HasColumnType("TEXT");
b.Property<DateTime>("CreatedAt")
.HasColumnType("TEXT");
b.Property<string>("Email")
.IsRequired()
.HasMaxLength(50)
.HasColumnType("TEXT");
b.Property<string>("Password")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("TEXT");
b.Property<string>("Username")
.HasMaxLength(50)
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Users");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,109 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace DatabaseTest.Migrations
{
/// <inheritdoc />
public partial class Security : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropPrimaryKey(
name: "PK_Tokens",
table: "Tokens");
migrationBuilder.DropColumn(
name: "Id",
table: "Tokens");
migrationBuilder.DropColumn(
name: "OwnerId",
table: "Permissions");
migrationBuilder.AlterColumn<string>(
name: "Id",
table: "Users",
type: "TEXT",
maxLength: 36,
nullable: false,
oldClrType: typeof(long),
oldType: "INTEGER")
.OldAnnotation("Sqlite:Autoincrement", true);
migrationBuilder.AlterColumn<string>(
name: "UserId",
table: "Tokens",
type: "TEXT",
maxLength: 36,
nullable: false,
oldClrType: typeof(long),
oldType: "INTEGER");
migrationBuilder.AddColumn<string>(
name: "UserId",
table: "Permissions",
type: "TEXT",
maxLength: 36,
nullable: false,
defaultValue: "");
migrationBuilder.AddPrimaryKey(
name: "PK_Tokens",
table: "Tokens",
column: "Token");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropPrimaryKey(
name: "PK_Tokens",
table: "Tokens");
migrationBuilder.DropColumn(
name: "UserId",
table: "Permissions");
migrationBuilder.AlterColumn<long>(
name: "Id",
table: "Users",
type: "INTEGER",
nullable: false,
oldClrType: typeof(string),
oldType: "TEXT",
oldMaxLength: 36)
.Annotation("Sqlite:Autoincrement", true);
migrationBuilder.AlterColumn<long>(
name: "UserId",
table: "Tokens",
type: "INTEGER",
nullable: false,
oldClrType: typeof(string),
oldType: "TEXT",
oldMaxLength: 36);
migrationBuilder.AddColumn<long>(
name: "Id",
table: "Tokens",
type: "INTEGER",
nullable: false,
defaultValue: 0L)
.Annotation("Sqlite:Autoincrement", true);
migrationBuilder.AddColumn<long>(
name: "OwnerId",
table: "Permissions",
type: "INTEGER",
nullable: false,
defaultValue: 0L);
migrationBuilder.AddPrimaryKey(
name: "PK_Tokens",
table: "Tokens",
column: "Id");
}
}
}

View File

@@ -0,0 +1,97 @@
// <auto-generated />
using System;
using DatabaseTest;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace DatabaseTest.Migrations
{
[DbContext(typeof(DatabaseContext))]
partial class DatabaseContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "8.0.7");
modelBuilder.Entity("HopFrame.Database.Models.Entries.PermissionEntry", b =>
{
b.Property<long>("RecordId")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<DateTime>("GrantedAt")
.HasColumnType("TEXT");
b.Property<string>("PermissionText")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("TEXT");
b.Property<string>("UserId")
.IsRequired()
.HasMaxLength(36)
.HasColumnType("TEXT");
b.HasKey("RecordId");
b.ToTable("Permissions");
});
modelBuilder.Entity("HopFrame.Database.Models.Entries.TokenEntry", b =>
{
b.Property<string>("Token")
.HasMaxLength(36)
.HasColumnType("TEXT");
b.Property<DateTime>("CreatedAt")
.HasColumnType("TEXT");
b.Property<int>("Type")
.HasMaxLength(1)
.HasColumnType("INTEGER");
b.Property<string>("UserId")
.IsRequired()
.HasMaxLength(36)
.HasColumnType("TEXT");
b.HasKey("Token");
b.ToTable("Tokens");
});
modelBuilder.Entity("HopFrame.Database.Models.Entries.UserEntry", b =>
{
b.Property<string>("Id")
.HasMaxLength(36)
.HasColumnType("TEXT");
b.Property<DateTime>("CreatedAt")
.HasColumnType("TEXT");
b.Property<string>("Email")
.IsRequired()
.HasMaxLength(50)
.HasColumnType("TEXT");
b.Property<string>("Password")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("TEXT");
b.Property<string>("Username")
.HasMaxLength(50)
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Users");
});
#pragma warning restore 612, 618
}
}
}

60
DatabaseTest/Program.cs Normal file
View File

@@ -0,0 +1,60 @@
using DatabaseTest;
using HopFrame.Api;
using HopFrame.Api.Controller;
using HopFrame.Security.Authentication;
using Microsoft.OpenApi.Models;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers()
.AddController<SecurityController<DatabaseContext>>();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddDbContext<DatabaseContext>();
builder.Services.AddHopFrameAuthentication<DatabaseContext>();
//builder.Logging.AddFilter<HopFrameAuthentication<DatabaseContext>>(options => options == LogLevel.None);
builder.Services.AddSwaggerGen(c => {
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme {
Description = @"JWT Authorization header using the Bearer scheme. \r\n\r\n
Enter 'Bearer' [space] and then your token in the text input below.",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey,
Scheme = "Bearer"
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement {{
new OpenApiSecurityScheme {
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
},
Scheme = "oauth2",
Name = "Bearer",
In = ParameterLocation.Header,
},
ArraySegment<string>.Empty
}});
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) {
app.UseSwagger();
app.UseSwaggerUI();
}
//app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

View File

@@ -0,0 +1,41 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:19326",
"sslPort": 44320
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5158",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7283;http://localhost:5158",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@@ -0,0 +1,10 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"HopFrame.Security.Authentication.HopFrameAuthentication": "None"
}
},
"AllowedHosts": "*"
}