From 38a8135171385e5f2c60859743aa86dac3db0a31 Mon Sep 17 00:00:00 2001 From: Andre Wanlin Date: Sun, 22 Oct 2023 11:42:01 -0500 Subject: [PATCH] Use correct authHandler Signed-off-by: Andre Wanlin --- .../src/api/AzureDevOpsApi.ts | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/plugins/azure-devops-backend/src/api/AzureDevOpsApi.ts b/plugins/azure-devops-backend/src/api/AzureDevOpsApi.ts index ecd0e4704a..451d5a9a71 100644 --- a/plugins/azure-devops-backend/src/api/AzureDevOpsApi.ts +++ b/plugins/azure-devops-backend/src/api/AzureDevOpsApi.ts @@ -49,7 +49,11 @@ import { import { TeamMember as AdoTeamMember } from 'azure-devops-node-api/interfaces/common/VSSInterfaces'; import { Logger } from 'winston'; import { PolicyEvaluationRecord } from 'azure-devops-node-api/interfaces/PolicyInterfaces'; -import { WebApi, getPersonalAccessTokenHandler } from 'azure-devops-node-api'; +import { + WebApi, + getBearerHandler, + getPersonalAccessTokenHandler, +} from 'azure-devops-node-api'; import { TeamProjectReference, WebApiTeam, @@ -72,22 +76,22 @@ export class AzureDevOpsApi { private async getWebApi(host?: string, org?: string): Promise { const validHost = host ?? this.config.getString('azureDevOps.host'); const validOrg = org ?? this.config.getString('azureDevOps.organization'); + const url = `https://${validHost}/${validOrg}`; + const scmIntegrations = ScmIntegrations.fromConfig(this.config); const credentialsProvider = DefaultAzureDevOpsCredentialsProvider.fromIntegrations(scmIntegrations); const credentials = await credentialsProvider.getCredentials({ - url: `https://${validHost}/${validOrg}`, + url, }); - let validToken: string; - if (credentials && credentials.token) { - validToken = credentials.token; - } else { - validToken = this.config.getString('azureDevOps.token'); - } - - const authHandler = getPersonalAccessTokenHandler(validToken); - const webApi = new WebApi(`https://${validHost}/${validOrg}`, authHandler); + const authHandler = + this.config.getString('azureDevOps.token') || credentials?.type === 'pat' + ? getPersonalAccessTokenHandler( + credentials!.token ?? this.config.getString('azureDevOps.token'), + ) + : getBearerHandler(credentials!.token); + const webApi = new WebApi(url, authHandler); return webApi; }