2021-06-15 21:02:04 +02:00
|
|
|
import { Component } from "@angular/core";
|
2022-06-08 21:56:26 +02:00
|
|
|
import { ActivatedRoute, Params, Router } from "@angular/router";
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2022-06-14 17:10:53 +02:00
|
|
|
import { ApiService } from "@bitwarden/common/abstractions/api.service";
|
|
|
|
import { CryptoService } from "@bitwarden/common/abstractions/crypto.service";
|
|
|
|
import { I18nService } from "@bitwarden/common/abstractions/i18n.service";
|
|
|
|
import { LogService } from "@bitwarden/common/abstractions/log.service";
|
2022-12-19 19:56:16 +01:00
|
|
|
import { OrganizationUserService } from "@bitwarden/common/abstractions/organization-user/organization-user.service";
|
|
|
|
import { OrganizationUserAcceptRequest } from "@bitwarden/common/abstractions/organization-user/requests";
|
2022-08-24 18:33:05 +02:00
|
|
|
import { OrganizationApiServiceAbstraction } from "@bitwarden/common/abstractions/organization/organization-api.service.abstraction";
|
2022-06-14 17:10:53 +02:00
|
|
|
import { PlatformUtilsService } from "@bitwarden/common/abstractions/platformUtils.service";
|
2022-08-08 11:04:36 +02:00
|
|
|
import { PolicyApiServiceAbstraction } from "@bitwarden/common/abstractions/policy/policy-api.service.abstraction";
|
|
|
|
import { PolicyService } from "@bitwarden/common/abstractions/policy/policy.service.abstraction";
|
2022-06-14 17:10:53 +02:00
|
|
|
import { StateService } from "@bitwarden/common/abstractions/state.service";
|
|
|
|
import { Utils } from "@bitwarden/common/misc/utils";
|
|
|
|
import { Policy } from "@bitwarden/common/models/domain/policy";
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-06-15 21:02:04 +02:00
|
|
|
import { BaseAcceptComponent } from "../common/base.accept.component";
|
2018-07-12 22:05:42 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: "app-accept-organization",
|
|
|
|
templateUrl: "accept-organization.component.html",
|
|
|
|
})
|
2021-06-15 21:02:04 +02:00
|
|
|
export class AcceptOrganizationComponent extends BaseAcceptComponent {
|
2018-07-12 22:05:42 +02:00
|
|
|
orgName: string;
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-06-15 21:02:04 +02:00
|
|
|
protected requiredParameters: string[] = ["organizationId", "organizationUserId", "token"];
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-12-14 17:10:26 +01:00
|
|
|
constructor(
|
|
|
|
router: Router,
|
|
|
|
platformUtilsService: PlatformUtilsService,
|
|
|
|
i18nService: I18nService,
|
|
|
|
route: ActivatedRoute,
|
|
|
|
private apiService: ApiService,
|
|
|
|
stateService: StateService,
|
|
|
|
private cryptoService: CryptoService,
|
2022-08-08 11:04:36 +02:00
|
|
|
private policyApiService: PolicyApiServiceAbstraction,
|
2021-12-14 17:10:26 +01:00
|
|
|
private policyService: PolicyService,
|
2022-08-24 18:33:05 +02:00
|
|
|
private logService: LogService,
|
2022-12-19 19:56:16 +01:00
|
|
|
private organizationApiService: OrganizationApiServiceAbstraction,
|
|
|
|
private organizationUserService: OrganizationUserService
|
2021-12-14 17:10:26 +01:00
|
|
|
) {
|
|
|
|
super(router, platformUtilsService, i18nService, route, stateService);
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2022-06-08 21:56:26 +02:00
|
|
|
async authedHandler(qParams: Params): Promise<void> {
|
|
|
|
this.actionPromise = this.prepareAcceptRequest(qParams).then(async (request) => {
|
2022-12-19 19:56:16 +01:00
|
|
|
await this.organizationUserService.postOrganizationUserAccept(
|
2021-06-02 18:35:49 +02:00
|
|
|
qParams.organizationId,
|
2021-06-15 21:02:04 +02:00
|
|
|
qParams.organizationUserId,
|
2021-12-17 15:57:11 +01:00
|
|
|
request
|
2021-12-14 17:10:26 +01:00
|
|
|
);
|
2022-06-08 21:56:26 +02:00
|
|
|
});
|
2021-06-15 21:02:04 +02:00
|
|
|
|
|
|
|
await this.actionPromise;
|
|
|
|
this.platformUtilService.showToast(
|
|
|
|
"success",
|
|
|
|
this.i18nService.t("inviteAccepted"),
|
|
|
|
this.i18nService.t("inviteAcceptedDesc"),
|
2018-07-25 15:10:24 +02:00
|
|
|
{ timeout: 10000 }
|
2021-06-15 21:02:04 +02:00
|
|
|
);
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-12-14 17:10:26 +01:00
|
|
|
await this.stateService.setOrganizationInvitation(null);
|
|
|
|
this.router.navigate(["/vault"]);
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2022-06-08 21:56:26 +02:00
|
|
|
async unauthedHandler(qParams: Params): Promise<void> {
|
2021-06-15 21:02:04 +02:00
|
|
|
this.orgName = qParams.organizationName;
|
|
|
|
if (this.orgName != null) {
|
2021-12-07 20:41:45 +01:00
|
|
|
// Fix URL encoding of space issue with Angular
|
|
|
|
this.orgName = this.orgName.replace(/\+/g, " ");
|
2021-06-15 21:02:04 +02:00
|
|
|
}
|
2021-12-14 17:10:26 +01:00
|
|
|
await this.stateService.setOrganizationInvitation(qParams);
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
|
|
|
|
2022-06-08 21:56:26 +02:00
|
|
|
private async prepareAcceptRequest(qParams: Params): Promise<OrganizationUserAcceptRequest> {
|
|
|
|
const request = new OrganizationUserAcceptRequest();
|
|
|
|
request.token = qParams.token;
|
|
|
|
|
|
|
|
if (await this.performResetPasswordAutoEnroll(qParams)) {
|
2022-08-24 18:33:05 +02:00
|
|
|
const response = await this.organizationApiService.getKeys(qParams.organizationId);
|
2022-06-08 21:56:26 +02:00
|
|
|
|
|
|
|
if (response == null) {
|
|
|
|
throw new Error(this.i18nService.t("resetPasswordOrgKeysError"));
|
|
|
|
}
|
|
|
|
|
|
|
|
const publicKey = Utils.fromB64ToArray(response.publicKey);
|
|
|
|
|
|
|
|
// RSA Encrypt user's encKey.key with organization public key
|
|
|
|
const encKey = await this.cryptoService.getEncKey();
|
|
|
|
const encryptedKey = await this.cryptoService.rsaEncrypt(encKey.key, publicKey.buffer);
|
|
|
|
|
|
|
|
// Add reset password key to accept request
|
|
|
|
request.resetPasswordKey = encryptedKey.encryptedString;
|
|
|
|
}
|
|
|
|
return request;
|
|
|
|
}
|
|
|
|
|
2022-08-24 18:33:05 +02:00
|
|
|
private async performResetPasswordAutoEnroll(qParams: Params): Promise<boolean> {
|
2021-06-02 18:35:49 +02:00
|
|
|
let policyList: Policy[] = null;
|
2021-12-17 15:57:11 +01:00
|
|
|
try {
|
2022-08-08 11:04:36 +02:00
|
|
|
const policies = await this.policyApiService.getPoliciesByToken(
|
2021-06-02 18:35:49 +02:00
|
|
|
qParams.organizationId,
|
|
|
|
qParams.token,
|
|
|
|
qParams.email,
|
|
|
|
qParams.organizationUserId
|
2021-12-17 15:57:11 +01:00
|
|
|
);
|
2021-06-02 18:35:49 +02:00
|
|
|
policyList = this.policyService.mapPoliciesFromToken(policies);
|
2021-10-20 18:30:04 +02:00
|
|
|
} catch (e) {
|
|
|
|
this.logService.error(e);
|
2018-07-12 22:05:42 +02:00
|
|
|
}
|
2021-06-02 18:35:49 +02:00
|
|
|
|
|
|
|
if (policyList != null) {
|
|
|
|
const result = this.policyService.getResetPasswordPolicyOptions(
|
|
|
|
policyList,
|
|
|
|
qParams.organizationId
|
|
|
|
);
|
|
|
|
// Return true if policy enabled and auto-enroll enabled
|
|
|
|
return result[1] && result[0].autoEnrollEnabled;
|
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-06-02 18:35:49 +02:00
|
|
|
return false;
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2018-07-12 22:05:42 +02:00
|
|
|
}
|