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

markdown mail service when self hosted

This commit is contained in:
Kyle Spearrin 2017-08-16 10:59:13 -04:00
parent 8af74bd0e4
commit 50a522bbd7
42 changed files with 342 additions and 35 deletions

View File

@ -8,36 +8,48 @@
<ItemGroup>
<EmbeddedResource Include="licensing.cer" />
<EmbeddedResource Include="MailTemplates\VerifyDelete.cshtml" />
<EmbeddedResource Include="MailTemplates\VerifyDelete.text.cshtml" />
<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" />
<EmbeddedResource Include="MailTemplates\ChangeEmailAlreadyExists.text.cshtml" />
<EmbeddedResource Include="MailTemplates\OrganizationUserInvited.cshtml" />
<EmbeddedResource Include="MailTemplates\OrganizationUserInvited.text.cshtml" />
<EmbeddedResource Include="MailTemplates\OrganizationUserAccepted.cshtml" />
<EmbeddedResource Include="MailTemplates\OrganizationUserAccepted.text.cshtml" />
<EmbeddedResource Include="MailTemplates\OrganizationUserConfirmed.cshtml" />
<EmbeddedResource Include="MailTemplates\OrganizationUserConfirmed.text.cshtml" />
<EmbeddedResource Include="MailTemplates\MasterPasswordHint.text.cshtml" />
<EmbeddedResource Include="MailTemplates\ChangeEmail.cshtml" />
<EmbeddedResource Include="MailTemplates\ChangeEmail.text.cshtml" />
<EmbeddedResource Include="MailTemplates\NoMasterPasswordHint.text.cshtml" />
<EmbeddedResource Include="MailTemplates\NoMasterPasswordHint.cshtml" />
<EmbeddedResource Include="MailTemplates\MasterPasswordHint.cshtml" />
<EmbeddedResource Include="MailTemplates\Welcome.text.cshtml" />
<EmbeddedResource Include="MailTemplates\Welcome.cshtml" />
<EmbeddedResource Include="MailTemplates\_MailLayout.cshtml" />
<EmbeddedResource Include="MailTemplates\_MailLayout.text.cshtml" />
<EmbeddedResource Include="MailTemplates\_BasicMailLayout.text.cshtml" />
<EmbeddedResource Include="MailTemplates\_BasicMailLayout.cshtml" />
<EmbeddedResource Include="MailTemplates\Markdown\ChangeEmail.md" />
<EmbeddedResource Include="MailTemplates\Markdown\ChangeEmailAlreadyExists.md" />
<EmbeddedResource Include="MailTemplates\Markdown\MasterPasswordHint.md" />
<EmbeddedResource Include="MailTemplates\Markdown\NoMasterPasswordHint.md" />
<EmbeddedResource Include="MailTemplates\Markdown\OrganizationUserAccepted.md" />
<EmbeddedResource Include="MailTemplates\Markdown\OrganizationUserConfirmed.md" />
<EmbeddedResource Include="MailTemplates\Markdown\OrganizationUserInvited.md" />
<EmbeddedResource Include="MailTemplates\Markdown\TwoFactorEmail.md" />
<EmbeddedResource Include="MailTemplates\Markdown\VerifyDelete.md" />
<EmbeddedResource Include="MailTemplates\Markdown\VerifyEmail.md" />
<EmbeddedResource Include="MailTemplates\Markdown\Welcome.md" />
<EmbeddedResource Include="MailTemplates\Razor\VerifyDelete.cshtml" />
<EmbeddedResource Include="MailTemplates\Razor\VerifyDelete.text.cshtml" />
<EmbeddedResource Include="MailTemplates\Razor\VerifyEmail.cshtml" />
<EmbeddedResource Include="MailTemplates\Razor\VerifyEmail.text.cshtml" />
<EmbeddedResource Include="MailTemplates\Razor\TwoFactorEmail.cshtml" />
<EmbeddedResource Include="MailTemplates\Razor\TwoFactorEmail.text.cshtml" />
<EmbeddedResource Include="MailTemplates\Razor\ChangeEmailAlreadyExists.cshtml" />
<EmbeddedResource Include="MailTemplates\Razor\ChangeEmailAlreadyExists.text.cshtml" />
<EmbeddedResource Include="MailTemplates\Razor\OrganizationUserInvited.cshtml" />
<EmbeddedResource Include="MailTemplates\Razor\OrganizationUserInvited.text.cshtml" />
<EmbeddedResource Include="MailTemplates\Razor\OrganizationUserAccepted.cshtml" />
<EmbeddedResource Include="MailTemplates\Razor\OrganizationUserAccepted.text.cshtml" />
<EmbeddedResource Include="MailTemplates\Razor\OrganizationUserConfirmed.cshtml" />
<EmbeddedResource Include="MailTemplates\Razor\OrganizationUserConfirmed.text.cshtml" />
<EmbeddedResource Include="MailTemplates\Razor\MasterPasswordHint.text.cshtml" />
<EmbeddedResource Include="MailTemplates\Razor\ChangeEmail.cshtml" />
<EmbeddedResource Include="MailTemplates\Razor\ChangeEmail.text.cshtml" />
<EmbeddedResource Include="MailTemplates\Razor\NoMasterPasswordHint.text.cshtml" />
<EmbeddedResource Include="MailTemplates\Razor\NoMasterPasswordHint.cshtml" />
<EmbeddedResource Include="MailTemplates\Razor\MasterPasswordHint.cshtml" />
<EmbeddedResource Include="MailTemplates\Razor\Welcome.text.cshtml" />
<EmbeddedResource Include="MailTemplates\Razor\Welcome.cshtml" />
<EmbeddedResource Include="MailTemplates\Razor\_MailLayout.cshtml" />
<EmbeddedResource Include="MailTemplates\Razor\_MailLayout.text.cshtml" />
<EmbeddedResource Include="MailTemplates\Razor\_BasicMailLayout.text.cshtml" />
<EmbeddedResource Include="MailTemplates\Razor\_BasicMailLayout.cshtml" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Braintree" Version="3.8.0" />
<PackageReference Include="CommonMark.NET" Version="0.15.1" />
<PackageReference Include="IdentityServer4" Version="1.5.2" />
<PackageReference Include="IdentityServer4.AspNetIdentity" Version="1.0.1" />
<PackageReference Include="Microsoft.AspNetCore.DataProtection.AzureStorage" Version="1.0.2" />

