1
1
import { WebApi } from 'azure-devops-node-api' ;
2
2
import axios from 'axios' ;
3
+ import { DefaultAzureCredential , AzureCliCredential } from '@azure/identity' ;
3
4
import {
4
5
AzureDevOpsError ,
5
6
AzureDevOpsResourceNotFoundError ,
@@ -38,7 +39,7 @@ export async function searchWorkItems(
38
39
} ;
39
40
40
41
// Get the authorization header from the connection
41
- const authHeader = await getAuthorizationHeader ( connection ) ;
42
+ const authHeader = await getAuthorizationHeader ( ) ;
42
43
43
44
// Extract organization and project from the connection URL
44
45
const { organization, project } = extractOrgAndProject (
@@ -127,10 +128,9 @@ function extractOrgAndProject(
127
128
/**
128
129
* Get the authorization header from the connection
129
130
*
130
- * @param connection The Azure DevOps WebApi connection
131
131
* @returns The authorization header
132
132
*/
133
- async function getAuthorizationHeader ( connection : WebApi ) : Promise < string > {
133
+ async function getAuthorizationHeader ( ) : Promise < string > {
134
134
try {
135
135
// For PAT authentication, we can construct the header directly
136
136
if (
@@ -143,15 +143,27 @@ async function getAuthorizationHeader(connection: WebApi): Promise<string> {
143
143
return `Basic ${ base64Token } ` ;
144
144
}
145
145
146
- // For other auth methods, we'll make a simple API call to get a valid token
147
- // This is a workaround since we can't directly access the auth handler's token
148
- const coreApi = await connection . getCoreApi ( ) ;
149
- await coreApi . getProjects ( ) ;
146
+ // For Azure Identity / Azure CLI auth, we need to get a token
147
+ // using the Azure DevOps resource ID
148
+ // Choose the appropriate credential based on auth method
149
+ const credential =
150
+ process . env . AZURE_DEVOPS_AUTH_METHOD ?. toLowerCase ( ) === 'azure-cli'
151
+ ? new AzureCliCredential ( )
152
+ : new DefaultAzureCredential ( ) ;
153
+
154
+ // Azure DevOps resource ID for token acquisition
155
+ const AZURE_DEVOPS_RESOURCE_ID = '499b84ac-1321-427f-aa17-267ca6975798' ;
156
+
157
+ // Get token for Azure DevOps
158
+ const token = await credential . getToken (
159
+ `${ AZURE_DEVOPS_RESOURCE_ID } /.default` ,
160
+ ) ;
161
+
162
+ if ( ! token || ! token . token ) {
163
+ throw new Error ( 'Failed to acquire token for Azure DevOps' ) ;
164
+ }
150
165
151
- // At this point, the connection should have made a request and we can
152
- // extract the auth header from the most recent request
153
- // If this fails, we'll fall back to a default approach
154
- return `Basic ${ Buffer . from ( ':' + process . env . AZURE_DEVOPS_PAT ) . toString ( 'base64' ) } ` ;
166
+ return `Bearer ${ token . token } ` ;
155
167
} catch ( error ) {
156
168
throw new AzureDevOpsValidationError (
157
169
`Failed to get authorization header: ${ error instanceof Error ? error . message : String ( error ) } ` ,
0 commit comments