Skip to content

Commit 48c8f16

Browse files
authored
Merge pull request element-hq#12590 from vector-im/t3chguy/sso
riot-desktop open SSO in browser so user doesn't have to auth twice
2 parents 94ceb5a + bb6afd7 commit 48c8f16

6 files changed

Lines changed: 87 additions & 3 deletions

File tree

‎electron_app/src/electron-main.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const tray = require('./tray');
3535
const vectorMenu = require('./vectormenu');
3636
const webContentsHandler = require('./webcontents-handler');
3737
const updater = require('./updater');
38+
const protocolInit = require('./protocol');
3839

3940
const windowStateKeeper = require('electron-window-state');
4041
const Store = require('electron-store');
@@ -427,6 +428,9 @@ if (!gotLock) {
427428
app.exit();
428429
}
429430

431+
// do this after we know we are the primary instance of the app
432+
protocolInit();
433+
430434
const launcher = new AutoLaunch({
431435
name: vectorConfig.brand || 'Riot',
432436
isHidden: true,

‎electron_app/src/protocol.js‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
Copyright 2020 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
const {app} = require('electron');
18+
19+
const processUrl = (url) => {
20+
if (!global.mainWindow) return;
21+
console.log("Handling link: ", url);
22+
global.mainWindow.loadURL(url.replace("riot://", "vector://"));
23+
};
24+
25+
module.exports = () => {
26+
// get all args except `hidden` as it'd mean the app would not get focused
27+
// XXX: passing args to protocol handlers only works on Windows,
28+
// so unpackaged deep-linking and --profile passing won't work on Mac/Linux
29+
const args = process.argv.slice(1).filter(arg => arg !== "--hidden" && arg !== "-hidden");
30+
if (app.isPackaged) {
31+
app.setAsDefaultProtocolClient('riot', process.execPath, args);
32+
} else if (process.platform === 'win32') { // on Mac/Linux this would just cause the electron binary to open
33+
// special handler for running without being packaged, e.g `electron .` by passing our app path to electron
34+
app.setAsDefaultProtocolClient('riot', process.execPath, [app.getAppPath(), ...args]);
35+
}
36+
37+
if (process.platform === 'darwin') {
38+
// Protocol handler for macos
39+
app.on('open-url', function(ev, url) {
40+
ev.preventDefault();
41+
processUrl(url);
42+
});
43+
} else {
44+
// Protocol handler for win32/Linux
45+
app.on('second-instance', (ev, commandLine) => {
46+
const url = commandLine[commandLine.length - 1];
47+
if (!url.startsWith("riot://")) return;
48+
processUrl(url);
49+
});
50+
}
51+
};
52+
53+

‎electron_app/src/webcontents-handler.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,10 @@ function onEditableContextMenu(ev, params) {
174174

175175
module.exports = (webContents) => {
176176
webContents.on('new-window', onWindowOrNavigate);
177-
// XXX: https://github.com/vector-im/riot-web/issues/8247
178-
// webContents.on('will-navigate', onWindowOrNavigate);
177+
webContents.on('will-navigate', (ev, target) => {
178+
if (target.startsWith("vector://")) return;
179+
return onWindowOrNavigate(ev, target);
180+
});
179181

180182
webContents.on('context-menu', function(ev, params) {
181183
if (params.linkURL || params.srcURL) {

‎package.json‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,11 @@
188188
"output": "electron_app/dist",
189189
"app": "electron_app"
190190
},
191-
"afterSign": "scripts/electron_afterSign.js"
191+
"afterSign": "scripts/electron_afterSign.js",
192+
"protocols": [{
193+
"name": "riot",
194+
"schemes": ["riot"]
195+
}]
192196
},
193197
"jest": {
194198
"modulePathIgnorePatterns": [

‎src/i18n/strings/en_EN.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"Invalid configuration: can only specify one of default_server_config, default_server_name, or default_hs_url.": "Invalid configuration: can only specify one of default_server_config, default_server_name, or default_hs_url.",
88
"Invalid configuration: no default server specified.": "Invalid configuration: no default server specified.",
99
"Riot Desktop on %(platformName)s": "Riot Desktop on %(platformName)s",
10+
"Go to your browser to complete Sign In": "Go to your browser to complete Sign In",
1011
"Unknown device": "Unknown device",
1112
"%(appName)s via %(browserName)s on %(osName)s": "%(appName)s via %(browserName)s on %(osName)s",
1213
"You need to be using HTTPS to place a screen-sharing call.": "You need to be using HTTPS to place a screen-sharing call.",

‎src/vector/platform/ElectronPlatform.js‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Copyright 2016 Aviral Dasgupta
55
Copyright 2016 OpenMarket Ltd
66
Copyright 2018 New Vector Ltd
77
Copyright 2019 Michael Telatynski <7t3chguy@gmail.com>
8+
Copyright 2020 The Matrix.org Foundation C.I.C.
89
910
Licensed under the Apache License, Version 2.0 (the "License");
1011
you may not use this file except in compliance with the License.
@@ -24,6 +25,11 @@ import BaseEventIndexManager from 'matrix-react-sdk/src/indexing/BaseEventIndexM
2425
import dis from 'matrix-react-sdk/src/dispatcher';
2526
import { _t } from 'matrix-react-sdk/src/languageHandler';
2627
import * as rageshake from 'matrix-react-sdk/src/rageshake/rageshake';
28+
import {MatrixClient} from "matrix-js-sdk";
29+
import Modal from "matrix-react-sdk/src/Modal";
30+
import InfoDialog from "matrix-react-sdk/src/components/views/dialogs/InfoDialog";
31+
import Spinner from "matrix-react-sdk/src/components/views/elements/Spinner";
32+
import React from "react";
2733

2834
const ipcRenderer = window.ipcRenderer;
2935

@@ -392,4 +398,18 @@ export default class ElectronPlatform extends VectorBasePlatform {
392398
console.error(error);
393399
});
394400
}
401+
402+
getSSOCallbackUrl(hsUrl: string, isUrl: string): URL {
403+
const url = super.getSSOCallbackUrl(hsUrl, isUrl);
404+
url.protocol = "riot";
405+
return url;
406+
}
407+
408+
startSingleSignOn(mxClient: MatrixClient, loginType: "sso" | "cas") {
409+
super.startSingleSignOn(mxClient, loginType); // this will get intercepted by electron-main will-navigate
410+
Modal.createTrackedDialog('Electron', 'SSO', InfoDialog, {
411+
title: _t("Go to your browser to complete Sign In"),
412+
description: <Spinner />,
413+
});
414+
}
395415
}

0 commit comments

Comments
 (0)