[PM-4800] Send item domain name to fastmail (#2867)

* Send item domain name to fastmail

- Added a metadata field (forDomain:) to the Fastmail Forwarder API
  request that's set to the domain name of the item being added to the
  vault, or to "" if the username generator is being used in standalone
  mode. This allows the user's Fastmail account to display the domain
  name for the username that was generated.

* Minor changes for readability

* dotnet format

---------

Co-authored-by:  Audrey  <ajensen@bitwarden.com>
This commit is contained in:
cubemike99 2023-11-17 17:17:25 -05:00 committed by GitHub
parent 0723999652
commit f98dfa6581
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 8 deletions

View file

@ -46,7 +46,11 @@ namespace Bit.Core.Models.Domain
case ForwardedEmailServiceType.DuckDuckGo:
return new ForwarderOptions { ApiKey = DuckDuckGoApiKey };
case ForwardedEmailServiceType.Fastmail:
return new ForwarderOptions { ApiKey = FastMailApiKey };
return new FastmailForwarderOptions
{
ApiKey = FastMailApiKey,
Website = EmailWebsite
};
case ForwardedEmailServiceType.FirefoxRelay:
return new ForwarderOptions { ApiKey = FirefoxRelayApiAccessToken };
case ForwardedEmailServiceType.SimpleLogin:

View file

@ -9,16 +9,21 @@ using Newtonsoft.Json.Linq;
namespace Bit.Core.Services.EmailForwarders
{
public class FastmailForwarder : BaseForwarder<ForwarderOptions>
public class FastmailForwarderOptions : ForwarderOptions
{
public string Website { get; set; }
}
public class FastmailForwarder : BaseForwarder<FastmailForwarderOptions>
{
protected override string RequestUri => "https://api.fastmail.com/jmap/api/";
protected override void ConfigureHeaders(HttpRequestHeaders headers, ForwarderOptions options)
protected override void ConfigureHeaders(HttpRequestHeaders headers, FastmailForwarderOptions options)
{
headers.Add("Authorization", $"Bearer {options.ApiKey}");
}
protected override async Task<HttpContent> GetContentAsync(IApiService apiService, ForwarderOptions options)
protected override async Task<HttpContent> GetContentAsync(IApiService apiService, FastmailForwarderOptions options)
{
string accountId = null;
try
@ -55,7 +60,8 @@ namespace Bit.Core.Services.EmailForwarders
["state"] = "enabled",
["description"] = "",
["url"] = "",
["emailPrefix"] = ""
["emailPrefix"] = "",
["forDomain"] = options.Website ?? ""
}
}
},

View file

@ -148,6 +148,13 @@ namespace Bit.Core.Services
.GenerateAsync(_apiService, forwardedEmailOptions);
}
if (options.ServiceType == ForwardedEmailServiceType.Fastmail)
{
var fastmailEmailOptions = (FastmailForwarderOptions)options.GetForwarderOptions();
return await new FastmailForwarder()
.GenerateAsync(_apiService, fastmailEmailOptions);
}
BaseForwarder<ForwarderOptions> simpleForwarder = null;
switch (options.ServiceType)
@ -161,9 +168,6 @@ namespace Bit.Core.Services
case ForwardedEmailServiceType.DuckDuckGo:
simpleForwarder = new DuckDuckGoForwarder();
break;
case ForwardedEmailServiceType.Fastmail:
simpleForwarder = new FastmailForwarder();
break;
default:
_logger.Value.Error($"Error UsernameGenerationService: ForwardedEmailServiceType {options.ServiceType} not implemented.");
return Constants.DefaultUsernameGenerated;