@@ -5,6 +5,7 @@ import { ApiClientError } from "./apiClientError.js";
55import { paths , operations } from "./openapi.js" ;
66import { CommonProperties , TelemetryEvent } from "../../telemetry/types.js" ;
77import { packageInfo } from "../../helpers/packageInfo.js" ;
8+ import logger , { LogId } from "../../logger.js" ;
89
910const ATLAS_API_VERSION = "2025-03-12" ;
1011
@@ -34,9 +35,7 @@ export class ApiClient {
3435
3536 private getAccessToken = async ( ) => {
3637 if ( this . oauth2Client && ( ! this . accessToken || this . accessToken . expired ( ) ) ) {
37- this . accessToken = await this . oauth2Client . getToken ( {
38- agent : this . options . userAgent ,
39- } ) ;
38+ this . accessToken = await this . oauth2Client . getToken ( { } ) ;
4039 }
4140 return this . accessToken ?. token . access_token as string | undefined ;
4241 } ;
@@ -49,7 +48,9 @@ export class ApiClient {
4948
5049 try {
5150 const accessToken = await this . getAccessToken ( ) ;
52- request . headers . set ( "Authorization" , `Bearer ${ accessToken } ` ) ;
51+ if ( accessToken ) {
52+ request . headers . set ( "Authorization" , `Bearer ${ accessToken } ` ) ;
53+ }
5354 return request ;
5455 } catch {
5556 // ignore not availble tokens, API will return 401
@@ -81,20 +82,38 @@ export class ApiClient {
8182 auth : {
8283 tokenHost : this . options . baseUrl ,
8384 tokenPath : "/api/oauth/token" ,
85+ revokePath : "/api/oauth/revoke" ,
86+ } ,
87+ http : {
88+ headers : {
89+ "User-Agent" : this . options . userAgent ,
90+ } ,
8491 } ,
8592 } ) ;
8693 this . client . use ( this . authMiddleware ) ;
8794 }
8895 }
8996
9097 public hasCredentials ( ) : boolean {
91- return ! ! ( this . oauth2Client && this . accessToken ) ;
98+ return ! ! this . oauth2Client ;
9299 }
93100
94101 public async validateAccessToken ( ) : Promise < void > {
95102 await this . getAccessToken ( ) ;
96103 }
97104
105+ public async close ( ) : Promise < void > {
106+ if ( this . accessToken ) {
107+ try {
108+ await this . accessToken . revoke ( "access_token" ) ;
109+ } catch ( error : unknown ) {
110+ const err = error instanceof Error ? error : new Error ( String ( error ) ) ;
111+ logger . error ( LogId . atlasApiRevokeFailure , "apiClient" , `Failed to revoke access token: ${ err . message } ` ) ;
112+ }
113+ this . accessToken = undefined ;
114+ }
115+ }
116+
98117 public async getIpInfo ( ) : Promise < {
99118 currentIpv4Address : string ;
100119 } > {
0 commit comments