| | | 1 | | using ArturRios.Messaging.Email; |
| | | 2 | | |
| | | 3 | | namespace ArturRios.Heimdall.WebApi.Email; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Settings for outbound transactional email. Two halves: whether Mailgun is configured at all, |
| | | 7 | | /// which decides between real delivery and the logging senders at start-up, and the front-end |
| | | 8 | | /// links the two emails point at. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <remarks> |
| | | 11 | | /// The Mailgun credentials themselves are read by <see cref="MailgunEmailService" /> from its |
| | | 12 | | /// own environment variables at call time — this class only observes whether they are present, |
| | | 13 | | /// so an unconfigured environment degrades to logging instead of failing every send. |
| | | 14 | | /// </remarks> |
| | | 15 | | public class EmailDeliveryOptions |
| | | 16 | | { |
| | | 17 | | private const string VerificationUrlVariable = "HEIMDALL_EMAIL_VERIFICATION_URL"; |
| | | 18 | | private const string PasswordResetUrlVariable = "HEIMDALL_PASSWORD_RESET_URL"; |
| | | 19 | | |
| | | 20 | | /// <summary>Base link a verification token is appended to (UC-14).</summary> |
| | 1838 | 21 | | public string VerificationUrl { get; init; } = string.Empty; |
| | | 22 | | |
| | | 23 | | /// <summary>Base link a password reset token is appended to (UC-13).</summary> |
| | 1841 | 24 | | public string PasswordResetUrl { get; init; } = string.Empty; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Whether Mailgun has both the credentials it needs. False in any environment that has not |
| | | 28 | | /// been given them — notably the functional test suite, which must never reach the network. |
| | | 29 | | /// </summary> |
| | 1798 | 30 | | public bool MailgunConfigured { get; init; } |
| | | 31 | | |
| | 899 | 32 | | public static EmailDeliveryOptions FromEnvironment() => new() |
| | 899 | 33 | | { |
| | 899 | 34 | | VerificationUrl = Environment.GetEnvironmentVariable(VerificationUrlVariable) ?? string.Empty, |
| | 899 | 35 | | PasswordResetUrl = Environment.GetEnvironmentVariable(PasswordResetUrlVariable) ?? string.Empty, |
| | 899 | 36 | | MailgunConfigured = |
| | 899 | 37 | | !string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(MailgunEmailService.ApiKeyVariable)) && |
| | 899 | 38 | | !string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(MailgunEmailService.DomainVariable)) |
| | 899 | 39 | | }; |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Appends a token to a configured base link as a <c>token</c> query parameter, respecting a |
| | | 43 | | /// query string the base may already carry. Returns an empty string when no link is |
| | | 44 | | /// configured, which is the signal for the senders to write the bare token instead. |
| | | 45 | | /// </summary> |
| | | 46 | | public static string BuildLink(string baseUrl, string token) |
| | | 47 | | { |
| | 25 | 48 | | if (string.IsNullOrWhiteSpace(baseUrl)) |
| | | 49 | | { |
| | 6 | 50 | | return string.Empty; |
| | | 51 | | } |
| | | 52 | | |
| | 19 | 53 | | var separator = baseUrl.Contains('?') ? '&' : '?'; |
| | | 54 | | |
| | 19 | 55 | | return $"{baseUrl}{separator}token={Uri.EscapeDataString(token)}"; |
| | | 56 | | } |
| | | 57 | | } |