From d1154f77b4102e8d9fb18cfc9142e7a7d59e67b6 Mon Sep 17 00:00:00 2001 From: Marquis Kurt Date: Sat, 11 May 2019 16:03:40 -0400 Subject: [PATCH] Make redirection work for hyperspace://protocol --- package-lock.json | 3 +- package.json | 1 + public/config.json | 2 +- public/desktop.js | 68 +++++++++++++++++++++++++++++++---------- src/pages/Welcome.tsx | 8 ++--- src/utilities/login.tsx | 2 +- 6 files changed, 60 insertions(+), 24 deletions(-) diff --git a/package-lock.json b/package-lock.json index c938d21..2b69769 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11878,7 +11878,8 @@ "mdi-material-ui": { "version": "5.11.0", "resolved": "https://registry.npmjs.org/mdi-material-ui/-/mdi-material-ui-5.11.0.tgz", - "integrity": "sha512-9fIvdiKCKAfBoW11LqZsgaxZtu9WCQEd8FL9/8ceLHvStSf+fZM6sC7exwXaXZmzfwtJMfN1KiMGsPBPSTQFQg==" + "integrity": "sha512-9fIvdiKCKAfBoW11LqZsgaxZtu9WCQEd8FL9/8ceLHvStSf+fZM6sC7exwXaXZmzfwtJMfN1KiMGsPBPSTQFQg==", + "dev": true }, "mdn-data": { "version": "1.1.4", diff --git a/package.json b/package.json index d5180cd..14817e6 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "scripts": { "start": "HTTPS=true BROWSER='Safari Technology Preview' react-scripts start", "electrify": "npm run build; electron .", + "electrify-nobuild": "electron .", "build": "react-scripts build", "build-desktop": "build -mwl deb AppImage snap", "test": "react-scripts test", diff --git a/public/config.json b/public/config.json index 60594ab..f57832f 100644 --- a/public/config.json +++ b/public/config.json @@ -1,6 +1,6 @@ { "version": "1.0.0beta4", - "location": "https://hyperspaceapp-next.herokuapp.com", + "location": "desktop", "branding": { "name": "Hyperspace", "logo": "logo.svg", diff --git a/public/desktop.js b/public/desktop.js index ae592c9..6f03895 100644 --- a/public/desktop.js +++ b/public/desktop.js @@ -9,41 +9,77 @@ const path = require('path'); // Check for any updates to the app autoUpdater.checkForUpdatesAndNotify(); -// Create the protocol to use for Hyperspace in redirect URIs -// Also mark it as secure so that Mastodon is happy -protocol.registerStandardSchemes(['hyperspace'], {secure: true}); - // Create a container for the window let mainWindow; +protocol.registerSchemesAsPrivileged([ + { scheme: 'hyperspace', privileges: { standard: true, secure: true } } +]) + /** * Register the protocol for Hyperspace */ function registerProtocol() { protocol.registerFileProtocol('hyperspace', (request, callback) => { - //Throw a METHOD_NOT_SUPPORTED error if it isn't a GET request + + // Check to make sure we're doing a GET request if (request.method !== "GET") { callback({error: -322}); return null; } - // If the URL scheme doesn't contain the protocol, throw an error + // Check to make sure we're actually working with a hyperspace + // protocol and that the host is 'hyperspace' const parsedUrl = new URL(request.url); - if (parsedUrl.protocol !== "hyperspace") { + if (parsedUrl.protocol !== "hyperspace:") { callback({error: -302}); return; } - if (parsedUrl.host !== "hyperspace") { callback({error: -105}); return; } - }, (error) => { - if (error) { - console.error("Failed to register Hyperspace protocol."); - console.error(error.message); + + // + // Target Checks + // + + const target = parsedUrl.pathname.split("/"); + + //Check that the target isn't something else + if (target[0] !== "") { + callback({error: -6}); + return; } + + if (target[target.length -1] === "") { + target[target.length -1] = "index.html"; + } + + let baseDirectory; + if (target[1] === "app" || target[1] === "oauth") { + baseDirectory = __dirname + "/../build/"; + } else { + callback({error: -6}); + } + + baseDirectory = path.normalize(baseDirectory); + + const relTarget = path.normalize(path.join(...target.slice(2))); + if (relTarget.startsWith('..')) { + callback({error: -6}); + return; + } + const absTarget = path.join(baseDirectory, relTarget); + + callback({ + path: absTarget, + }); + + }, (error) => { + if (error) console.error('Failed to register protocol') }); + } /** @@ -55,12 +91,12 @@ function createWindow() { width: 1000, height: 600, minWidth: 476, - titleBarStyle: 'hidden', + //titleBarStyle: 'hidden', webPreferences: {nodeIntegration: true} } ); - mainWindow.loadURL(`file://${path.join(__dirname, '../build/index.html#/')}`); + mainWindow.loadURL("hyperspace://hyperspace/app/"); mainWindow.on('closed', () => { mainWindow = null @@ -186,7 +222,7 @@ function createMenubar() { app.on('ready', () => { registerProtocol(); createWindow(); - createMenubar(); + //createMenubar(); }); // Standard quit behavior changes for macOS @@ -199,7 +235,7 @@ app.on('window-all-closed', () => { // When the app is activated, create the window and menu bar app.on('activate', () => { if (mainWindow === null) { - registerProtocol(); + //registerProtocol(); createWindow(); createMenubar(); } diff --git a/src/pages/Welcome.tsx b/src/pages/Welcome.tsx index 690e54c..108a8fc 100644 --- a/src/pages/Welcome.tsx +++ b/src/pages/Welcome.tsx @@ -64,8 +64,6 @@ class WelcomePage extends Component { let config: Config = result; if (result.location === "dynamic") { console.warn("Recirect URI is set to dyanmic, which may affect how sign-in works for some users. Careful!"); - } else if (result.location === "desktop") { - console.warn("Recirect URI is set to desktop, which may affect how sign-in works for some users. This will use https://localhost; careful!"); } this.setState({ logoUrl: config.branding? result.branding.logo: "logo.png", @@ -170,7 +168,7 @@ class WelcomePage extends Component { this.state.brandName? this.state.brandName: "Hyperspace", scopes, baseurl, - this.state.defaultRedirectAddress + getRedirectAddress(this.state.defaultRedirectAddress) ).then((resp: any) => { let saveSessionForCrashing: SaveClientSession = { clientId: resp.clientId, @@ -290,10 +288,10 @@ class WelcomePage extends Component { undefined: clientLoginSession.authUrl.includes("urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob")? undefined: - `https://${window.location.host}`, + window.location.protocol === "hyperspace:"? "hyperspace://hyperspace/app/": `https://${window.location.host}`, ).then((tokenData: any) => { localStorage.setItem("access_token", tokenData.access_token); - window.location.href=`https://${window.location.host}/#/`; + window.location.href = window.location.protocol === "hyperspace:"? "hyperspace://hyperspace/app/": `https://${window.location.host}/#/`; }).catch((err: Error) => { this.props.enqueueSnackbar(`Couldn't authorize ${this.state.brandName? this.state.brandName: "Hyperspace"}: ${err.name}`, {variant: 'error'}); console.error(err.message); diff --git a/src/utilities/login.tsx b/src/utilities/login.tsx index 6da92ff..f451845 100644 --- a/src/utilities/login.tsx +++ b/src/utilities/login.tsx @@ -31,7 +31,7 @@ export function createHyperspaceApp(name: string, scopes: string, baseurl: strin export function getRedirectAddress(type: "desktop" | "dynamic" | string): string { switch(type) { case "desktop": - return "hyperspace://"; + return "hyperspace://hyperspace/app/"; case "dynamic": return `https://${window.location.host}`; default: