1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-10-09 05:57:40 +02:00
bitwarden-browser/src/app/accounts/accept-organization.component.ts

150 lines
6.4 KiB
TypeScript
Raw Normal View History

2018-07-12 22:05:42 +02:00
import {
Component,
OnInit,
} from '@angular/core';
2018-07-12 22:05:42 +02:00
import {
ActivatedRoute,
Router,
} from '@angular/router';
import {
Toast,
ToasterService,
} from 'angular2-toaster';
import { ApiService } from 'jslib/abstractions/api.service';
import { CryptoService } from 'jslib/abstractions/crypto.service';
2018-07-12 22:05:42 +02:00
import { I18nService } from 'jslib/abstractions/i18n.service';
import { PolicyService } from 'jslib/abstractions/policy.service';
import { StateService } from 'jslib/abstractions/state.service';
2018-07-12 22:05:42 +02:00
import { UserService } from 'jslib/abstractions/user.service';
import { Utils } from 'jslib/misc/utils';
import { Policy } from 'jslib/models/domain/policy';
2018-07-12 22:05:42 +02:00
import { OrganizationUserAcceptRequest } from 'jslib/models/request/organizationUserAcceptRequest';
import { OrganizationUserResetPasswordEnrollmentRequest } from 'jslib/models/request/organizationUserResetPasswordEnrollmentRequest';
2018-07-12 22:05:42 +02:00
@Component({
selector: 'app-accept-organization',
templateUrl: 'accept-organization.component.html',
})
export class AcceptOrganizationComponent implements OnInit {
loading = true;
authed = false;
orgName: string;
email: string;
actionPromise: Promise<any>;
constructor(private router: Router, private toasterService: ToasterService,
private i18nService: I18nService, private route: ActivatedRoute,
private apiService: ApiService, private userService: UserService,
private stateService: StateService, private cryptoService: CryptoService,
private policyService: PolicyService) { }
2018-07-12 22:05:42 +02:00
ngOnInit() {
let fired = false;
this.route.queryParams.subscribe(async qParams => {
2018-07-12 22:05:42 +02:00
if (fired) {
return;
}
fired = true;
await this.stateService.remove('orgInvitation');
let error = qParams.organizationId == null || qParams.organizationUserId == null || qParams.token == null;
let errorMessage: string = null;
2018-07-12 22:05:42 +02:00
if (!error) {
this.authed = await this.userService.isAuthenticated();
if (this.authed) {
const request = new OrganizationUserAcceptRequest();
request.token = qParams.token;
try {
if (await this.performResetPasswordAutoEnroll(qParams)) {
this.actionPromise = this.apiService.postOrganizationUserAccept(qParams.organizationId,
qParams.organizationUserId, request).then(() => {
// Retrieve Public Key
return this.apiService.getOrganizationKeys(qParams.organizationId);
}).then(async response => {
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);
// Create request and execute enrollment
const resetRequest = new OrganizationUserResetPasswordEnrollmentRequest();
resetRequest.resetPasswordKey = encryptedKey.encryptedString;
// Get User Id
const userId = await this.userService.getUserId();
return this.apiService.putOrganizationUserResetPasswordEnrollment(qParams.organizationId, userId, resetRequest);
});
} else {
this.actionPromise = this.apiService.postOrganizationUserAccept(qParams.organizationId,
qParams.organizationUserId, request);
}
2018-07-12 22:05:42 +02:00
await this.actionPromise;
const toast: Toast = {
type: 'success',
title: this.i18nService.t('inviteAccepted'),
body: this.i18nService.t('inviteAcceptedDesc'),
timeout: 10000,
};
this.toasterService.popAsync(toast);
this.router.navigate(['/vault']);
} catch (e) {
2018-07-12 22:05:42 +02:00
error = true;
errorMessage = e.message;
2018-07-12 22:05:42 +02:00
}
} else {
await this.stateService.save('orgInvitation', qParams);
2018-07-12 22:05:42 +02:00
this.email = qParams.email;
this.orgName = qParams.organizationName;
2018-07-25 15:10:24 +02:00
if (this.orgName != null) {
// Fix URL encoding of space issue with Angular
2018-07-25 15:13:46 +02:00
this.orgName = this.orgName.replace(/\+/g, ' ');
2018-07-25 15:10:24 +02:00
}
2018-07-12 22:05:42 +02:00
}
}
if (error) {
const toast: Toast = {
type: 'error',
title: null,
body: errorMessage != null ? this.i18nService.t('inviteAcceptFailedShort', errorMessage) :
this.i18nService.t('inviteAcceptFailed'),
timeout: 10000,
};
this.toasterService.popAsync(toast);
2018-07-12 22:05:42 +02:00
this.router.navigate(['/']);
}
this.loading = false;
});
}
private async performResetPasswordAutoEnroll(qParams: any): Promise<boolean> {
let policyList: Policy[] = null;
try {
const policies = await this.apiService.getPoliciesByToken(qParams.organizationId, qParams.token,
qParams.email, qParams.organizationUserId);
policyList = this.policyService.mapPoliciesFromToken(policies);
} catch { }
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;
}
return false;
}
2018-07-12 22:05:42 +02:00
}