mirror of
https://github.com/bitwarden/browser.git
synced 2025-01-15 20:11:30 +01:00
Refactor accept-organization and accept-emergency (#1026)
This commit is contained in:
parent
b8aff13ce2
commit
14e9784297
@ -1,7 +1,4 @@
|
|||||||
import {
|
import { Component } from '@angular/core';
|
||||||
Component,
|
|
||||||
OnInit,
|
|
||||||
} from '@angular/core';
|
|
||||||
import {
|
import {
|
||||||
ActivatedRoute,
|
ActivatedRoute,
|
||||||
Router,
|
Router,
|
||||||
@ -17,77 +14,47 @@ import { I18nService } from 'jslib-common/abstractions/i18n.service';
|
|||||||
import { StateService } from 'jslib-common/abstractions/state.service';
|
import { StateService } from 'jslib-common/abstractions/state.service';
|
||||||
import { UserService } from 'jslib-common/abstractions/user.service';
|
import { UserService } from 'jslib-common/abstractions/user.service';
|
||||||
import { EmergencyAccessAcceptRequest } from 'jslib-common/models/request/emergencyAccessAcceptRequest';
|
import { EmergencyAccessAcceptRequest } from 'jslib-common/models/request/emergencyAccessAcceptRequest';
|
||||||
|
import { BaseAcceptComponent } from '../common/base.accept.component';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-accept-emergency',
|
selector: 'app-accept-emergency',
|
||||||
templateUrl: 'accept-emergency.component.html',
|
templateUrl: 'accept-emergency.component.html',
|
||||||
})
|
})
|
||||||
export class AcceptEmergencyComponent implements OnInit {
|
export class AcceptEmergencyComponent extends BaseAcceptComponent {
|
||||||
loading = true;
|
|
||||||
authed = false;
|
|
||||||
name: string;
|
name: string;
|
||||||
email: string;
|
|
||||||
actionPromise: Promise<any>;
|
|
||||||
|
|
||||||
constructor(private router: Router, private toasterService: ToasterService,
|
protected requiredParameters: string[] = ['id', 'name', 'email', 'token'];
|
||||||
private i18nService: I18nService, private route: ActivatedRoute,
|
protected failedShortMessage = 'emergencyInviteAcceptFailedShort';
|
||||||
private apiService: ApiService, private userService: UserService,
|
protected failedMessage = 'emergencyInviteAcceptFailed';
|
||||||
private stateService: StateService) { }
|
|
||||||
|
|
||||||
ngOnInit() {
|
constructor(router: Router, toasterService: ToasterService,
|
||||||
let fired = false;
|
i18nService: I18nService, route: ActivatedRoute,
|
||||||
this.route.queryParams.subscribe(async qParams => {
|
private apiService: ApiService, userService: UserService,
|
||||||
if (fired) {
|
stateService: StateService) {
|
||||||
return;
|
super(router, toasterService, i18nService, route, userService, stateService);
|
||||||
}
|
}
|
||||||
fired = true;
|
|
||||||
await this.stateService.remove('emergencyInvitation');
|
|
||||||
let error = qParams.id == null || qParams.name == null || qParams.email == null || qParams.token == null;
|
|
||||||
let errorMessage: string = null;
|
|
||||||
if (!error) {
|
|
||||||
this.authed = await this.userService.isAuthenticated();
|
|
||||||
if (this.authed) {
|
|
||||||
const request = new EmergencyAccessAcceptRequest();
|
|
||||||
request.token = qParams.token;
|
|
||||||
try {
|
|
||||||
this.actionPromise = this.apiService.postEmergencyAccessAccept(qParams.id, request);
|
|
||||||
await this.actionPromise;
|
|
||||||
const toast: Toast = {
|
|
||||||
type: 'success',
|
|
||||||
title: this.i18nService.t('inviteAccepted'),
|
|
||||||
body: this.i18nService.t('emergencyInviteAcceptedDesc'),
|
|
||||||
timeout: 10000,
|
|
||||||
};
|
|
||||||
this.toasterService.popAsync(toast);
|
|
||||||
this.router.navigate(['/vault']);
|
|
||||||
} catch (e) {
|
|
||||||
error = true;
|
|
||||||
errorMessage = e.message;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
await this.stateService.save('emergencyInvitation', qParams);
|
|
||||||
this.email = qParams.email;
|
|
||||||
this.name = qParams.name;
|
|
||||||
if (this.name != null) {
|
|
||||||
// Fix URL encoding of space issue with Angular
|
|
||||||
this.name = this.name.replace(/\+/g, ' ');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (error) {
|
async authedHandler(qParams: any): Promise<void> {
|
||||||
const toast: Toast = {
|
const request = new EmergencyAccessAcceptRequest();
|
||||||
type: 'error',
|
request.token = qParams.token;
|
||||||
title: null,
|
this.actionPromise = this.apiService.postEmergencyAccessAccept(qParams.id, request);
|
||||||
body: errorMessage != null ? this.i18nService.t('emergencyInviteAcceptFailedShort', errorMessage) :
|
await this.actionPromise;
|
||||||
this.i18nService.t('emergencyInviteAcceptFailed'),
|
const toast: Toast = {
|
||||||
timeout: 10000,
|
type: 'success',
|
||||||
};
|
title: this.i18nService.t('inviteAccepted'),
|
||||||
this.toasterService.popAsync(toast);
|
body: this.i18nService.t('emergencyInviteAcceptedDesc'),
|
||||||
this.router.navigate(['/']);
|
timeout: 10000,
|
||||||
}
|
};
|
||||||
|
this.toasterService.popAsync(toast);
|
||||||
|
this.router.navigate(['/vault']);
|
||||||
|
}
|
||||||
|
|
||||||
this.loading = false;
|
async unauthedHandler(qParams: any): Promise<void> {
|
||||||
});
|
this.name = qParams.name;
|
||||||
|
if (this.name != null) {
|
||||||
|
// Fix URL encoding of space issue with Angular
|
||||||
|
this.name = this.name.replace(/\+/g, ' ');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,4 @@
|
|||||||
import {
|
import { Component } from '@angular/core';
|
||||||
Component,
|
|
||||||
OnInit,
|
|
||||||
} from '@angular/core';
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
ActivatedRoute,
|
ActivatedRoute,
|
||||||
Router,
|
Router,
|
||||||
@ -25,108 +21,75 @@ import { OrganizationUserResetPasswordEnrollmentRequest } from 'jslib-common/mod
|
|||||||
|
|
||||||
import { Utils } from 'jslib-common/misc/utils';
|
import { Utils } from 'jslib-common/misc/utils';
|
||||||
import { Policy } from 'jslib-common/models/domain/policy';
|
import { Policy } from 'jslib-common/models/domain/policy';
|
||||||
|
import { BaseAcceptComponent } from '../common/base.accept.component';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-accept-organization',
|
selector: 'app-accept-organization',
|
||||||
templateUrl: 'accept-organization.component.html',
|
templateUrl: 'accept-organization.component.html',
|
||||||
})
|
})
|
||||||
export class AcceptOrganizationComponent implements OnInit {
|
export class AcceptOrganizationComponent extends BaseAcceptComponent {
|
||||||
loading = true;
|
|
||||||
authed = false;
|
|
||||||
orgName: string;
|
orgName: string;
|
||||||
email: string;
|
|
||||||
actionPromise: Promise<any>;
|
|
||||||
|
|
||||||
constructor(private router: Router, private toasterService: ToasterService,
|
protected requiredParameters: string[] = ['organizationId', 'organizationUserId', 'token'];
|
||||||
private i18nService: I18nService, private route: ActivatedRoute,
|
|
||||||
private apiService: ApiService, private userService: UserService,
|
|
||||||
private stateService: StateService, private cryptoService: CryptoService,
|
|
||||||
private policyService: PolicyService) { }
|
|
||||||
|
|
||||||
ngOnInit() {
|
constructor(router: Router, toasterService: ToasterService,
|
||||||
let fired = false;
|
i18nService: I18nService, route: ActivatedRoute,
|
||||||
this.route.queryParams.subscribe(async qParams => {
|
private apiService: ApiService, userService: UserService,
|
||||||
if (fired) {
|
stateService: StateService, private cryptoService: CryptoService,
|
||||||
return;
|
private policyService: PolicyService) {
|
||||||
}
|
super(router, toasterService, i18nService, route, userService, stateService);
|
||||||
fired = true;
|
}
|
||||||
await this.stateService.remove('orgInvitation');
|
|
||||||
let error = qParams.organizationId == null || qParams.organizationUserId == null || qParams.token == null;
|
|
||||||
let errorMessage: string = null;
|
|
||||||
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);
|
async authedHandler(qParams: any): Promise<void> {
|
||||||
|
const request = new OrganizationUserAcceptRequest();
|
||||||
// RSA Encrypt user's encKey.key with organization public key
|
request.token = qParams.token;
|
||||||
const encKey = await this.cryptoService.getEncKey();
|
if (await this.performResetPasswordAutoEnroll(qParams)) {
|
||||||
const encryptedKey = await this.cryptoService.rsaEncrypt(encKey.key, publicKey.buffer);
|
this.actionPromise = this.apiService.postOrganizationUserAccept(qParams.organizationId,
|
||||||
|
qParams.organizationUserId, request).then(() => {
|
||||||
// Create request and execute enrollment
|
// Retrieve Public Key
|
||||||
const resetRequest = new OrganizationUserResetPasswordEnrollmentRequest();
|
return this.apiService.getOrganizationKeys(qParams.organizationId);
|
||||||
resetRequest.resetPasswordKey = encryptedKey.encryptedString;
|
}).then(async response => {
|
||||||
|
if (response == null) {
|
||||||
// Get User Id
|
throw new Error(this.i18nService.t('resetPasswordOrgKeysError'));
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
|
||||||
error = true;
|
|
||||||
errorMessage = e.message;
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
await this.stateService.save('orgInvitation', qParams);
|
|
||||||
this.email = qParams.email;
|
|
||||||
this.orgName = qParams.organizationName;
|
|
||||||
if (this.orgName != null) {
|
|
||||||
// Fix URL encoding of space issue with Angular
|
|
||||||
this.orgName = this.orgName.replace(/\+/g, ' ');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (error) {
|
const publicKey = Utils.fromB64ToArray(response.publicKey);
|
||||||
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);
|
|
||||||
this.router.navigate(['/']);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.loading = false;
|
// 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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']);
|
||||||
|
}
|
||||||
|
|
||||||
|
async unauthedHandler(qParams: any): Promise<void> {
|
||||||
|
this.orgName = qParams.organizationName;
|
||||||
|
if (this.orgName != null) {
|
||||||
|
// Fix URL encoding of space issue with Angular
|
||||||
|
this.orgName = this.orgName.replace(/\+/g, ' ');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async performResetPasswordAutoEnroll(qParams: any): Promise<boolean> {
|
private async performResetPasswordAutoEnroll(qParams: any): Promise<boolean> {
|
||||||
|
@ -76,20 +76,12 @@ export class LoginComponent extends BaseLoginComponent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async goAfterLogIn() {
|
async goAfterLogIn() {
|
||||||
const orgInvite = await this.stateService.get<any>('orgInvitation');
|
const loginRedirect = await this.stateService.get<any>('loginRedirect');
|
||||||
const emergencyInvite = await this.stateService.get<any>('emergencyInvitation');
|
if (loginRedirect != null) {
|
||||||
if (orgInvite != null) {
|
this.router.navigate([loginRedirect.route], { queryParams: loginRedirect.qParams });
|
||||||
this.router.navigate(['accept-organization'], { queryParams: orgInvite });
|
await this.stateService.remove('loginRedirect');
|
||||||
} else if (emergencyInvite != null) {
|
|
||||||
this.router.navigate(['accept-emergency'], { queryParams: emergencyInvite });
|
|
||||||
} else {
|
} else {
|
||||||
const loginRedirect = await this.stateService.get<any>('loginRedirect');
|
this.router.navigate([this.successRoute]);
|
||||||
if (loginRedirect != null) {
|
|
||||||
this.router.navigate([loginRedirect.route], { queryParams: loginRedirect.qParams });
|
|
||||||
await this.stateService.remove('loginRedirect');
|
|
||||||
} else {
|
|
||||||
this.router.navigate([this.successRoute]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -60,24 +60,16 @@ export class TwoFactorComponent extends BaseTwoFactorComponent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async goAfterLogIn() {
|
async goAfterLogIn() {
|
||||||
const orgInvite = await this.stateService.get<any>('orgInvitation');
|
const loginRedirect = await this.stateService.get<any>('loginRedirect');
|
||||||
const emergencyInvite = await this.stateService.get<any>('emergencyInvitation');
|
if (loginRedirect != null) {
|
||||||
if (orgInvite != null) {
|
this.router.navigate([loginRedirect.route], { queryParams: loginRedirect.qParams });
|
||||||
this.router.navigate(['accept-organization'], { queryParams: orgInvite });
|
await this.stateService.remove('loginRedirect');
|
||||||
} else if (emergencyInvite != null) {
|
|
||||||
this.router.navigate(['accept-emergency'], { queryParams: emergencyInvite });
|
|
||||||
} else {
|
} else {
|
||||||
const loginRedirect = await this.stateService.get<any>('loginRedirect');
|
this.router.navigate([this.successRoute], {
|
||||||
if (loginRedirect != null) {
|
queryParams: {
|
||||||
this.router.navigate([loginRedirect.route], { queryParams: loginRedirect.qParams });
|
identifier: this.identifier,
|
||||||
await this.stateService.remove('loginRedirect');
|
},
|
||||||
} else {
|
});
|
||||||
this.router.navigate([this.successRoute], {
|
|
||||||
queryParams: {
|
|
||||||
identifier: this.identifier,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
90
src/app/common/base.accept.component.ts
Normal file
90
src/app/common/base.accept.component.ts
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
import {
|
||||||
|
Directive,
|
||||||
|
OnInit,
|
||||||
|
} from '@angular/core';
|
||||||
|
import {
|
||||||
|
ActivatedRoute,
|
||||||
|
Router,
|
||||||
|
} from '@angular/router';
|
||||||
|
|
||||||
|
import {
|
||||||
|
Toast,
|
||||||
|
ToasterService,
|
||||||
|
} from 'angular2-toaster';
|
||||||
|
|
||||||
|
import { I18nService } from 'jslib-common/abstractions/i18n.service';
|
||||||
|
import { StateService } from 'jslib-common/abstractions/state.service';
|
||||||
|
import { UserService } from 'jslib-common/abstractions/user.service';
|
||||||
|
|
||||||
|
@Directive()
|
||||||
|
export abstract class BaseAcceptComponent implements OnInit {
|
||||||
|
loading = true;
|
||||||
|
authed = false;
|
||||||
|
email: string;
|
||||||
|
actionPromise: Promise<any>;
|
||||||
|
|
||||||
|
protected requiredParameters: string[] = [];
|
||||||
|
protected failedShortMessage = 'inviteAcceptFailedShort';
|
||||||
|
protected failedMessage = 'inviteAcceptFailed';
|
||||||
|
|
||||||
|
constructor(protected router: Router, protected toasterService: ToasterService,
|
||||||
|
protected i18nService: I18nService, protected route: ActivatedRoute,
|
||||||
|
protected userService: UserService, private stateService: StateService) { }
|
||||||
|
|
||||||
|
abstract authedHandler(qParams: any): Promise<void>;
|
||||||
|
abstract unauthedHandler(qParams: any): Promise<void>;
|
||||||
|
|
||||||
|
ngOnInit() {
|
||||||
|
let fired = false;
|
||||||
|
this.route.queryParams.subscribe(async qParams => {
|
||||||
|
if (fired) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
fired = true;
|
||||||
|
await this.stateService.remove('loginRedirect');
|
||||||
|
|
||||||
|
let error = this.requiredParameters.some(e => qParams?.[e] == null || qParams[e] === '');
|
||||||
|
let errorMessage: string = null;
|
||||||
|
if (!error) {
|
||||||
|
this.authed = await this.userService.isAuthenticated();
|
||||||
|
|
||||||
|
if (this.authed) {
|
||||||
|
try {
|
||||||
|
await this.authedHandler(qParams);
|
||||||
|
} catch (e) {
|
||||||
|
error = true;
|
||||||
|
errorMessage = e.message;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
await this.stateService.save('loginRedirect', {
|
||||||
|
route: this.getRedirectRoute(),
|
||||||
|
qParams: qParams,
|
||||||
|
});
|
||||||
|
|
||||||
|
this.email = qParams.email;
|
||||||
|
await this.unauthedHandler(qParams);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
const toast: Toast = {
|
||||||
|
type: 'error',
|
||||||
|
title: null,
|
||||||
|
body: errorMessage != null ? this.i18nService.t(this.failedShortMessage, errorMessage) :
|
||||||
|
this.i18nService.t(this.failedMessage),
|
||||||
|
timeout: 10000,
|
||||||
|
};
|
||||||
|
this.toasterService.popAsync(toast);
|
||||||
|
this.router.navigate(['/']);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.loading = false;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
getRedirectRoute() {
|
||||||
|
const urlTree = this.router.parseUrl(this.router.url);
|
||||||
|
urlTree.queryParams = {};
|
||||||
|
return urlTree.toString();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user