bitwarden-estensione-browser/src/app/tools/weak-passwords-report.compo...

91 lines
3.5 KiB
TypeScript
Raw Normal View History

2018-12-11 23:49:51 +01:00
import {
Component,
ComponentFactoryResolver,
OnInit,
} from '@angular/core';
import { CipherService } from 'jslib/abstractions/cipher.service';
2018-12-12 15:29:51 +01:00
import { MessagingService } from 'jslib/abstractions/messaging.service';
2018-12-11 23:49:51 +01:00
import { PasswordGenerationService } from 'jslib/abstractions/passwordGeneration.service';
2018-12-12 15:29:51 +01:00
import { UserService } from 'jslib/abstractions/user.service';
2018-12-11 23:49:51 +01:00
import { CipherView } from 'jslib/models/view/cipherView';
import { CipherType } from 'jslib/enums/cipherType';
2018-12-12 15:11:10 +01:00
import { CipherReportComponent } from './cipher-report.component';
2018-12-11 23:49:51 +01:00
@Component({
selector: 'app-weak-passwords-report',
templateUrl: 'weak-passwords-report.component.html',
})
2018-12-12 15:11:10 +01:00
export class WeakPasswordsReportComponent extends CipherReportComponent implements OnInit {
2018-12-11 23:49:51 +01:00
passwordStrengthMap = new Map<string, [string, string]>();
2018-12-12 17:22:11 +01:00
private passwordStrengthCache = new Map<string, number>();
2018-12-14 20:42:04 +01:00
constructor(protected cipherService: CipherService, protected passwordGenerationService: PasswordGenerationService,
2018-12-12 15:29:51 +01:00
componentFactoryResolver: ComponentFactoryResolver, messagingService: MessagingService,
userService: UserService) {
super(componentFactoryResolver, userService, messagingService, true);
2018-12-12 15:11:10 +01:00
}
2018-12-11 23:49:51 +01:00
2018-12-12 15:29:51 +01:00
async ngOnInit() {
2018-12-14 19:56:01 +01:00
if (await this.checkAccess()) {
2018-12-12 15:29:51 +01:00
await super.load();
}
2018-12-11 23:49:51 +01:00
}
2018-12-12 15:11:10 +01:00
async setCiphers() {
2018-12-14 20:42:04 +01:00
const allCiphers = await this.getAllCiphers();
2018-12-11 23:49:51 +01:00
const weakPasswordCiphers: CipherView[] = [];
allCiphers.forEach((c) => {
if (c.type !== CipherType.Login || c.login.password == null || c.login.password === '') {
return;
}
const hasUsername = c.login.username != null && c.login.username.trim() !== '';
const cacheKey = c.login.password + '_____' + (hasUsername ? c.login.username : '');
if (!this.passwordStrengthCache.has(cacheKey)) {
let userInput: string[] = [];
if (hasUsername) {
const atPosition = c.login.username.indexOf('@');
if (atPosition > -1) {
userInput = userInput.concat(
c.login.username.substr(0, atPosition).trim().toLowerCase().split(/[^A-Za-z0-9]/))
.filter((i) => i.length >= 3);
} else {
userInput = c.login.username.trim().toLowerCase().split(/[^A-Za-z0-9]/)
.filter((i) => i.length >= 3);
}
}
const result = this.passwordGenerationService.passwordStrength(c.login.password,
userInput.length > 0 ? userInput : null);
this.passwordStrengthCache.set(cacheKey, result.score);
2018-12-12 17:22:11 +01:00
}
const score = this.passwordStrengthCache.get(cacheKey);
if (score != null && score <= 2) {
2018-12-12 17:22:11 +01:00
this.passwordStrengthMap.set(c.id, this.scoreKey(score));
weakPasswordCiphers.push(c);
}
2018-12-11 23:49:51 +01:00
});
this.ciphers = weakPasswordCiphers;
}
2018-12-14 20:42:04 +01:00
protected getAllCiphers(): Promise<CipherView[]> {
return this.cipherService.getAllDecrypted();
}
2018-12-11 23:49:51 +01:00
private scoreKey(score: number): [string, string] {
switch (score) {
case 4:
return ['strong', 'success'];
case 3:
return ['good', 'primary'];
case 2:
return ['weak', 'warning'];
default:
return ['veryWeak', 'danger'];
}
}
}