Skip to content

Commit 633f4ae

Browse files
committed
Respect auth token from Node.js start
1 parent 14ecbb6 commit 633f4ae

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

src/ElectronNET.API/ElectronNetRuntime.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public static class ElectronNetRuntime
1717
internal const int DefaultWebPort = 8001;
1818
internal const string ElectronPortArgumentName = "electronPort";
1919
internal const string ElectronPidArgumentName = "electronPID";
20+
internal const string ElectronAuthTokenArgumentName = "electronAuthToken";
2021

2122
/// <summary>Initializes the <see cref="ElectronNetRuntime"/> class.</summary>
2223
static ElectronNetRuntime()

src/ElectronNET.API/Runtime/StartupManager.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ private void CollectProcessData()
7575
if (portArg != null)
7676
{
7777
var parts = portArg.Split('=', StringSplitOptions.TrimEntries);
78+
7879
if (parts.Length > 1 && int.TryParse(parts[1], NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out var result))
7980
{
8081
ElectronNetRuntime.ElectronSocketPort = result;
81-
8282
Console.WriteLine("Use Electron Port: " + result);
8383
}
8484
}
@@ -88,18 +88,33 @@ private void CollectProcessData()
8888
if (pidArg != null)
8989
{
9090
var parts = pidArg.Split('=', StringSplitOptions.TrimEntries);
91+
9192
if (parts.Length > 1 && int.TryParse(parts[1], NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out var result))
9293
{
9394
ElectronNetRuntime.ElectronProcessId = result;
94-
9595
Console.WriteLine("Electron Process ID: " + result);
9696
}
9797
}
98+
99+
var authTokenArg = argsList.FirstOrDefault(e => e.Contains(ElectronNetRuntime.ElectronAuthTokenArgumentName, StringComparison.OrdinalIgnoreCase));
100+
101+
if (authTokenArg != null)
102+
{
103+
var parts = authTokenArg.Split('=', StringSplitOptions.TrimEntries);
104+
105+
if (parts.Length > 1 && !string.IsNullOrWhiteSpace(parts[1]))
106+
{
107+
var result = parts[1];
108+
ElectronNetRuntime.ElectronAuthToken = result;
109+
Console.WriteLine("Use Auth Token: " + result);
110+
}
111+
}
98112
}
99113

100114
private void SetElectronExecutable()
101115
{
102-
string executable = ElectronNetRuntime.BuildInfo.ElectronExecutable;
116+
var executable = ElectronNetRuntime.BuildInfo.ElectronExecutable;
117+
103118
if (string.IsNullOrEmpty(executable))
104119
{
105120
throw new Exception("AssemblyMetadataAttribute 'ElectronExecutable' could not be found!");

src/ElectronNET.Host/main.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const { BrowserWindow } = require('electron');
33
const { createServer } = require('http');
44
const { randomUUID } = require('crypto');
55
const { Server } = require('socket.io');
6+
const { platform } = require('os');
67
const path = require('path');
78
const fs = require('fs');
89
const cProcess = require('child_process').spawn;
@@ -395,13 +396,13 @@ function startAspCoreBackend(electronPort) {
395396
envParam,
396397
`/electronPort=${electronPort}`,
397398
`/electronPID=${process.pid}`,
399+
`/electronAuthToken=${authToken}`,
398400
// forward user supplied args (avoid duplicate environment)
399401
...forwardedArgs.filter(a => !(envParam && a.startsWith('--environment=')))
400402
].filter(p => p);
401403
let binaryFile = manifestJsonFile.executable;
402404

403-
const os = require('os');
404-
if (os.platform() === 'win32') {
405+
if (platform() === 'win32') {
405406
binaryFile = binaryFile + '.exe';
406407
}
407408

@@ -430,8 +431,7 @@ function startAspCoreBackendUnpackaged(electronPort) {
430431
].filter(p => p);
431432
let binaryFile = manifestJsonFile.executable;
432433

433-
const os = require('os');
434-
if (os.platform() === 'win32') {
434+
if (platform() === 'win32') {
435435
binaryFile = binaryFile + '.exe';
436436
}
437437

0 commit comments

Comments
 (0)