1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-13 14:53:33 +00:00

support for uri matching types to cipher service

This commit is contained in:
Kyle Spearrin
2018-03-02 12:03:03 -05:00
parent dc0befc9be
commit ad4c81ed84
6 changed files with 72 additions and 18 deletions

View File

@@ -20,8 +20,8 @@ export abstract class CipherService {
getAll: () => Promise<Cipher[]>; getAll: () => Promise<Cipher[]>;
getAllDecrypted: () => Promise<CipherView[]>; getAllDecrypted: () => Promise<CipherView[]>;
getAllDecryptedForGrouping: (groupingId: string, folder?: boolean) => Promise<CipherView[]>; getAllDecryptedForGrouping: (groupingId: string, folder?: boolean) => Promise<CipherView[]>;
getAllDecryptedForDomain: (domain: string, includeOtherTypes?: CipherType[]) => Promise<CipherView[]>; getAllDecryptedForUrl: (url: string, includeOtherTypes?: CipherType[]) => Promise<CipherView[]>;
getLastUsedForDomain: (domain: string) => Promise<CipherView>; getLastUsedForUrl: (url: string) => Promise<CipherView>;
updateLastUsedDate: (id: string) => Promise<void>; updateLastUsedDate: (id: string) => Promise<void>;
saveNeverDomain: (domain: string) => Promise<void>; saveNeverDomain: (domain: string) => Promise<void>;
saveWithServer: (cipher: Cipher) => Promise<any>; saveWithServer: (cipher: Cipher) => Promise<any>;

View File

@@ -1,4 +1,5 @@
export abstract class UtilsService { export abstract class UtilsService {
copyToClipboard: (text: string, doc?: Document) => void; copyToClipboard: (text: string, doc?: Document) => void;
getHostname: (uriString: string) => string; getHostname: (uriString: string) => string;
getHost: (uriString: string) => string;
} }

View File

@@ -1,6 +1,6 @@
export enum UriMatchType { export enum UriMatchType {
BaseDomain = 0, BaseDomain = 0,
FullHostname = 1, Host = 1,
StartsWith = 2, StartsWith = 2,
Exact = 3, Exact = 3,
RegularExpression = 4, RegularExpression = 4,

View File

@@ -1,4 +1,5 @@
import { CipherType } from '../enums/cipherType'; import { CipherType } from '../enums/cipherType';
import { UriMatchType } from '../enums/uriMatchType';
import { CipherData } from '../models/data/cipherData'; import { CipherData } from '../models/data/cipherData';
@@ -31,9 +32,11 @@ import { ApiService } from '../abstractions/api.service';
import { CipherService as CipherServiceAbstraction } from '../abstractions/cipher.service'; import { CipherService as CipherServiceAbstraction } from '../abstractions/cipher.service';
import { CryptoService } from '../abstractions/crypto.service'; import { CryptoService } from '../abstractions/crypto.service';
import { I18nService } from '../abstractions/i18n.service'; import { I18nService } from '../abstractions/i18n.service';
import { PlatformUtilsService } from '../abstractions/platformUtils.service';
import { SettingsService } from '../abstractions/settings.service'; import { SettingsService } from '../abstractions/settings.service';
import { StorageService } from '../abstractions/storage.service'; import { StorageService } from '../abstractions/storage.service';
import { UserService } from '../abstractions/user.service'; import { UserService } from '../abstractions/user.service';
import { UtilsService } from '../abstractions/utils.service';
const Keys = { const Keys = {
ciphersPrefix: 'ciphers_', ciphersPrefix: 'ciphers_',
@@ -46,7 +49,8 @@ export class CipherService implements CipherServiceAbstraction {
constructor(private cryptoService: CryptoService, private userService: UserService, constructor(private cryptoService: CryptoService, private userService: UserService,
private settingsService: SettingsService, private apiService: ApiService, private settingsService: SettingsService, private apiService: ApiService,
private storageService: StorageService, private i18nService: I18nService) { private storageService: StorageService, private i18nService: I18nService,
private platformUtilsService: PlatformUtilsService, private utilsService: UtilsService) {
} }
clearCache(): void { clearCache(): void {
@@ -170,11 +174,12 @@ export class CipherService implements CipherServiceAbstraction {
}); });
} }
async getAllDecryptedForDomain(domain: string, includeOtherTypes?: CipherType[]): Promise<CipherView[]> { async getAllDecryptedForUrl(url: string, includeOtherTypes?: CipherType[]): Promise<CipherView[]> {
if (domain == null && !includeOtherTypes) { if (url == null && !includeOtherTypes) {
return Promise.resolve([]); return Promise.resolve([]);
} }
const domain = this.platformUtilsService.getDomain(url);
const eqDomainsPromise = domain == null ? Promise.resolve([]) : const eqDomainsPromise = domain == null ? Promise.resolve([]) :
this.settingsService.getEquivalentDomains().then((eqDomains: any[][]) => { this.settingsService.getEquivalentDomains().then((eqDomains: any[][]) => {
let matches: any[] = []; let matches: any[] = [];
@@ -196,20 +201,55 @@ export class CipherService implements CipherServiceAbstraction {
const ciphers = result[1]; const ciphers = result[1];
return ciphers.filter((cipher) => { return ciphers.filter((cipher) => {
// TODO: uris if (includeOtherTypes && includeOtherTypes.indexOf(cipher.type) > -1) {
//if (domain && cipher.type === CipherType.Login && cipher.login.domain && return true;
// matchingDomains.indexOf(cipher.login.domain) > -1) { }
// return true;
//} else if (includeOtherTypes && includeOtherTypes.indexOf(cipher.type) > -1) { if (url != null && cipher.type === CipherType.Login && cipher.login.uris != null) {
// return true; for (let i = 0; i < cipher.login.uris.length; i++) {
//} const u = cipher.login.uris[i];
if (u.uri == null) {
continue;
}
switch (u.match) {
case null:
case undefined:
case UriMatchType.BaseDomain:
if (domain != null && u.domain != null && matchingDomains.indexOf(u.domain) > -1) {
return true;
}
case UriMatchType.Host:
const urlHost = this.utilsService.getHost(url);
if (urlHost != null && urlHost === this.utilsService.getHost(u.uri)) {
return true;
}
case UriMatchType.Exact:
if (url === u.uri) {
return true;
}
case UriMatchType.StartsWith:
if (url.startsWith(u.uri)) {
return true;
}
case UriMatchType.RegularExpression:
const regex = new RegExp(u.uri, 'i');
if (regex.test(url)) {
return true;
}
case UriMatchType.Never:
default:
break;
}
}
}
return false; return false;
}); });
} }
async getLastUsedForDomain(domain: string): Promise<CipherView> { async getLastUsedForUrl(url: string): Promise<CipherView> {
const ciphers = await this.getAllDecryptedForDomain(domain); const ciphers = await this.getAllDecryptedForUrl(url);
if (ciphers.length === 0) { if (ciphers.length === 0) {
return null; return null;
} }

View File

@@ -53,7 +53,7 @@ export class SearchService implements SearchServiceAbstraction {
private transformQuery(query: string) { private transformQuery(query: string) {
if (query.indexOf('>') === 0) { if (query.indexOf('>') === 0) {
return query.substr(1).trimLeft(); return query.substr(1);
} }
return '*' + query + '*'; return '*' + query + '*';
} }

View File

@@ -132,6 +132,16 @@ export class UtilsService implements UtilsServiceAbstraction {
} }
static getHostname(uriString: string): string { static getHostname(uriString: string): string {
const url = UtilsService.getUrl(uriString);
return url != null ? url.hostname : null;
}
static getHost(uriString: string): string {
const url = UtilsService.getUrl(uriString);
return url != null ? url.host : null;
}
private static getUrl(uriString: string): URL {
if (uriString == null) { if (uriString == null) {
return null; return null;
} }
@@ -143,8 +153,7 @@ export class UtilsService implements UtilsServiceAbstraction {
if (uriString.startsWith('http://') || uriString.startsWith('https://')) { if (uriString.startsWith('http://') || uriString.startsWith('https://')) {
try { try {
const url = new URL(uriString); return new URL(uriString);
return url.hostname;
} catch (e) { } } catch (e) { }
} }
@@ -155,6 +164,10 @@ export class UtilsService implements UtilsServiceAbstraction {
return UtilsService.getHostname(uriString); return UtilsService.getHostname(uriString);
} }
getHost(uriString: string): string {
return UtilsService.getHost(uriString);
}
copyToClipboard(text: string, doc?: Document) { copyToClipboard(text: string, doc?: Document) {
UtilsService.copyToClipboard(text, doc); UtilsService.copyToClipboard(text, doc);
} }