| | | 1 | | using ArturRios.Heimdall.Shared.Messages; |
| | | 2 | | using FluentValidation; |
| | | 3 | | |
| | | 4 | | namespace ArturRios.Heimdall.Command.Input.Validation; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Shape validation for <see cref="SetScopeEmailLinksCommand" />: each link, when present, is an |
| | | 8 | | /// absolute http or https URL that fits its column. An absent or blank one is a clear, not an error. |
| | | 9 | | /// </summary> |
| | | 10 | | public class SetScopeEmailLinksCommandValidator : AbstractValidator<SetScopeEmailLinksCommand> |
| | | 11 | | { |
| | | 12 | | private const int MaxLength = 2048; |
| | | 13 | | |
| | 39 | 14 | | public SetScopeEmailLinksCommandValidator() |
| | | 15 | | { |
| | 39 | 16 | | RuleFor(command => command.EmailVerificationUrl) |
| | 39 | 17 | | .Must(BeUsableLink) |
| | 39 | 18 | | .WithMessage(ScopeMessages.EmailLinkInvalid); |
| | | 19 | | |
| | 39 | 20 | | RuleFor(command => command.PasswordResetUrl) |
| | 39 | 21 | | .Must(BeUsableLink) |
| | 39 | 22 | | .WithMessage(ScopeMessages.EmailLinkInvalid); |
| | 39 | 23 | | } |
| | | 24 | | |
| | | 25 | | // The scheme is checked, not just absoluteness, for the reason UpdateScopeCommandValidator gives |
| | | 26 | | // for the privacy notice URI: on Unix, Uri.TryCreate parses "/some/path" as an absolute file:// |
| | | 27 | | // URI, so an absoluteness check alone accepts a local filesystem path as an emailed link. |
| | | 28 | | private static bool BeUsableLink(string? url) => |
| | 78 | 29 | | string.IsNullOrWhiteSpace(url) || |
| | 78 | 30 | | (url.Length <= MaxLength |
| | 78 | 31 | | && Uri.TryCreate(url, UriKind.Absolute, out var parsed) |
| | 78 | 32 | | && (parsed.Scheme == Uri.UriSchemeHttps || parsed.Scheme == Uri.UriSchemeHttp)); |
| | | 33 | | } |