View File

@ -0,0 +1 @@
To finalize changing your email address enter the following code in the pop-up window: {{token}}

View File

@ -0,0 +1,3 @@
A user ({{fromEmail}}) recently tried to change their account to use this email address ({{toEmail}}). An account already exists with this email ({{toEmail}}).
If you did not try to change an email address, you can safely ignore this email.

View File

@ -0,0 +1,7 @@
You (or someone) recently requested your master password hint.
Your hint is: "{{hint}}"
Login: <{{vaultUrl}}>
If you did not request your master password hint you can safely ignore this email.

View File

@ -0,0 +1,3 @@
You (or someone) recently requested your master password hint. Unfortunately, your account does not have a master password hint.
If you did not request your master password hint you can safely ignore this email.

View File

@ -0,0 +1,5 @@
This email is to notify you that {{userEmail}} has accepted your invitation to join {{organizationName}}.
To confirm this user, log into the bitwarden web vault, manage your organization "People" and confirm the user.
If you do not wish to confirm this user, you can also remove them from the organization on the same page.

View File

@ -0,0 +1,3 @@
This email is to notify you that you have been confirmed as a user of {{organizationName}}.
Any collections and logins being shared with you by this organization will now appear in your bitwarden vault.

View File

@ -0,0 +1,6 @@
You have been invited to join the {{organizationName}} organization. To accept this invite, click the
following link:
<{{url}}>
If you do not wish to join this organization, you can safely ignore this email.

View File

@ -0,0 +1,3 @@
Your two-step verification code is: {{token}}
Use this code to complete logging in with bitwarden.

View File

