2024-12-09 11:58:50 -08:00
|
|
|
// FIXME: Update this file to be type safe and remove this and next line
|
|
|
|
|
// @ts-strict-ignore
|
2021-07-22 12:28:45 -05:00
|
|
|
import { Directive, Input } from "@angular/core";
|
2024-03-21 17:09:44 +01:00
|
|
|
import { firstValueFrom } from "rxjs";
|
2021-07-22 12:28:45 -05:00
|
|
|
|
2023-02-06 16:53:37 -05:00
|
|
|
import { CaptchaIFrame } from "@bitwarden/common/auth/captcha-iframe";
|
2023-06-06 15:34:53 -05:00
|
|
|
import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service";
|
|
|
|
|
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
|
|
|
|
|
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
|
|
|
|
|
import { Utils } from "@bitwarden/common/platform/misc/utils";
|
2024-08-27 10:33:58 -07:00
|
|
|
import { ToastService } from "@bitwarden/components";
|
2021-07-22 12:28:45 -05:00
|
|
|
|
|
|
|
|
@Directive()
|
|
|
|
|
export abstract class CaptchaProtectedComponent {
|
|
|
|
|
@Input() captchaSiteKey: string = null;
|
|
|
|
|
captchaToken: string = null;
|
|
|
|
|
captcha: CaptchaIFrame;
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2021-07-22 12:28:45 -05:00
|
|
|
constructor(
|
|
|
|
|
protected environmentService: EnvironmentService,
|
|
|
|
|
protected i18nService: I18nService,
|
|
|
|
|
protected platformUtilsService: PlatformUtilsService,
|
2024-08-27 10:33:58 -07:00
|
|
|
protected toastService: ToastService,
|
2021-07-22 12:28:45 -05:00
|
|
|
) {}
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2021-07-22 12:28:45 -05:00
|
|
|
async setupCaptcha() {
|
2024-03-21 17:09:44 +01:00
|
|
|
const env = await firstValueFrom(this.environmentService.environment$);
|
|
|
|
|
const webVaultUrl = env.getWebVaultUrl();
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2021-07-22 12:28:45 -05:00
|
|
|
this.captcha = new CaptchaIFrame(
|
|
|
|
|
window,
|
|
|
|
|
webVaultUrl,
|
|
|
|
|
this.i18nService,
|
|
|
|
|
(token: string) => {
|
|
|
|
|
this.captchaToken = token;
|
|
|
|
|
},
|
|
|
|
|
(error: string) => {
|
2024-08-27 10:33:58 -07:00
|
|
|
this.toastService.showToast({
|
|
|
|
|
variant: "error",
|
|
|
|
|
title: this.i18nService.t("errorOccurred"),
|
|
|
|
|
message: error,
|
|
|
|
|
});
|
2021-07-22 12:28:45 -05:00
|
|
|
},
|
|
|
|
|
(info: string) => {
|
2024-08-27 10:33:58 -07:00
|
|
|
this.toastService.showToast({
|
|
|
|
|
variant: "info",
|
|
|
|
|
title: this.i18nService.t("info"),
|
|
|
|
|
message: info,
|
|
|
|
|
});
|
2021-07-22 12:28:45 -05:00
|
|
|
},
|
|
|
|
|
);
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
|
|
2021-07-22 12:28:45 -05:00
|
|
|
showCaptcha() {
|
|
|
|
|
return !Utils.isNullOrWhitespace(this.captchaSiteKey);
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
|
|
2021-07-22 12:28:45 -05:00
|
|
|
protected handleCaptchaRequired(response: { captchaSiteKey: string }): boolean {
|
|
|
|
|
if (Utils.isNullOrWhitespace(response.captchaSiteKey)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.captchaSiteKey = response.captchaSiteKey;
|
|
|
|
|
this.captcha.init(response.captchaSiteKey);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|