2021-11-29 01:14:49 +01:00
|
|
|
import { Component, NgZone } from "@angular/core";
|
2018-07-13 15:13:37 +02:00
|
|
|
import { ActivatedRoute, Router } from "@angular/router";
|
2021-10-15 00:59:43 +02:00
|
|
|
import { first } from "rxjs/operators";
|
|
|
|
|
2022-06-14 17:10:53 +02:00
|
|
|
import { LoginComponent as BaseLoginComponent } from "@bitwarden/angular/components/login.component";
|
|
|
|
import { ApiService } from "@bitwarden/common/abstractions/api.service";
|
|
|
|
import { AuthService } from "@bitwarden/common/abstractions/auth.service";
|
|
|
|
import { CryptoFunctionService } from "@bitwarden/common/abstractions/cryptoFunction.service";
|
|
|
|
import { EnvironmentService } from "@bitwarden/common/abstractions/environment.service";
|
|
|
|
import { I18nService } from "@bitwarden/common/abstractions/i18n.service";
|
|
|
|
import { LogService } from "@bitwarden/common/abstractions/log.service";
|
|
|
|
import { MessagingService } from "@bitwarden/common/abstractions/messaging.service";
|
|
|
|
import { PasswordGenerationService } from "@bitwarden/common/abstractions/passwordGeneration.service";
|
|
|
|
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 { InternalPolicyService } from "@bitwarden/common/abstractions/policy/policy.service.abstraction";
|
2022-06-14 17:10:53 +02:00
|
|
|
import { PolicyData } from "@bitwarden/common/models/data/policyData";
|
|
|
|
import { MasterPasswordPolicyOptions } from "@bitwarden/common/models/domain/masterPasswordPolicyOptions";
|
|
|
|
import { Policy } from "@bitwarden/common/models/domain/policy";
|
|
|
|
import { ListResponse } from "@bitwarden/common/models/response/listResponse";
|
|
|
|
import { PolicyResponse } from "@bitwarden/common/models/response/policyResponse";
|
2021-06-02 18:35:49 +02:00
|
|
|
|
2022-07-26 19:34:45 +02:00
|
|
|
import { RouterService, StateService } from "../core";
|
2022-02-24 12:10:07 +01:00
|
|
|
|
2018-06-05 21:02:53 +02:00
|
|
|
@Component({
|
|
|
|
selector: "app-login",
|
|
|
|
templateUrl: "login.component.html",
|
|
|
|
})
|
2022-08-26 18:09:28 +02:00
|
|
|
// eslint-disable-next-line rxjs-angular/prefer-takeuntil
|
2018-06-05 21:02:53 +02:00
|
|
|
export class LoginComponent extends BaseLoginComponent {
|
2021-06-02 18:35:49 +02:00
|
|
|
showResetPasswordAutoEnrollWarning = false;
|
2022-02-03 06:00:57 +01:00
|
|
|
enforcedPasswordPolicyOptions: MasterPasswordPolicyOptions;
|
|
|
|
policies: ListResponse<PolicyResponse>;
|
2021-06-02 18:35:49 +02:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
authService: AuthService,
|
|
|
|
router: Router,
|
2021-12-14 17:10:26 +01:00
|
|
|
i18nService: I18nService,
|
|
|
|
private route: ActivatedRoute,
|
2021-06-02 18:35:49 +02:00
|
|
|
platformUtilsService: PlatformUtilsService,
|
2020-08-11 17:30:30 +02:00
|
|
|
environmentService: EnvironmentService,
|
2021-06-02 18:35:49 +02:00
|
|
|
passwordGenerationService: PasswordGenerationService,
|
|
|
|
cryptoFunctionService: CryptoFunctionService,
|
2021-11-29 01:14:49 +01:00
|
|
|
private apiService: ApiService,
|
2022-08-08 11:04:36 +02:00
|
|
|
private policyApiService: PolicyApiServiceAbstraction,
|
|
|
|
private policyService: InternalPolicyService,
|
2021-11-29 01:14:49 +01:00
|
|
|
logService: LogService,
|
2022-02-02 17:20:15 +01:00
|
|
|
ngZone: NgZone,
|
2022-03-03 18:17:19 +01:00
|
|
|
protected stateService: StateService,
|
2022-04-25 15:41:44 +02:00
|
|
|
private messagingService: MessagingService,
|
|
|
|
private routerService: RouterService
|
2021-12-17 15:57:11 +01:00
|
|
|
) {
|
|
|
|
super(
|
2018-06-05 21:02:53 +02:00
|
|
|
authService,
|
2021-12-17 15:57:11 +01:00
|
|
|
router,
|
2020-08-11 17:30:30 +02:00
|
|
|
platformUtilsService,
|
|
|
|
i18nService,
|
|
|
|
stateService,
|
|
|
|
environmentService,
|
|
|
|
passwordGenerationService,
|
|
|
|
cryptoFunctionService,
|
2021-12-14 17:10:26 +01:00
|
|
|
logService,
|
2021-12-17 15:57:11 +01:00
|
|
|
ngZone
|
|
|
|
);
|
2022-03-03 18:17:19 +01:00
|
|
|
this.onSuccessfulLogin = async () => {
|
|
|
|
this.messagingService.send("setFullWidth");
|
|
|
|
};
|
2021-06-02 18:35:49 +02:00
|
|
|
this.onSuccessfulLoginNavigate = this.goAfterLogIn;
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2021-06-02 18:35:49 +02:00
|
|
|
|
|
|
|
async ngOnInit() {
|
2022-08-26 18:09:28 +02:00
|
|
|
// eslint-disable-next-line rxjs-angular/prefer-takeuntil, rxjs/no-async-subscribe
|
2021-06-02 18:35:49 +02:00
|
|
|
this.route.queryParams.pipe(first()).subscribe(async (qParams) => {
|
2018-07-13 16:51:52 +02:00
|
|
|
if (qParams.email != null && qParams.email.indexOf("@") > -1) {
|
2018-07-13 15:13:37 +02:00
|
|
|
this.email = qParams.email;
|
|
|
|
}
|
2018-07-20 16:29:40 +02:00
|
|
|
if (qParams.premium != null) {
|
2022-04-25 15:41:44 +02:00
|
|
|
this.routerService.setPreviousUrl("/settings/premium");
|
2018-07-20 16:29:40 +02:00
|
|
|
} else if (qParams.org != null) {
|
2022-05-11 21:39:39 +02:00
|
|
|
const route = this.router.createUrlTree(["create-organization"], {
|
2022-04-25 15:41:44 +02:00
|
|
|
queryParams: { plan: qParams.org },
|
2018-07-20 16:29:40 +02:00
|
|
|
});
|
2022-04-25 15:41:44 +02:00
|
|
|
this.routerService.setPreviousUrl(route.toString());
|
2018-07-20 16:29:40 +02:00
|
|
|
}
|
2021-11-22 14:41:40 +01:00
|
|
|
|
|
|
|
// Are they coming from an email for sponsoring a families organization
|
|
|
|
if (qParams.sponsorshipToken != null) {
|
2022-04-25 15:41:44 +02:00
|
|
|
const route = this.router.createUrlTree(["setup/families-for-enterprise"], {
|
2022-04-27 15:09:38 +02:00
|
|
|
queryParams: { token: qParams.sponsorshipToken },
|
2018-07-13 15:13:37 +02:00
|
|
|
});
|
2022-04-25 15:41:44 +02:00
|
|
|
this.routerService.setPreviousUrl(route.toString());
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2018-07-13 15:13:37 +02:00
|
|
|
await super.ngOnInit();
|
2022-02-02 17:20:15 +01:00
|
|
|
this.rememberEmail = await this.stateService.getRememberEmail();
|
2021-12-17 15:57:11 +01:00
|
|
|
});
|
2021-06-02 18:35:49 +02:00
|
|
|
|
2021-12-14 17:10:26 +01:00
|
|
|
const invite = await this.stateService.getOrganizationInvitation();
|
2021-06-02 18:35:49 +02:00
|
|
|
if (invite != null) {
|
|
|
|
let policyList: Policy[] = null;
|
|
|
|
try {
|
2022-08-08 11:04:36 +02:00
|
|
|
this.policies = await this.policyApiService.getPoliciesByToken(
|
2021-06-02 18:35:49 +02:00
|
|
|
invite.organizationId,
|
|
|
|
invite.token,
|
|
|
|
invite.email,
|
|
|
|
invite.organizationUserId
|
|
|
|
);
|
2022-02-03 06:00:57 +01:00
|
|
|
policyList = this.policyService.mapPoliciesFromToken(this.policies);
|
2021-10-20 18:30:04 +02:00
|
|
|
} catch (e) {
|
|
|
|
this.logService.error(e);
|
|
|
|
}
|
2021-06-02 18:35:49 +02:00
|
|
|
|
|
|
|
if (policyList != null) {
|
2022-02-03 06:00:57 +01:00
|
|
|
const resetPasswordPolicy = this.policyService.getResetPasswordPolicyOptions(
|
2021-06-02 18:35:49 +02:00
|
|
|
policyList,
|
|
|
|
invite.organizationId
|
|
|
|
);
|
|
|
|
// Set to true if policy enabled and auto-enroll enabled
|
2022-02-03 06:00:57 +01:00
|
|
|
this.showResetPasswordAutoEnrollWarning =
|
|
|
|
resetPasswordPolicy[1] && resetPasswordPolicy[0].autoEnrollEnabled;
|
|
|
|
|
|
|
|
this.enforcedPasswordPolicyOptions =
|
|
|
|
await this.policyService.getMasterPasswordPolicyOptions(policyList);
|
2021-06-02 18:35:49 +02:00
|
|
|
}
|
2018-07-13 15:13:37 +02:00
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2018-07-13 16:51:52 +02:00
|
|
|
|
|
|
|
async goAfterLogIn() {
|
2022-02-03 06:00:57 +01:00
|
|
|
// Check master password against policy
|
|
|
|
if (this.enforcedPasswordPolicyOptions != null) {
|
|
|
|
const strengthResult = this.passwordGenerationService.passwordStrength(
|
|
|
|
this.masterPassword,
|
|
|
|
this.getPasswordStrengthUserInput()
|
|
|
|
);
|
|
|
|
const masterPasswordScore = strengthResult == null ? null : strengthResult.score;
|
|
|
|
|
|
|
|
// If invalid, save policies and require update
|
|
|
|
if (
|
|
|
|
!this.policyService.evaluateMasterPassword(
|
|
|
|
masterPasswordScore,
|
|
|
|
this.masterPassword,
|
|
|
|
this.enforcedPasswordPolicyOptions
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
const policiesData: { [id: string]: PolicyData } = {};
|
|
|
|
this.policies.data.map((p) => (policiesData[p.id] = new PolicyData(p)));
|
|
|
|
await this.policyService.replace(policiesData);
|
|
|
|
this.router.navigate(["update-password"]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-25 15:41:44 +02:00
|
|
|
const previousUrl = this.routerService.getPreviousUrl();
|
|
|
|
if (previousUrl) {
|
|
|
|
this.router.navigateByUrl(previousUrl);
|
2018-07-13 16:51:52 +02:00
|
|
|
} else {
|
2021-06-15 21:02:04 +02:00
|
|
|
this.router.navigate([this.successRoute]);
|
2018-07-13 16:51:52 +02:00
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2022-02-02 17:20:15 +01:00
|
|
|
|
|
|
|
async submit() {
|
|
|
|
await this.stateService.setRememberEmail(this.rememberEmail);
|
|
|
|
if (!this.rememberEmail) {
|
|
|
|
await this.stateService.setRememberedEmail(null);
|
|
|
|
}
|
|
|
|
await super.submit();
|
|
|
|
}
|
2022-02-03 06:00:57 +01:00
|
|
|
|
|
|
|
private getPasswordStrengthUserInput() {
|
|
|
|
let userInput: string[] = [];
|
|
|
|
const atPosition = this.email.indexOf("@");
|
|
|
|
if (atPosition > -1) {
|
|
|
|
userInput = userInput.concat(
|
|
|
|
this.email
|
|
|
|
.substr(0, atPosition)
|
|
|
|
.trim()
|
|
|
|
.toLowerCase()
|
|
|
|
.split(/[^A-Za-z0-9]/)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return userInput;
|
|
|
|
}
|
2018-06-05 21:02:53 +02:00
|
|
|
}
|