@ -0,0 +1,3 @@
Click the link below to delete your bitwarden account ({{email}}). If you did not request this email to delete your bitwarden account, you can safely ignore it.
<{{url}}>

View File

@ -0,0 +1,3 @@
Verify this email address for your bitwarden account by clicking the following link. If you did not request this email to verify a bitwarden account, you can safely ignore it.
<{{Url}}>

View File

@ -0,0 +1,37 @@
Thank you for creating an account with bitwarden. You may now log in with your new account.
Did you know that bitwarden is free to sync with all of your devices? Download bitwarden today on:
Mobile
============
## iOS
<https://itunes.apple.com/us/app/bitwarden-free-password-manager/id1137397744?mt=8>
## Android
<https://play.google.com/store/apps/details?id=com.x8bit.bitwarden>
Desktop
============
## Chrome Extension
<https://chrome.google.com/webstore/detail/bitwarden-free-password-m/nngceckbapebfimnlniiiahkandclblb?utm_source=welcome_email&utm_medium=email>
## Firefox Extension
<https://addons.mozilla.org/firefox/addon/bitwarden-password-manager/>
## Opera Extension
<https://addons.opera.com/extensions/details/bitwarden-free-password-manager/?utm_source=welcome_email&utm_medium=email>
Web
============
You can also access your vault from any web-enabled device using our web vault at: <{{vaultUrl}}>
------------
If you have any questions or problems you can email us from our website at:
<https://bitwarden.com/contact/?utm_source=welcome_email&utm_medium=email>
Thank you!
The bitwarden Team

View File

@ -3,6 +3,7 @@
Layout = "_BasicMailLayout";
}
<p>
Verify this email address for your bitwarden account by clicking the following link:
Verify this email address for your bitwarden account by clicking the following link. If you did not request this email to
verify a bitwarden account, you can safely ignore it.
</p>
<p><a href="@Raw(Model.Url)" target="_blank" clicktracking=off>@Model.Url</a></p>

View File

@ -3,6 +3,8 @@
Layout = "_BasicMailLayout.text";
}
Verify this email address for your bitwarden
account by clicking the following link:
account by clicking the following link. If you
did not request this email to verify a bitwarden
account, you can safely ignore it.
@Raw(Model.Url)

View File

