1
0
mirror of https://github.com/bitwarden/mobile synced 2025-12-18 01:03:24 +00:00

[SG-516] Additional forwarded email providers for username generator - mobile (#2304)

* [SG-516] Added DuckDuckGo provider

* [SG-516] Add Fastmail as generator provider

* [SG-516] code clean up

* [SG-516] Default to service empty if first time on screen. Order services by alphabetic order.

* [SG-516] Removed unnecessary prop.

* [PS-2278] Fixed inverted eye bug.

* [SG-516] Add icon glyph converter

* [SG-516] Fixed enum default value and ordering
This commit is contained in:
André Bispo
2023-01-26 13:53:48 +00:00
committed by GitHub
parent b8d53b0f81
commit 68a6449339
9 changed files with 264 additions and 26 deletions

View File

@@ -760,6 +760,14 @@ namespace Bit.Core.Services
case ForwardedEmailServiceType.SimpleLogin:
requestMessage.Headers.Add("Authentication", config.ApiToken);
break;
case ForwardedEmailServiceType.DuckDuckGo:
requestMessage.Headers.Add("Authorization", $"Bearer {config.ApiToken}");
break;
case ForwardedEmailServiceType.Fastmail:
requestMessage.Headers.Add("Authorization", $"Bearer {config.ApiToken}");
requestMessage.Content = new StringContent(await CreateFastmailRequest(config.ApiToken),
Encoding.UTF8, "application/json");
break;
}
HttpResponseMessage response;
@@ -790,12 +798,99 @@ namespace Bit.Core.Services
return result["full_address"]?.ToString();
case ForwardedEmailServiceType.SimpleLogin:
return result["alias"]?.ToString();
case ForwardedEmailServiceType.DuckDuckGo:
return $"{result["address"]?.ToString()}@duck.com";
case ForwardedEmailServiceType.Fastmail:
return HandleFastMailResponse(result);
default:
return string.Empty;
}
}
}
private string HandleFastMailResponse(JObject result)
{
if (result["methodResponses"] == null || !result["methodResponses"].HasValues ||
!result["methodResponses"][0].HasValues)
{
throw new Exception("Fastmail error: could not parse response.");
}
if (result["methodResponses"][0][0].ToString() == "MaskedEmail/set")
{
if (result["methodResponses"][0][1]?["created"]?["new-masked-email"] != null)
{
return result["methodResponses"][0][1]?["created"]?["new-masked-email"]?["email"].ToString();
}
if (result["methodResponses"][0][1]?["notCreated"]?["new-masked-email"] != null)
{
throw new Exception("Fastmail error: " +
result["methodResponses"][0][1]?["created"]?["new-masked-email"]?["description"].ToString());
}
}
else if (result["methodResponses"][0][0].ToString() == "error")
{
throw new Exception("Fastmail error: " + result["methodResponses"][0][1]?["description"].ToString());
}
throw new Exception("Fastmail error: could not parse response.");
}
private async Task<string> CreateFastmailRequest(string apiKey)
{
using (var httpclient = new HttpClient())
{
HttpResponseMessage response;
try
{
httpclient.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
httpclient.DefaultRequestHeaders.Add("Accept", "application/json");
response = await httpclient.GetAsync(new Uri("https://api.fastmail.com/jmap/session"));
}
catch (Exception e)
{
throw new ApiException(HandleWebError(e));
}
if (!response.IsSuccessStatusCode)
{
throw new ApiException(new ErrorResponse
{
StatusCode = response.StatusCode,
Message = $"Fastmail error: {(int)response.StatusCode} {response.ReasonPhrase}."
});
}
var result = JObject.Parse(await response.Content.ReadAsStringAsync());
var accountId = result["primaryAccounts"]?["https://www.fastmail.com/dev/maskedemail"]?.ToString();
var requestJObj = new JObject
{
new JProperty("using",
new JArray { "https://www.fastmail.com/dev/maskedemail", "urn:ietf:params:jmap:core" }),
new JProperty("methodCalls",
new JArray
{
new JArray
{
"MaskedEmail/set",
new JObject
{
["accountId"] = accountId,
["create"] = new JObject
{
["new-masked-email"] = new JObject
{
["state"] = "enabled",
["description"] = "",
["url"] = "",
["emailPrefix"] = ""
}
}
},
"0"
}
})
};
return requestJObj.ToString();
}
}
private ErrorResponse HandleWebError(Exception e)
{
return new ErrorResponse