mirror of
https://github.com/bitwarden/browser
synced 2025-12-20 18:23:31 +00:00
move getDomain to jslib Utils
This commit is contained in:
@@ -13,7 +13,6 @@ export abstract class PlatformUtilsService {
|
||||
isIE: () => boolean;
|
||||
isMacAppStore: () => boolean;
|
||||
analyticsId: () => string;
|
||||
getDomain: (uriString: string) => string;
|
||||
isViewOpen: () => boolean;
|
||||
lockTimeout: () => number;
|
||||
launchUri: (uri: string, options?: any) => void;
|
||||
|
||||
@@ -17,7 +17,6 @@ import { MessagingService } from '../../abstractions/messaging.service';
|
||||
import { PlatformUtilsService } from '../../abstractions/platformUtils.service';
|
||||
|
||||
import { AnalyticsIds } from '../../misc/analytics';
|
||||
import { Utils } from '../../misc/utils';
|
||||
|
||||
export class ElectronPlatformUtilsService implements PlatformUtilsService {
|
||||
identityClientId: string;
|
||||
@@ -99,10 +98,6 @@ export class ElectronPlatformUtilsService implements PlatformUtilsService {
|
||||
return this.analyticsIdCache;
|
||||
}
|
||||
|
||||
getDomain(uriString: string): string {
|
||||
return Utils.getHostname(uriString);
|
||||
}
|
||||
|
||||
isViewOpen(): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import * as tldjs from 'tldjs';
|
||||
|
||||
import { I18nService } from '../abstractions/i18n.service';
|
||||
|
||||
// tslint:disable-next-line
|
||||
@@ -163,6 +165,36 @@ export class Utils {
|
||||
}
|
||||
}
|
||||
|
||||
static getDomain(uriString: string): string {
|
||||
if (uriString == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
uriString = uriString.trim();
|
||||
if (uriString === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (uriString.startsWith('http://') || uriString.startsWith('https://')) {
|
||||
try {
|
||||
const url = Utils.getUrlObject(uriString);
|
||||
if (url.hostname === 'localhost' || Utils.validIpAddress(url.hostname)) {
|
||||
return url.hostname;
|
||||
}
|
||||
|
||||
const urlDomain = tldjs != null && tldjs.getDomain != null ? tldjs.getDomain(url.hostname) : null;
|
||||
return urlDomain != null ? urlDomain : url.hostname;
|
||||
} catch (e) { }
|
||||
}
|
||||
|
||||
const domain = tldjs != null && tldjs.getDomain != null ? tldjs.getDomain(uriString) : null;
|
||||
if (domain != null) {
|
||||
return domain;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
static getQueryParams(uriString: string): Map<string, string> {
|
||||
const url = Utils.getUrl(uriString);
|
||||
if (url == null || url.search == null || url.search === '') {
|
||||
@@ -197,6 +229,12 @@ export class Utils {
|
||||
};
|
||||
}
|
||||
|
||||
private static validIpAddress(ipString: string): boolean {
|
||||
// tslint:disable-next-line
|
||||
const ipRegex = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
|
||||
return ipRegex.test(ipString);
|
||||
}
|
||||
|
||||
private static isMobile(win: Window) {
|
||||
let mobile = false;
|
||||
((a) => {
|
||||
@@ -225,6 +263,10 @@ export class Utils {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Utils.getUrlObject(uriString);
|
||||
}
|
||||
|
||||
private static getUrlObject(uriString: string): URL {
|
||||
try {
|
||||
if (nodeURL != null) {
|
||||
return nodeURL.URL ? new nodeURL.URL(uriString) : nodeURL.parse(uriString);
|
||||
|
||||
@@ -4,8 +4,6 @@ import { View } from './view';
|
||||
|
||||
import { LoginUri } from '../domain/loginUri';
|
||||
|
||||
import { PlatformUtilsService } from '../../abstractions/platformUtils.service';
|
||||
|
||||
import { Utils } from '../../misc/utils';
|
||||
|
||||
export class LoginUriView implements View {
|
||||
@@ -35,15 +33,9 @@ export class LoginUriView implements View {
|
||||
|
||||
get domain(): string {
|
||||
if (this._domain == null && this.uri != null) {
|
||||
const containerService = (Utils.global as any).bitwardenContainerService;
|
||||
if (containerService) {
|
||||
const platformUtilsService: PlatformUtilsService = containerService.getPlatformUtilsService();
|
||||
this._domain = platformUtilsService.getDomain(this.uri);
|
||||
if (this._domain === '') {
|
||||
this._domain = null;
|
||||
}
|
||||
} else {
|
||||
throw new Error('global bitwardenContainerService not initialized.');
|
||||
this._domain = Utils.getDomain(this.uri);
|
||||
if (this._domain === '') {
|
||||
this._domain = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -37,7 +37,6 @@ import { ApiService } from '../abstractions/api.service';
|
||||
import { CipherService as CipherServiceAbstraction } from '../abstractions/cipher.service';
|
||||
import { CryptoService } from '../abstractions/crypto.service';
|
||||
import { I18nService } from '../abstractions/i18n.service';
|
||||
import { PlatformUtilsService } from '../abstractions/platformUtils.service';
|
||||
import { SearchService } from '../abstractions/search.service';
|
||||
import { SettingsService } from '../abstractions/settings.service';
|
||||
import { StorageService } from '../abstractions/storage.service';
|
||||
@@ -59,7 +58,7 @@ export class CipherService implements CipherServiceAbstraction {
|
||||
constructor(private cryptoService: CryptoService, private userService: UserService,
|
||||
private settingsService: SettingsService, private apiService: ApiService,
|
||||
private storageService: StorageService, private i18nService: I18nService,
|
||||
private platformUtilsService: PlatformUtilsService, private searchService: () => SearchService) {
|
||||
private searchService: () => SearchService) {
|
||||
}
|
||||
|
||||
get decryptedCipherCache() {
|
||||
@@ -311,7 +310,7 @@ export class CipherService implements CipherServiceAbstraction {
|
||||
return Promise.resolve([]);
|
||||
}
|
||||
|
||||
const domain = this.platformUtilsService.getDomain(url);
|
||||
const domain = Utils.getDomain(url);
|
||||
const eqDomainsPromise = domain == null ? Promise.resolve([]) :
|
||||
this.settingsService.getEquivalentDomains().then((eqDomains: any[][]) => {
|
||||
let matches: any[] = [];
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
import { CryptoService } from '../abstractions/crypto.service';
|
||||
import { PlatformUtilsService } from '../abstractions/platformUtils.service';
|
||||
|
||||
export class ContainerService {
|
||||
constructor(private cryptoService: CryptoService,
|
||||
private platformUtilsService: PlatformUtilsService) {
|
||||
constructor(private cryptoService: CryptoService) {
|
||||
}
|
||||
|
||||
// deprecated, use attachToGlobal instead
|
||||
@@ -20,8 +18,4 @@ export class ContainerService {
|
||||
getCryptoService(): CryptoService {
|
||||
return this.cryptoService;
|
||||
}
|
||||
|
||||
getPlatformUtilsService(): PlatformUtilsService {
|
||||
return this.platformUtilsService;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user