mirror of
https://github.com/bitwarden/mobile
synced 2025-12-20 02:03:49 +00:00
[PM-1835] Add ForwardEmail alias to Username Generator (#2803)
* Add ForwardEmail alias to Username Generator * remove unnecessary initializer * Corrected order of alias Generators * PM-4307 - Trigger ForwardEmailDomainName PropertyChanged after initialization
This commit is contained in:
52
src/Core/Services/EmailForwarders/ForwardEmailForwarder.cs
Normal file
52
src/Core/Services/EmailForwarders/ForwardEmailForwarder.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Abstractions;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Bit.Core.Services.EmailForwarders
|
||||
{
|
||||
public class ForwardEmailForwarderOptions : ForwarderOptions
|
||||
{
|
||||
public string DomainName { get; set; }
|
||||
}
|
||||
|
||||
public class ForwardEmailForwarder : BaseForwarder<ForwardEmailForwarderOptions>
|
||||
{
|
||||
private readonly string _domain;
|
||||
protected override string RequestUri => $"https://api.forwardemail.net/v1/domains/{_domain}/aliases";
|
||||
|
||||
public ForwardEmailForwarder(string domain)
|
||||
{
|
||||
_domain = domain;
|
||||
}
|
||||
|
||||
protected override bool CanGenerate(ForwardEmailForwarderOptions options)
|
||||
{
|
||||
return !string.IsNullOrWhiteSpace(options.ApiKey) && !string.IsNullOrWhiteSpace(options.DomainName);
|
||||
}
|
||||
|
||||
protected override void ConfigureHeaders(HttpRequestHeaders headers, ForwardEmailForwarderOptions options)
|
||||
{
|
||||
headers.Add("Authorization", $"Basic {Convert.ToBase64String(Encoding.UTF8.GetBytes(options.ApiKey + ":"))}");
|
||||
}
|
||||
|
||||
protected override Task<HttpContent> GetContentAsync(IApiService apiService, ForwardEmailForwarderOptions options)
|
||||
{
|
||||
return Task.FromResult<HttpContent>(new StringContent(
|
||||
JsonConvert.SerializeObject(
|
||||
new
|
||||
{
|
||||
description = "Generated by Bitwarden."
|
||||
}), Encoding.UTF8, "application/json"));
|
||||
}
|
||||
|
||||
protected override string HandleResponse(JObject result)
|
||||
{
|
||||
return $"{result["name"]}@{result["domain"]?["name"] ?? _domain}";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -141,6 +141,13 @@ namespace Bit.Core.Services
|
||||
.GenerateAsync(_apiService, (AnonAddyForwarderOptions)options.GetForwarderOptions());
|
||||
}
|
||||
|
||||
if (options.ServiceType == ForwardedEmailServiceType.ForwardEmail)
|
||||
{
|
||||
var forwardedEmailOptions = (ForwardEmailForwarderOptions)options.GetForwarderOptions();
|
||||
return await new ForwardEmailForwarder(forwardedEmailOptions.DomainName)
|
||||
.GenerateAsync(_apiService, forwardedEmailOptions);
|
||||
}
|
||||
|
||||
BaseForwarder<ForwarderOptions> simpleForwarder = null;
|
||||
|
||||
switch (options.ServiceType)
|
||||
|
||||
Reference in New Issue
Block a user