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-02-24 12:10:07 +01:00
|
|
|
import { LoginComponent as BaseLoginComponent } from "jslib-angular/components/login.component";
|
2021-06-07 20:13:58 +02:00
|
|
|
import { ApiService } from "jslib-common/abstractions/api.service";
|
|
|
|
import { AuthService } from "jslib-common/abstractions/auth.service";
|
|
|
|
import { CryptoFunctionService } from "jslib-common/abstractions/cryptoFunction.service";
|
|
|
|
import { EnvironmentService } from "jslib-common/abstractions/environment.service";
|
|
|
|
import { I18nService } from "jslib-common/abstractions/i18n.service";
|
2021-10-20 18:30:04 +02:00
|
|
|
import { LogService } from "jslib-common/abstractions/log.service";
|
2021-06-07 20:13:58 +02:00
|
|
|
import { PasswordGenerationService } from "jslib-common/abstractions/passwordGeneration.service";
|
|
|
|
import { PlatformUtilsService } from "jslib-common/abstractions/platformUtils.service";
|
|
|
|
import { PolicyService } from "jslib-common/abstractions/policy.service";
|
2022-02-03 06:00:57 +01:00
|
|
|
import { PolicyData } from "jslib-common/models/data/policyData";
|
|
|
|
import { MasterPasswordPolicyOptions } from "jslib-common/models/domain/masterPasswordPolicyOptions";
|
2021-06-07 20:13:58 +02:00
|
|
|
import { Policy } from "jslib-common/models/domain/policy";
|
2022-02-03 06:00:57 +01:00
|
|
|
import { ListResponse } from "jslib-common/models/response/listResponse";
|
|
|
|
import { PolicyResponse } from "jslib-common/models/response/policyResponse";
|
2021-06-02 18:35:49 +02:00
|
|
|
|
2022-02-24 12:10:07 +01:00
|
|
|
import { StateService } from "../../abstractions/state.service";
|
|
|
|
|
2018-06-05 21:02:53 +02:00
|
|
|
@Component({
|
|
|
|
selector: "app-login",
|
|
|
|
templateUrl: "login.component.html",
|
|
|
|
})
|
|
|
|
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,
|
2021-06-02 18:35:49 +02:00
|
|
|
private policyService: PolicyService,
|
2021-11-29 01:14:49 +01:00
|
|
|
logService: LogService,
|
2022-02-02 17:20:15 +01:00
|
|
|
ngZone: NgZone,
|
|
|
|
protected stateService: StateService
|
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
|
|
|
|
);
|
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() {
|
|
|
|
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) {
|
2021-12-14 17:10:26 +01:00
|
|
|
this.stateService.setLoginRedirect({ route: "/settings/premium" });
|
2018-07-20 16:29:40 +02:00
|
|
|
} else if (qParams.org != null) {
|
2021-12-14 17:10:26 +01:00
|
|
|
this.stateService.setLoginRedirect({
|
2018-07-20 16:29:40 +02:00
|
|
|
route: "/settings/create-organization",
|
|
|
|
qParams: { plan: qParams.org },
|
|
|
|
});
|
|
|
|
}
|
2021-11-22 14:41:40 +01:00
|
|
|
|
|
|
|
// Are they coming from an email for sponsoring a families organization
|
|
|
|
if (qParams.sponsorshipToken != null) {
|
|
|
|
// After logging in redirect them to setup the families sponsorship
|
2021-12-14 17:10:26 +01:00
|
|
|
this.stateService.setLoginRedirect({
|
2021-11-22 14:41:40 +01:00
|
|
|
route: "/setup/families-for-enterprise",
|
|
|
|
qParams: { token: qParams.sponsorshipToken },
|
2018-07-13 15:13:37 +02:00
|
|
|
});
|
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-02-03 06:00:57 +01:00
|
|
|
this.policies = await this.apiService.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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 17:10:26 +01:00
|
|
|
const loginRedirect = await this.stateService.getLoginRedirect();
|
2021-06-15 21:02:04 +02:00
|
|
|
if (loginRedirect != null) {
|
|
|
|
this.router.navigate([loginRedirect.route], { queryParams: loginRedirect.qParams });
|
2021-12-14 17:10:26 +01:00
|
|
|
await this.stateService.setLoginRedirect(null);
|
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
|
|
|
}
|