import { Component, OnInit, ViewChild, ViewContainerRef } from "@angular/core"; import { ApiService } from "@bitwarden/common/abstractions/api.service"; import { UserVerificationService } from "@bitwarden/common/auth/abstractions/user-verification/user-verification.service.abstraction"; import { StateService } from "@bitwarden/common/platform/abstractions/state.service"; import { DialogService } from "@bitwarden/components"; import { ApiKeyComponent } from "./api-key.component"; @Component({ selector: "app-security-keys", templateUrl: "security-keys.component.html", }) export class SecurityKeysComponent implements OnInit { @ViewChild("viewUserApiKeyTemplate", { read: ViewContainerRef, static: true }) viewUserApiKeyModalRef: ViewContainerRef; @ViewChild("rotateUserApiKeyTemplate", { read: ViewContainerRef, static: true }) rotateUserApiKeyModalRef: ViewContainerRef; showChangeKdf = true; constructor( private userVerificationService: UserVerificationService, private stateService: StateService, private apiService: ApiService, private dialogService: DialogService, ) {} async ngOnInit() { this.showChangeKdf = await this.userVerificationService.hasMasterPassword(); } async viewUserApiKey() { const entityId = await this.stateService.getUserId(); await ApiKeyComponent.open(this.dialogService, { data: { keyType: "user", entityId: entityId, postKey: this.apiService.postUserApiKey.bind(this.apiService), scope: "api", grantType: "client_credentials", apiKeyTitle: "apiKey", apiKeyWarning: "userApiKeyWarning", apiKeyDescription: "userApiKeyDesc", }, }); } async rotateUserApiKey() { const entityId = await this.stateService.getUserId(); await ApiKeyComponent.open(this.dialogService, { data: { keyType: "user", isRotation: true, entityId: entityId, postKey: this.apiService.postUserRotateApiKey.bind(this.apiService), scope: "api", grantType: "client_credentials", apiKeyTitle: "apiKey", apiKeyWarning: "userApiKeyWarning", apiKeyDescription: "apiKeyRotateDesc", }, }); } }