Skip to content

Commit

Permalink
[eas-cli] Use node server default port selection for SSO login server (
Browse files Browse the repository at this point in the history
  • Loading branch information
wschurman authored Jul 13, 2023
1 parent 3c3d315 commit 5232641
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 55 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This is the log of notable changes to EAS CLI and related packages.

- Add styling to SSO auth redirect completion page. ([#1929](https://github.com/expo/eas-cli/pull/1929) by [@wschurman](https://github.com/wschurman))
- Ignore entitlements from native template when `/ios` is gitignored. ([#1906](https://github.com/expo/eas-cli/pull/1906) by [@byCedric](https://github.com/byCedric))
- Use node server default port selection for SSO login server. ([#1930](https://github.com/expo/eas-cli/pull/1930) by [@wschurman](https://github.com/wschurman))

### 🐛 Bug fixes

Expand Down
1 change: 0 additions & 1 deletion packages/eas-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
"fast-glob": "3.2.12",
"figures": "3.2.0",
"form-data": "4.0.0",
"freeport-async": "2.0.0",
"fs-extra": "10.1.0",
"getenv": "1.0.0",
"gradle-to-js": "2.0.1",
Expand Down
12 changes: 0 additions & 12 deletions packages/eas-cli/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { JSONValue } from '@expo/json-file';

import { ApiV2Error } from './ApiV2Error';
import fetch, { RequestError, RequestInit } from './fetch';
import { getFreePortAsync } from './utils/port';

interface RequestOptions {
body: JSONValue;
Expand Down Expand Up @@ -105,14 +104,3 @@ export function getEASUpdateURL(projectId: string): string {
return new URL(projectId, `https://u.expo.dev`).href;
}
}

export async function getSsoLocalServerPortAsync(): Promise<number> {
let startPort: number;
if (process.env.SSO_LOCAL_SERVER_PORT) {
startPort = Number(process.env.SSO_LOCAL_SERVER_PORT);
} else {
startPort = 19300;
}
const port = await getFreePortAsync(startPort);
return port;
}
29 changes: 16 additions & 13 deletions packages/eas-cli/src/user/expoSsoLauncher.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import assert from 'assert';
import openBrowserAsync from 'better-opn';
import http from 'http';
import { Socket } from 'node:net';
Expand Down Expand Up @@ -35,22 +36,18 @@ const successBody = `
</body>
</html>`;

export async function getSessionUsingBrowserAuthFlowAsync(options: {
export async function getSessionUsingBrowserAuthFlowAsync({
expoWebsiteUrl,
}: {
expoWebsiteUrl: string;
serverPort: number;
}): Promise<string> {
const { expoWebsiteUrl, serverPort } = options;
if (!expoWebsiteUrl || !serverPort) {
throw new Error('Expo website URL and local server port are required.');
}
const scheme = 'http';
const hostname = 'localhost';
const path = '/auth/callback';
const redirectUri = `${scheme}://${hostname}:${serverPort}${path}`;

const buildExpoSsoLoginUrl = (): string => {
const buildExpoSsoLoginUrl = (port: number): string => {
const data = {
app_redirect_uri: redirectUri,
app_redirect_uri: `${scheme}://${hostname}:${port}${path}`,
};
const params = querystring.stringify(data);
return `${expoWebsiteUrl}/sso-login?${params}`;
Expand Down Expand Up @@ -89,8 +86,17 @@ export async function getSessionUsingBrowserAuthFlowAsync(options: {
}
);

server.listen(serverPort, hostname, () => {
server.listen(0, hostname, () => {
Log.log('Waiting for browser login...');

const address = server.address();
assert(
address !== null && typeof address === 'object',
'Server address and port should be set after listening has begun'
);
const port = address.port;
const authorizeUrl = buildExpoSsoLoginUrl(port);
openBrowserAsync(authorizeUrl);
});

server.on('connection', connection => {
Expand All @@ -100,9 +106,6 @@ export async function getSessionUsingBrowserAuthFlowAsync(options: {
connections.delete(connection);
});
});

const authorizeUrl = buildExpoSsoLoginUrl();
openBrowserAsync(authorizeUrl);
});
};

Expand Down
8 changes: 3 additions & 5 deletions packages/eas-cli/src/user/fetchSessionSecretAndSsoUser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getExpoWebsiteBaseUrl, getSsoLocalServerPortAsync } from '../api';
import { getExpoWebsiteBaseUrl } from '../api';
import { getSessionUsingBrowserAuthFlowAsync } from './expoSsoLauncher';
import { fetchUserAsync } from './fetchUser';

Expand All @@ -7,11 +7,9 @@ export async function fetchSessionSecretAndSsoUserAsync(): Promise<{
id: string;
username: string;
}> {
const config = {
const sessionSecret = await getSessionUsingBrowserAuthFlowAsync({
expoWebsiteUrl: getExpoWebsiteBaseUrl(),
serverPort: await getSsoLocalServerPortAsync(),
};
const sessionSecret = await getSessionUsingBrowserAuthFlowAsync(config);
});

const userData = await fetchUserAsync({ sessionSecret });

Expand Down
11 changes: 0 additions & 11 deletions packages/eas-cli/src/utils/port.ts

This file was deleted.

8 changes: 0 additions & 8 deletions ts-declarations/freeport-async/index.d.ts

This file was deleted.

5 changes: 0 additions & 5 deletions yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5232641

Please sign in to comment.