Skip to content

Commit

Permalink
[PM-3660] Address PR feedback (#6157)
Browse files Browse the repository at this point in the history
* [PM-3660] chore: simplify object assignment

* [PM-3660] fix: remove unused origin field

* [PM-3660] feat: add Fido2Key tests

* [PM-3660] chore: convert popOut to async func

* [PM-3660] chore: refactor if-statements

* [PM-3660] chore: simplify closePopOut

* [PM-3660] fix: remove confusing comment

* [PM-3660] chore: move guid utils away from platform utils

* [PM-3660] chore: use null instead of undefined

* [PM-3660] chore: use `switch` instead of `if`
  • Loading branch information
coroiu authored Sep 7, 2023
1 parent 7705afc commit 64fa5fc
Show file tree
Hide file tree
Showing 14 changed files with 318 additions and 186 deletions.
4 changes: 4 additions & 0 deletions apps/browser/src/platform/browser/browser-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export class BrowserApi {
);
}

static async removeWindow(windowId: number) {
await chrome.tabs.remove(windowId);
}

static async getTabFromCurrentWindowId(): Promise<chrome.tabs.Tab> | null {
return await BrowserApi.tabsQueryFirst({
active: true,
Expand Down
109 changes: 53 additions & 56 deletions apps/browser/src/popup/services/popup-utils.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,69 +55,66 @@ export class PopupUtilsService {
}
}

popOut(win: Window, href: string = null, options: { center?: boolean } = {}): Promise<Popout> {
return new Promise((resolve, reject) => {
if (href === null) {
href = win.location.href;
}
async popOut(
win: Window,
href: string = null,
options: { center?: boolean } = {}
): Promise<Popout> {
if (href === null) {
href = win.location.href;
}

if (typeof chrome !== "undefined" && chrome.windows && chrome.windows.create) {
if (href.indexOf("?uilocation=") > -1) {
href = href
.replace("uilocation=popup", "uilocation=popout")
.replace("uilocation=tab", "uilocation=popout")
.replace("uilocation=sidebar", "uilocation=popout");
} else {
const hrefParts = href.split("#");
href =
hrefParts[0] + "?uilocation=popout" + (hrefParts.length > 0 ? "#" + hrefParts[1] : "");
}

const bodyRect = document.querySelector("body").getBoundingClientRect();
const width = Math.round(bodyRect.width ? bodyRect.width + 60 : 375);
const height = Math.round(bodyRect.height || 600);
const top = options.center ? Math.round((screen.height - height) / 2) : undefined;
const left = options.center ? Math.round((screen.width - width) / 2) : undefined;
chrome.windows.create(
{
url: href,
type: "popup",
width,
height,
top,
left,
},
(window) => resolve({ type: "window", window })
);

if (win && this.inPopup(win)) {
BrowserApi.closePopup(win);
}
} else if (typeof chrome !== "undefined" && chrome.tabs && chrome.tabs.create) {
if (chrome?.windows?.create != null) {
if (href.indexOf("?uilocation=") > -1) {
href = href
.replace("uilocation=popup", "uilocation=tab")
.replace("uilocation=popout", "uilocation=tab")
.replace("uilocation=sidebar", "uilocation=tab");
chrome.tabs.create(
{
url: href,
},
(tab) => resolve({ type: "tab", tab })
);
.replace("uilocation=popup", "uilocation=popout")
.replace("uilocation=tab", "uilocation=popout")
.replace("uilocation=sidebar", "uilocation=popout");
} else {
reject(new Error("Cannot open tab or window"));
const hrefParts = href.split("#");
href =
hrefParts[0] + "?uilocation=popout" + (hrefParts.length > 0 ? "#" + hrefParts[1] : "");
}
});

const bodyRect = document.querySelector("body").getBoundingClientRect();
const width = Math.round(bodyRect.width ? bodyRect.width + 60 : 375);
const height = Math.round(bodyRect.height || 600);
const top = options.center ? Math.round((screen.height - height) / 2) : undefined;
const left = options.center ? Math.round((screen.width - width) / 2) : undefined;
const window = await BrowserApi.createWindow({
url: href,
type: "popup",
width,
height,
top,
left,
});

if (win && this.inPopup(win)) {
BrowserApi.closePopup(win);
}

return { type: "window", window };
} else if (chrome?.tabs?.create != null) {
href = href
.replace("uilocation=popup", "uilocation=tab")
.replace("uilocation=popout", "uilocation=tab")
.replace("uilocation=sidebar", "uilocation=tab");

const tab = await BrowserApi.createNewTab(href);
return { type: "tab", tab };
} else {
throw new Error("Cannot open tab or window");
}
}

closePopOut(popout: Popout): Promise<void> {
return new Promise((resolve) => {
if (popout.type === "window") {
chrome.windows.remove(popout.window.id, resolve);
} else {
chrome.tabs.remove(popout.tab.id, resolve);
}
});
switch (popout.type) {
case "window":
return BrowserApi.removeWindow(popout.window.id);
case "tab":
return BrowserApi.removeTab(popout.tab.id);
}
}

/**
Expand Down
98 changes: 0 additions & 98 deletions libs/common/src/platform/misc/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ export class Utils {
["google.com", new Set(["script.google.com"])],
]);

/** Used by guidToStandardFormat */
private static byteToHex: string[] = [];

static init() {
if (Utils.inited) {
return;
Expand All @@ -63,10 +60,6 @@ export class Utils {
// If it's not browser or node then it must be a service worker
Utils.global = self;
}

for (let i = 0; i < 256; ++i) {
Utils.byteToHex.push((i + 0x100).toString(16).substring(1));
}
}

static fromB64ToArray(str: string): Uint8Array {
Expand Down Expand Up @@ -584,97 +577,6 @@ export class Utils {

return null;
}

/*
License for: guidToRawFormat, guidToStandardFormat
Source: https://github.com/uuidjs/uuid/
The MIT License (MIT)
Copyright (c) 2010-2020 Robert Kieffer and other contributors
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/

/** Convert standard format (XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX) UUID to raw 16 byte array. */
static guidToRawFormat(guid: string) {
if (!Utils.isGuid(guid)) {
throw TypeError("GUID parameter is invalid");
}

let v;
const arr = new Uint8Array(16);

// Parse ########-....-....-....-............
arr[0] = (v = parseInt(guid.slice(0, 8), 16)) >>> 24;
arr[1] = (v >>> 16) & 0xff;
arr[2] = (v >>> 8) & 0xff;
arr[3] = v & 0xff;

// Parse ........-####-....-....-............
arr[4] = (v = parseInt(guid.slice(9, 13), 16)) >>> 8;
arr[5] = v & 0xff;

// Parse ........-....-####-....-............
arr[6] = (v = parseInt(guid.slice(14, 18), 16)) >>> 8;
arr[7] = v & 0xff;

// Parse ........-....-....-####-............
arr[8] = (v = parseInt(guid.slice(19, 23), 16)) >>> 8;
arr[9] = v & 0xff;

// Parse ........-....-....-....-############
// (Use "/" to avoid 32-bit truncation when bit-shifting high-order bytes)
arr[10] = ((v = parseInt(guid.slice(24, 36), 16)) / 0x10000000000) & 0xff;
arr[11] = (v / 0x100000000) & 0xff;
arr[12] = (v >>> 24) & 0xff;
arr[13] = (v >>> 16) & 0xff;
arr[14] = (v >>> 8) & 0xff;
arr[15] = v & 0xff;

return arr;
}

/** Convert raw 16 byte array to standard format (XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX) UUID. */
static guidToStandardFormat(bufferSource: BufferSource) {
const arr =
bufferSource instanceof ArrayBuffer
? new Uint8Array(bufferSource)
: new Uint8Array(bufferSource.buffer);
// Note: Be careful editing this code! It's been tuned for performance
// and works in ways you may not expect. See https://github.com/uuidjs/uuid/pull/434
const guid = (
Utils.byteToHex[arr[0]] +
Utils.byteToHex[arr[1]] +
Utils.byteToHex[arr[2]] +
Utils.byteToHex[arr[3]] +
"-" +
Utils.byteToHex[arr[4]] +
Utils.byteToHex[arr[5]] +
"-" +
Utils.byteToHex[arr[6]] +
Utils.byteToHex[arr[7]] +
"-" +
Utils.byteToHex[arr[8]] +
Utils.byteToHex[arr[9]] +
"-" +
Utils.byteToHex[arr[10]] +
Utils.byteToHex[arr[11]] +
Utils.byteToHex[arr[12]] +
Utils.byteToHex[arr[13]] +
Utils.byteToHex[arr[14]] +
Utils.byteToHex[arr[15]]
).toLowerCase();

// Consistency check for valid UUID. If this throws, it's likely due to one
// of the following:
// - One or more input array values don't map to a hex octet (leading to
// "undefined" in the uuid)
// - Invalid input values for the RFC `version` or `variant` fields
if (!Utils.isGuid(guid)) {
throw TypeError("Converted GUID is invalid");
}

return guid;
}
}

Utils.init();
2 changes: 0 additions & 2 deletions libs/common/src/vault/api/fido2-key.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ export class Fido2KeyApi extends BaseResponse {
rpId: string;
userHandle: string;
counter: string;

// Extras
rpName: string;
userDisplayName: string;

Expand Down
2 changes: 0 additions & 2 deletions libs/common/src/vault/models/data/fido2-key.data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ export class Fido2KeyData {
rpId: string;
userHandle: string;
counter: string;

// Extras
rpName: string;
userDisplayName: string;

Expand Down
Loading

0 comments on commit 64fa5fc

Please sign in to comment.