Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion firebase-vscode/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## NEXT

- [Fixed] Fixed an issue where command would be executed against directory default project instead of the currently selected project.
- [Fixed] Fixed an issue where commands would be executed against directory default project instead of the currently selected project.
- [Fixed] Fixed an issue where expired auth tokens would be used.

## 0.10.0

Expand Down
14 changes: 13 additions & 1 deletion src/apiv2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ export function setAccessToken(token = ""): void {
* @returns An access token
*/
export async function getAccessToken(): Promise<string> {
if (accessToken) {
const valid = auth.haveValidTokens(refreshToken, []);
const usingADC = !auth.loggedIn();
if (accessToken && (valid || usingADC)) {
return accessToken;
}
const data = await auth.getAccessToken(refreshToken, []);
Expand Down Expand Up @@ -462,6 +464,16 @@ export class Client {
this.logResponse(res, body, options);

if (res.status >= 400) {
if (res.status === 401 && this.opts.auth) {
// If we get a 401, access token is expired or otherwise invalid.
// Throw it away and get a new one. We check for validity before using
// tokens, so this should not happen.
logger.debug(
"Got a 401 Unauthenticated error for a call that required authentication. Refreshing tokens.",
);
setAccessToken();
setAccessToken(await getAccessToken());
}
if (options.retryCodes?.includes(res.status)) {
const err = responseToError({ statusCode: res.status }, body) || undefined;
if (operation.retry(err)) {
Expand Down
17 changes: 14 additions & 3 deletions src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,11 @@ export function findAccountByEmail(email: string): Account | undefined {
return getAllAccounts().find((a) => a.user.email === email);
}

function haveValidTokens(refreshToken: string, authScopes: string[]) {
export function loggedIn() {
return !!lastAccessToken;
}

export function haveValidTokens(refreshToken: string, authScopes: string[]) {
if (!lastAccessToken?.access_token) {
const tokens = configstore.get("tokens");
if (refreshToken === tokens?.refresh_token) {
Expand All @@ -575,8 +579,15 @@ function haveValidTokens(refreshToken: string, authScopes: string[]) {
// To avoid token expiration in the middle of a long process we only hand out
// tokens if they have a _long_ time before the server rejects them.
const isExpired = (lastAccessToken?.expires_at || 0) < Date.now() + FIFTEEN_MINUTES_IN_MS;

return hasTokens && hasSameScopes && !isExpired;
const valid = hasTokens && hasSameScopes && !isExpired;
if (hasTokens) {
logger.debug(
`Checked if tokens are valid: ${valid}, expires at: ${lastAccessToken?.expires_at}`,
);
} else {
logger.debug("No OAuth tokens found");
}
return valid;
}

function deleteAccount(account: Account) {
Expand Down