bitwarden-estensione-browser/apps/web/src/app/accounts/login.component.ts

180 lines
6.4 KiB
TypeScript
Raw Normal View History

2021-12-17 15:57:11 +01:00
import { Component, NgZone } from "@angular/core";
import { ActivatedRoute, Router } from "@angular/router";
import { first } from "rxjs/operators";
2022-02-24 12:10:07 +01:00
import { LoginComponent as BaseLoginComponent } from "jslib-angular/components/login.component";
2021-12-17 15:57:11 +01: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";
import { LogService } from "jslib-common/abstractions/log.service";
2022-03-03 18:17:19 +01:00
import { MessagingService } from "jslib-common/abstractions/messaging.service";
2021-12-17 15:57:11 +01: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";
import { PolicyData } from "jslib-common/models/data/policyData";
import { MasterPasswordPolicyOptions } from "jslib-common/models/domain/masterPasswordPolicyOptions";
2021-12-17 15:57:11 +01:00
import { Policy } from "jslib-common/models/domain/policy";
import { ListResponse } from "jslib-common/models/response/listResponse";
import { PolicyResponse } from "jslib-common/models/response/policyResponse";
2022-02-24 12:10:07 +01:00
import { StateService } from "../../abstractions/state.service";
import { RouterService } from "../services/router.service";
2022-02-24 12:10:07 +01:00
2018-06-05 21:02:53 +02:00
@Component({
2021-12-17 15:57:11 +01:00
selector: "app-login",
templateUrl: "login.component.html",
2018-06-05 21:02:53 +02:00
})
export class LoginComponent extends BaseLoginComponent {
2021-12-17 15:57:11 +01:00
showResetPasswordAutoEnrollWarning = false;
enforcedPasswordPolicyOptions: MasterPasswordPolicyOptions;
policies: ListResponse<PolicyResponse>;
2021-12-17 15:57:11 +01:00
constructor(
authService: AuthService,
router: Router,
i18nService: I18nService,
private route: ActivatedRoute,
platformUtilsService: PlatformUtilsService,
environmentService: EnvironmentService,
passwordGenerationService: PasswordGenerationService,
cryptoFunctionService: CryptoFunctionService,
private apiService: ApiService,
private policyService: PolicyService,
logService: LogService,
ngZone: NgZone,
2022-03-03 18:17:19 +01:00
protected stateService: StateService,
private messagingService: MessagingService,
private routerService: RouterService
2021-12-17 15:57:11 +01:00
) {
super(
authService,
router,
platformUtilsService,
i18nService,
stateService,
environmentService,
passwordGenerationService,
cryptoFunctionService,
logService,
ngZone
);
2022-03-03 18:17:19 +01:00
this.onSuccessfulLogin = async () => {
this.messagingService.send("setFullWidth");
};
2021-12-17 15:57:11 +01:00
this.onSuccessfulLoginNavigate = this.goAfterLogIn;
}
2021-12-17 15:57:11 +01:00
async ngOnInit() {
this.route.queryParams.pipe(first()).subscribe(async (qParams) => {
if (qParams.email != null && qParams.email.indexOf("@") > -1) {
this.email = qParams.email;
}
if (qParams.premium != null) {
this.routerService.setPreviousUrl("/settings/premium");
2021-12-17 15:57:11 +01:00
} else if (qParams.org != null) {
const route = this.router.createUrlTree(["create-organization"], {
queryParams: { plan: qParams.org },
2021-12-17 15:57:11 +01:00
});
this.routerService.setPreviousUrl(route.toString());
2021-12-17 15:57:11 +01:00
}
Feature/families for enterprise (#1300) * Added manual routing * Families for enterprise/account settings (#1290) * Added sponsored families page * Revert "Added manual routing" This reverts commit a970ba78ffa98545176b636630e48115efcf51cc. * Add messages to page * Remove stages and simplify design * Switch to new figma design * Add screen reader * Add calls to server * Reorder methods * Used to organization filters * Connected page to server * Add preliminary text to subscription page * Sponsor existing family organization flow * Update jslib Co-authored-by: Matt Gibson <mgibson@bitwarden.com> * Add revoke sponsorship flow * Add spinner to send offer button * Determine if subscription has sponsored items * Work on subscription button * Add message for new family organization * Families for enterprise/subscription page (#1292) * Work on subscription button * Determine if subscription has sponsored items * Work on subscriptions page * Add message for new family organization Co-authored-by: Matt Gibson <mgibson@bitwarden.com> * Families for enterprise/redeem card (#1295) * Add toast localization message * Use helpers to property display sponsorship items * Split table rows into component so buttons load (#1296) * Split table rows into component so buttons load * Update jslib * Families for enterprise/localizations (#1299) * Add more localizations * Remove unneeded comments * Fix help article * Run linting * Do not show redeem button if no orgs exist to redeem * Implement new process for accepting sponsorships * Hide business checkbox * Update jslib * Removed commented code * Remove commented html * Cleaned up imports * Use proper message * Remove merge conflict message * Remove confusing comment * Listened to PR feedback * Remove unused property * Update help text * Fix aria labels * Add try catch * Made toast before emit * Minor copy changes * Update jslib * Remove unneeded loading Co-authored-by: Matt Gibson <mgibson@bitwarden.com>
2021-11-22 14:41:40 +01:00
2021-12-17 15:57:11 +01:00
// Are they coming from an email for sponsoring a families organization
if (qParams.sponsorshipToken != null) {
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
});
this.routerService.setPreviousUrl(route.toString());
2021-12-17 15:57:11 +01:00
}
await super.ngOnInit();
this.rememberEmail = await this.stateService.getRememberEmail();
2021-12-17 15:57:11 +01:00
});
2021-12-17 15:57:11 +01:00
const invite = await this.stateService.getOrganizationInvitation();
if (invite != null) {
let policyList: Policy[] = null;
try {
this.policies = await this.apiService.getPoliciesByToken(
2021-12-17 15:57:11 +01:00
invite.organizationId,
invite.token,
invite.email,
invite.organizationUserId
);
policyList = this.policyService.mapPoliciesFromToken(this.policies);
2021-12-17 15:57:11 +01:00
} catch (e) {
this.logService.error(e);
}
2021-12-17 15:57:11 +01:00
if (policyList != null) {
const resetPasswordPolicy = this.policyService.getResetPasswordPolicyOptions(
2021-12-17 15:57:11 +01:00
policyList,
invite.organizationId
);
// Set to true if policy enabled and auto-enroll enabled
this.showResetPasswordAutoEnrollWarning =
resetPasswordPolicy[1] && resetPasswordPolicy[0].autoEnrollEnabled;
this.enforcedPasswordPolicyOptions =
await this.policyService.getMasterPasswordPolicyOptions(policyList);
2021-12-17 15:57:11 +01:00
}
2018-07-13 15:13:37 +02:00
}
2021-12-17 15:57:11 +01:00
}
2021-12-17 15:57:11 +01:00
async goAfterLogIn() {
// 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;
}
}
const previousUrl = this.routerService.getPreviousUrl();
if (previousUrl) {
this.router.navigateByUrl(previousUrl);
2021-12-17 15:57:11 +01:00
} else {
this.router.navigate([this.successRoute]);
}
2021-12-17 15:57:11 +01:00
}
async submit() {
await this.stateService.setRememberEmail(this.rememberEmail);
if (!this.rememberEmail) {
await this.stateService.setRememberedEmail(null);
}
await super.submit();
}
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
}