File tree Expand file tree Collapse file tree 3 files changed +29
-5
lines changed Expand file tree Collapse file tree 3 files changed +29
-5
lines changed Original file line number Diff line number Diff line change 11## NEXT
22
3- - [ Fixed] Fixed an issue where command would be executed against directory default project instead of the currently selected project.
3+ - [ Fixed] Fixed an issue where commands would be executed against directory default project instead of the currently selected project.
4+ - [ Fixed] Fixed an issue where expired auth tokens would be used.
45
56## 0.10.0
67
Original file line number Diff line number Diff line change @@ -127,7 +127,9 @@ export function setAccessToken(token = ""): void {
127127 * @returns An access token
128128 */
129129export async function getAccessToken ( ) : Promise < string > {
130- if ( accessToken ) {
130+ const valid = auth . haveValidTokens ( refreshToken , [ ] ) ;
131+ const usingADC = ! auth . loggedIn ( ) ;
132+ if ( accessToken && ( valid || usingADC ) ) {
131133 return accessToken ;
132134 }
133135 const data = await auth . getAccessToken ( refreshToken , [ ] ) ;
@@ -462,6 +464,16 @@ export class Client {
462464 this . logResponse ( res , body , options ) ;
463465
464466 if ( res . status >= 400 ) {
467+ if ( res . status === 401 && this . opts . auth ) {
468+ // If we get a 401, access token is expired or otherwise invalid.
469+ // Throw it away and get a new one. We check for validity before using
470+ // tokens, so this should not happen.
471+ logger . debug (
472+ "Got a 401 Unauthenticated error for a call that required authentication. Refreshing tokens." ,
473+ ) ;
474+ setAccessToken ( ) ;
475+ setAccessToken ( await getAccessToken ( ) ) ;
476+ }
465477 if ( options . retryCodes ?. includes ( res . status ) ) {
466478 const err = responseToError ( { statusCode : res . status } , body ) || undefined ;
467479 if ( operation . retry ( err ) ) {
Original file line number Diff line number Diff line change @@ -560,7 +560,11 @@ export function findAccountByEmail(email: string): Account | undefined {
560560 return getAllAccounts ( ) . find ( ( a ) => a . user . email === email ) ;
561561}
562562
563- function haveValidTokens ( refreshToken : string , authScopes : string [ ] ) {
563+ export function loggedIn ( ) {
564+ return ! ! lastAccessToken ;
565+ }
566+
567+ export function haveValidTokens ( refreshToken : string , authScopes : string [ ] ) {
564568 if ( ! lastAccessToken ?. access_token ) {
565569 const tokens = configstore . get ( "tokens" ) ;
566570 if ( refreshToken === tokens ?. refresh_token ) {
@@ -575,8 +579,15 @@ function haveValidTokens(refreshToken: string, authScopes: string[]) {
575579 // To avoid token expiration in the middle of a long process we only hand out
576580 // tokens if they have a _long_ time before the server rejects them.
577581 const isExpired = ( lastAccessToken ?. expires_at || 0 ) < Date . now ( ) + FIFTEEN_MINUTES_IN_MS ;
578-
579- return hasTokens && hasSameScopes && ! isExpired ;
582+ const valid = hasTokens && hasSameScopes && ! isExpired ;
583+ if ( hasTokens ) {
584+ logger . debug (
585+ `Checked if tokens are valid: ${ valid } , expires at: ${ lastAccessToken ?. expires_at } ` ,
586+ ) ;
587+ } else {
588+ logger . debug ( "No OAuth tokens found" ) ;
589+ }
590+ return valid ;
580591}
581592
582593function deleteAccount ( account : Account ) {
You can’t perform that action at this time.
0 commit comments