bitwarden-estensione-browser/src/services/webPlatformUtils.service.ts

312 lines
11 KiB
TypeScript
Raw Normal View History

import Swal, { SweetAlertIcon } from 'sweetalert2';
2018-06-08 20:56:26 +02:00
import { DeviceType } from 'jslib-common/enums/deviceType';
Dark Theme (#1017) * Stylesheets * Theme Configuration * Options Area * swal2 style * Icon styling * Fix theme not saving * Update English * Update messages.json * dropdown and login logo * btn-link and totp fix * Organisation Styling * Update webauthn-fallback.ts * Fix contrast issues * Add Paypal Container and Loading svg file * Password Generator contrast fix * Dark Mode Fix buttons and foreground * Fix button hover * Fix Styles after rebase * Add hover on nav dropdown-item * Disable Theme Preview * Options Fix for Default Theme Changes * Updated Colour Scheme * Toast fix * Button and Text Styling * Options Update and Messages Fix * Added Search Icon and Fixed Callout styling * Add theme styling to Stripe * Refactor logic for setting color * Reorder logic to avoid race condition * PayPal Loading and Misc Fix * text-state bug fix * Badge Colour Fix * Remove PayPal Tagline The colour cannot be styled so it's not visible on a dark theme * Adding the Styling from #1131 * Update to New Design * Form and Nav restyle * Modal Opacity and Callout * Nav Colours * Missing Borders * Light theme fix * Improved border for listgroup * Change Org Nav Colour * Save theme to localStorage for persistence * Undo change to Wired image * !Important removal and tweaks * Fix regression with navbar * Light theme by default * Refactor to use getEffectiveTheme * Refactor theme constants to use enum * Set theme in index.html before app loads * Use scss selector to set logo image * Export Sass to TS * Update jslib Co-authored-by: Thomas Rittson <trittson@bitwarden.com>
2021-09-30 00:06:20 +02:00
import { ThemeType } from 'jslib-common/enums/themeType';
2018-06-05 21:02:53 +02:00
import { I18nService } from 'jslib-common/abstractions/i18n.service';
import { LogService } from 'jslib-common/abstractions/log.service';
import { MessagingService } from 'jslib-common/abstractions/messaging.service';
import { PlatformUtilsService } from 'jslib-common/abstractions/platformUtils.service';
Dark Theme (#1017) * Stylesheets * Theme Configuration * Options Area * swal2 style * Icon styling * Fix theme not saving * Update English * Update messages.json * dropdown and login logo * btn-link and totp fix * Organisation Styling * Update webauthn-fallback.ts * Fix contrast issues * Add Paypal Container and Loading svg file * Password Generator contrast fix * Dark Mode Fix buttons and foreground * Fix button hover * Fix Styles after rebase * Add hover on nav dropdown-item * Disable Theme Preview * Options Fix for Default Theme Changes * Updated Colour Scheme * Toast fix * Button and Text Styling * Options Update and Messages Fix * Added Search Icon and Fixed Callout styling * Add theme styling to Stripe * Refactor logic for setting color * Reorder logic to avoid race condition * PayPal Loading and Misc Fix * text-state bug fix * Badge Colour Fix * Remove PayPal Tagline The colour cannot be styled so it's not visible on a dark theme * Adding the Styling from #1131 * Update to New Design * Form and Nav restyle * Modal Opacity and Callout * Nav Colours * Missing Borders * Light theme fix * Improved border for listgroup * Change Org Nav Colour * Save theme to localStorage for persistence * Undo change to Wired image * !Important removal and tweaks * Fix regression with navbar * Light theme by default * Refactor to use getEffectiveTheme * Refactor theme constants to use enum * Set theme in index.html before app loads * Use scss selector to set logo image * Export Sass to TS * Update jslib Co-authored-by: Thomas Rittson <trittson@bitwarden.com>
2021-09-30 00:06:20 +02:00
import { StorageService } from 'jslib-common/abstractions/storage.service';
import { ConstantsService } from 'jslib-common/services/constants.service';
2018-06-05 21:02:53 +02:00
export class WebPlatformUtilsService implements PlatformUtilsService {
identityClientId: string = 'web';
2018-07-09 15:32:24 +02:00
private browserCache: DeviceType = null;
Dark Theme (#1017) * Stylesheets * Theme Configuration * Options Area * swal2 style * Icon styling * Fix theme not saving * Update English * Update messages.json * dropdown and login logo * btn-link and totp fix * Organisation Styling * Update webauthn-fallback.ts * Fix contrast issues * Add Paypal Container and Loading svg file * Password Generator contrast fix * Dark Mode Fix buttons and foreground * Fix button hover * Fix Styles after rebase * Add hover on nav dropdown-item * Disable Theme Preview * Options Fix for Default Theme Changes * Updated Colour Scheme * Toast fix * Button and Text Styling * Options Update and Messages Fix * Added Search Icon and Fixed Callout styling * Add theme styling to Stripe * Refactor logic for setting color * Reorder logic to avoid race condition * PayPal Loading and Misc Fix * text-state bug fix * Badge Colour Fix * Remove PayPal Tagline The colour cannot be styled so it's not visible on a dark theme * Adding the Styling from #1131 * Update to New Design * Form and Nav restyle * Modal Opacity and Callout * Nav Colours * Missing Borders * Light theme fix * Improved border for listgroup * Change Org Nav Colour * Save theme to localStorage for persistence * Undo change to Wired image * !Important removal and tweaks * Fix regression with navbar * Light theme by default * Refactor to use getEffectiveTheme * Refactor theme constants to use enum * Set theme in index.html before app loads * Use scss selector to set logo image * Export Sass to TS * Update jslib Co-authored-by: Thomas Rittson <trittson@bitwarden.com>
2021-09-30 00:06:20 +02:00
private prefersColorSchemeDark = window.matchMedia('(prefers-color-scheme: dark)');
2018-06-05 21:02:53 +02:00
constructor(private i18nService: I18nService, private messagingService: MessagingService,
Dark Theme (#1017) * Stylesheets * Theme Configuration * Options Area * swal2 style * Icon styling * Fix theme not saving * Update English * Update messages.json * dropdown and login logo * btn-link and totp fix * Organisation Styling * Update webauthn-fallback.ts * Fix contrast issues * Add Paypal Container and Loading svg file * Password Generator contrast fix * Dark Mode Fix buttons and foreground * Fix button hover * Fix Styles after rebase * Add hover on nav dropdown-item * Disable Theme Preview * Options Fix for Default Theme Changes * Updated Colour Scheme * Toast fix * Button and Text Styling * Options Update and Messages Fix * Added Search Icon and Fixed Callout styling * Add theme styling to Stripe * Refactor logic for setting color * Reorder logic to avoid race condition * PayPal Loading and Misc Fix * text-state bug fix * Badge Colour Fix * Remove PayPal Tagline The colour cannot be styled so it's not visible on a dark theme * Adding the Styling from #1131 * Update to New Design * Form and Nav restyle * Modal Opacity and Callout * Nav Colours * Missing Borders * Light theme fix * Improved border for listgroup * Change Org Nav Colour * Save theme to localStorage for persistence * Undo change to Wired image * !Important removal and tweaks * Fix regression with navbar * Light theme by default * Refactor to use getEffectiveTheme * Refactor theme constants to use enum * Set theme in index.html before app loads * Use scss selector to set logo image * Export Sass to TS * Update jslib Co-authored-by: Thomas Rittson <trittson@bitwarden.com>
2021-09-30 00:06:20 +02:00
private logService: LogService, private storageService: () => StorageService) { }
2018-06-05 21:02:53 +02:00
getDevice(): DeviceType {
2018-07-09 15:32:24 +02:00
if (this.browserCache != null) {
return this.browserCache;
}
if (navigator.userAgent.indexOf(' Firefox/') !== -1 || navigator.userAgent.indexOf(' Gecko/') !== -1) {
this.browserCache = DeviceType.FirefoxBrowser;
} else if (navigator.userAgent.indexOf(' OPR/') >= 0) {
this.browserCache = DeviceType.OperaBrowser;
} else if (navigator.userAgent.indexOf(' Edg/') !== -1) {
2018-07-09 15:32:24 +02:00
this.browserCache = DeviceType.EdgeBrowser;
} else if (navigator.userAgent.indexOf(' Vivaldi/') !== -1) {
this.browserCache = DeviceType.VivaldiBrowser;
} else if (navigator.userAgent.indexOf(' Safari/') !== -1 && navigator.userAgent.indexOf('Chrome') === -1) {
this.browserCache = DeviceType.SafariBrowser;
} else if ((window as any).chrome && navigator.userAgent.indexOf(' Chrome/') !== -1) {
this.browserCache = DeviceType.ChromeBrowser;
} else if (navigator.userAgent.indexOf(' Trident/') !== -1) {
this.browserCache = DeviceType.IEBrowser;
} else {
this.browserCache = DeviceType.UnknownBrowser;
}
return this.browserCache;
2018-06-05 21:02:53 +02:00
}
getDeviceString(): string {
2018-07-09 15:32:24 +02:00
const device = DeviceType[this.getDevice()].toLowerCase();
return device.replace('browser', '');
2018-06-05 21:02:53 +02:00
}
isFirefox(): boolean {
2018-07-09 15:32:24 +02:00
return this.getDevice() === DeviceType.FirefoxBrowser;
2018-06-05 21:02:53 +02:00
}
isChrome(): boolean {
2018-07-09 15:32:24 +02:00
return this.getDevice() === DeviceType.ChromeBrowser;
2018-06-05 21:02:53 +02:00
}
isEdge(): boolean {
2018-07-09 15:32:24 +02:00
return this.getDevice() === DeviceType.EdgeBrowser;
2018-06-06 15:43:28 +02:00
}
2018-06-05 21:02:53 +02:00
isOpera(): boolean {
2018-07-09 15:32:24 +02:00
return this.getDevice() === DeviceType.OperaBrowser;
2018-06-05 21:02:53 +02:00
}
isVivaldi(): boolean {
2018-07-09 15:32:24 +02:00
return this.getDevice() === DeviceType.VivaldiBrowser;
2018-06-05 21:02:53 +02:00
}
isSafari(): boolean {
2018-07-09 15:32:24 +02:00
return this.getDevice() === DeviceType.SafariBrowser;
2018-06-07 23:12:11 +02:00
}
isIE(): boolean {
2018-07-09 15:32:24 +02:00
return this.getDevice() === DeviceType.IEBrowser;
2018-06-05 21:02:53 +02:00
}
isMacAppStore(): boolean {
return false;
}
2019-08-20 19:47:58 +02:00
isViewOpen(): Promise<boolean> {
return Promise.resolve(false);
2018-06-05 21:02:53 +02:00
}
launchUri(uri: string, options?: any): void {
2018-06-07 23:12:11 +02:00
const a = document.createElement('a');
a.href = uri;
if (options == null || !options.sameWindow) {
a.target = '_blank';
a.rel = 'noreferrer noopener';
}
a.classList.add('d-none');
document.body.appendChild(a);
2018-06-07 23:12:11 +02:00
a.click();
document.body.removeChild(a);
2018-06-05 21:02:53 +02:00
}
saveFile(win: Window, blobData: any, blobOptions: any, fileName: string): void {
let blob: Blob = null;
2018-10-30 14:54:14 +01:00
let type: string = null;
const fileNameLower = fileName.toLowerCase();
let doDownload = true;
2018-10-30 14:54:14 +01:00
if (fileNameLower.endsWith('.pdf')) {
type = 'application/pdf';
doDownload = false;
2018-10-30 14:54:14 +01:00
} else if (fileNameLower.endsWith('.xlsx')) {
type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
} else if (fileNameLower.endsWith('.docx')) {
type = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
} else if (fileNameLower.endsWith('.pptx')) {
type = 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
} else if (fileNameLower.endsWith('.csv')) {
type = 'text/csv';
} else if (fileNameLower.endsWith('.png')) {
type = 'image/png';
} else if (fileNameLower.endsWith('.jpg') || fileNameLower.endsWith('.jpeg')) {
type = 'image/jpeg';
} else if (fileNameLower.endsWith('.gif')) {
type = 'image/gif';
}
if (type != null) {
blobOptions = blobOptions || {};
if (blobOptions.type == null) {
blobOptions.type = type;
}
}
if (blobOptions != null && !this.isIE()) {
blob = new Blob([blobData], blobOptions);
} else {
blob = new Blob([blobData]);
}
2018-06-08 18:23:46 +02:00
if (navigator.msSaveOrOpenBlob) {
navigator.msSaveBlob(blob, fileName);
} else {
const a = win.document.createElement('a');
if (doDownload) {
a.download = fileName;
} else if (!this.isSafari()) {
a.target = '_blank';
}
a.href = URL.createObjectURL(blob);
2018-06-08 18:23:46 +02:00
a.style.position = 'fixed';
win.document.body.appendChild(a);
a.click();
win.document.body.removeChild(a);
}
2018-06-05 21:02:53 +02:00
}
2021-04-07 20:42:57 +02:00
getApplicationVersion(): Promise<string> {
return Promise.resolve(process.env.APPLICATION_VERSION || '-');
2018-06-05 21:02:53 +02:00
}
2021-03-16 17:44:31 +01:00
supportsWebAuthn(win: Window): boolean {
return (typeof(PublicKeyCredential) !== 'undefined');
2018-06-05 21:02:53 +02:00
}
supportsDuo(): boolean {
return true;
}
showToast(type: 'error' | 'success' | 'warning' | 'info', title: string, text: string | string[],
options?: any): void {
this.messagingService.send('showToast', {
text: text,
title: title,
type: type,
options: options,
});
2018-06-05 21:02:53 +02:00
}
async showDialog(body: string, title?: string, confirmText?: string, cancelText?: string, type?: string,
bodyIsHtml: boolean = false) {
2020-03-04 20:14:27 +01:00
let iconClasses: string = null;
2018-06-08 20:56:26 +02:00
if (type != null) {
// If you add custom types to this part, the type to SweetAlertIcon cast below needs to be changed.
2018-06-08 20:56:26 +02:00
switch (type) {
case 'success':
iconClasses = 'fa-check text-success';
2018-06-08 20:56:26 +02:00
break;
case 'warning':
iconClasses = 'fa-warning text-warning';
2018-06-08 20:56:26 +02:00
break;
case 'error':
iconClasses = 'fa-bolt text-danger';
2018-06-08 20:56:26 +02:00
break;
case 'info':
iconClasses = 'fa-info-circle text-info';
2018-06-08 20:56:26 +02:00
break;
default:
break;
}
}
const bootstrapModal = document.querySelector('div.modal');
if (bootstrapModal != null) {
bootstrapModal.removeAttribute('tabindex');
}
const iconHtmlStr = iconClasses != null ? `<i class="swal-custom-icon fa ${iconClasses}"></i>` : undefined;
const confirmed = await Swal.fire({
heightAuto: false,
buttonsStyling: false,
icon: type as SweetAlertIcon, // required to be any of the SweetAlertIcons to output the iconHtml.
iconHtml: iconHtmlStr,
text: bodyIsHtml ? null : body,
html: bodyIsHtml ? body : null,
2021-05-13 16:08:16 +02:00
titleText: title,
showCancelButton: (cancelText != null),
cancelButtonText: cancelText,
showConfirmButton: true,
confirmButtonText: confirmText == null ? this.i18nService.t('ok') : confirmText,
2018-06-08 20:56:26 +02:00
});
if (bootstrapModal != null) {
bootstrapModal.setAttribute('tabindex', '-1');
}
return confirmed.value;
2018-06-05 21:02:53 +02:00
}
isDev(): boolean {
return process.env.NODE_ENV === 'development';
2018-06-05 21:02:53 +02:00
}
2018-06-30 20:14:52 +02:00
isSelfHost(): boolean {
return process.env.ENV.toString() === 'selfhosted';
2018-06-30 20:14:52 +02:00
}
copyToClipboard(text: string, options?: any): void | boolean {
2018-08-17 18:25:21 +02:00
let win = window;
let doc = window.document;
if (options && (options.window || options.win)) {
win = options.window || options.win;
doc = win.document;
} else if (options && options.doc) {
doc = options.doc;
}
2018-08-20 15:20:24 +02:00
if ((win as any).clipboardData && (win as any).clipboardData.setData) {
2018-06-05 21:02:53 +02:00
// IE specific code path to prevent textarea being shown while dialog is visible.
2018-08-17 18:25:21 +02:00
(win as any).clipboardData.setData('Text', text);
2018-06-05 21:02:53 +02:00
} else if (doc.queryCommandSupported && doc.queryCommandSupported('copy')) {
const textarea = doc.createElement('textarea');
textarea.textContent = text;
// Prevent scrolling to bottom of page in MS Edge.
textarea.style.position = 'fixed';
2018-07-26 04:01:26 +02:00
let copyEl = doc.body;
// For some reason copy command won't work when modal is open if appending to body
if (doc.body.classList.contains('modal-open')) {
2018-07-26 04:01:26 +02:00
copyEl = doc.body.querySelector<HTMLElement>('.modal');
}
copyEl.appendChild(textarea);
2018-06-05 21:02:53 +02:00
textarea.select();
let success = false;
2018-06-05 21:02:53 +02:00
try {
// Security exception may be thrown by some browsers.
success = doc.execCommand('copy');
if (!success) {
this.logService.debug('Copy command unsupported or disabled.');
}
2018-06-05 21:02:53 +02:00
} catch (e) {
// tslint:disable-next-line
console.warn('Copy to clipboard failed.', e);
} finally {
2018-07-26 04:01:26 +02:00
copyEl.removeChild(textarea);
2018-06-05 21:02:53 +02:00
}
return success;
2018-06-05 21:02:53 +02:00
}
}
2019-02-27 04:42:30 +01:00
readFromClipboard(options?: any): Promise<string> {
throw new Error('Cannot read from clipboard on web.');
}
supportsBiometric() {
return Promise.resolve(false);
}
authenticateBiometric() {
return Promise.resolve(false);
}
supportsSecureStorage() {
return false;
}
Dark Theme (#1017) * Stylesheets * Theme Configuration * Options Area * swal2 style * Icon styling * Fix theme not saving * Update English * Update messages.json * dropdown and login logo * btn-link and totp fix * Organisation Styling * Update webauthn-fallback.ts * Fix contrast issues * Add Paypal Container and Loading svg file * Password Generator contrast fix * Dark Mode Fix buttons and foreground * Fix button hover * Fix Styles after rebase * Add hover on nav dropdown-item * Disable Theme Preview * Options Fix for Default Theme Changes * Updated Colour Scheme * Toast fix * Button and Text Styling * Options Update and Messages Fix * Added Search Icon and Fixed Callout styling * Add theme styling to Stripe * Refactor logic for setting color * Reorder logic to avoid race condition * PayPal Loading and Misc Fix * text-state bug fix * Badge Colour Fix * Remove PayPal Tagline The colour cannot be styled so it's not visible on a dark theme * Adding the Styling from #1131 * Update to New Design * Form and Nav restyle * Modal Opacity and Callout * Nav Colours * Missing Borders * Light theme fix * Improved border for listgroup * Change Org Nav Colour * Save theme to localStorage for persistence * Undo change to Wired image * !Important removal and tweaks * Fix regression with navbar * Light theme by default * Refactor to use getEffectiveTheme * Refactor theme constants to use enum * Set theme in index.html before app loads * Use scss selector to set logo image * Export Sass to TS * Update jslib Co-authored-by: Thomas Rittson <trittson@bitwarden.com>
2021-09-30 00:06:20 +02:00
getDefaultSystemTheme(): Promise<ThemeType.Light | ThemeType.Dark> {
return Promise.resolve(this.prefersColorSchemeDark.matches ? ThemeType.Dark : ThemeType.Light);
}
async getEffectiveTheme(): Promise<ThemeType.Light | ThemeType.Dark> {
const theme = await this.storageService().get<ThemeType>(ConstantsService.themeKey);
if (theme === ThemeType.Dark) {
return ThemeType.Dark;
} else if (theme === ThemeType.System) {
return this.getDefaultSystemTheme();
} else {
return ThemeType.Light;
}
}
Dark Theme (#1017) * Stylesheets * Theme Configuration * Options Area * swal2 style * Icon styling * Fix theme not saving * Update English * Update messages.json * dropdown and login logo * btn-link and totp fix * Organisation Styling * Update webauthn-fallback.ts * Fix contrast issues * Add Paypal Container and Loading svg file * Password Generator contrast fix * Dark Mode Fix buttons and foreground * Fix button hover * Fix Styles after rebase * Add hover on nav dropdown-item * Disable Theme Preview * Options Fix for Default Theme Changes * Updated Colour Scheme * Toast fix * Button and Text Styling * Options Update and Messages Fix * Added Search Icon and Fixed Callout styling * Add theme styling to Stripe * Refactor logic for setting color * Reorder logic to avoid race condition * PayPal Loading and Misc Fix * text-state bug fix * Badge Colour Fix * Remove PayPal Tagline The colour cannot be styled so it's not visible on a dark theme * Adding the Styling from #1131 * Update to New Design * Form and Nav restyle * Modal Opacity and Callout * Nav Colours * Missing Borders * Light theme fix * Improved border for listgroup * Change Org Nav Colour * Save theme to localStorage for persistence * Undo change to Wired image * !Important removal and tweaks * Fix regression with navbar * Light theme by default * Refactor to use getEffectiveTheme * Refactor theme constants to use enum * Set theme in index.html before app loads * Use scss selector to set logo image * Export Sass to TS * Update jslib Co-authored-by: Thomas Rittson <trittson@bitwarden.com>
2021-09-30 00:06:20 +02:00
onDefaultSystemThemeChange(callback: ((theme: ThemeType.Light | ThemeType.Dark) => unknown)) {
this.prefersColorSchemeDark.addEventListener('change', ({ matches }) => {
callback(matches ? ThemeType.Dark : ThemeType.Light);
});
}
2018-06-05 21:02:53 +02:00
}