1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-14 23:33:31 +00:00

feedback from fastmail (#3375)

* feedback from fastmail

* follow redirects for getting account id api
This commit is contained in:
Kyle Spearrin
2022-08-25 10:54:02 -04:00
committed by GitHub
parent bd88b5b365
commit bb70113279
10 changed files with 32 additions and 55 deletions

View File

@@ -8,9 +8,12 @@ export class FastmailForwarder implements Forwarder {
if (options.apiKey == null || options.apiKey === "") {
throw "Invalid Fastmail API token.";
}
if (options?.fastmail.accountId == null || options.fastmail.accountId === "") {
throw "Invalid Fastmail account ID.";
const accountId = await this.getAccountId(apiService, options);
if (accountId == null || accountId === "") {
throw "Unable to obtain Fastmail masked email account ID.";
}
const requestInit: RequestInit = {
redirect: "manual",
cache: "no-store",
@@ -27,13 +30,11 @@ export class FastmailForwarder implements Forwarder {
[
"MaskedEmail/set",
{
accountId: "u" + options.fastmail.accountId,
accountId: accountId,
create: {
"new-masked-email": {
state: "enabled",
description:
(options.website != null ? options.website + " - " : "") +
"Generated by Bitwarden",
description: "",
url: options.website,
emailPrefix: options.fastmail.prefix,
},
@@ -59,9 +60,29 @@ export class FastmailForwarder implements Forwarder {
}
}
}
if (response.status === 401) {
if (response.status === 401 || response.status === 403) {
throw "Invalid Fastmail API token.";
}
throw "Unknown Fastmail error occurred.";
}
private async getAccountId(apiService: ApiService, options: ForwarderOptions): Promise<string> {
const requestInit: RequestInit = {
cache: "no-store",
method: "GET",
headers: new Headers({
Authorization: "Bearer " + options.apiKey,
}),
};
const url = "https://api.fastmail.com/.well-known/jmap";
const request = new Request(url, requestInit);
const response = await apiService.nativeFetch(request);
if (response.status === 200) {
const json = await response.json();
if (json.primaryAccounts != null) {
return json.primaryAccounts["https://www.fastmail.com/dev/maskedemail"];
}
}
return null;
}
}

View File

@@ -6,7 +6,6 @@ export class ForwarderOptions {
}
export class FastmailForwarderOptions {
accountId: string;
prefix: string;
}

View File

@@ -131,7 +131,6 @@ export class UsernameGenerationService implements BaseUsernameGenerationService
} else if (o.forwardedService === "fastmail") {
forwarder = new FastmailForwarder();
forwarderOptions.apiKey = o.forwardedFastmailApiToken;
forwarderOptions.fastmail.accountId = o.forwardedFastmailAccountId;
} else if (o.forwardedService === "duckduckgo") {
forwarder = new DuckDuckGoForwarder();
forwarderOptions.apiKey = o.forwardedDuckDuckGoToken;