@ -0,0 +1,210 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Bit.Core.Models.Table;
using Bit.Core.Models.Mail;
using System.IO;
using System.Net;
using System.Reflection;
namespace Bit.Core.Services
{
public class MarkdownMailService : IMailService
{
private const string Namespace = "Bit.Core.MailTemplates.Markdown";
private readonly GlobalSettings _globalSettings;
private readonly IMailDeliveryService _mailDeliveryService;
public MarkdownMailService(
GlobalSettings globalSettings,
IMailDeliveryService mailDeliveryService)
{
_globalSettings = globalSettings;
_mailDeliveryService = mailDeliveryService;
}
public async Task SendVerifyEmailEmailAsync(string email, Guid userId, string token)
{
var model = new Dictionary<string, string>
{
["url"] = string.Format("{0}/verify-email?userId={1}&token={2}",
_globalSettings.BaseServiceUri.Vault, userId, WebUtility.UrlEncode(token))
};
var message = await CreateMessageAsync("Verify Your Email", email, "VerifyEmail", model);
message.MetaData.Add("SendGridBypassListManagement", true);
await _mailDeliveryService.SendEmailAsync(message);
}
public async Task SendVerifyDeleteEmailAsync(string email, Guid userId, string token)
{
var model = new Dictionary<string, string>
{
["url"] = string.Format("{0}/verify-recover-delete?userId={1}&token={2}&email={3}",
_globalSettings.BaseServiceUri.Vault,
userId,
WebUtility.UrlEncode(token),
WebUtility.UrlEncode(email)),
["email"] = WebUtility.HtmlEncode(email)
};
var message = await CreateMessageAsync("Delete Your Account", email, "VerifyDelete", model);
message.MetaData.Add("SendGridBypassListManagement", true);
await _mailDeliveryService.SendEmailAsync(message);
}
public async Task SendChangeEmailAlreadyExistsEmailAsync(string fromEmail, string toEmail)
{
var model = new Dictionary<string, string>
{
["fromEmail"] = WebUtility.HtmlEncode(fromEmail),
["toEmail"] = WebUtility.HtmlEncode(toEmail),
};
var message = await CreateMessageAsync("Your Email Change", toEmail, "ChangeEmailAlreadyExists", model);
message.MetaData.Add("SendGridBypassListManagement", true);
await _mailDeliveryService.SendEmailAsync(message);
}
public async Task SendChangeEmailEmailAsync(string newEmailAddress, string token)
{
var model = new Dictionary<string, string>
{
["token"] = token
};
var message = await CreateMessageAsync("Your Email Change", newEmailAddress, "ChangeEmail", model);
message.MetaData.Add("SendGridBypassListManagement", true);
await _mailDeliveryService.SendEmailAsync(message);
}
public async Task SendTwoFactorEmailAsync(string email, string token)
{
var model = new Dictionary<string, string>
{
["token"] = token
};
var message = await CreateMessageAsync("Your Two-step Login Verification Code", email, "TwoFactorEmail", model);
message.MetaData.Add("SendGridBypassListManagement", true);
await _mailDeliveryService.SendEmailAsync(message);
}
public async Task SendMasterPasswordHintEmailAsync(string email, string hint)
{
var model = new Dictionary<string, string>
{
["hint"] = WebUtility.HtmlEncode(hint),
["vaultUrl"] = _globalSettings.BaseServiceUri.Vault
};
var message = await CreateMessageAsync("Your Master Password Hint", email, "MasterPasswordHint", model);
message.MetaData.Add("SendGridBypassListManagement", true);
await _mailDeliveryService.SendEmailAsync(message);
}
public async Task SendNoMasterPasswordHintEmailAsync(string email)
{
var message = await CreateMessageAsync("Your Master Password Hint", email, "NoMasterPasswordHint", null);
message.MetaData.Add("SendGridBypassListManagement", true);
await _mailDeliveryService.SendEmailAsync(message);
}
public async Task SendOrganizationAcceptedEmailAsync(string organizationName, string userEmail,
IEnumerable<string> adminEmails)
{
var model = new Dictionary<string, string>
{
["userEmail"] = WebUtility.HtmlEncode(userEmail),
["organizationName"] = WebUtility.HtmlEncode(organizationName)
};
var message = await CreateMessageAsync($"User {userEmail} Has Accepted Invite", adminEmails,
"OrganizationUserAccepted", model);
message.MetaData.Add("SendGridBypassListManagement", true);
await _mailDeliveryService.SendEmailAsync(message);
}
public async Task SendOrganizationConfirmedEmailAsync(string organizationName, string email)
{
var model = new Dictionary<string, string>
{
["organizationName"] = WebUtility.HtmlEncode(organizationName)
};
var message = await CreateMessageAsync($"You Have Been Confirmed To {organizationName}", email,
"OrganizationUserConfirmed", model);
message.MetaData.Add("SendGridBypassListManagement", true);
await _mailDeliveryService.SendEmailAsync(message);
}
public async Task SendOrganizationInviteEmailAsync(string organizationName, OrganizationUser orgUser, string token)
{
var model = new Dictionary<string, string>
{
["organizationName"] = WebUtility.HtmlEncode(organizationName),
["url"] = string.Format("{0}/accept-organization?organizationId={1}&organizationUserId={2}" +
"&email={3}&organizationName={4}&token={5}",
_globalSettings.BaseServiceUri.Vault,
orgUser.OrganizationId,
orgUser.Id,
WebUtility.UrlEncode(orgUser.Email),
WebUtility.UrlEncode(organizationName),
WebUtility.UrlEncode(token))
};
var message = await CreateMessageAsync($"Join {organizationName}", orgUser.Email, "OrganizationUserInvited", model);
message.MetaData.Add("SendGridBypassListManagement", true);
await _mailDeliveryService.SendEmailAsync(message);
}
public async Task SendWelcomeEmailAsync(User user)
{
var model = new Dictionary<string, string>
{
["vaultUrl"] = _globalSettings.BaseServiceUri.Vault
};
var message = await CreateMessageAsync("Welcome", user.Email, "Welcome", model);
await _mailDeliveryService.SendEmailAsync(message);
}
private async Task<MailMessage> CreateMessageAsync(string subject, string toEmail, string fileName,
Dictionary<string, string> model)
{
return await CreateMessageAsync(subject, new List<string> { toEmail }, fileName, model);
}
private async Task<MailMessage> CreateMessageAsync(string subject, IEnumerable<string> toEmails, string fileName,
Dictionary<string, string> model)
{
var message = new MailMessage
{
ToEmails = toEmails,
Subject = subject,
MetaData = new Dictionary<string, object>()
};
var assembly = typeof(MarkdownMailService).GetTypeInfo().Assembly;
using(var s = assembly.GetManifestResourceStream($"{Namespace}.{fileName}.md"))
using(var sr = new StreamReader(s))
{
var markdown = await sr.ReadToEndAsync();
if(model != null)
{
foreach(var prop in model)
{
markdown = markdown.Replace($"{{{{{prop.Key}}}}}", prop.Value);
}
}
message.HtmlContent = CommonMark.CommonMarkConverter.Convert(markdown);
message.TextContent = markdown;
}
return message;
}
}
}

