@@ -14,40 +14,91 @@ See the License for the specific language governing permissions and
1414limitations under the License.
1515*/
1616
17- const { app} = require ( 'electron' ) ;
17+ const { app} = require ( "electron" ) ;
18+ const path = require ( "path" ) ;
19+ const fs = require ( "fs" ) ;
20+
21+ const PROTOCOL = "riot://" ;
22+ const SEARCH_PARAM = "riot-desktop-ssoid" ;
23+ const STORE_FILE_NAME = "sso-sessions.json" ;
24+
25+ // we getPath userData before electron-main changes it, so this is the default value
26+ const storePath = path . join ( app . getPath ( "userData" ) , STORE_FILE_NAME ) ;
1827
1928const processUrl = ( url ) => {
2029 if ( ! global . mainWindow ) return ;
2130 console . log ( "Handling link: " , url ) ;
22- global . mainWindow . loadURL ( url . replace ( "riot://" , "vector://" ) ) ;
31+ global . mainWindow . loadURL ( url . replace ( PROTOCOL , "vector://" ) ) ;
2332} ;
2433
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 ] ) ;
34+ const readStore = ( ) => {
35+ try {
36+ const s = fs . readFileSync ( storePath , { encoding : "utf8" } ) ;
37+ const o = JSON . parse ( s ) ;
38+ return typeof o === "object" ? o : { } ;
39+ } catch ( e ) {
40+ return { } ;
3541 }
42+ } ;
3643
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- }
44+ const writeStore = ( data ) => {
45+ fs . writeFileSync ( storePath , JSON . stringify ( data ) ) ;
46+ } ;
47+
48+ module . exports = {
49+ recordSSOSession : ( sessionID ) => {
50+ const userDataPath = app . getPath ( 'userData' ) ;
51+ const store = readStore ( ) ;
52+ for ( const key in store ) {
53+ // ensure each instance only has one (the latest) session ID to prevent the file growing unbounded
54+ if ( store [ key ] === userDataPath ) {
55+ delete store [ key ] ;
56+ break ;
57+ }
58+ }
59+ store [ sessionID ] = userDataPath ;
60+ writeStore ( store ) ;
61+ } ,
62+ getProfileFromDeeplink : ( args ) => {
63+ // check if we are passed a profile in the SSO callback url
64+ const deeplinkUrl = args . find ( arg => arg . startsWith ( 'riot://' ) ) ;
65+ if ( deeplinkUrl && deeplinkUrl . includes ( SEARCH_PARAM ) ) {
66+ const parsedUrl = new URL ( deeplinkUrl ) ;
67+ if ( parsedUrl . protocol === 'riot:' ) {
68+ const ssoID = parsedUrl . searchParams . get ( SEARCH_PARAM ) ;
69+ const store = readStore ( ) ;
70+ console . log ( "Forwarding to profile: " , store [ ssoID ] ) ;
71+ return store [ ssoID ] ;
72+ }
73+ }
74+ } ,
75+ protocolInit : ( ) => {
76+ // get all args except `hidden` as it'd mean the app would not get focused
77+ // XXX: passing args to protocol handlers only works on Windows, so unpackaged deep-linking
78+ // --profile/--profile-dir are passed via the SEARCH_PARAM var in the callback url
79+ const args = process . argv . slice ( 1 ) . filter ( arg => arg !== "--hidden" && arg !== "-hidden" ) ;
80+ if ( app . isPackaged ) {
81+ app . setAsDefaultProtocolClient ( 'riot' , process . execPath , args ) ;
82+ } else if ( process . platform === 'win32' ) { // on Mac/Linux this would just cause the electron binary to open
83+ // special handler for running without being packaged, e.g `electron .` by passing our app path to electron
84+ app . setAsDefaultProtocolClient ( 'riot' , process . execPath , [ app . getAppPath ( ) , ...args ] ) ;
85+ }
86+
87+ if ( process . platform === 'darwin' ) {
88+ // Protocol handler for macos
89+ app . on ( 'open-url' , function ( ev , url ) {
90+ ev . preventDefault ( ) ;
91+ processUrl ( url ) ;
92+ } ) ;
93+ } else {
94+ // Protocol handler for win32/Linux
95+ app . on ( 'second-instance' , ( ev , commandLine ) => {
96+ const url = commandLine [ commandLine . length - 1 ] ;
97+ if ( ! url . startsWith ( PROTOCOL ) ) return ;
98+ processUrl ( url ) ;
99+ } ) ;
100+ }
101+ } ,
51102} ;
52103
53104
0 commit comments