Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

CLI: Improve auth login typing + browser sign in response usage (#733)

Ben Fornefeld10db4e95e6de
diff --git a/packages/cli/package.json b/packages/cli/package.json
index 56e650dc8..ddb7dd6f5 100644
--- a/packages/cli/package.json
+++ b/packages/cli/package.json
@@ -38,6 +38,7 @@
"prepublishOnly": "pnpm build",
"build": "tsc --noEmit --skipLibCheck && tsup --minify",
"dev": "tsup --watch",
+ "lint": "eslint src",
"test": "pnpm build && cd testground/demo-basic && ../../dist/index.js template build",
"check-deps": "knip",
"update-deps": "ncu -u && pnpm i",
diff --git a/packages/cli/src/commands/auth/login.ts b/packages/cli/src/commands/auth/login.ts
index 6bdbc16f4..32ed48efe 100644
--- a/packages/cli/src/commands/auth/login.ts
+++ b/packages/cli/src/commands/auth/login.ts
@@ -20,7 +20,8 @@ import { handleE2BRequestError } from '../../utils/errors'
export const loginCommand = new commander.Command('login')
.description('log in to CLI')
.action(async () => {
- let userConfig
+ let userConfig: UserConfig | null = null
+
try {
userConfig = getUserConfig()
} catch (err) {
@@ -35,16 +36,18 @@ export const loginCommand = new commander.Command('login')
return
} else if (!userConfig) {
console.log('Attempting to log in...')
- userConfig = await signInWithBrowser()
- if (!userConfig) {
+ const signInResponse = await signInWithBrowser()
+ if (!signInResponse) {
console.info('Login aborted')
return
}
+ const accessToken =
+ process.env.E2B_ACCESS_TOKEN || signInResponse.accessToken
+
const signal = connectionConfig.getSignal()
const config = new e2b.ConnectionConfig({
- accessToken: process.env.E2B_ACCESS_TOKEN || userConfig?.accessToken,
- apiKey: process.env.E2B_API_KEY || userConfig?.teamApiKey,
+ accessToken,
})
const client = new e2b.ApiClient(config)
const res = await client.api.GET('/teams', { signal })
@@ -61,9 +64,14 @@ export const loginCommand = new commander.Command('login')
process.exit(1)
}
- userConfig.teamName = defaultTeam.name
- userConfig.teamId = defaultTeam.teamID
- userConfig.teamApiKey = defaultTeam.apiKey
+ userConfig = {
+ email: signInResponse.email,
+ accessToken,
+ teamName: defaultTeam.name,
+ teamId: defaultTeam.teamID,
+ teamApiKey: defaultTeam.apiKey,
+ }
+
fs.mkdirSync(path.dirname(USER_CONFIG_PATH), { recursive: true })
fs.writeFileSync(USER_CONFIG_PATH, JSON.stringify(userConfig, null, 2))
}
@@ -76,7 +84,13 @@ export const loginCommand = new commander.Command('login')
process.exit(0)
})
-async function signInWithBrowser(): Promise<UserConfig> {
+interface SignInWithBrowserResponse {
+ email: string
+ accessToken: string
+ defaultTeamId: string
+}
+
+async function signInWithBrowser(): Promise<SignInWithBrowserResponse> {
const server = http.createServer()
const { port } = await listen.default(server, 0, '127.0.0.1')
const loginUrl = new URL(`${DOCS_BASE}/api/cli`)
@@ -92,7 +106,7 @@ async function signInWithBrowser(): Promise<UserConfig> {
.searchParams
const searchParamsObj = Object.fromEntries(
searchParams.entries()
- ) as unknown as UserConfig & {
+ ) as unknown as SignInWithBrowserResponse & {
error?: string
}
const { error } = searchParamsObj
diff --git a/packages/cli/src/user.ts b/packages/cli/src/user.ts
index 808933c9a..fc3217fe0 100644
--- a/packages/cli/src/user.ts
+++ b/packages/cli/src/user.ts
@@ -2,19 +2,32 @@ import * as os from 'os'
import * as path from 'path'
import * as fs from 'fs'
+/**
+ * User configuration stored in ~/.e2b/config.json
+ */
export interface UserConfig {
email: string
accessToken: string
- defaultTeamApiKey?: string
- defaultTeamId?: string
teamName: string
teamId: string
teamApiKey: string
dockerProxySet?: boolean
+
+ /**
+ * @deprecated Kept for backward compatibility. Use {@link UserConfig.teamApiKey} instead.
+ */
+ defaultTeamApiKey?: string
+
+ /**
+ * @deprecated Kept for backward compatibility. Use {@link UserConfig.teamId} instead.
+ */
+ defaultTeamId?: string
}
export const USER_CONFIG_PATH = path.join(os.homedir(), '.e2b', 'config.json') // TODO: Keep in Keychain
-export const DOCS_BASE = process.env.E2B_DOCS_BASE || `https://${process.env.E2B_DOMAIN || 'e2b.dev'}/docs`
+export const DOCS_BASE =
+ process.env.E2B_DOCS_BASE ||
+ `https://${process.env.E2B_DOMAIN || 'e2b.dev'}/docs`
export function getUserConfig(): UserConfig | null {
if (!fs.existsSync(USER_CONFIG_PATH)) return null
diff --git a/packages/js-sdk/src/api/index.ts b/packages/js-sdk/src/api/index.ts
index 4d765919c..4452bd831 100644
--- a/packages/js-sdk/src/api/index.ts
+++ b/packages/js-sdk/src/api/index.ts
@@ -34,6 +34,7 @@ class ApiClient {
requireApiKey?: boolean
} = { requireAccessToken: false, requireApiKey: true }
) {
+ // FIXME: This statement makes no sense
if (!opts?.requireApiKey && !config.apiKey) {
throw new AuthenticationError(
'API key is required, please visit the Team tab at https://e2b.dev/dashboard to get your API key. ' +