1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-25 12:45:18 +01:00

verify email apis and emails

This commit is contained in:
Kyle Spearrin 2017-07-01 23:20:19 -04:00
parent c8528384f8
commit 97ad8bd943
12 changed files with 111 additions and 3 deletions

View File

@ -10,7 +10,6 @@ using Bit.Core.Models.Table;
using Bit.Core.Enums;
using System.Linq;
using Bit.Core.Repositories;
using System.Collections.Generic;
namespace Bit.Api.Controllers
{
@ -104,6 +103,30 @@ namespace Bit.Api.Controllers
await Task.Delay(2000);
throw new BadRequestException(ModelState);
}
[HttpPost("verify-email")]
public async Task PostVerifyEmail()
{
var user = await _userService.GetUserByPrincipalAsync(User);
if(user == null)
{
throw new UnauthorizedAccessException();
}
await _userService.SendEmailVerificationAsync(user);
}
[HttpPost("verify-email-token")]
[AllowAnonymous]
public async Task PostVerifyEmailToken()
{
var user = await _userService.GetUserByIdAsync(new Guid());
if(user == null)
{
throw new UnauthorizedAccessException();
}
await _userService.ConfirmEmailAsync(user, "");
}
[HttpPut("password")]
[HttpPost("password")]

View File

@ -14,6 +14,8 @@
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="MailTemplates\VerifyEmail.cshtml" />
<EmbeddedResource Include="MailTemplates\VerifyEmail.text.cshtml" />
<EmbeddedResource Include="MailTemplates\TwoFactorEmail.cshtml" />
<EmbeddedResource Include="MailTemplates\TwoFactorEmail.text.cshtml" />
<EmbeddedResource Include="MailTemplates\ChangeEmailAlreadyExists.cshtml" />

View File

