Skip to content

Commit

Permalink
fetch url
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanessa-Kris committed Jul 14, 2024
1 parent 9bbb907 commit 1773973
Show file tree
Hide file tree
Showing 9 changed files with 504 additions and 132 deletions.
3 changes: 2 additions & 1 deletion config-overrides.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const TerserPlugin = require("terser-webpack-plugin");
module.exports = function override(config) {
config.resolve.fallback = {
fs: require.resolve("browserify-fs"),
crypto: require.resolve('crypto-browserify'),
path: require.resolve("path-browserify"),
assert: require.resolve("assert"),
util: require.resolve("util/"),
Expand All @@ -17,7 +18,7 @@ module.exports = function override(config) {
tls: false,
net: false,
dns: false,
fs: false,
vm: false
};

config.module.rules.push({
Expand Down
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"dependencies": {
"@emotion/react": "^11.11.4",
"@emotion/styled": "^11.11.5",
"@getstation/electron-google-oauth2": "^14.0.0",
"@grpc/grpc-js": "^1.10.8",
"@grpc/proto-loader": "^0.7.13",
"@mui/icons-material": "^5.16.0",
Expand All @@ -19,11 +20,15 @@
"@testing-library/react": "^16.0.0",
"@testing-library/user-event": "^14.5.2",
"assert": "^2.1.0",
"base64-arraybuffer": "^1.0.2",
"browserify-fs": "^1.0.0",
"buffer": "^6.0.3",
"constants-browserify": "^1.0.0",
"crypto": "^1.0.1",
"crypto-browserify": "^3.12.0",
"electron-json-storage": "^4.6.0",
"electron-store": "^10.0.0",
"fernet": "^0.3.2",
"google-protobuf": "^3.21.2",
"i18next": "^23.11.4",
"keytar": "^7.9.0",
Expand All @@ -42,6 +47,7 @@
"stream-browserify": "^3.0.0",
"tweetnacl": "^1.0.3",
"tweetnacl-util": "^0.15.1",
"utf8": "^3.0.0",
"util": "^0.12.5",
"web-vitals": "^2.1.0"
},
Expand Down Expand Up @@ -121,5 +127,8 @@
"category": "Communication",
"icon": "./public/icon.png"
}
},
"config": {
"forge": "/config-overrides.js"
}
}
162 changes: 89 additions & 73 deletions public/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ const url = require("url");
const storage = require("electron-json-storage");
const vault = require("./vault");
const publisher = require("./publisher");
const safestorage = require('./storage');
const safestorage = require("./storage");
const os = require("os");
const GoogleOAuth2 = require("@getstation/electron-google-oauth2").default;

let mainWindow;

function createWindow() {
const mainWindow = new BrowserWindow({
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
webPreferences: {
Expand Down Expand Up @@ -149,16 +152,11 @@ ipcMain.handle(

ipcMain.handle(
"list-entity-stored-tokens",
async (
event,
{
long_lived_token
}
) => {
async (event, { long_lived_token }) => {
return new Promise((resolve, reject) => {
vault.listEntityStoredTokens(
{
long_lived_token: long_lived_token,
long_lived_token: long_lived_token,
},
(err, response) => {
if (err) {
Expand All @@ -176,15 +174,10 @@ ipcMain.handle(
"get-oauth2-authorization-url",
async (
event,
{
platform,
state,
code_verifier,
autogenerate_code_verifier,
}
{ platform, state, code_verifier, autogenerate_code_verifier }
) => {
return new Promise((resolve, reject) => {
publisher.getOAuth2AuthorizationUrl(
publisher.getOAuth2AuthorizationUrl(
{
platform: platform,
state: state,
Expand All @@ -203,6 +196,32 @@ ipcMain.handle(
}
);

ipcMain.handle(
"exchange-oauth2-code-and-store",
async (
event,
{ long_lived_token, platform, authorization_code, code_verifier }
) => {
return new Promise((resolve, reject) => {
publisher.exchangeOAuth2CodeAndStore(
{
long_lived_token: long_lived_token,
platform: platform,
authorization_code: authorization_code,
code_verifier: code_verifier,
},
(err, response) => {
if (err) {
reject(err);
} else {
resolve(response);
}
}
);
});
}
);

ipcMain.handle("store-params", async (event, { key, params }) => {
return new Promise((resolve, reject) => {
const encryptedParams = safestorage.encryptString(JSON.stringify(params));
Expand Down Expand Up @@ -252,7 +271,6 @@ ipcMain.handle("delete-session", async () => {
});
});


ipcMain.handle("retrieve-params", async (event, { key }) => {
return new Promise((resolve, reject) => {
storage.get(key, (error, data) => {
Expand Down Expand Up @@ -298,63 +316,61 @@ ipcMain.handle("retrieve-onboarding-step", async () => {
});
});

ipcMain.handle("store-server-keys", async (event, { clientDeviceIdPrivKey, clientPublishPrivKey }) => {
return new Promise((resolve, reject) => {
try {
const encryptedDeviceKey = safestorage.encryptString(clientDeviceIdPrivKey);
const encryptedPublishKey = safestorage.encryptString(clientPublishPrivKey);
storage.set("clientDeviceIdPrivKey", { data: encryptedDeviceKey }, (error) => {
if (error) {
reject(error);
} else {
storage.set("clientPublishPrivKey", { data: encryptedPublishKey }, (error) => {
if (error) {
reject(error);
} else {
resolve();
}
});
}
});
} catch (error) {
reject(error);
}
});
});
// ipcMain.handle("open-external-url", async (event, url) => {
// try {
// await shell.openExternal(url);
// return true;
// } catch (error) {
// console.error("Failed to open external URL:", error);
// return false;
// }
// });

ipcMain.handle("retrieve-server-keys", async () => {
return new Promise((resolve, reject) => {
storage.get("clientDeviceIdPrivKey", (error, deviceKeyData) => {
if (error) {
reject(error);
} else {
storage.get("clientPublishPrivKey", (error, publishKeyData) => {
if (error) {
reject(error);
} else {
try {
const decryptedDeviceKey = safestorage.decryptString(deviceKeyData.data);
const decryptedPublishKey = safestorage.decryptString(publishKeyData.data);
resolve({
clientDeviceIdPrivKey: decryptedDeviceKey,
clientPublishPrivKey: decryptedPublishKey,
});
} catch (decryptionError) {
reject(decryptionError);
}
}
});
}
// Handle open-oauth invocation from renderer
ipcMain.handle(
"open-oauth",
async (event, { oauthUrl, expectedRedirect, clientID, scope }) => {
const googleOAuth2 = new GoogleOAuth2(clientID, "", scope, {
successRedirectURL: expectedRedirect,
});
});
});

ipcMain.handle("open-external-url", async (event, url) => {
try {
await shell.openExternal(url);
event.returnValue = true; // Optional: return a value if needed
} catch (error) {
console.error("Failed to open external URL:", error);
event.returnValue = false; // Optional: handle failure if needed
const code = await googleOAuth2.openAuthWindowAndGetAuthorizationCode(
oauthUrl
);
console.log(code);

// Get system architecture and app name
// const arch = os.arch();
// const appName = app.getName();

// console.log(os.platform());
// console.log(arch);
// console.log(appName);

// // Set user agent to mimic the latest version of Firefox with dynamic values
// const userAgent = `Mozilla/5.0 (${os.platform()}; ${arch}; rv:91.0) Gecko/20100101 Firefox/91.0 ${appName}`;
// authWindow.webContents.userAgent = userAgent;

// authWindow.loadURL(oauthUrl);
// // authWindow.webContents.on("did-redirect-navigation", (event, newUrl) => {
// // const parsedUrl = new URL(newUrl);
// // console.log(">>>1", parsedUrl);
// // // Send the full URL including query parameters to the renderer process
// // mainWindow.webContents.send("oauth-url", newUrl);
// // authWindow.close();
// // });

// authWindow.webContents.on("will-navigate", (event, newUrl) => {
// const parsedUrl = new URL(newUrl);
// const parsedRedirectUrl = new URL(expectedRedirect);
// console.log(newUrl);
// if (
// parsedUrl.hostname === parsedRedirectUrl.hostname &&
// parsedUrl.pathname === parsedRedirectUrl.pathname
// ) {
// mainWindow.webContents.send("oauth-url", newUrl);
// authWindow.close();
// }
// });
}
});
);
Loading

0 comments on commit 1773973

Please sign in to comment.