Skip to content

Commit

Permalink
fix for new rust+ auth flow
Browse files Browse the repository at this point in the history
  • Loading branch information
liamcottle committed Apr 9, 2021
1 parent 522bf85 commit db9d81b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 19 deletions.
35 changes: 16 additions & 19 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const ElectronStore = require('electron-store');
const ExpoPushTokenManager = require('@/js/ipc/main/ExpoPushTokenManager');
const FCMNotificationManager = require('@/js/ipc/main/FCMNotificationManager');
const RustCompanionManager = require('@/js/ipc/main/RustCompanionManager');
const querystring = require('querystring');

// Scheme must be registered before the app is ready
protocol.registerSchemesAsPrivileged([{
Expand All @@ -34,32 +33,30 @@ ipcMain.on('connect-with-rustplus', (ipcEvent, data) => {
width: 800,
height: 600,
frame: true,
autoHideMenuBar: true
autoHideMenuBar: true,
webPreferences: {
enableRemoteModule: true, // get version in about modal
contextIsolation: false, // required for preload to work in browser
preload: __dirname + '/preload.js'
},
});

authWindow.webContents.on('will-redirect', (event, urlString) => {
if(urlString.startsWith('http://localhost/rustplus-login-callback')){

// prevent redirect
event.preventDefault();

// get query parameters
let queryParameters = querystring.parse(new URL(urlString).searchParams.toString());
// listen for ipc callback from ReactNativeWebView.postMessage injected into the rust+ login website
ipcMain.on('connect-with-rustplus.react-native-callback', (_, data) => {

// send steamId and token back to renderer process
ipcEvent.sender.send('connect-with-rustplus.success', {
'steamId': queryParameters.steamId,
'token': queryParameters.token,
});
// forward auth data to original ipc caller of 'connect-with-rustplus'
ipcEvent.sender.send('connect-with-rustplus.success', {
'steamId': data.steamId,
'token': data.token,
});

// close auth window
authWindow.destroy();
// close auth window
authWindow.destroy();

}
});

// load rust+ companion login page
authWindow.loadURL("https://companion-rust.facepunch.com/login?returnUrl=" + encodeURIComponent(`http://localhost/rustplus-login-callback`));
authWindow.loadURL("https://companion-rust.facepunch.com/login");

});

Expand Down
24 changes: 24 additions & 0 deletions src/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,28 @@ window.DataStore = {
Config: require('@/js/datastore/ConfigDataStore'),
FCM: require('@/js/datastore/FCMDataStore'),
Servers: require('@/js/datastore/ServerDataStore'),
};

/**
* Rust+ recently changed the login flow, which no longer sends auth data in the URL callback.
*
* Auth data is now sent to a ReactNativeWebView.postMessage handler on the main window object,
* which is available to the Rust+ app since it is a ReactNative app.
*
* We can emulate this functionality by modifying the window object ourselves. Which allows allows us to forward
* the auth data to background.js via our ipc calls.
*/
window.ReactNativeWebView = {
postMessage: function(message) {

// parse auth data
var auth = JSON.parse(message);

// send auth data back to background.js
window.ipcRenderer.send('connect-with-rustplus.react-native-callback', {
'steamId': auth.SteamId,
'token': auth.Token,
});

},
};

0 comments on commit db9d81b

Please sign in to comment.