Skip to content

Commit

Permalink
[PM-4800] Send item domain name to fastmail (bitwarden#2867)
Browse files Browse the repository at this point in the history
* 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 ✨ <[email protected]>
  • Loading branch information
cubemike99 and audreyality authored Nov 17, 2023
1 parent 0723999 commit f98dfa6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
6 changes: 5 additions & 1 deletion src/Core/Models/Domain/UsernameGenerationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ public ForwarderOptions GetForwarderOptions()
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:
Expand Down
14 changes: 10 additions & 4 deletions src/Core/Services/EmailForwarders/FastmailForwarder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,21 @@

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
Expand Down Expand Up @@ -55,7 +60,8 @@ protected override async Task<HttpContent> GetContentAsync(IApiService apiServic
["state"] = "enabled",
["description"] = "",
["url"] = "",
["emailPrefix"] = ""
["emailPrefix"] = "",
["forDomain"] = options.Website ?? ""
}
}
},
Expand Down
10 changes: 7 additions & 3 deletions src/Core/Services/UsernameGenerationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ private async Task<string> GenerateForwardedEmailAliasAsync(UsernameGenerationOp
.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)
Expand All @@ -161,9 +168,6 @@ private async Task<string> GenerateForwardedEmailAliasAsync(UsernameGenerationOp
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;
Expand Down

0 comments on commit f98dfa6

Please sign in to comment.