diff --git a/jslib b/jslib index e4cd0af2..a6482be6 160000 --- a/jslib +++ b/jslib @@ -1 +1 @@ -Subproject commit e4cd0af2f9ae924f9b749fefe3695f7c69135bf6 +Subproject commit a6482be6b3871989f7f00c6be8caeb65838d42fb diff --git a/src/app/services/services.module.ts b/src/app/services/services.module.ts index adebe8d6..22e7c034 100644 --- a/src/app/services/services.module.ts +++ b/src/app/services/services.module.ts @@ -8,6 +8,7 @@ import { MemoryStorageService } from "../../services/memoryStorage.service"; import { PasswordRepromptService } from "../../services/passwordReprompt.service"; import { StateService } from "../../services/state.service"; import { WebPlatformUtilsService } from "../../services/webPlatformUtils.service"; +import { KeyConnectorService } from "../../services/keyConnector.service"; import { EventService } from "./event.service"; import { ModalService } from "./modal.service"; @@ -49,6 +50,7 @@ import { StateService as StateServiceAbstraction } from "jslib-common/abstractio import { StateMigrationService as StateMigrationServiceAbstraction } from "jslib-common/abstractions/stateMigration.service"; import { StorageService as StorageServiceAbstraction } from "jslib-common/abstractions/storage.service"; import { VaultTimeoutService as VaultTimeoutServiceAbstraction } from "jslib-common/abstractions/vaultTimeout.service"; +import { KeyConnectorService as KeyConnectorServiceAbstraction } from "jslib-common/abstractions/keyConnector.service"; import { ThemeType } from "jslib-common/enums/themeType"; @@ -190,6 +192,10 @@ export function initFactory( provide: PasswordRepromptServiceAbstraction, useClass: PasswordRepromptService, }, + { + provide: KeyConnectorServiceAbstraction, + useClass: KeyConnectorService, + }, ], }) export class ServicesModule {} diff --git a/src/connectors/cme.ts b/src/connectors/cme.ts index 4538fb98..c1433e75 100644 --- a/src/connectors/cme.ts +++ b/src/connectors/cme.ts @@ -50,7 +50,7 @@ async function start() { } if (operation === "get") { - const getRequest = new Request(keyConnectorUrl.href + "/user-keys", { + const getRequest = new Request(keyConnectorUrl.href + "user-keys", { cache: "no-store", method: "GET", headers: new Headers({ @@ -68,7 +68,7 @@ async function start() { } success(new KeyConnectorUserKeyResponse(await response.json())); } else if (operation === "post") { - const postRequest = new Request(keyConnectorUrl.href + "/user-keys", { + const postRequest = new Request(keyConnectorUrl.href + "user-keys", { cache: "no-store", method: "POST", headers: new Headers({ @@ -116,7 +116,7 @@ function success(response: KeyConnectorUserKeyResponse) { return; } parent.postMessage( - "success|" + response != null && response.key != null ? response.key : "", + "success|" + (response != null && response.key != null ? response.key : ""), parentUrl ); sentSuccess = true; diff --git a/src/scss/plugins.scss b/src/scss/plugins.scss index 8c49f4d0..f585b295 100644 --- a/src/scss/plugins.scss +++ b/src/scss/plugins.scss @@ -30,6 +30,12 @@ width: 100%; } +#cme_iframe { + border: none; + height: 0; + width: 0; +} + .list-group-2fa { .logo-2fa { min-width: 100px; diff --git a/src/services/keyConnector.service.ts b/src/services/keyConnector.service.ts new file mode 100644 index 00000000..82b4fff7 --- /dev/null +++ b/src/services/keyConnector.service.ts @@ -0,0 +1,66 @@ +import { Injectable } from "@angular/core"; + +import { ApiService } from "jslib-common/abstractions/api.service"; +import { CryptoService } from "jslib-common/abstractions/crypto.service"; +import { EnvironmentService } from "jslib-common/abstractions/environment.service"; +import { I18nService } from "jslib-common/abstractions/i18n.service"; +import { LogService } from "jslib-common/abstractions/log.service"; +import { OrganizationService } from "jslib-common/abstractions/organization.service"; +import { PlatformUtilsService } from "jslib-common/abstractions/platformUtils.service"; +import { StateService } from "jslib-common/abstractions/state.service"; +import { TokenService } from "jslib-common/abstractions/token.service"; + +import { CMEIFrame } from "jslib-common/misc/cme_iframe"; +import { Utils } from "jslib-common/misc/utils"; +import { SymmetricCryptoKey } from "jslib-common/models/domain/symmetricCryptoKey"; + +import { KeyConnectorService as BaseKeyConnectorService } from "jslib-common/services/keyConnector.service"; + +@Injectable() +export class KeyConnectorService extends BaseKeyConnectorService { + constructor( + stateService: StateService, + cryptoService: CryptoService, + apiService: ApiService, + tokenService: TokenService, + logService: LogService, + organizationService: OrganizationService, + private environmentService: EnvironmentService, + private i18nService: I18nService, + private platformUtilsService: PlatformUtilsService + ) { + super(stateService, cryptoService, apiService, tokenService, logService, organizationService); + } + + async getAndSetKey(url: string): Promise { + const el = document.createElement("iframe"); + el.id = "cme_iframe"; + document.body.appendChild(el); + + const webVaultUrl = this.environmentService.getWebVaultUrl(); + + const promise: Promise = new Promise(async (resolve) => { + const iframe = new CMEIFrame( + window, + webVaultUrl, + (key: string) => { + const keyArr = Utils.fromB64ToArray(key); + const k = new SymmetricCryptoKey(keyArr); + this.cryptoService.setKey(k).then(resolve); + }, + (error: string) => { + this.platformUtilsService.showToast("error", null, error); + }, + (info: string) => { + this.logService.info(info); + } + ); + + iframe.init(await this.apiService.getActiveBearerToken(), url); + }); + + promise.finally(() => el.remove()); + + return promise; + } +}