-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PM-1060] Added new forwarder (Forward Email <https://forwardemail.net>…
…) (#4809) * Added new forwarder (Forward Email <https://forwardemail.net>) * fix: fixed Basic authorization header * fix: fixed returned email value * feat: added verbose message for end-users (e.g. "Not Found" vs. "Domain does not exist on your account." (automatically localized with i18n for user) * fix: fixed Buffer.from to Utils.fromBufferToB64 * fix: fixed fromBufferToB64 to fromUtf8ToB64 * Remove try-catch to properly display api errors --------- Co-authored-by: Daniel James Smith <djsmith@web.de>
- Loading branch information
Showing
9 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
libs/common/src/tools/generator/username/email-forwarders/forward-email-forwarder.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { ApiService } from "../../../../abstractions/api.service"; | ||
import { Utils } from "../../../../misc/utils"; | ||
|
||
import { Forwarder } from "./forwarder"; | ||
import { ForwarderOptions } from "./forwarder-options"; | ||
|
||
export class ForwardEmailForwarder implements Forwarder { | ||
async generate(apiService: ApiService, options: ForwarderOptions): Promise<string> { | ||
if (options.apiKey == null || options.apiKey === "") { | ||
throw "Invalid Forward Email API key."; | ||
} | ||
if (options.forwardemail?.domain == null || options.forwardemail.domain === "") { | ||
throw "Invalid Forward Email domain."; | ||
} | ||
const requestInit: RequestInit = { | ||
redirect: "manual", | ||
cache: "no-store", | ||
method: "POST", | ||
headers: new Headers({ | ||
Authorization: "Basic " + Utils.fromUtf8ToB64(options.apiKey + ":"), | ||
"Content-Type": "application/json", | ||
}), | ||
}; | ||
const url = `https://api.forwardemail.net/v1/domains/${options.forwardemail.domain}/aliases`; | ||
requestInit.body = JSON.stringify({ | ||
labels: options.website, | ||
description: | ||
(options.website != null ? "Website: " + options.website + ". " : "") + | ||
"Generated by Bitwarden.", | ||
}); | ||
const request = new Request(url, requestInit); | ||
const response = await apiService.nativeFetch(request); | ||
if (response.status === 200 || response.status === 201) { | ||
const json = await response.json(); | ||
return json?.name + "@" + (json?.domain?.name || options.forwardemail.domain); | ||
} | ||
if (response.status === 401) { | ||
throw "Invalid Forward Email API key."; | ||
} | ||
const json = await response.json(); | ||
if (json?.message != null) { | ||
throw "Forward Email error:\n" + json.message; | ||
} | ||
if (json?.error != null) { | ||
throw "Forward Email error:\n" + json.error; | ||
} | ||
throw "Unknown Forward Email error occurred."; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters