Skip to content

Commit 3c0a6e4

Browse files
authored
Fix bearer token authentication for Git clone operations (azure-devops) (#6072)
* fix(azure-devops): fix bearer token auth for Git clone Bearer tokens from DefaultAzureCredential were failing with 401 because Git.fromAuth() doesn't handle { token } format. Changed to use basic auth format like PAT tokens, which Azure DevOps supports for OAuth tokens. Signed-off-by: tonyhogsten <tony.hogsten@if.se> * fix(azure-devops): fix bearer token auth for Git clone Bearer tokens from DefaultAzureCredential were failing with 401 because Git.fromAuth() doesn't handle { token } format. Changed to use basic auth format like PAT tokens, which Azure DevOps supports for OAuth tokens. Signed-off-by: tonyhogsten <tony.hogsten@if.se> * update test to match the object returned. Signed-off-by: tonyhogsten <tony.hogsten@if.se> * revert change of import order Signed-off-by: tonyhogsten <tony.hogsten@if.se> * Revert change of import order Signed-off-by: tonyhogsten <tony.hogsten@if.se> * revert change Signed-off-by: tonyhogsten <tony.hogsten@if.se> * Remove example and reference the documentation:. Signed-off-by: Tony Hogsten <tony.hogsten@if.se> Signed-off-by: tonyhogsten <tony.hogsten@if.se> --------- Signed-off-by: tonyhogsten <tony.hogsten@if.se> Signed-off-by: Tony Hogsten <tony.hogsten@if.se>
1 parent 50a0659 commit 3c0a6e4

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@backstage-community/plugin-scaffolder-backend-module-azure-devops': patch
3+
---
4+
5+
Fix bearer token authentication for Git clone operations

workspaces/azure-devops/plugins/scaffolder-backend-module-azure-devops/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ integrations:
4646
- personalAccessToken: ${PERSONAL_ACCESS_TOKEN}
4747
```
4848
49+
To use other authentication methods, see the [documentation.](https://backstage.io/docs/integrations/azure/locations)
50+
4951
## Usage
5052
5153
You can use the action in any of the steps of your [Software Template](https://backstage.io/docs/features/software-templates/).

workspaces/azure-devops/plugins/scaffolder-backend-module-azure-devops/src/actions/devopsRepoClone.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ describe('createAzureDevOpsCloneRepoAction', () => {
100100
await createAzureDevOpsCloneRepoAction({ integrations }).handler(ctx);
101101
expect(cloneRepo).toHaveBeenCalledWith(
102102
expect.objectContaining({
103-
auth: { token: 'bearer-token' },
103+
auth: { username: 'not-empty', password: 'bearer-token' },
104104
}),
105105
);
106106
});

workspaces/azure-devops/plugins/scaffolder-backend-module-azure-devops/src/actions/helpers.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,23 @@ export async function getGitCredentials(
102102
DefaultAzureDevOpsCredentialsProvider.fromIntegrations(integrations);
103103
const credentials = await credentialProvider.getCredentials({ url: url });
104104

105-
let auth: { username: string; password: string } | { token: string };
106105
if (token) {
107-
auth = { username: 'not-empty', password: token };
108-
} else if (credentials?.type === 'pat') {
109-
auth = { username: 'not-empty', password: credentials.token };
110-
} else if (credentials?.type === 'bearer') {
111-
auth = { token: credentials.token };
112-
} else {
106+
return { username: 'not-empty', password: token };
107+
}
108+
109+
if (!credentials) {
113110
throw new InputError(
114-
`No credentials provided ${url}, please check your integrations config`,
111+
`No credentials provided for ${url}, please check your integrations config`,
115112
);
116113
}
117-
return auth;
114+
115+
if (credentials.type === 'pat' || credentials.type === 'bearer') {
116+
return { username: 'not-empty', password: credentials.token };
117+
}
118+
119+
throw new InputError(
120+
`Unsupported credential type '${credentials.type}' for ${url}`,
121+
);
118122
}
119123

120124
export async function getAuthHandler(

0 commit comments

Comments
 (0)