Skip to content

Commit 440e4d2

Browse files
committed
chore: desktop app accounts communication via accounts proxy
1 parent 290ba72 commit 440e4d2

File tree

4 files changed

+42
-14
lines changed

4 files changed

+42
-14
lines changed

serve-proxy.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ const path = require('path');
77
const fs = require('fs');
88
const httpProxy = require('http-proxy');
99

10+
const ACCOUNT_PROD = 'https://account.phcode.dev';
11+
const ACCOUNT_STAGING = 'https://account-stage.phcode.dev';
12+
const ACCOUNT_DEV = 'http://localhost:5000';
13+
1014
// Account server configuration - switch between local and production
11-
let accountServer = 'https://account.phcode.dev'; // Production
15+
let accountServer = ACCOUNT_PROD; // Production
1216
// Set to local development server if --localAccount flag is provided
1317

1418
// Default configuration
@@ -24,6 +28,8 @@ let config = {
2428
// Parse command line arguments
2529
function parseArgs() {
2630
const args = process.argv.slice(2);
31+
let hasLocalAccount = false;
32+
let hasStagingAccount = false;
2733

2834
for (let i = 0; i < args.length; i++) {
2935
const arg = args[i];
@@ -46,11 +52,21 @@ function parseArgs() {
4652
} else if (arg === '--log-ip') {
4753
config.logIp = true;
4854
} else if (arg === '--localAccount') {
49-
accountServer = 'http://localhost:5000';
55+
hasLocalAccount = true;
56+
accountServer = ACCOUNT_DEV;
57+
} else if (arg === '--stagingAccount') {
58+
hasStagingAccount = true;
59+
accountServer = ACCOUNT_STAGING;
5060
} else if (!arg.startsWith('-')) {
5161
config.root = path.resolve(arg);
5262
}
5363
}
64+
65+
// Check for mutually exclusive flags
66+
if (hasLocalAccount && hasStagingAccount) {
67+
console.error('Error: --localAccount and --stagingAccount cannot be used together');
68+
process.exit(1);
69+
}
5470
}
5571

5672
// Create proxy server
@@ -81,15 +97,15 @@ proxy.on('proxyReq', (proxyReq, req) => {
8197

8298
// Transform referer from localhost:8000 to phcode.dev
8399
if (originalReferer && originalReferer.includes('localhost:8000')) {
84-
const newReferer = originalReferer.replace(/localhost:8000/g, 'phcode.dev');
100+
const newReferer = originalReferer.replace(/http:\/\/localhost:8000/g, 'https://phcode.dev');
85101
proxyReq.setHeader('Referer', newReferer);
86102
} else if (!originalReferer) {
87103
proxyReq.setHeader('Referer', 'https://phcode.dev/');
88104
}
89105

90106
// Transform origin from localhost:8000 to phcode.dev
91107
if (originalOrigin && originalOrigin.includes('localhost:8000')) {
92-
const newOrigin = originalOrigin.replace(/localhost:8000/g, 'phcode.dev');
108+
const newOrigin = originalOrigin.replace(/http:\/\/localhost:8000/g, 'https://phcode.dev');
93109
proxyReq.setHeader('Origin', newOrigin);
94110
}
95111

src/services/login-desktop.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,19 @@ define(function (require, exports, module) {
8080
* For desktop apps, this directly uses the configured account URL
8181
*/
8282
function getAccountBaseURL() {
83+
if (location.hostname === 'localhost' || location.hostname === '127.0.0.1') {
84+
return '/proxy/accounts';
85+
}
8386
return Phoenix.config.account_url.replace(/\/$/, ''); // Remove trailing slash
8487
}
8588

89+
/**
90+
* Get the account website URL for opening browser tabs
91+
*/
92+
function _getAccountWebURL() {
93+
return Phoenix.config.account_url;
94+
}
95+
8696
const ERR_RETRY_LATER = "retry_later";
8797
const ERR_INVALID = "invalid";
8898

@@ -96,7 +106,7 @@ define(function (require, exports, module) {
96106
* never rejects.
97107
*/
98108
async function _resolveAPIKey(apiKey, validationCode) {
99-
const resolveURL = `${Phoenix.config.account_url}resolveAppSessionID?appSessionID=${apiKey}&validationCode=${validationCode}`;
109+
const resolveURL = `${getAccountBaseURL()}/resolveAppSessionID?appSessionID=${apiKey}&validationCode=${validationCode}`;
100110
if (!navigator.onLine) {
101111
return {err: ERR_RETRY_LATER};
102112
}
@@ -196,7 +206,7 @@ define(function (require, exports, module) {
196206
const authPortURL = _getAutoAuthPortURL();
197207
const platformStr = PLATFORM_STRINGS[Phoenix.platform] || Phoenix.platform;
198208
const appName = encodeURIComponent(`${Strings.APP_NAME} Desktop on ${platformStr}`);
199-
const resolveURL = `${Phoenix.config.account_url}getAppAuthSession?autoAuthPort=${authPortURL}&appName=${appName}`;
209+
const resolveURL = `${getAccountBaseURL()}/getAppAuthSession?autoAuthPort=${authPortURL}&appName=${appName}`;
200210
// {"isSuccess":true,"appSessionID":"a uuid...","validationCode":"SWXP07"}
201211
try {
202212
if(Phoenix.isTestWindow && fetchFn === fetch){
@@ -254,7 +264,7 @@ define(function (require, exports, module) {
254264
}
255265
const {appSessionID, validationCode} = appAuthSession;
256266
await setAutoVerificationCode(validationCode);
257-
const appSignInURL = `${Phoenix.config.account_url}authorizeApp?appSessionID=${appSessionID}`;
267+
const appSignInURL = `${_getAccountWebURL()}authorizeApp?appSessionID=${appSessionID}`;
258268

259269
// Show dialog with validation code
260270
const dialogData = {
@@ -350,7 +360,7 @@ define(function (require, exports, module) {
350360
}
351361

352362
async function signOutAccount() {
353-
const resolveURL = `${Phoenix.config.account_url}logoutSession`;
363+
const resolveURL = `${getAccountBaseURL()}/logoutSession`;
354364
try {
355365
let input = {
356366
appSessionID: userProfile.apiKey
@@ -378,7 +388,7 @@ define(function (require, exports, module) {
378388
Strings.SIGNED_OUT_FAILED_MESSAGE
379389
);
380390
dialog.done(() => {
381-
NativeApp.openURLInDefaultBrowser(Phoenix.config.account_url + "#advanced");
391+
NativeApp.openURLInDefaultBrowser(_getAccountWebURL() + "#advanced");
382392
});
383393
Metrics.countEvent(Metrics.EVENT_TYPE.AUTH, 'logoutFail', Phoenix.platform);
384394
return;
@@ -399,7 +409,7 @@ define(function (require, exports, module) {
399409
Strings.SIGNED_OUT_FAILED_MESSAGE
400410
);
401411
dialog.done(() => {
402-
NativeApp.openURLInDefaultBrowser(Phoenix.config.account_url + "#advanced");
412+
NativeApp.openURLInDefaultBrowser(_getAccountWebURL() + "#advanced");
403413
});
404414
Metrics.countEvent(Metrics.EVENT_TYPE.AUTH, 'getAppAuth', Phoenix.platform);
405415
logger.reportError(error, "Failed to call logout calling" + resolveURL);

src/services/manage-licenses.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ define(function (require, exports, module) {
4444
* Get the API base URL for license operations
4545
*/
4646
function _getAPIBaseURL() {
47+
if (location.hostname === 'localhost' || location.hostname === '127.0.0.1') {
48+
return '/proxy/accounts';
49+
}
4750
return Phoenix.config.account_url.replace(/\/$/, ''); // Remove trailing slash
4851
}
4952

src/services/readme-login-desktop-no_dist.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,8 @@ pref.on('change', _verifyLogin);
241241
242242
For testing desktop authentication with a local account server:
243243
244-
1. **Configure Account URL:**
245-
- Edit `src/config.json`
246-
- Change `account_url` from `https://account.phcode.dev/` to `http://localhost:5000/` (or your local server URL)
244+
1. **Configure Proxy Server:**
245+
- use `npm run serveLocalAccount` to serve phoenix repo server, instead of using npm run serve command.
247246
248247
2. **Rebuild Application:**
249248
```bash
@@ -326,4 +325,4 @@ if(resolveResponse.userDetails) {
326325

327326
For desktop implementation details, see the source code in `src/services/login-desktop.js`. For browser authentication, see `src/services/login-browser.js` and `readme-login-browser-no_dist.md`.
328327

329-
For deeper understanding of the Kernel Mode Trust security architecture and secure credential storage implementation, refer to the Kernel Mode Trust source files (out of scope for this document).
328+
For deeper understanding of the Kernel Mode Trust security architecture and secure credential storage implementation, refer to the Kernel Mode Trust source files (out of scope for this document).

0 commit comments

Comments
 (0)