View File

@ -10,20 +10,20 @@ using System.Net;
namespace Bit.Core.Services
{
public class RazorViewMailService : IMailService
public class RazorMailService : IMailService
{
private readonly GlobalSettings _globalSettings;
private readonly IRazorLightEngine _engine;
private readonly IMailDeliveryService _mailDeliveryService;
public RazorViewMailService(
public RazorMailService(
GlobalSettings globalSettings,
IMailDeliveryService mailDeliveryService)
{
_globalSettings = globalSettings;
_mailDeliveryService = mailDeliveryService;
var manager = new CustomEmbeddedResourceTemplateManager("Bit.Core.MailTemplates");
var manager = new CustomEmbeddedResourceTemplateManager("Bit.Core.MailTemplates.Razor");
var core = new EngineCore(manager, EngineConfiguration.Default);
var pageFactory = new DefaultPageFactory(core.KeyCompile);
var lookup = new DefaultPageLookup(pageFactory);
@ -151,8 +151,8 @@ namespace Bit.Core.Services
WebVaultUrl = _globalSettings.BaseServiceUri.Vault,
SiteName = _globalSettings.SiteName
};
message.HtmlContent = _engine.Parse("OrganizationUserInvited", model);
message.TextContent = _engine.Parse("OrganizationUserInvited.text", model);
message.HtmlContent = _engine.Parse("OrganizationUserAccepted", model);
message.TextContent = _engine.Parse("OrganizationUserAccepted.text", model);
await _mailDeliveryService.SendEmailAsync(message);
}

View File

@ -88,7 +88,7 @@ namespace Bit.Core.Services
AddSubstitution(message, "{{fromEmail}}", fromEmail);
AddSubstitution(message, "{{toEmail}}", toEmail);
AddCategories(message, new List<string> { AdministrativeCategoryName, "Change Email Alrady Exists" });
AddCategories(message, new List<string> { AdministrativeCategoryName, "Change Email Already Exists" });
await _mailDeliveryService.SendEmailAsync(message);
}

View File

@ -54,7 +54,15 @@ namespace Bit.Core.Utilities
public static void AddDefaultServices(this IServiceCollection services, GlobalSettings globalSettings)
{
services.AddSingleton<IMailService, RazorViewMailService>();
if(globalSettings.SelfHosted)
{
services.AddSingleton<IMailService, MarkdownMailService>();
}
else
{
services.AddSingleton<IMailService, RazorMailService>();
}
services.AddSingleton<ILicensingService, RsaLicensingService>();
if(CoreHelpers.SettingHasValue(globalSettings.Mail.SendGridApiKey))