mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-05 18:48:41 +00:00
feat: email verification via smtp
This commit is contained in:
@@ -27,6 +27,7 @@ namespace SharpbinV3.Server.Controllers
|
||||
IOptions<AuthSettings> options,
|
||||
VerificationService verification,
|
||||
TotpVerificationProvider totp,
|
||||
EmailService email,
|
||||
AppDbContext db
|
||||
) : ControllerBase
|
||||
{
|
||||
@@ -35,6 +36,7 @@ namespace SharpbinV3.Server.Controllers
|
||||
private readonly ApiKeyService _apiKeyService = apiKeyService;
|
||||
private readonly VerificationService _verification = verification;
|
||||
private readonly TotpVerificationProvider _totp = totp;
|
||||
private readonly EmailService _email = email;
|
||||
private readonly AppDbContext _db = db;
|
||||
|
||||
[HttpPost]
|
||||
@@ -62,6 +64,9 @@ namespace SharpbinV3.Server.Controllers
|
||||
return BadRequest(new { success = false, message = "Display name should not exceed 26 characters." });
|
||||
|
||||
var user = await _authService.CreateUser(request.Username, request.Password, request.Email, request.DisplayName);
|
||||
if (!string.IsNullOrWhiteSpace(user.Email))
|
||||
await _authService.SendEmailVerification(user);
|
||||
|
||||
return Ok(
|
||||
new
|
||||
{
|
||||
@@ -322,5 +327,16 @@ namespace SharpbinV3.Server.Controllers
|
||||
|
||||
return Ok(new { success = true, message = "API key deleted." });
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("verify-email")]
|
||||
public async Task<IActionResult> VerifyEmail(string token)
|
||||
{
|
||||
(bool verifiedEmail, string message) = await _authService.VerifyEmailVerificationToken(token);
|
||||
if (verifiedEmail)
|
||||
return Ok(new { success = true, message });
|
||||
else
|
||||
return BadRequest(new { success = false, message });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,7 +63,6 @@ namespace SharpbinV3.Server.Controllers
|
||||
[EnableRateLimiting("Strict")]
|
||||
public async Task<IActionResult> UpdateByUUID(Guid uuid, [FromBody] UpdateUserDto updatedUser)
|
||||
{
|
||||
Console.WriteLine($"{ModelState.IsValid}");
|
||||
if (!ModelState.IsValid)
|
||||
return BadRequest(ModelState);
|
||||
|
||||
@@ -389,6 +388,7 @@ namespace SharpbinV3.Server.Controllers
|
||||
CreatedAt = user.CreatedAt,
|
||||
DisplayName = user.DisplayName,
|
||||
Email = user.Email,
|
||||
EmailVerified = user.EmailVerified,
|
||||
LastLogin = user.LastLogin,
|
||||
Roles = user.Roles,
|
||||
Visibility = user.Visibility,
|
||||
|
||||
@@ -14,6 +14,7 @@ namespace SharpbinV3.Server.DTOs.User
|
||||
public class UserResponseDto : UserSimpleDto
|
||||
{
|
||||
public string? Email { get; set; }
|
||||
public bool EmailVerified { get; set; } = false;
|
||||
public long? LastLogin { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ namespace SharpbinV3.Server.Data
|
||||
public DbSet<UserTotp> UserTotps => Set<UserTotp>();
|
||||
public DbSet<Report> Reports => Set<Report>();
|
||||
public DbSet<ApiKey> ApiKeys => Set<ApiKey>();
|
||||
public DbSet<EmailVerificationToken> EmailVerificationTokens => Set<EmailVerificationToken>();
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
@@ -63,6 +64,12 @@ namespace SharpbinV3.Server.Data
|
||||
.WithMany(u => u.ApiKeys)
|
||||
.HasForeignKey(a => a.UserUUID)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
modelBuilder.Entity<EmailVerificationToken>()
|
||||
.HasOne(a => a.User)
|
||||
.WithMany(u => u.EmailVerificationTokens)
|
||||
.HasForeignKey(a => a.UserUUID)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace SharpbinV3.Server.Data.Entities
|
||||
{
|
||||
public enum EmailTokenType
|
||||
{
|
||||
VerifyEmail,
|
||||
ResetPassword,
|
||||
ChangeEmail,
|
||||
DeleteAccount,
|
||||
}
|
||||
|
||||
public sealed class EmailVerificationToken
|
||||
{
|
||||
[Key]
|
||||
public Guid UUID { get; set; } = Guid.CreateVersion7();
|
||||
public required Guid UserUUID { get; set; }
|
||||
public User User { get; set; } = null!;
|
||||
public required string TokenHash { get; set; }
|
||||
public long CreatedAt { get; set; } = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
||||
public long ExpiresAt { get; set; }
|
||||
public required EmailTokenType Type { get; set; }
|
||||
public bool Used { get; set; } = false;
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,8 @@ namespace SharpbinV3.Server.Data.Entities
|
||||
public required string PasswordHash { get; set; }
|
||||
|
||||
public string? Email { get; set; }
|
||||
public bool EmailVerified { get; set; } = false;
|
||||
|
||||
public string? DisplayName { get; set; }
|
||||
public long? LastLogin { get; set; }
|
||||
|
||||
@@ -24,6 +26,7 @@ namespace SharpbinV3.Server.Data.Entities
|
||||
public List<RefreshToken> RefreshTokens { get; set; } = [];
|
||||
public List<Report> Reports { get; set; } = [];
|
||||
public List<ApiKey> ApiKeys { get; set; } = [];
|
||||
public List<EmailVerificationToken> EmailVerificationTokens { get; set; } = [];
|
||||
public UserTotp? Totp { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
+453
@@ -0,0 +1,453 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using SharpbinV3.Server.Data;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace SharpbinV3.Server.Migrations
|
||||
{
|
||||
[DbContext(typeof(AppDbContext))]
|
||||
[Migration("20260221080317_EmailVerificationToken")]
|
||||
partial class EmailVerificationToken
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder.HasAnnotation("ProductVersion", "10.0.1");
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Server.Data.Entities.ApiKey", b =>
|
||||
{
|
||||
b.Property<Guid>("UUID")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<long>("CreatedAt")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("KeyHash")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<long?>("LastUsedAt")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<Guid>("UserUUID")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.HasKey("UUID");
|
||||
|
||||
b.HasIndex("UserUUID");
|
||||
|
||||
b.ToTable("ApiKeys");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Server.Data.Entities.EmailVerificationToken", b =>
|
||||
{
|
||||
b.Property<Guid>("UUID")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<long>("CreatedAt")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<long>("ExpiresAt")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("TokenHash")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<int>("Type")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<bool>("Used")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<Guid>("UserUUID")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.HasKey("UUID");
|
||||
|
||||
b.HasIndex("UserUUID");
|
||||
|
||||
b.ToTable("EmailVerificationTokens");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Server.Data.Entities.Paste", b =>
|
||||
{
|
||||
b.Property<int>("PID")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<Guid?>("AuthorUUID")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<byte[]>("Content")
|
||||
.IsRequired()
|
||||
.HasColumnType("BLOB");
|
||||
|
||||
b.Property<long>("CreatedAt")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<long?>("EditedAt")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<long>("ExpiresAt")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("ID")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<bool>("IsCompressed")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<long>("Size")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("Syntax")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Title")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<long>("TrueSize")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<Guid>("UUID")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<int>("Views")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<int>("Visibility")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.HasKey("PID");
|
||||
|
||||
b.HasIndex("AuthorUUID");
|
||||
|
||||
b.HasIndex("ExpiresAt");
|
||||
|
||||
b.HasIndex("ID")
|
||||
.IsUnique();
|
||||
|
||||
b.HasIndex("PID")
|
||||
.IsUnique();
|
||||
|
||||
b.HasIndex("Size");
|
||||
|
||||
b.HasIndex("Syntax");
|
||||
|
||||
b.HasIndex("UUID")
|
||||
.IsUnique();
|
||||
|
||||
b.HasIndex("Views");
|
||||
|
||||
b.HasIndex("Visibility");
|
||||
|
||||
b.ToTable("Pastes");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Server.Data.Entities.PasteView", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<int>("PastePID")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<long>("ViewedAt")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("ViewerHash")
|
||||
.IsRequired()
|
||||
.HasMaxLength(64)
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("PastePID", "ViewerHash")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("PasteViews");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Server.Data.Entities.RefreshToken", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<DateTimeOffset>("CreatedAt")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<DateTimeOffset>("ExpiresAt")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("JwtId")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("ReplacedByToken")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<bool>("Revoked")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<byte[]>("RowVersion")
|
||||
.IsRequired()
|
||||
.HasColumnType("BLOB");
|
||||
|
||||
b.Property<string>("TokenHash")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<bool>("Used")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<Guid>("UserUUID")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("TokenHash")
|
||||
.IsUnique();
|
||||
|
||||
b.HasIndex("UserUUID");
|
||||
|
||||
b.ToTable("RefreshTokens");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Server.Data.Entities.Report", b =>
|
||||
{
|
||||
b.Property<int>("ReportID")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<long>("CreatedAt")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("Description")
|
||||
.HasMaxLength(1000)
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<int?>("PastePID")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<Guid>("ReporterUUID")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<int>("Status")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<int>("TargetType")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<int>("Type")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<long?>("UpdatedAt")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<Guid?>("UserUUID")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.HasKey("ReportID");
|
||||
|
||||
b.HasIndex("PastePID");
|
||||
|
||||
b.HasIndex("ReportID")
|
||||
.IsUnique();
|
||||
|
||||
b.HasIndex("ReporterUUID");
|
||||
|
||||
b.HasIndex("TargetType");
|
||||
|
||||
b.HasIndex("UserUUID");
|
||||
|
||||
b.ToTable("Reports");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Server.Data.Entities.User", b =>
|
||||
{
|
||||
b.Property<Guid>("UUID")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<long>("CreatedAt")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("DisplayName")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Email")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<bool>("EmailVerified")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<long?>("LastLogin")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("PasswordHash")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.PrimitiveCollection<string>("Roles")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<int?>("UID")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("Username")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<int>("Visibility")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.HasKey("UUID");
|
||||
|
||||
b.HasIndex("UID");
|
||||
|
||||
b.HasIndex("UUID")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("Users");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Server.Data.Entities.UserTotp", b =>
|
||||
{
|
||||
b.Property<Guid>("UserUUID")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<long>("CreatedAt")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<byte[]>("EncryptedSecret")
|
||||
.IsRequired()
|
||||
.HasColumnType("BLOB");
|
||||
|
||||
b.HasKey("UserUUID");
|
||||
|
||||
b.ToTable("UserTotps");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Server.Data.Entities.ApiKey", b =>
|
||||
{
|
||||
b.HasOne("SharpbinV3.Server.Data.Entities.User", "User")
|
||||
.WithMany("ApiKeys")
|
||||
.HasForeignKey("UserUUID")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("User");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Server.Data.Entities.EmailVerificationToken", b =>
|
||||
{
|
||||
b.HasOne("SharpbinV3.Server.Data.Entities.User", "User")
|
||||
.WithMany("EmailVerificationTokens")
|
||||
.HasForeignKey("UserUUID")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("User");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Server.Data.Entities.Paste", b =>
|
||||
{
|
||||
b.HasOne("SharpbinV3.Server.Data.Entities.User", "User")
|
||||
.WithMany("Pastes")
|
||||
.HasForeignKey("AuthorUUID")
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
b.Navigation("User");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Server.Data.Entities.PasteView", b =>
|
||||
{
|
||||
b.HasOne("SharpbinV3.Server.Data.Entities.Paste", "Paste")
|
||||
.WithMany("PasteViews")
|
||||
.HasForeignKey("PastePID")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Paste");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Server.Data.Entities.RefreshToken", b =>
|
||||
{
|
||||
b.HasOne("SharpbinV3.Server.Data.Entities.User", "User")
|
||||
.WithMany("RefreshTokens")
|
||||
.HasForeignKey("UserUUID")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("User");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Server.Data.Entities.Report", b =>
|
||||
{
|
||||
b.HasOne("SharpbinV3.Server.Data.Entities.Paste", "Paste")
|
||||
.WithMany("Reports")
|
||||
.HasForeignKey("PastePID")
|
||||
.OnDelete(DeleteBehavior.SetNull);
|
||||
|
||||
b.HasOne("SharpbinV3.Server.Data.Entities.User", "User")
|
||||
.WithMany("Reports")
|
||||
.HasForeignKey("UserUUID")
|
||||
.OnDelete(DeleteBehavior.SetNull);
|
||||
|
||||
b.Navigation("Paste");
|
||||
|
||||
b.Navigation("User");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Server.Data.Entities.UserTotp", b =>
|
||||
{
|
||||
b.HasOne("SharpbinV3.Server.Data.Entities.User", "User")
|
||||
.WithOne("Totp")
|
||||
.HasForeignKey("SharpbinV3.Server.Data.Entities.UserTotp", "UserUUID")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("User");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Server.Data.Entities.Paste", b =>
|
||||
{
|
||||
b.Navigation("PasteViews");
|
||||
|
||||
b.Navigation("Reports");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Server.Data.Entities.User", b =>
|
||||
{
|
||||
b.Navigation("ApiKeys");
|
||||
|
||||
b.Navigation("EmailVerificationTokens");
|
||||
|
||||
b.Navigation("Pastes");
|
||||
|
||||
b.Navigation("RefreshTokens");
|
||||
|
||||
b.Navigation("Reports");
|
||||
|
||||
b.Navigation("Totp");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace SharpbinV3.Server.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class EmailVerificationToken : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.AddColumn<bool>(
|
||||
name: "EmailVerified",
|
||||
table: "Users",
|
||||
type: "INTEGER",
|
||||
nullable: false,
|
||||
defaultValue: false);
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "EmailVerificationTokens",
|
||||
columns: table => new
|
||||
{
|
||||
UUID = table.Column<Guid>(type: "TEXT", nullable: false),
|
||||
UserUUID = table.Column<Guid>(type: "TEXT", nullable: false),
|
||||
TokenHash = table.Column<string>(type: "TEXT", nullable: false),
|
||||
CreatedAt = table.Column<long>(type: "INTEGER", nullable: false),
|
||||
ExpiresAt = table.Column<long>(type: "INTEGER", nullable: false),
|
||||
Type = table.Column<int>(type: "INTEGER", nullable: false),
|
||||
Used = table.Column<bool>(type: "INTEGER", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_EmailVerificationTokens", x => x.UUID);
|
||||
table.ForeignKey(
|
||||
name: "FK_EmailVerificationTokens_Users_UserUUID",
|
||||
column: x => x.UserUUID,
|
||||
principalTable: "Users",
|
||||
principalColumn: "UUID",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_EmailVerificationTokens_UserUUID",
|
||||
table: "EmailVerificationTokens",
|
||||
column: "UserUUID");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "EmailVerificationTokens");
|
||||
|
||||
migrationBuilder.DropColumn(
|
||||
name: "EmailVerified",
|
||||
table: "Users");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -47,6 +47,38 @@ namespace SharpbinV3.Server.Migrations
|
||||
b.ToTable("ApiKeys");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Server.Data.Entities.EmailVerificationToken", b =>
|
||||
{
|
||||
b.Property<Guid>("UUID")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<long>("CreatedAt")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<long>("ExpiresAt")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<string>("TokenHash")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<int>("Type")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<bool>("Used")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<Guid>("UserUUID")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.HasKey("UUID");
|
||||
|
||||
b.HasIndex("UserUUID");
|
||||
|
||||
b.ToTable("EmailVerificationTokens");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Server.Data.Entities.Paste", b =>
|
||||
{
|
||||
b.Property<int>("PID")
|
||||
@@ -258,6 +290,9 @@ namespace SharpbinV3.Server.Migrations
|
||||
b.Property<string>("Email")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<bool>("EmailVerified")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
b.Property<long?>("LastLogin")
|
||||
.HasColumnType("INTEGER");
|
||||
|
||||
@@ -317,6 +352,17 @@ namespace SharpbinV3.Server.Migrations
|
||||
b.Navigation("User");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Server.Data.Entities.EmailVerificationToken", b =>
|
||||
{
|
||||
b.HasOne("SharpbinV3.Server.Data.Entities.User", "User")
|
||||
.WithMany("EmailVerificationTokens")
|
||||
.HasForeignKey("UserUUID")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("User");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("SharpbinV3.Server.Data.Entities.Paste", b =>
|
||||
{
|
||||
b.HasOne("SharpbinV3.Server.Data.Entities.User", "User")
|
||||
@@ -388,6 +434,8 @@ namespace SharpbinV3.Server.Migrations
|
||||
{
|
||||
b.Navigation("ApiKeys");
|
||||
|
||||
b.Navigation("EmailVerificationTokens");
|
||||
|
||||
b.Navigation("Pastes");
|
||||
|
||||
b.Navigation("RefreshTokens");
|
||||
|
||||
@@ -70,6 +70,7 @@ namespace SharpbinV3.Server
|
||||
builder.Services.AddSingleton<IHostedService, PasteCleanUpService>();
|
||||
builder.Services.AddSingleton<IHostedService, PasteViewCleanUpService>();
|
||||
builder.Services.AddScoped<UserService>();
|
||||
builder.Services.AddScoped<EmailService>();
|
||||
builder.Services.AddScoped<AuthService>();
|
||||
builder.Services.AddScoped<PasteService>();
|
||||
builder.Services.AddScoped<ApiKeyService>();
|
||||
@@ -92,7 +93,9 @@ namespace SharpbinV3.Server
|
||||
.Bind(builder.Configuration.GetSection("JwtSettings"))
|
||||
.ValidateDataAnnotations()
|
||||
.ValidateOnStart();
|
||||
builder.Services.Configure<AuthSettings>(builder.Configuration.GetSection("AuthSettings"));
|
||||
builder.Services.AddOptions<AppSettings>().Bind(builder.Configuration).ValidateDataAnnotations().ValidateOnStart();
|
||||
builder.Services.AddOptions<AuthSettings>().Bind(builder.Configuration.GetSection("AuthSettings")).ValidateDataAnnotations();
|
||||
builder.Services.AddOptions<EmailSettings>().Bind(builder.Configuration.GetSection("EmailSettings")).ValidateDataAnnotations();
|
||||
|
||||
builder
|
||||
.Services.AddAuthentication(options =>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Data;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
@@ -25,15 +26,20 @@ namespace SharpbinV3.Server.Services
|
||||
|
||||
public sealed class AuthService(
|
||||
AppDbContext db,
|
||||
EmailService email,
|
||||
IOptions<JWTSettings> jwtOptions,
|
||||
IOptions<AuthSettings> authOptions,
|
||||
IOptions<AppSettings> appSettings,
|
||||
ILogger<AuthService> logger
|
||||
)
|
||||
{
|
||||
private readonly AppDbContext _db = db;
|
||||
private readonly EmailService _email = email;
|
||||
private readonly JWTSettings _jwtSettings = jwtOptions.Value;
|
||||
private readonly AuthSettings _authSettings = authOptions.Value;
|
||||
private readonly AppSettings _appSettings = appSettings.Value;
|
||||
private readonly ILogger _logger = logger;
|
||||
private const int EmailVerificationTokenLength = 64;
|
||||
|
||||
public async Task<User> CreateUser(string username, string password, string? email, string? displayName)
|
||||
{
|
||||
@@ -214,5 +220,57 @@ namespace SharpbinV3.Server.Services
|
||||
var userUUID = Guid.Parse(uuidClaim);
|
||||
return await _db.Users.FirstOrDefaultAsync(u => u.UUID == userUUID);
|
||||
}
|
||||
|
||||
public async Task SendEmailVerification(User user)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(user.Email))
|
||||
return;
|
||||
|
||||
string token = Utilities.GenerateSecureRandomString(EmailVerificationTokenLength);
|
||||
string tokenHash = Utilities.ComputeSha256(token);
|
||||
|
||||
await _email.SendAsync(
|
||||
user.Email,
|
||||
"Verify Your Email",
|
||||
$"{(_appSettings.Https ? "https" : "http")}://{_appSettings.Domain}/verify-email?token={token}"
|
||||
);
|
||||
var verificationToken = new EmailVerificationToken
|
||||
{
|
||||
TokenHash = tokenHash,
|
||||
UserUUID = user.UUID,
|
||||
User = user,
|
||||
ExpiresAt = DateTimeOffset.UtcNow.AddDays(1).ToUnixTimeMilliseconds(),
|
||||
Type = EmailTokenType.VerifyEmail,
|
||||
};
|
||||
await _db.EmailVerificationTokens.AddAsync(verificationToken);
|
||||
await _db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task<(bool success, string message)> VerifyEmailVerificationToken(string token)
|
||||
{
|
||||
if (token.Length != EmailVerificationTokenLength)
|
||||
return (false, "Invalid token.");
|
||||
|
||||
string tokenHash = Utilities.ComputeSha256(token);
|
||||
var verificationToken = await _db.EmailVerificationTokens.Include(t => t.User).FirstOrDefaultAsync(t => t.TokenHash == tokenHash);
|
||||
|
||||
if (
|
||||
verificationToken == null
|
||||
|| verificationToken.User == null
|
||||
|| verificationToken.ExpiresAt < DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
|
||||
|| verificationToken.Used
|
||||
)
|
||||
return (false, "Token is expired or already used.");
|
||||
|
||||
if (verificationToken != null && !verificationToken.Used)
|
||||
{
|
||||
verificationToken.Used = true;
|
||||
verificationToken.User.EmailVerified = true;
|
||||
await _db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
await _db.SaveChangesAsync();
|
||||
return (true, "Email successfully verified.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
using MailKit.Net.Smtp;
|
||||
using MailKit.Security;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MimeKit;
|
||||
using SharpbinV3.Server.Settings;
|
||||
|
||||
namespace SharpbinV3.Server.Services
|
||||
{
|
||||
public class EmailService(IOptions<EmailSettings> emailSettings)
|
||||
{
|
||||
private readonly EmailSettings settings = emailSettings.Value;
|
||||
|
||||
public async Task SendAsync(string to, string subject, string body)
|
||||
{
|
||||
var message = new MimeMessage();
|
||||
message.From.Add(new MailboxAddress("Sharpbin", settings.From));
|
||||
message.To.Add(MailboxAddress.Parse(to));
|
||||
message.Subject = subject;
|
||||
|
||||
message.Body = new TextPart("html") { Text = body };
|
||||
|
||||
using var smtp = new SmtpClient();
|
||||
await smtp.ConnectAsync(settings.Host, settings.Port, SecureSocketOptions.StartTls);
|
||||
|
||||
await smtp.AuthenticateAsync(settings.User, settings.Password);
|
||||
|
||||
await smtp.SendAsync(message);
|
||||
await smtp.DisconnectAsync(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace SharpbinV3.Server.Settings
|
||||
{
|
||||
public class AppSettings
|
||||
{
|
||||
[Required, MaxLength(256)]
|
||||
public string Domain { get; set; } = string.Empty;
|
||||
public bool Https { get; set; } = true;
|
||||
}
|
||||
}
|
||||
@@ -8,5 +8,6 @@
|
||||
public bool First_User_Admin { get; set; } = true;
|
||||
public bool Admins_Require_2FA { get; set; } = true;
|
||||
public string API_Key_HMAC_Secret { get; set; } = string.Empty;
|
||||
public string Email_Verification_HMAC_Secret { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace SharpbinV3.Server.Settings
|
||||
{
|
||||
public class EmailSettings
|
||||
{
|
||||
[MaxLength(256)]
|
||||
public string Host { get; set; } = string.Empty;
|
||||
|
||||
[Range(1, 65535)]
|
||||
public int Port { get; set; } = 587;
|
||||
|
||||
[MaxLength(256)]
|
||||
public string User { get; set; } = string.Empty;
|
||||
|
||||
[MaxLength(256)]
|
||||
public string Password { get; set; } = string.Empty;
|
||||
|
||||
[EmailAddress]
|
||||
public string From { get; set; } = string.Empty;
|
||||
public string Verification_HMAC_Secret { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
|
||||
<PackageReference Include="MailKit" Version="4.15.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.1" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.1" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.1" />
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
{
|
||||
"Domain": "localhost:5173",
|
||||
"Https": false,
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
|
||||
Reference in New Issue
Block a user