Skip to content

Commit 3d6d55f

Browse files
Clean up api.ts
1 parent 767c6ee commit 3d6d55f

File tree

1 file changed

+22
-40
lines changed

1 file changed

+22
-40
lines changed

src/cloud/auth.ts

Lines changed: 22 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import { ApiService } from "./api"
2121
export const AUTH_PROVIDER_ID = "fastapi-vscode"
2222
const NAME = "FastAPI Cloud"
2323
const AUTH_POLL_INTERVAL_MS = 3000
24+
const SECRET_STORAGE_KEY = "fastapi-cloud-access-token"
25+
const SESSION_ID = "fastapi-cloud-session"
26+
const ACCOUNT_ID = "fastapi-cloud-account"
2427

2528
interface AuthConfig {
2629
access_token: string
@@ -81,21 +84,20 @@ export class CloudAuthenticationProvider
8184
private async checkAndFireAuthState() {
8285
const loggedIn = await this.hasValidToken()
8386
if (loggedIn !== this.lastAuthState) {
84-
// Track sign in when transitioning from logged out to logged in
8587
if (loggedIn && !this.lastAuthState) {
8688
trackCloudSignIn()
8789
}
8890
this.lastAuthState = loggedIn
8991
this._onDidChangeSessions.fire({ added: [], removed: [], changed: [] })
9092
}
9193
}
94+
9295
private getAuthUri(): Uri | null {
9396
// In browser (vscode.dev), we can't access local filesystem auth
9497
if (env.uiKind === UIKind.Web) {
9598
return null
9699
}
97100

98-
// Get home directory from environment
99101
const home = process.env.HOME || process.env.USERPROFILE
100102
if (!home) return null
101103

@@ -119,30 +121,22 @@ export class CloudAuthenticationProvider
119121
return this._onDidChangeSessions.event
120122
}
121123

122-
public async getSessions(): Promise<AuthenticationSession[]> {
124+
private async getToken(): Promise<string | undefined> {
125+
if (env.uiKind === UIKind.Web) {
126+
return this.context.secrets.get(SECRET_STORAGE_KEY)
127+
}
123128
const authUri = this.getAuthUri()
124-
log(
125-
`getSessions called (uiKind=${env.uiKind}, authUri=${authUri?.toString() ?? "null"})`,
129+
if (!authUri) return undefined
130+
const content = await workspace.fs.readFile(authUri)
131+
const authConfig: AuthConfig = JSON.parse(
132+
Buffer.from(content).toString("utf8"),
126133
)
134+
return authConfig.access_token
135+
}
127136

137+
public async getSessions(): Promise<AuthenticationSession[]> {
128138
try {
129-
let token: string | undefined
130-
131-
if (env.uiKind === UIKind.Web) {
132-
// In browser, use SecretStorage
133-
const secretStorage: SecretStorage = this.context.secrets
134-
token = await secretStorage.get("fastapi-cloud-access-token")
135-
log(
136-
`getSessions: SecretStorage token ${token ? `found (${token.length} chars)` : "not found"}`,
137-
)
138-
} else {
139-
if (!authUri) return []
140-
const content = await workspace.fs.readFile(authUri)
141-
const authConfig: AuthConfig = JSON.parse(
142-
Buffer.from(content).toString("utf8"),
143-
)
144-
token = authConfig.access_token
145-
}
139+
const token = await this.getToken()
146140

147141
if (!token || isTokenExpired(token)) {
148142
log(
@@ -153,7 +147,6 @@ export class CloudAuthenticationProvider
153147

154148
log("getSessions: returning valid session")
155149

156-
// Fetch user info for account label (cached after first successful fetch)
157150
if (!this.cachedLabel) {
158151
const info = await ApiService.getUser(token)
159152
if (info?.email) {
@@ -164,10 +157,10 @@ export class CloudAuthenticationProvider
164157

165158
return [
166159
{
167-
id: "fastapi-cloud-session",
160+
id: SESSION_ID,
168161
accessToken: token,
169162
account: {
170-
id: "fastapi-cloud-account",
163+
id: ACCOUNT_ID,
171164
label,
172165
},
173166
scopes: [],
@@ -179,19 +172,8 @@ export class CloudAuthenticationProvider
179172
}
180173

181174
private async hasValidToken(): Promise<boolean> {
182-
const authUri = this.getAuthUri()
183175
try {
184-
let token: string | undefined
185-
if (env.uiKind === UIKind.Web) {
186-
token = await this.context.secrets.get("fastapi-cloud-access-token")
187-
} else {
188-
if (!authUri) return false
189-
const content = await workspace.fs.readFile(authUri)
190-
const authConfig: AuthConfig = JSON.parse(
191-
Buffer.from(content).toString("utf8"),
192-
)
193-
token = authConfig.access_token
194-
}
176+
const token = await this.getToken()
195177
return !!token && !isTokenExpired(token)
196178
} catch {
197179
return false
@@ -202,7 +184,7 @@ export class CloudAuthenticationProvider
202184
if (env.uiKind === UIKind.Web) {
203185
// In browser, use SecretStorage
204186
const secretStorage: SecretStorage = this.context.secrets
205-
await secretStorage.store("fastapi-cloud-access-token", token)
187+
await secretStorage.store(SECRET_STORAGE_KEY, token)
206188
return
207189
}
208190

@@ -288,7 +270,7 @@ export class CloudAuthenticationProvider
288270
// In browsers envs like vscode.dev, we use SecretStorage instead of filesystem
289271
if (env.uiKind === UIKind.Web) {
290272
const secretStorage: SecretStorage = this.context.secrets
291-
await secretStorage.delete("fastapi-cloud-access-token")
273+
await secretStorage.delete(SECRET_STORAGE_KEY)
292274
// Otherwise, we need to delete the auth file from filesystem if it exists
293275
} else if (authUri) {
294276
await workspace.fs.delete(authUri)
@@ -307,7 +289,7 @@ export class CloudAuthenticationProvider
307289
}
308290

309291
async signOut(): Promise<void> {
310-
await this.removeSession("fastapi-cloud-session")
292+
await this.removeSession(SESSION_ID)
311293
this.cachedLabel = null
312294
}
313295

0 commit comments

Comments
 (0)