Skip to content

Commit d6dd900

Browse files
Move azure token logic into seperate file (#1606)
Part of OPS-2860
1 parent df9d451 commit d6dd900

2 files changed

Lines changed: 43 additions & 27 deletions

File tree

packages/blocks/azure/src/lib/actions/custom-azure-api-action.ts

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,9 @@
11
import { createCustomApiCallAction } from '@openops/blocks-common';
22
import { Property } from '@openops/blocks-framework';
3-
import {
4-
authenticateUserWithAzure,
5-
azureAuth,
6-
getUseHostSessionProperty,
7-
} from '@openops/common';
8-
import { runCommand } from '../azure-cli';
3+
import { azureAuth, getUseHostSessionProperty } from '@openops/common';
4+
import { getAzureAccessToken } from '../auth/get-azure-access-token';
95
import { getSubscriptionsDropdownForHostSession } from '../common-properties';
106

11-
const getHostAccessToken = async (
12-
auth: unknown,
13-
subscription: string,
14-
): Promise<string> => {
15-
const output = await runCommand(
16-
'account get-access-token --resource https://management.azure.com --output json',
17-
auth,
18-
true,
19-
subscription,
20-
);
21-
const parsed = JSON.parse(output ?? '{}');
22-
const token = parsed?.accessToken;
23-
if (!token) {
24-
throw new Error('Failed to obtain Azure access token');
25-
}
26-
return token as string;
27-
};
28-
297
export const customAzureApiCallAction = createCustomApiCallAction({
308
auth: azureAuth,
319
name: 'custom_azure_api_call',
@@ -65,9 +43,11 @@ export const customAzureApiCallAction = createCustomApiCallAction({
6543
const selectedSubscription =
6644
context.propsValue?.subscriptions?.['subDropdown'];
6745

68-
const token = shouldUseHostCredentials
69-
? await getHostAccessToken(context.auth, selectedSubscription)
70-
: (await authenticateUserWithAzure(context.auth)).access_token;
46+
const token = await getAzureAccessToken(
47+
context.auth,
48+
!!shouldUseHostCredentials,
49+
selectedSubscription,
50+
);
7151

7252
return {
7353
Authorization: `Bearer ${token}`,
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { authenticateUserWithAzure } from '@openops/common';
2+
import { runCommand } from '../azure-cli';
3+
4+
function parseAzAccessToken(raw?: string | null): string {
5+
const parsed = JSON.parse(raw ?? '{}');
6+
const token = parsed?.accessToken;
7+
if (!token) {
8+
throw new Error('Failed to obtain Azure access token');
9+
}
10+
return token;
11+
}
12+
13+
export const getHostAccessToken = async (
14+
auth: unknown,
15+
subscription?: string,
16+
): Promise<string> => {
17+
const output = await runCommand(
18+
'account get-access-token --resource https://management.azure.com --output json',
19+
auth,
20+
true,
21+
subscription,
22+
);
23+
return parseAzAccessToken(output);
24+
};
25+
26+
export const getAzureAccessToken = async (
27+
auth: unknown,
28+
useHost: boolean,
29+
subscription?: string,
30+
): Promise<string> => {
31+
if (useHost) {
32+
return getHostAccessToken(auth, subscription);
33+
}
34+
const { access_token } = await authenticateUserWithAzure(auth);
35+
return access_token;
36+
};

0 commit comments

Comments
 (0)