diff --git a/libs/common/src/auth/enums/user-decryption-option.enum.ts b/libs/common/src/auth/enums/user-decryption-option.enum.ts deleted file mode 100644 index b72e01d6a2..0000000000 --- a/libs/common/src/auth/enums/user-decryption-option.enum.ts +++ /dev/null @@ -1,5 +0,0 @@ -export enum UserDecryptionOption { - MASTER_PASSWORD = "masterPasswordOption", - TRUSTED_DEVICE = "trustedDeviceOption", - KEY_CONNECTOR = "keyConnectorOption", -} diff --git a/libs/common/src/auth/login-strategies/login.strategy.ts b/libs/common/src/auth/login-strategies/login.strategy.ts index e1e5ab9530..95e19c6e67 100644 --- a/libs/common/src/auth/login-strategies/login.strategy.ts +++ b/libs/common/src/auth/login-strategies/login.strategy.ts @@ -5,7 +5,12 @@ import { LogService } from "../../abstractions/log.service"; import { MessagingService } from "../../abstractions/messaging.service"; import { PlatformUtilsService } from "../../abstractions/platformUtils.service"; import { StateService } from "../../abstractions/state.service"; -import { Account, AccountProfile, AccountTokens } from "../../models/domain/account"; +import { + Account, + AccountDecryptionOptions, + AccountProfile, + AccountTokens, +} from "../../models/domain/account"; import { KeysRequest } from "../../models/request/keys.request"; import { TokenService } from "../abstractions/token.service"; import { TwoFactorService } from "../abstractions/two-factor.service"; @@ -102,8 +107,6 @@ export abstract class LogInStrategy { protected async saveAccountInformation(tokenResponse: IdentityTokenResponse) { const accountInformation = await this.tokenService.decodeToken(tokenResponse.accessToken); - // TODO: add AccountDecryptionOptions to the account - await this.stateService.addAccount( new Account({ profile: { @@ -126,6 +129,9 @@ export abstract class LogInStrategy { refreshToken: tokenResponse.refreshToken, }, }, + decryptionOptions: AccountDecryptionOptions.fromResponse( + tokenResponse.userDecryptionOptions + ), }) ); } diff --git a/libs/common/src/auth/models/domain/user-decryption-options/key-connector-user-decryption-option.model.ts b/libs/common/src/auth/models/domain/user-decryption-options/key-connector-user-decryption-option.model.ts deleted file mode 100644 index e24ebcfd3f..0000000000 --- a/libs/common/src/auth/models/domain/user-decryption-options/key-connector-user-decryption-option.model.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { Jsonify } from "type-fest"; - -import { UserDecryptionOptionBase } from "./user-decryption-options-base.model"; - -export class KeyConnectorUserDecryptionOption extends UserDecryptionOptionBase { - keyConnectorUrl: string; - constructor(data: Jsonify) { - super(data.enabled); - this.keyConnectorUrl = data.keyConnectorUrl; - } -} diff --git a/libs/common/src/auth/models/domain/user-decryption-options/master-password-user-decryption-option.model.ts b/libs/common/src/auth/models/domain/user-decryption-options/master-password-user-decryption-option.model.ts deleted file mode 100644 index b36e192334..0000000000 --- a/libs/common/src/auth/models/domain/user-decryption-options/master-password-user-decryption-option.model.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { UserDecryptionOptionBase } from "./user-decryption-options-base.model"; - -export class MasterPasswordUserDecryptionOption extends UserDecryptionOptionBase { - constructor(enabled: boolean) { - super(enabled); - } -} diff --git a/libs/common/src/auth/models/domain/user-decryption-options/trusted-device-user-decryption-option.model.ts b/libs/common/src/auth/models/domain/user-decryption-options/trusted-device-user-decryption-option.model.ts deleted file mode 100644 index 7f6e0707c3..0000000000 --- a/libs/common/src/auth/models/domain/user-decryption-options/trusted-device-user-decryption-option.model.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { Jsonify } from "type-fest"; - -import { UserDecryptionOptionBase } from "./user-decryption-options-base.model"; - -export class TrustedDeviceUserDecryptionOption extends UserDecryptionOptionBase { - hasAdminApproval: boolean; - constructor(data: Jsonify) { - super(data.enabled); - this.hasAdminApproval = data.hasAdminApproval; - } -} diff --git a/libs/common/src/auth/models/domain/user-decryption-options/user-decryption-options-base.model.ts b/libs/common/src/auth/models/domain/user-decryption-options/user-decryption-options-base.model.ts deleted file mode 100644 index 6804a1d8fa..0000000000 --- a/libs/common/src/auth/models/domain/user-decryption-options/user-decryption-options-base.model.ts +++ /dev/null @@ -1,6 +0,0 @@ -export class UserDecryptionOptionBase { - enabled: boolean; - constructor(enabled: boolean) { - this.enabled = enabled; - } -} diff --git a/libs/common/src/auth/models/response/identity-token.response.ts b/libs/common/src/auth/models/response/identity-token.response.ts index 6c649c6863..4b6cf0f1ca 100644 --- a/libs/common/src/auth/models/response/identity-token.response.ts +++ b/libs/common/src/auth/models/response/identity-token.response.ts @@ -1,13 +1,8 @@ import { KdfType } from "../../../enums"; import { BaseResponse } from "../../../models/response/base.response"; -import { UserDecryptionOption } from "../../enums/user-decryption-option.enum"; -import { UserDecryptionOptionResponseType } from "../../types/user-decryption-option-response"; -import { KeyConnectorDecryptionOptionResponse } from "./key-connector-decryption-option.response"; -import { MasterPasswordDecryptionOptionResponse } from "./master-password-decryption-option.response"; import { MasterPasswordPolicyResponse } from "./master-password-policy.response"; -import { TrustedDeviceDecryptionOptionResponse } from "./trusted-device-decryption-option.response"; -import { UserDecryptionOptionResponse } from "./user-decryption-option.response"; +import { UserDecryptionOptionsResponse } from "./user-decryption-options/user-decryption-options.response"; export class IdentityTokenResponse extends BaseResponse { accessToken: string; @@ -28,7 +23,7 @@ export class IdentityTokenResponse extends BaseResponse { apiUseKeyConnector: boolean; keyConnectorUrl: string; - userDecryptionOptions: Array; + userDecryptionOptions: UserDecryptionOptionsResponse; constructor(response: any) { super(response); @@ -52,26 +47,8 @@ export class IdentityTokenResponse extends BaseResponse { this.getResponseProperty("MasterPasswordPolicy") ); - const serverUserDecryptionOptions = this.getResponseProperty("UserDecryptionOptions"); - - if (serverUserDecryptionOptions) { - this.userDecryptionOptions = serverUserDecryptionOptions.map( - (serverUserDecryptionOption: any) => { - const response = new UserDecryptionOptionResponse(serverUserDecryptionOption); - - switch (response.object) { - case UserDecryptionOption.MASTER_PASSWORD: { - return new MasterPasswordDecryptionOptionResponse(serverUserDecryptionOption); - } - case UserDecryptionOption.TRUSTED_DEVICE: { - return new TrustedDeviceDecryptionOptionResponse(serverUserDecryptionOption); - } - case UserDecryptionOption.KEY_CONNECTOR: { - return new KeyConnectorDecryptionOptionResponse(serverUserDecryptionOption); - } - } - } - ); - } + this.userDecryptionOptions = new UserDecryptionOptionsResponse( + this.getResponseProperty("UserDecryptionOptions") + ); } } diff --git a/libs/common/src/auth/models/response/key-connector-decryption-option.response.ts b/libs/common/src/auth/models/response/key-connector-decryption-option.response.ts deleted file mode 100644 index f5c157ce25..0000000000 --- a/libs/common/src/auth/models/response/key-connector-decryption-option.response.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { UserDecryptionOptionResponse } from "./user-decryption-option.response"; - -export class KeyConnectorDecryptionOptionResponse extends UserDecryptionOptionResponse { - keyConnectorUrl: string; - - constructor(response: any) { - super(response); - this.keyConnectorUrl = this.getResponseProperty("KeyConnectorUrl"); - } -} diff --git a/libs/common/src/auth/models/response/master-password-decryption-option.response.ts b/libs/common/src/auth/models/response/master-password-decryption-option.response.ts deleted file mode 100644 index 31493810f6..0000000000 --- a/libs/common/src/auth/models/response/master-password-decryption-option.response.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { UserDecryptionOptionResponse } from "./user-decryption-option.response"; - -export class MasterPasswordDecryptionOptionResponse extends UserDecryptionOptionResponse { - constructor(response: any) { - super(response); - } -} diff --git a/libs/common/src/auth/models/response/trusted-device-decryption-option.response.ts b/libs/common/src/auth/models/response/trusted-device-decryption-option.response.ts deleted file mode 100644 index cde2acd631..0000000000 --- a/libs/common/src/auth/models/response/trusted-device-decryption-option.response.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { UserDecryptionOptionResponse } from "./user-decryption-option.response"; - -export class TrustedDeviceDecryptionOptionResponse extends UserDecryptionOptionResponse { - hasAdminApproval: boolean; - - constructor(response: any) { - super(response); - this.hasAdminApproval = this.getResponseProperty("HasAdminApproval"); - } -} diff --git a/libs/common/src/auth/models/response/user-decryption-option.response.ts b/libs/common/src/auth/models/response/user-decryption-option.response.ts deleted file mode 100644 index 77e445c61a..0000000000 --- a/libs/common/src/auth/models/response/user-decryption-option.response.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { BaseResponse } from "../../../models/response/base.response"; - -export class UserDecryptionOptionResponse extends BaseResponse { - object: string; - - constructor(response: any) { - super(response); - - this.object = this.getResponseProperty("Object"); - } -} diff --git a/libs/common/src/auth/models/response/user-decryption-options/key-connector-user-decryption-option.response.ts b/libs/common/src/auth/models/response/user-decryption-options/key-connector-user-decryption-option.response.ts new file mode 100644 index 0000000000..4a7149aea1 --- /dev/null +++ b/libs/common/src/auth/models/response/user-decryption-options/key-connector-user-decryption-option.response.ts @@ -0,0 +1,10 @@ +import { BaseResponse } from "../../../../models/response/base.response"; + +export class KeyConnectorUserDecryptionOptionResponse extends BaseResponse { + keyConnectorUrl: string; + + constructor(response: any) { + super(response); + this.keyConnectorUrl = this.getResponseProperty("KeyConnectorUrl"); + } +} diff --git a/libs/common/src/auth/models/response/user-decryption-options/trusted-device-user-decryption-option.response.ts b/libs/common/src/auth/models/response/user-decryption-options/trusted-device-user-decryption-option.response.ts new file mode 100644 index 0000000000..d7375c370f --- /dev/null +++ b/libs/common/src/auth/models/response/user-decryption-options/trusted-device-user-decryption-option.response.ts @@ -0,0 +1,10 @@ +import { BaseResponse } from "../../../../models/response/base.response"; + +export class TrustedDeviceUserDecryptionOptionResponse extends BaseResponse { + hasAdminApproval: boolean; + + constructor(response: any) { + super(response); + this.hasAdminApproval = this.getResponseProperty("HasAdminApproval"); + } +} diff --git a/libs/common/src/auth/models/response/user-decryption-options/user-decryption-options.response.ts b/libs/common/src/auth/models/response/user-decryption-options/user-decryption-options.response.ts new file mode 100644 index 0000000000..196bb28a94 --- /dev/null +++ b/libs/common/src/auth/models/response/user-decryption-options/user-decryption-options.response.ts @@ -0,0 +1,27 @@ +import { BaseResponse } from "../../../../models/response/base.response"; + +import { KeyConnectorUserDecryptionOptionResponse } from "./key-connector-user-decryption-option.response"; +import { TrustedDeviceUserDecryptionOptionResponse } from "./trusted-device-user-decryption-option.response"; + +export class UserDecryptionOptionsResponse extends BaseResponse { + hasMasterPassword: boolean; + trustedDeviceOption?: TrustedDeviceUserDecryptionOptionResponse; + keyConnectorOption?: KeyConnectorUserDecryptionOptionResponse; + + constructor(response: any) { + super(response); + + this.hasMasterPassword = this.getResponseProperty("HasMasterPassword"); + + if (response.TrustedDeviceOption) { + this.trustedDeviceOption = new TrustedDeviceUserDecryptionOptionResponse( + this.getResponseProperty("TrustedDeviceOption") + ); + } + if (response.KeyConnectorOption) { + this.keyConnectorOption = new KeyConnectorUserDecryptionOptionResponse( + this.getResponseProperty("KeyConnectorOption") + ); + } + } +} diff --git a/libs/common/src/auth/types/user-decryption-option-response.ts b/libs/common/src/auth/types/user-decryption-option-response.ts deleted file mode 100644 index 623928aa23..0000000000 --- a/libs/common/src/auth/types/user-decryption-option-response.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { KeyConnectorDecryptionOptionResponse } from "../models/response/key-connector-decryption-option.response"; -import { MasterPasswordDecryptionOptionResponse } from "../models/response/master-password-decryption-option.response"; -import { TrustedDeviceDecryptionOptionResponse } from "../models/response/trusted-device-decryption-option.response"; - -export type UserDecryptionOptionResponseType = - | MasterPasswordDecryptionOptionResponse - | TrustedDeviceDecryptionOptionResponse - | KeyConnectorDecryptionOptionResponse; diff --git a/libs/common/src/models/domain/account.ts b/libs/common/src/models/domain/account.ts index 838742bb47..b95ef3b309 100644 --- a/libs/common/src/models/domain/account.ts +++ b/libs/common/src/models/domain/account.ts @@ -8,15 +8,11 @@ import { ProviderData } from "../../admin-console/models/data/provider.data"; import { Policy } from "../../admin-console/models/domain/policy"; import { CollectionView } from "../../admin-console/models/view/collection.view"; import { AuthenticationStatus } from "../../auth/enums/authentication-status"; -import { UserDecryptionOption } from "../../auth/enums/user-decryption-option.enum"; import { EnvironmentUrls } from "../../auth/models/domain/environment-urls"; import { ForceResetPasswordReason } from "../../auth/models/domain/force-reset-password-reason"; -import { KeyConnectorUserDecryptionOption } from "../../auth/models/domain/user-decryption-options/key-connector-user-decryption-option.model"; -import { MasterPasswordUserDecryptionOption } from "../../auth/models/domain/user-decryption-options/master-password-user-decryption-option.model"; -import { TrustedDeviceUserDecryptionOption } from "../../auth/models/domain/user-decryption-options/trusted-device-user-decryption-option.model"; -import { KeyConnectorDecryptionOptionResponse } from "../../auth/models/response/key-connector-decryption-option.response"; -import { TrustedDeviceDecryptionOptionResponse } from "../../auth/models/response/trusted-device-decryption-option.response"; -import { UserDecryptionOptionResponseType } from "../../auth/types/user-decryption-option-response"; +import { KeyConnectorUserDecryptionOptionResponse } from "../../auth/models/response/user-decryption-options/key-connector-user-decryption-option.response"; +import { TrustedDeviceUserDecryptionOptionResponse } from "../../auth/models/response/user-decryption-options/trusted-device-user-decryption-option.response"; +import { UserDecryptionOptionsResponse } from "../../auth/models/response/user-decryption-options/user-decryption-options.response"; import { KdfType, UriMatchType } from "../../enums"; import { Utils } from "../../misc/utils"; import { GeneratedPasswordHistory } from "../../tools/generator/password"; @@ -277,9 +273,9 @@ export class AccountTokens { } export class AccountDecryptionOptions { - [UserDecryptionOption.MASTER_PASSWORD]?: MasterPasswordUserDecryptionOption; - [UserDecryptionOption.TRUSTED_DEVICE]?: TrustedDeviceUserDecryptionOption; - [UserDecryptionOption.KEY_CONNECTOR]?: KeyConnectorUserDecryptionOption; + hasMasterPassword: boolean; + trustedDeviceOption?: TrustedDeviceUserDecryptionOptionResponse; + keyConnectorOption?: KeyConnectorUserDecryptionOptionResponse; constructor(init?: Partial) { if (init) { @@ -287,42 +283,21 @@ export class AccountDecryptionOptions { } } - static fromIdTokenResponse( - // serverUserDecryptionOptions: Array<{ Object: string; [key: string]: any }> - userDecryptionOptionResponse: Array - ) { - const accountDecryptionOptions = new AccountDecryptionOptions(); - - // Convert UserDecryptionOptions array to dictionary - for (const optionResponse of userDecryptionOptionResponse) { - const type = optionResponse.object as UserDecryptionOption; - - switch (type) { - case UserDecryptionOption.MASTER_PASSWORD: - accountDecryptionOptions[UserDecryptionOption.MASTER_PASSWORD] = - new MasterPasswordUserDecryptionOption(true); - break; - case UserDecryptionOption.TRUSTED_DEVICE: - accountDecryptionOptions[UserDecryptionOption.TRUSTED_DEVICE] = - new TrustedDeviceUserDecryptionOption({ - enabled: true, - hasAdminApproval: (optionResponse as TrustedDeviceDecryptionOptionResponse) - .hasAdminApproval, - }); - break; - case UserDecryptionOption.KEY_CONNECTOR: - accountDecryptionOptions[UserDecryptionOption.KEY_CONNECTOR] = - new KeyConnectorUserDecryptionOption({ - enabled: true, - keyConnectorUrl: (optionResponse as KeyConnectorDecryptionOptionResponse) - .keyConnectorUrl, - }); - break; - default: - continue; - } + static fromResponse(response: UserDecryptionOptionsResponse): AccountDecryptionOptions { + if (response == null) { + return null; + } + + const accountDecryptionOptions = new AccountDecryptionOptions(); + accountDecryptionOptions.hasMasterPassword = response.hasMasterPassword; + + if (response.trustedDeviceOption) { + accountDecryptionOptions.trustedDeviceOption = response.trustedDeviceOption; + } + + if (response.keyConnectorOption) { + accountDecryptionOptions.keyConnectorOption = response.keyConnectorOption; } - return accountDecryptionOptions; } static fromJSON(obj: Jsonify): AccountDecryptionOptions {