Skip to content

Commit

Permalink
feat: users and tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
kirinnee committed Oct 21, 2023
1 parent de318c8 commit 9d00a1b
Show file tree
Hide file tree
Showing 58 changed files with 1,943 additions and 289 deletions.
2 changes: 1 addition & 1 deletion App/Config/settings.lapras.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Metrics:

# Infra-based
Database:
Main:
MAIN:
Host: sulfone-zinc-main-database
User: admin
Password: supersecret
Expand Down
3 changes: 1 addition & 2 deletions App/Config/settings.pichu.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ Metrics:
Enabled: true

# Infra-based
Database:
Main: {}
Database: {}
Cache: {}
BlockStorage: {}
# external
Expand Down
4 changes: 2 additions & 2 deletions App/Config/settings.tauros.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ Metrics:

# Infra-based
Database:
Main:
MAIN:
Host: sulfone-zinc-main-database
User: admin
Password: supersecret
AutoMigrate: true
AutoMigrate: false

Cache: {}
BlockStorage: {}
Expand Down
2 changes: 1 addition & 1 deletion App/Config/settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ Metrics:

# Infra-based
Database:
Main:
MAIN:
Database: sulfone-zinc
AutoMigrate: false
Host: sulfone-zinc-main-database
Expand Down
47 changes: 0 additions & 47 deletions App/Migrations/20231017125833_InitialCreate.cs

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 75 additions & 0 deletions App/Migrations/20231021080634_InitialCreate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace App.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Users",
columns: table => new
{
Id = table.Column<string>(type: "text", nullable: false),
Username = table.Column<string>(type: "text", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Users", x => x.Id);
});

migrationBuilder.CreateTable(
name: "Tokens",
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
Name = table.Column<string>(type: "text", nullable: false),
ApiToken = table.Column<string>(type: "text", nullable: false),
Revoked = table.Column<bool>(type: "boolean", nullable: false),
UserId = table.Column<string>(type: "text", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Tokens", x => x.Id);
table.ForeignKey(
name: "FK_Tokens_Users_UserId",
column: x => x.UserId,
principalTable: "Users",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});

migrationBuilder.CreateIndex(
name: "IX_Tokens_ApiToken",
table: "Tokens",
column: "ApiToken",
unique: true);

migrationBuilder.CreateIndex(
name: "IX_Tokens_UserId",
table: "Tokens",
column: "UserId");

migrationBuilder.CreateIndex(
name: "IX_Users_Username",
table: "Users",
column: "Username",
unique: true);
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Tokens");

migrationBuilder.DropTable(
name: "Users");
}
}
}
47 changes: 43 additions & 4 deletions App/Migrations/MainDbContextModelSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,69 @@ protected override void BuildModel(ModelBuilder modelBuilder)

NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);

modelBuilder.Entity("App.Modules.Users.Data.UserData", b =>
modelBuilder.Entity("App.Modules.Users.Data.TokenData", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");

b.Property<string>("Sub")
b.Property<string>("ApiToken")
.IsRequired()
.HasColumnType("text");

b.Property<string>("Username")
b.Property<string>("Name")
.IsRequired()
.HasColumnType("text");

b.Property<bool>("Revoked")
.HasColumnType("boolean");

b.Property<string>("UserId")
.IsRequired()
.HasColumnType("text");

b.HasKey("Id");

b.HasIndex("Sub")
b.HasIndex("ApiToken")
.IsUnique();

b.HasIndex("UserId");

b.ToTable("Tokens");
});

modelBuilder.Entity("App.Modules.Users.Data.UserData", b =>
{
b.Property<string>("Id")
.HasColumnType("text");

b.Property<string>("Username")
.IsRequired()
.HasColumnType("text");

b.HasKey("Id");

b.HasIndex("Username")
.IsUnique();

b.ToTable("Users");
});

modelBuilder.Entity("App.Modules.Users.Data.TokenData", b =>
{
b.HasOne("App.Modules.Users.Data.UserData", "User")
.WithMany("Tokens")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();

b.Navigation("User");
});

modelBuilder.Entity("App.Modules.Users.Data.UserData", b =>
{
b.Navigation("Tokens");
});
#pragma warning restore 612, 618
}
}
Expand Down
9 changes: 9 additions & 0 deletions App/Modules/DomainServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ public static IServiceCollection AddDomainServices(this IServiceCollection s)
s.AddScoped<IUserRepository, UserRepository>()
.AutoTrace<IUserRepository>();

s.AddScoped<ITokenRepository, TokenRepository>()
.AutoTrace<ITokenRepository>();

s.AddScoped<IApiKeyGenerator, ApiKeyGenerator>()
.AutoTrace<IApiKeyGenerator>();

s.AddScoped<ITokenService, TokenService>()
.AutoTrace<ITokenService>();




Expand Down
9 changes: 0 additions & 9 deletions App/Modules/Users/API/V1/Models.cs

This file was deleted.

25 changes: 25 additions & 0 deletions App/Modules/Users/API/V1/TokenMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using CSharp_Result;
using Domain.Model;

namespace App.Modules.Users.API.V1;

public static class TokenMapper
{
public static TokenRecord ToRecord(this CreateTokenReq req) =>
new() { Name = req.Name, Revoked = false, };

public static TokenRecord ToRecord(this UpdateTokenReq req) =>
new() { Name = req.Name, Revoked = false, };

public static TokenPrincipalResp ToResp(this TokenPrincipal token) =>
new(token.Id, token.Record.Name);

public static TokenOTPrincipalResp ToOTResp(this TokenPrincipal token) =>
new(token.Id, token.Record.Name, token.Property.ApiToken);

public static TokenOTResp ToOTResp(this Token token) =>
new(token.Principal.ToOTResp(), token.UserPrincipal.ToResp());

public static TokenResp ToResp(this Token token) =>
new(token.Principal.ToResp(), token.UserPrincipal.ToResp());
}
19 changes: 19 additions & 0 deletions App/Modules/Users/API/V1/TokenModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace App.Modules.Users.API.V1;

public record CreateTokenReq(string Name);

public record UpdateTokenReq(string Name);

public record TokenPrincipalResp(Guid Id, string Name);

/**
* One Time Token, only first time on creation
*/
public record TokenOTPrincipalResp(Guid Id, string Name, string ApiKey);

/**
* One time Token with owner, only first time on creation
*/
public record TokenOTResp(TokenOTPrincipalResp Token, UserPrincipalResp Owner);

public record TokenResp(TokenPrincipalResp Token, UserPrincipalResp Owner);
Loading

0 comments on commit 9d00a1b

Please sign in to comment.