| | | 1 | | // SPDX-FileCopyrightText: 2026 Alper Çelik <[email protected]> |
| | | 2 | | // |
| | | 3 | | // SPDX-License-Identifier: AGPL-3.0-or-later |
| | | 4 | | |
| | | 5 | | using System.ComponentModel.DataAnnotations; |
| | | 6 | | using System.Text; |
| | | 7 | | using Api.Auth.Models; |
| | | 8 | | using Api.Auth.Utils; |
| | | 9 | | using Api.Database; |
| | | 10 | | using Api.Database.Utils; |
| | | 11 | | using Api.Utils; |
| | | 12 | | using FluentValidation; |
| | | 13 | | using Geralt; |
| | | 14 | | using HotChocolate.Authorization; |
| | | 15 | | using Microsoft.EntityFrameworkCore; |
| | | 16 | | |
| | | 17 | | namespace Api.Auth.Mutations; |
| | | 18 | | |
| | 1 | 19 | | public record RegisterInput( |
| | 1 | 20 | | string Email, |
| | 1 | 21 | | string Password, |
| | 1 | 22 | | bool AdminRegistration, |
| | 1 | 23 | | string ClientName = "unknown" |
| | 1 | 24 | | ); |
| | | 25 | | |
| | | 26 | | [MutationType] |
| | | 27 | | public static partial class RegisterMutations |
| | | 28 | | { |
| | | 29 | | [AllowAnonymous] |
| | | 30 | | public static async Task<LoginPayload> RegisterMutation( |
| | | 31 | | [Service] PGContext db, |
| | | 32 | | [Service] IEFTransactionDIAccessorService txAccessor, |
| | | 33 | | [Service] IValidator<RegisterInput> validator, |
| | | 34 | | RegisterInput input, |
| | | 35 | | CancellationToken ct |
| | | 36 | | ) |
| | | 37 | | { |
| | | 38 | | await validator.ValidateOrThrowAsync(input, ct); |
| | | 39 | | |
| | | 40 | | var tx = await txAccessor.BeginOrGetTransactionAsync(); |
| | | 41 | | |
| | | 42 | | var password_bytes = Encoding.UTF8.GetBytes(input.Password.Normalize()); |
| | | 43 | | var hash_chars = new char[Argon2id.HashSize]; |
| | | 44 | | Argon2id.ComputeHash( |
| | | 45 | | hash_chars, |
| | | 46 | | password_bytes, |
| | | 47 | | AuthUtils.ARGON2ID_ITER, |
| | | 48 | | AuthUtils.ARGON2ID_MEM_BYTES |
| | | 49 | | ); |
| | | 50 | | string hash = new([.. hash_chars.Where(c => c != (char)byte.MinValue)]); |
| | | 51 | | |
| | | 52 | | var now = Now(); |
| | | 53 | | var user = new UserEF() |
| | | 54 | | { |
| | | 55 | | Id = Guid.CreateVersion7().WithPostfix(UserEF.IdPostfix), |
| | | 56 | | MetadataAddedAt = now, |
| | | 57 | | MetadataUpdatedAt = now, |
| | | 58 | | |
| | | 59 | | Email = input.Email.NormalizeEmail(), |
| | | 60 | | PasswordHash = hash, |
| | | 61 | | Admin = (await AuthUtils.CanAdminRegister(db)) && input.AdminRegistration, |
| | | 62 | | }; |
| | | 63 | | |
| | | 64 | | await db.Users.AddAsync(user); |
| | | 65 | | string token = await AuthUtils.CreateUserSession(user.Id, input.ClientName, db); |
| | | 66 | | await db.SaveChangesAsync(); |
| | | 67 | | await tx.CommitAsync(); |
| | | 68 | | |
| | | 69 | | return new(Queries.UserMapper.ToDto(user), token); |
| | | 70 | | } |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | public class RegisterInputValidator : AbstractValidator<RegisterInput> |
| | | 74 | | { |
| | | 75 | | public RegisterInputValidator(PGContext db, IEFTransactionDIAccessorService tx) |
| | | 76 | | { |
| | | 77 | | RuleFor(w => w).BeginTransaction(tx); |
| | | 78 | | |
| | | 79 | | RuleFor(r => r.Email) |
| | | 80 | | .Must(e => new EmailAddressAttribute().IsValid(e.NormalizeEmail())) |
| | | 81 | | .WithMessage("Email is invalid"); |
| | | 82 | | |
| | | 83 | | RuleFor(r => r.Email) |
| | | 84 | | .MustAsync( |
| | | 85 | | async (e, ct) => |
| | | 86 | | !await db.Users.Where(u => u.Email == e.NormalizeEmail()).AnyAsync(ct) |
| | | 87 | | ) |
| | | 88 | | .WithMessage("Email is already used"); |
| | | 89 | | |
| | | 90 | | RuleFor(r => r.AdminRegistration) |
| | | 91 | | .MustAsync(async (adr, ct) => !adr || await AuthUtils.CanAdminRegister(db)) |
| | | 92 | | .WithMessage("Can't Register As Admin"); |
| | | 93 | | } |
| | | 94 | | } |