UNPKG

eas-cli

Version:
99 lines (97 loc) 3.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getSessionUsingBrowserAuthFlowAsync = void 0; const tslib_1 = require("tslib"); const assert_1 = tslib_1.__importDefault(require("assert")); const better_opn_1 = tslib_1.__importDefault(require("better-opn")); const http_1 = tslib_1.__importDefault(require("http")); const querystring_1 = tslib_1.__importDefault(require("querystring")); const log_1 = tslib_1.__importDefault(require("../log")); const successBody = ` <!DOCTYPE html> <html lang="en"> <head> <title>Expo SSO Login</title> <meta charset="utf-8"> <style type="text/css"> html { margin: 0; padding: 0 } body { background-color: #fff; font-family: Tahoma,Verdana; font-size: 16px; color: #000; max-width: 100%; box-sizing: border-box; padding: .5rem; margin: 1em; overflow-wrap: break-word } </style> </head> <body> SSO login complete. You may now close this tab and return to the command prompt. </body> </html>`; async function getSessionUsingBrowserAuthFlowAsync({ expoWebsiteUrl, }) { const scheme = 'http'; const hostname = 'localhost'; const path = '/auth/callback'; const buildExpoSsoLoginUrl = (port) => { const data = { app_redirect_uri: `${scheme}://${hostname}:${port}${path}`, }; const params = querystring_1.default.stringify(data); return `${expoWebsiteUrl}/sso-login?${params}`; }; // Start server and begin auth flow const executeAuthFlow = () => { return new Promise(async (resolve, reject) => { const connections = new Set(); const server = http_1.default.createServer((request, response) => { try { if (!(request.method === 'GET' && request.url?.includes('/auth/callback'))) { throw new Error('Unexpected SSO login response.'); } const url = new URL(request.url, `http:${request.headers.host}`); const sessionSecret = url.searchParams.get('session_secret'); if (!sessionSecret) { throw new Error('Request missing session_secret search parameter.'); } resolve(sessionSecret); response.writeHead(200, { 'Content-Type': 'text/html' }); response.write(successBody); response.end(); } catch (error) { reject(error); } finally { server.close(); // Ensure that the server shuts down for (const connection of connections) { connection.destroy(); } } }); server.listen(0, hostname, () => { log_1.default.log('Waiting for browser login...'); const address = server.address(); (0, assert_1.default)(address !== null && typeof address === 'object', 'Server address and port should be set after listening has begun'); const port = address.port; const authorizeUrl = buildExpoSsoLoginUrl(port); void (0, better_opn_1.default)(authorizeUrl); }); server.on('connection', connection => { connections.add(connection); connection.on('close', () => { connections.delete(connection); }); }); }); }; return await executeAuthFlow(); } exports.getSessionUsingBrowserAuthFlowAsync = getSessionUsingBrowserAuthFlowAsync;