mirror of
https://github.com/bitwarden/browser
synced 2025-12-16 08:13:42 +00:00
[SG-515] add fastmail integration for generator (#3318)
* add fastmail integration for generator * prettier * introduce forwarder interface and implementations
This commit is contained in:
67
libs/common/src/emailForwarders/fastmailForwarder.ts
Normal file
67
libs/common/src/emailForwarders/fastmailForwarder.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import { ApiService } from "../abstractions/api.service";
|
||||
|
||||
import { Forwarder } from "./forwarder";
|
||||
import { ForwarderOptions } from "./forwarderOptions";
|
||||
|
||||
export class FastmailForwarder implements Forwarder {
|
||||
async generate(apiService: ApiService, options: ForwarderOptions): Promise<string> {
|
||||
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 requestInit: RequestInit = {
|
||||
redirect: "manual",
|
||||
cache: "no-store",
|
||||
method: "POST",
|
||||
headers: new Headers({
|
||||
Authorization: "Bearer " + options.apiKey,
|
||||
"Content-Type": "application/json",
|
||||
}),
|
||||
};
|
||||
const url = "https://api.fastmail.com/jmap/api/";
|
||||
requestInit.body = JSON.stringify({
|
||||
using: ["https://www.fastmail.com/dev/maskedemail", "urn:ietf:params:jmap:core"],
|
||||
methodCalls: [
|
||||
[
|
||||
"MaskedEmail/set",
|
||||
{
|
||||
accountId: "u" + options.fastmail.accountId,
|
||||
create: {
|
||||
"new-masked-email": {
|
||||
state: "enabled",
|
||||
description:
|
||||
(options.website != null ? options.website + " - " : "") +
|
||||
"Generated by Bitwarden",
|
||||
url: options.website,
|
||||
emailPrefix: options.fastmail.prefix,
|
||||
},
|
||||
},
|
||||
},
|
||||
"0",
|
||||
],
|
||||
],
|
||||
});
|
||||
const request = new Request(url, requestInit);
|
||||
const response = await apiService.nativeFetch(request);
|
||||
if (response.status === 200) {
|
||||
const json = await response.json();
|
||||
if (
|
||||
json.methodResponses != null &&
|
||||
json.methodResponses.length > 0 &&
|
||||
json.methodResponses[0].length > 0
|
||||
) {
|
||||
if (json.methodResponses[0][0] === "MaskedEmail/set") {
|
||||
return json.methodResponses[0][1]?.created?.["new-masked-email"]?.email;
|
||||
} else if (json.methodResponses[0][0] === "error") {
|
||||
throw "Fastmail error: " + json.methodResponses[0][1]?.description;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (response.status === 401) {
|
||||
throw "Invalid Fastmail API token.";
|
||||
}
|
||||
throw "Unknown Fastmail error occurred.";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user