-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a2390b8
commit c164d87
Showing
14 changed files
with
191 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/Armory/Notifications/Application/SendResetPasswordEmail/ResetPasswordEmailSender.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.Threading.Tasks; | ||
using Armory.Notifications.Domain; | ||
|
||
namespace Armory.Notifications.Application.SendResetPasswordEmail | ||
{ | ||
public class ResetPasswordEmailSender | ||
{ | ||
private readonly IEmailSender _sender; | ||
|
||
public ResetPasswordEmailSender(IEmailSender sender) | ||
{ | ||
_sender = sender; | ||
} | ||
|
||
public async Task SendResetPasswordEmail(string email, string callbackUrl) | ||
{ | ||
await _sender.SendResetPasswordEmail(email, callbackUrl); | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/Armory/Notifications/Application/SendResetPasswordEmail/SendResetPasswordEmailCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using Armory.Shared.Domain.Bus.Command; | ||
|
||
namespace Armory.Notifications.Application.SendResetPasswordEmail | ||
{ | ||
public class SendResetPasswordEmailCommand : Command | ||
{ | ||
public string Email { get; init; } | ||
public string Token { get; init; } | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
.../Notifications/Application/SendResetPasswordEmail/SendResetPasswordEmailCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Armory.Shared.Domain; | ||
using Armory.Shared.Domain.Bus.Command; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Armory.Notifications.Application.SendResetPasswordEmail | ||
{ | ||
public class SendResetPasswordEmailCommandHandler : CommandHandler<SendResetPasswordEmailCommand> | ||
{ | ||
private readonly ApplicationProperties _properties; | ||
private readonly ResetPasswordEmailSender _sender; | ||
|
||
public SendResetPasswordEmailCommandHandler(ResetPasswordEmailSender sender, | ||
IOptions<ApplicationProperties> properties) | ||
{ | ||
_sender = sender; | ||
_properties = properties.Value; | ||
} | ||
|
||
protected override async Task Handle(SendResetPasswordEmailCommand request, CancellationToken cancellationToken) | ||
{ | ||
var callbackUrl = | ||
$"{_properties.FrontendUrl}/reset_password?token={Utils.StringToBase64(request.Token)}&email={request.Email}"; | ||
await _sender.SendResetPasswordEmail(request.Email, callbackUrl); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace Armory.Notifications.Domain | ||
{ | ||
public class EmailSettings | ||
{ | ||
public string Host { get; set; } | ||
public string Username { get; set; } | ||
public string Password { get; set; } | ||
public int Port { get; set; } | ||
public bool EnableSsl { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace Armory.Notifications.Domain | ||
{ | ||
/// <summary> | ||
/// Interface for sending notifications via email | ||
/// </summary> | ||
public interface IEmailSender | ||
{ | ||
/// <summary> | ||
/// Send an email to reset the password | ||
/// </summary> | ||
/// <param name="email">Email to send</param> | ||
/// <param name="callbackUrl">Callback url</param> | ||
/// <returns></returns> | ||
Task SendResetPasswordEmail(string email, string callbackUrl); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System; | ||
using System.Net; | ||
using System.Net.Mail; | ||
using System.Threading.Tasks; | ||
using Armory.Notifications.Domain; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Armory.Notifications.Infrastructure | ||
{ | ||
public class EmailSender : IEmailSender | ||
{ | ||
private readonly SmtpClient _client; | ||
private readonly ILogger<EmailSender> _logger; | ||
private readonly EmailSettings _settings; | ||
|
||
public EmailSender(IOptions<EmailSettings> settings, ILogger<EmailSender> logger) | ||
{ | ||
_settings = settings.Value; | ||
_logger = logger; | ||
|
||
_client = new SmtpClient(_settings.Host, _settings.Port) | ||
{ | ||
Credentials = new NetworkCredential(_settings.Username, _settings.Password), | ||
EnableSsl = _settings.EnableSsl | ||
}; | ||
} | ||
|
||
public async Task SendResetPasswordEmail(string email, string callbackUrl) | ||
{ | ||
var message = new MailMessage(_settings.Username, email) | ||
{ | ||
Subject = "Reestrablecer contraseña", | ||
Body = | ||
$"Por favor, restablezca su contraseña haciendo clic <a href='{callbackUrl}' target='blank'>aquí</a>.", | ||
IsBodyHtml = true, | ||
Priority = MailPriority.High | ||
}; | ||
|
||
await SendEmail(message); | ||
} | ||
|
||
private async Task SendEmail(MailMessage message) | ||
{ | ||
try | ||
{ | ||
await _client.SendMailAsync(message); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "Error sending email"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Armory.Shared.Domain | ||
{ | ||
public class ApplicationProperties | ||
{ | ||
public string FrontendUrl { get; set; } | ||
} | ||
} |