@@ -93,6 +93,7 @@ const PHOENIX_FS_URL = `/PhoenixFS${randomNonce(8)}`;
9393const PHOENIX_STATIC_SERVER_URL = `/Static${ randomNonce ( 8 ) } ` ;
9494const PHOENIX_NODE_URL = `/PhoenixNode${ randomNonce ( 8 ) } ` ;
9595const PHOENIX_LIVE_PREVIEW_COMM_URL = `/PreviewComm${ randomNonce ( 8 ) } ` ;
96+ const PHOENIX_AUTO_AUTH_URL = `/AutoAuth${ randomNonce ( 8 ) } ` ;
9697
9798const savedConsoleLog = console . log ;
9899
@@ -190,7 +191,8 @@ function processCommand(line) {
190191 phoenixFSURL : `ws://localhost:${ port } ${ PHOENIX_FS_URL } ` ,
191192 phoenixNodeURL : `ws://localhost:${ port } ${ PHOENIX_NODE_URL } ` ,
192193 staticServerURL : `http://localhost:${ port } ${ PHOENIX_STATIC_SERVER_URL } ` ,
193- livePreviewCommURL : `ws://localhost:${ port } ${ PHOENIX_LIVE_PREVIEW_COMM_URL } `
194+ livePreviewCommURL : `ws://localhost:${ port } ${ PHOENIX_LIVE_PREVIEW_COMM_URL } ` ,
195+ autoAuthURL : `http://localhost:${ port } ${ PHOENIX_AUTO_AUTH_URL } `
194196 } , jsonCmd . commandID ) ;
195197 } ) ;
196198 return ;
@@ -207,6 +209,67 @@ rl.on('line', (line) => {
207209
208210const localhostOnly = 'localhost' ;
209211
212+ const AUTH_CONNECTOR_ID = "ph_auth" ;
213+ const EVENT_CONNECTED = "connected" ;
214+ let verificationCode = null ;
215+ async function setVerificationCode ( code ) {
216+ verificationCode = code ;
217+ }
218+ const nodeConnector = NodeConnector . createNodeConnector ( AUTH_CONNECTOR_ID , {
219+ setVerificationCode
220+ } ) ;
221+
222+ const ALLOWED_ORIGIN = 'https://account.phcode.io' ;
223+ function autoAuth ( req , res ) {
224+ const origin = req . headers . origin ;
225+ // localhost dev of loginService is not allowed here to not leak to production. So autoAuth is not available in
226+ // dev builds.
227+ const isAllowedOrigin = ! origin || ( ALLOWED_ORIGIN === origin ) ;
228+ if ( ! isAllowedOrigin ) {
229+ res . writeHead ( 403 , { 'Content-Type' : 'text/plain' } ) ;
230+ res . end ( 'Forbidden origin' ) ;
231+ return ;
232+ }
233+ if ( req . method === 'OPTIONS' ) {
234+ res . setHeader ( 'Access-Control-Allow-Origin' , origin ) ;
235+ res . setHeader ( 'Access-Control-Allow-Methods' , 'GET, OPTIONS' ) ;
236+ res . setHeader ( 'Access-Control-Allow-Headers' , 'Content-Type' ) ;
237+ res . setHeader ( 'Access-Control-Allow-Credentials' , 'true' ) ;
238+ res . setHeader ( 'Access-Control-Allow-Private-Network' , 'true' ) ;
239+ res . writeHead ( 204 ) ;
240+ res . end ( ) ;
241+ return ;
242+ }
243+ // Remove '/AutoAuth<rand>' from the beginning of the URL and construct file path
244+ const url = new URL ( req . url , `http://${ req . headers . host } ` ) ;
245+ const cleanPath = url . pathname . replace ( PHOENIX_AUTO_AUTH_URL , '' ) ;
246+ // Check if the request is for the autoVerifyCode endpoint
247+ if ( cleanPath === `/autoVerifyCode` && req . method === 'GET' ) {
248+ origin && res . setHeader ( 'Access-Control-Allow-Origin' , origin ) ;
249+ if ( ! verificationCode ) {
250+ res . setHeader ( 'Content-Type' , 'text/plain' ) ;
251+ res . writeHead ( 404 ) ;
252+ res . end ( 'Not Found' ) ;
253+ return ;
254+ }
255+ res . setHeader ( 'Access-Control-Allow-Credentials' , 'true' ) ;
256+ res . setHeader ( 'Access-Control-Allow-Private-Network' , 'true' ) ;
257+ res . setHeader ( 'Content-Type' , 'application/json' ) ;
258+ res . end ( JSON . stringify ( { code : verificationCode } ) ) ;
259+ verificationCode = null ; // verification code is only returned once
260+ } else if ( cleanPath === `/appVerified` && req . method === 'GET' ) {
261+ nodeConnector . triggerPeer ( EVENT_CONNECTED , "ok" ) ;
262+ origin && res . setHeader ( 'Access-Control-Allow-Origin' , origin ) ;
263+ res . setHeader ( 'Access-Control-Allow-Credentials' , 'true' ) ;
264+ res . setHeader ( 'Access-Control-Allow-Private-Network' , 'true' ) ;
265+ res . setHeader ( 'Content-Type' , 'application/json' ) ;
266+ res . end ( "ok" ) ;
267+ } else {
268+ res . writeHead ( 404 , { 'Content-Type' : 'text/plain' } ) ;
269+ res . end ( 'Not Found' ) ;
270+ }
271+ }
272+
210273// Create an HTTP server
211274const server = http . createServer ( ( req , res ) => {
212275 if ( req . url . startsWith ( PHOENIX_STATIC_SERVER_URL ) ) {
@@ -249,7 +312,9 @@ const server = http.createServer((req, res) => {
249312 }
250313 } ) ;
251314
252- } else {
315+ } else if ( req . url . startsWith ( PHOENIX_AUTO_AUTH_URL ) ) {
316+ return autoAuth ( req , res ) ;
317+ } else {
253318 res . writeHead ( 404 , { 'Content-Type' : 'text/plain' } ) ;
254319 res . end ( 'Not Found' ) ;
255320 }
@@ -269,5 +334,6 @@ server.listen(0, localhostOnly, () => {
269334 savedConsoleLog ( `Phoenix node tauri FS url is ws://localhost:${ port } ${ PHOENIX_FS_URL } ` ) ;
270335 savedConsoleLog ( `Phoenix node connector url is ws://localhost:${ port } ${ PHOENIX_NODE_URL } ` ) ;
271336 savedConsoleLog ( `Phoenix live preview comm url is ws://localhost:${ port } ${ PHOENIX_LIVE_PREVIEW_COMM_URL } ` ) ;
337+ savedConsoleLog ( `Phoenix AutoAuth url is ws://localhost:${ port } ${ PHOENIX_AUTO_AUTH_URL } ` ) ;
272338 serverPortResolve ( port ) ;
273339} ) ;
0 commit comments