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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 32 additions and 55 deletions

View File

@ -1965,13 +1965,9 @@
"apiKey": { "apiKey": {
"message": "API Key" "message": "API Key"
}, },
"accountId": {
"message": "Account ID",
"description": "ID is short for 'Identifier'"
},
"ssoKeyConnectorError": { "ssoKeyConnectorError": {
"message": "Key Connector error: make sure Key Connector is available and working correctly." "message": "Key Connector error: make sure Key Connector is available and working correctly."
}, },
"premiumSubcriptionRequired": { "premiumSubcriptionRequired": {
"message": "Premium subscription required" "message": "Premium subscription required"
}, },

View File

@ -401,21 +401,11 @@
<input <input
id="fastmail-apiToken" id="fastmail-apiToken"
type="password" type="password"
name="FastMailApiToken" name="FastmailApiToken"
[(ngModel)]="usernameOptions.forwardedFastmailApiToken" [(ngModel)]="usernameOptions.forwardedFastmailApiToken"
(blur)="saveUsernameOptions()" (blur)="saveUsernameOptions()"
/> />
</div> </div>
<div class="box-content-row" appBoxRow>
<label for="fastmail-accountId">{{ "accountId" | i18n }}</label>
<input
id="fastmail-accountId"
type="text"
name="FastmailAccountId"
[(ngModel)]="usernameOptions.forwardedFastmailAccountId"
(blur)="saveUsernameOptions()"
/>
</div>
</ng-container> </ng-container>
</div> </div>
</div> </div>

View File

@ -434,21 +434,11 @@
<input <input
id="fastmail-apiToken" id="fastmail-apiToken"
type="password" type="password"
name="FastMailApiToken" name="FastmailApiToken"
[(ngModel)]="usernameOptions.forwardedFastmailApiToken" [(ngModel)]="usernameOptions.forwardedFastmailApiToken"
(blur)="saveUsernameOptions()" (blur)="saveUsernameOptions()"
/> />
</div> </div>
<div class="box-content-row" appBoxRow>
<label for="fastmail-accountId">{{ "accountId" | i18n }}</label>
<input
id="fastmail-accountId"
type="text"
name="FastmailAccountId"
[(ngModel)]="usernameOptions.forwardedFastmailAccountId"
(blur)="saveUsernameOptions()"
/>
</div>
</ng-container> </ng-container>
</div> </div>
</div> </div>

View File

@ -1982,10 +1982,6 @@
"premiumSubcriptionRequired": { "premiumSubcriptionRequired": {
"message": "Premium subscription required" "message": "Premium subscription required"
}, },
"accountId": {
"message": "Account ID",
"description": "ID is short for 'Identifier'"
},
"organizationIsDisabled": { "organizationIsDisabled": {
"message": "Organization is disabled." "message": "Organization is disabled."
}, },

View File

@ -354,16 +354,6 @@
(blur)="saveUsernameOptions()" (blur)="saveUsernameOptions()"
/> />
</div> </div>
<div class="form-group col-4">
<label for="fastmail-accountId">{{ "accountId" | i18n }}</label>
<input
id="fastmail-accountId"
class="form-control"
type="text"
[(ngModel)]="usernameOptions.forwardedFastmailAccountId"
(blur)="saveUsernameOptions()"
/>
</div>
</div> </div>
</ng-container> </ng-container>
<div class="row" *ngIf="usernameOptions.type === 'subaddress'"> <div class="row" *ngIf="usernameOptions.type === 'subaddress'">

View File

@ -5312,9 +5312,5 @@
}, },
"numberOfUsers": { "numberOfUsers": {
"message": "Number of users" "message": "Number of users"
},
"accountId": {
"message": "Account ID",
"description": "ID is short for 'Identifier'"
} }
} }

View File

@ -74,7 +74,7 @@ export class GeneratorComponent implements OnInit {
{ name: "SimpleLogin", value: "simplelogin" }, { name: "SimpleLogin", value: "simplelogin" },
{ name: "AnonAddy", value: "anonaddy" }, { name: "AnonAddy", value: "anonaddy" },
{ name: "Firefox Relay", value: "firefoxrelay" }, { name: "Firefox Relay", value: "firefoxrelay" },
{ name: "FastMail", value: "fastmail" }, { name: "Fastmail", value: "fastmail" },
{ name: "DuckDuckGo", value: "duckduckgo" }, { name: "DuckDuckGo", value: "duckduckgo" },
]; ];
} }

View File

@ -8,9 +8,12 @@ export class FastmailForwarder implements Forwarder {
if (options.apiKey == null || options.apiKey === "") { if (options.apiKey == null || options.apiKey === "") {
throw "Invalid Fastmail API token."; 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 = { const requestInit: RequestInit = {
redirect: "manual", redirect: "manual",
cache: "no-store", cache: "no-store",
@ -27,13 +30,11 @@ export class FastmailForwarder implements Forwarder {
[ [
"MaskedEmail/set", "MaskedEmail/set",
{ {
accountId: "u" + options.fastmail.accountId, accountId: accountId,
create: { create: {
"new-masked-email": { "new-masked-email": {
state: "enabled", state: "enabled",
description: description: "",
(options.website != null ? options.website + " - " : "") +
"Generated by Bitwarden",
url: options.website, url: options.website,
emailPrefix: options.fastmail.prefix, 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 "Invalid Fastmail API token.";
} }
throw "Unknown Fastmail error occurred."; 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 { export class FastmailForwarderOptions {
accountId: string;
prefix: string; prefix: string;
} }

View File

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