@ -7,8 +7,6 @@ namespace Bit.Core.Identity
{
public class TwoFactorRememberTokenProvider : DataProtectorTokenProvider<User>
{
private readonly GlobalSettings _globalSettings;
public TwoFactorRememberTokenProvider(
IDataProtectionProvider dataProtectionProvider,
IOptions<TwoFactorRememberTokenProviderOptions> options)

View File

@ -0,0 +1,8 @@
@model Bit.Core.Models.Mail.VerifyEmailModel
@{
Layout = "_BasicMailLayout";
}
<p>
Verify this email address for your bitwarden account by clicking the following link:
</p>
<p><a href="@Model.Url" target="_blank">@Model.Url</a></p>

View File

@ -0,0 +1,8 @@
@model Bit.Core.Models.Mail.VerifyEmailModel
@{
Layout = "_BasicMailLayout.text";
}
Verify this email address for your bitwarden
account by clicking the following link:
@Model.Url

View File

@ -0,0 +1,15 @@
using System;
namespace Bit.Core.Models.Mail
{
public class VerifyEmailModel : BaseMailModel
{
public string Url => string.Format("{0}/verify-email?userId={1}&token={2}",
WebVaultUrl,
UserId,
Token);
public Guid UserId { get; set; }
public string Token { get; set; }
}
}

View File

@ -1,12 +1,14 @@
using System.Threading.Tasks;
using Bit.Core.Models.Table;
using System.Collections.Generic;
using System;
namespace Bit.Core.Services
{
public interface IMailService
{
Task SendWelcomeEmailAsync(User user);
Task SendVerifyEmailEmailAsync(string email, Guid userId, string token);
Task SendChangeEmailAlreadyExistsEmailAsync(string fromEmail, string toEmail);
Task SendChangeEmailEmailAsync(string newEmailAddress, string token);
Task SendTwoFactorEmailAsync(string email, string token);

View File

@ -24,6 +24,8 @@ namespace Bit.Core.Services
Task<bool> VerifyTwoFactorEmailAsync(User user, string token);
Task<U2fRegistration> StartU2fRegistrationAsync(User user);
Task<bool> CompleteU2fRegistrationAsync(User user, string deviceResponse);
Task SendEmailVerificationAsync(User user);
Task<IdentityResult> ConfirmEmailAsync(User user, string token);
Task InitiateEmailChangeAsync(User user, string newEmail);
Task<IdentityResult> ChangeEmailAsync(User user, string masterPassword, string newEmail, string newMasterPassword,
string token, string key);

View File

@ -30,6 +30,23 @@ namespace Bit.Core.Services
_engine = new RazorLightEngine(core, lookup);
}
public async Task SendVerifyEmailEmailAsync(string email, Guid userId, string token)
{
var message = CreateDefaultMessage("Verify Your Email", email);
var model = new VerifyEmailModel
{
Token = token,
UserId = userId,
WebVaultUrl = _globalSettings.BaseVaultUri,
SiteName = _globalSettings.SiteName
};
message.HtmlContent = _engine.Parse("VerifyEmail", model);
message.TextContent = _engine.Parse("VerifyEmail.text", model);
message.MetaData.Add("SendGridBypassListManagement", true);
await _mailDeliveryService.SendEmailAsync(message);
}
public async Task SendChangeEmailAlreadyExistsEmailAsync(string fromEmail, string toEmail)
{
var message = CreateDefaultMessage("Your Email Change", toEmail);

View File

@ -10,6 +10,7 @@ namespace Bit.Core.Services
public class SendGridTemplateMailService : IMailService
{
private const string WelcomeTemplateId = "045f8ad5-5547-4fa2-8d3d-6d46e401164d";
private const string VerifyEmailTemplateId = "TODO";
private const string ChangeEmailAlreadyExistsTemplateId = "b69d2038-6ad9-4cf6-8f7f-7880921cba43";
private const string ChangeEmailTemplateId = "ec2c1471-8292-4f17-b6b6-8223d514f86e";
private const string TwoFactorEmailTemplateId = "264cfe69-5258-4c89-8d90-76b4659de589";
@ -45,6 +46,21 @@ namespace Bit.Core.Services
await _mailDeliveryService.SendEmailAsync(message);
}
public async Task SendVerifyEmailEmailAsync(string email, Guid userId, string token)
{
var message = CreateDefaultMessage(
"Verify Your Email",
email,
VerifyEmailTemplateId);
AddSubstitution(message, "{{token}}", Uri.EscapeDataString(token));
AddSubstitution(message, "{{userId}}", userId.ToString());
AddCategories(message, new List<string> { AdministrativeCategoryName, "Verify Email" });
message.MetaData.Add("SendGridBypassListManagement", true);
await _mailDeliveryService.SendEmailAsync(message);
}
public async Task SendChangeEmailAlreadyExistsEmailAsync(string fromEmail, string toEmail)
{
var message = CreateDefaultMessage(

View File

@ -15,6 +15,7 @@ using Bit.Core.Models.Business;
using U2fLib = U2F.Core.Crypto.U2F;
using U2F.Core.Models;
using U2F.Core.Utils;
using Bit.Core.Exceptions;
namespace Bit.Core.Services
{
@ -289,6 +290,17 @@ namespace Bit.Core.Services
return true;
}
public async Task SendEmailVerificationAsync(User user)
{
if(user.EmailVerified)
{
throw new BadRequestException("Email already verifed.");
}
var token = await base.GenerateEmailConfirmationTokenAsync(user);
await _mailService.SendVerifyEmailEmailAsync(user.Email, user.Id, token);
}
public async Task InitiateEmailChangeAsync(User user, string newEmail)
{
var existingUser = await _userRepository.GetByEmailAsync(newEmail);

View File

@ -12,6 +12,11 @@ namespace Bit.Core.Services
return Task.FromResult(0);
}
public Task SendVerifyEmailEmailAsync(string email, Guid userId, string hint)
{
return Task.FromResult(0);
}
public Task SendChangeEmailEmailAsync(string newEmailAddress, string token)
{
return Task.FromResult(0);