| | | 1 | | using ArturRios.Data.Relational.Core.Interfaces; |
| | | 2 | | using ArturRios.Heimdall.Domain.Entities; |
| | | 3 | | using ArturRios.Util.Random; |
| | | 4 | | using Microsoft.EntityFrameworkCore; |
| | | 5 | | |
| | | 6 | | namespace ArturRios.Heimdall.Command.Services; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Default <see cref="IPasswordResetService" /> (UC-12 steps 3 and 4, FR-PR-02): builds a |
| | | 10 | | /// random, time-limited <see cref="PasswordResetToken" />, persists it, then delegates delivery |
| | | 11 | | /// to the configured <see cref="IPasswordResetSender" />. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <remarks> |
| | | 14 | | /// <para> |
| | | 15 | | /// The options ask for letters and digits only, matching <see cref="EmailVerificationService" />. |
| | | 16 | | /// As of ArturRios.Util 1.5.0 <c>CustomRandom.Text</c> honours those flags for every character, |
| | | 17 | | /// so the token is alphanumeric — and therefore URL-safe — throughout. The sender still escapes |
| | | 18 | | /// the token before putting it in a link; nothing depends on that escaping being a no-op. |
| | | 19 | | /// </para> |
| | | 20 | | /// <para> |
| | | 21 | | /// Secrecy comes from the source and the length: <c>CustomRandom.Text</c> draws from |
| | | 22 | | /// <c>RandomNumberGenerator</c>, and 48 alphanumeric characters are far past guessing range |
| | | 23 | | /// within the token's one-hour life. |
| | | 24 | | /// </para> |
| | | 25 | | /// </remarks> |
| | 46 | 26 | | public class PasswordResetService( |
| | 46 | 27 | | IAsyncRepository<PasswordResetToken, long> tokenWriter, |
| | 46 | 28 | | IAsyncReadOnlyRepository<Scope, long> scopeReader, |
| | 46 | 29 | | IPasswordResetSender sender, |
| | 46 | 30 | | PasswordResetOptions options) |
| | | 31 | | : IPasswordResetService |
| | | 32 | | { |
| | | 33 | | public async Task IssueAndSendAsync(Person person) |
| | | 34 | | { |
| | 32 | 35 | | var token = CustomRandom.Text(new RandomStringOptions |
| | 32 | 36 | | { |
| | 32 | 37 | | Length = 48, |
| | 32 | 38 | | IncludeLowercase = true, |
| | 32 | 39 | | IncludeUppercase = true, |
| | 32 | 40 | | IncludeDigits = true, |
| | 32 | 41 | | IncludeSpecialCharacters = false |
| | 32 | 42 | | }); |
| | | 43 | | |
| | 32 | 44 | | await tokenWriter.CreateAsync(new PasswordResetToken |
| | 32 | 45 | | { |
| | 32 | 46 | | PersonId = person.Id, |
| | 32 | 47 | | TokenHash = SingleUseTokenHash.Of(token), |
| | 32 | 48 | | ExpiresAt = DateTime.UtcNow.Add(options.TokenLifetime), |
| | 32 | 49 | | Used = false |
| | 32 | 50 | | }); |
| | | 51 | | |
| | | 52 | | // The person's scope may point the link at its own client application. An administrator has |
| | | 53 | | // no scope, and a scope with no link set leaves this null: both take the global setting. |
| | 32 | 54 | | string? linkBase = null; |
| | | 55 | | |
| | 32 | 56 | | if (person.ScopeId is { } scopeId) |
| | | 57 | | { |
| | 6 | 58 | | linkBase = await scopeReader.Query() |
| | 6 | 59 | | .Where(scope => scope.Id == scopeId) |
| | 6 | 60 | | .Select(scope => scope.PasswordResetUrl) |
| | 6 | 61 | | .FirstOrDefaultAsync(); |
| | | 62 | | } |
| | | 63 | | |
| | 32 | 64 | | await sender.SendAsync(person.Email, token, linkBase); |
| | 32 | 65 | | } |
| | | 66 | | } |