@takodotid/azure-rest 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/renovate.json +4 -0
- package/.github/workflows/publish.yaml +47 -0
- package/.github/workflows/test.yaml +32 -0
- package/.node-version +1 -0
- package/.vscode/settings.json +12 -0
- package/LICENSE +21 -0
- package/biome.json +56 -0
- package/dist/index.cjs +439 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +368 -0
- package/dist/index.d.ts +368 -0
- package/dist/index.js +396 -0
- package/dist/index.js.map +1 -0
- package/lefthook.yaml +11 -0
- package/package.json +56 -0
- package/pnpm-workspace.yaml +3 -0
- package/src/AzureClient.ts +147 -0
- package/src/index.ts +7 -0
- package/src/lib/AzureCliCredential.ts +135 -0
- package/src/lib/AzureCredential.ts +17 -0
- package/src/lib/DefaultChainedCredential.ts +34 -0
- package/src/lib/ManagedIdentityCredential.ts +75 -0
- package/src/lib/ServicePrincipalCredential.ts +132 -0
- package/src/lib/WorkloadIdentityCredential.ts +71 -0
- package/tsconfig.json +15 -0
- package/tsup.config.ts +10 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents an Azure access token and its expiration.
|
|
3
|
+
*/
|
|
4
|
+
type Credential = {
|
|
5
|
+
accessToken: string;
|
|
6
|
+
clientId?: string;
|
|
7
|
+
expiresAt: Date;
|
|
8
|
+
tokenType: string;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Abstract credential class for acquiring Azure tokens.
|
|
12
|
+
* Implement this to provide custom authentication logic.
|
|
13
|
+
*/
|
|
14
|
+
declare abstract class AzureCredential {
|
|
15
|
+
/**
|
|
16
|
+
* Gets an Azure access token for the given scope.
|
|
17
|
+
* @param scope The resource or scope for which the token is requested
|
|
18
|
+
* @returns A promise resolving to an AzureToken
|
|
19
|
+
*/
|
|
20
|
+
abstract getToken(scope: string): Promise<Credential>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Options for configuring the AzureClient instance.
|
|
25
|
+
*
|
|
26
|
+
* @property baseUrl - The base URL for Azure REST API endpoints (e.g. https://management.azure.com)
|
|
27
|
+
* @property credential - Credential configuration for authenticating requests
|
|
28
|
+
* @property helper - An AzureCredential implementation for acquiring tokens
|
|
29
|
+
* @property scope - The Azure resource scope for the token (e.g. https://management.azure.com/.default)
|
|
30
|
+
* @property builder - (Optional) Function to build request headers from a token. If not provided, an Authorization header is set by default.
|
|
31
|
+
*/
|
|
32
|
+
type AzureClientOptions = {
|
|
33
|
+
baseUrl: string;
|
|
34
|
+
credential: {
|
|
35
|
+
helper: AzureCredential;
|
|
36
|
+
scope: string;
|
|
37
|
+
builder?: (token: Credential) => Record<string, string>;
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Azure REST API client with credential refresh and HTTP verb helpers.
|
|
42
|
+
*/
|
|
43
|
+
declare class AzureClient {
|
|
44
|
+
options: AzureClientOptions;
|
|
45
|
+
private static readonly MAX_TOKEN_RETRIES;
|
|
46
|
+
private token;
|
|
47
|
+
/**
|
|
48
|
+
* @param options Azure client configuration (baseUrl, credential, etc)
|
|
49
|
+
*/
|
|
50
|
+
constructor(options: AzureClientOptions);
|
|
51
|
+
/**
|
|
52
|
+
* Sends a request to the Azure REST API, handling token refresh and retries.
|
|
53
|
+
* @param path The API path (relative to baseUrl)
|
|
54
|
+
* @param init Optional fetch options
|
|
55
|
+
* @returns The fetch Response object
|
|
56
|
+
* @throws If token refresh fails after max retries
|
|
57
|
+
*/
|
|
58
|
+
sendRequest(path: string, init?: RequestInit): Promise<Response>;
|
|
59
|
+
/**
|
|
60
|
+
* Sends a GET request to the Azure REST API.
|
|
61
|
+
* @param path The API path
|
|
62
|
+
* @param init Optional fetch options
|
|
63
|
+
* @returns The fetch Response object
|
|
64
|
+
*/
|
|
65
|
+
get(path: string, init?: RequestInit): Promise<Response>;
|
|
66
|
+
/**
|
|
67
|
+
* Sends a POST request with a JSON body to the Azure REST API.
|
|
68
|
+
* @param path The API path
|
|
69
|
+
* @param body The request body (will be JSON.stringified)
|
|
70
|
+
* @param init Optional fetch options
|
|
71
|
+
* @returns The fetch Response object
|
|
72
|
+
*/
|
|
73
|
+
post(path: string, body?: any, init?: RequestInit): Promise<Response>;
|
|
74
|
+
/**
|
|
75
|
+
* Sends a PUT request with a JSON body to the Azure REST API.
|
|
76
|
+
* @param path The API path
|
|
77
|
+
* @param body The request body (will be JSON.stringified)
|
|
78
|
+
* @param init Optional fetch options
|
|
79
|
+
* @returns The fetch Response object
|
|
80
|
+
*/
|
|
81
|
+
put(path: string, body?: any, init?: RequestInit): Promise<Response>;
|
|
82
|
+
/**
|
|
83
|
+
* Sends a PATCH request with a JSON body to the Azure REST API.
|
|
84
|
+
* @param path The API path
|
|
85
|
+
* @param body The request body (will be JSON.stringified)
|
|
86
|
+
* @param init Optional fetch options
|
|
87
|
+
* @returns The fetch Response object
|
|
88
|
+
*/
|
|
89
|
+
patch(path: string, body?: any, init?: RequestInit): Promise<Response>;
|
|
90
|
+
/**
|
|
91
|
+
* Sends a DELETE request to the Azure REST API.
|
|
92
|
+
* @param path The API path
|
|
93
|
+
* @param init Optional fetch options
|
|
94
|
+
* @returns The fetch Response object
|
|
95
|
+
*/
|
|
96
|
+
delete(path: string, init?: RequestInit): Promise<Response>;
|
|
97
|
+
/**
|
|
98
|
+
* Refreshes the Azure access token using the provided credential helper.
|
|
99
|
+
* @private
|
|
100
|
+
*/
|
|
101
|
+
private refreshToken;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* The raw response returned by Azure CLI when requesting an access token.
|
|
106
|
+
*
|
|
107
|
+
* @property accessToken - The access token string
|
|
108
|
+
* @property expiresOn - Expiry date in RFC3339 format (legacy)
|
|
109
|
+
* @property expires_on - Expiry as seconds since epoch (preferred)
|
|
110
|
+
* @property subscription - (Optional) Subscription ID, may not be present
|
|
111
|
+
* @property tenant - Tenant ID
|
|
112
|
+
* @property tokenType - Token type (usually 'Bearer')
|
|
113
|
+
*/
|
|
114
|
+
type CLITokenResponse = {
|
|
115
|
+
accessToken: string;
|
|
116
|
+
expiresOn: string;
|
|
117
|
+
expires_on: string;
|
|
118
|
+
subscription?: string;
|
|
119
|
+
tenant: string;
|
|
120
|
+
tokenType: string;
|
|
121
|
+
};
|
|
122
|
+
/**
|
|
123
|
+
* Options for AzureCliCredential.
|
|
124
|
+
*
|
|
125
|
+
* @property tenantId - The Azure tenant ID to use for authentication
|
|
126
|
+
*/
|
|
127
|
+
type AzureCLICredentialOptions = {
|
|
128
|
+
tenantId: string;
|
|
129
|
+
};
|
|
130
|
+
declare class AzureCliCredential implements AzureCredential {
|
|
131
|
+
options: AzureCLICredentialOptions;
|
|
132
|
+
constructor(options: AzureCLICredentialOptions);
|
|
133
|
+
/**
|
|
134
|
+
* Gets an Azure access token using the Azure CLI.
|
|
135
|
+
* @param scope The resource scope for the token
|
|
136
|
+
* @returns An object with token and expiresAt
|
|
137
|
+
* @throws If CLI is not installed or not logged in
|
|
138
|
+
*/
|
|
139
|
+
getToken(scope: string): Promise<{
|
|
140
|
+
accessToken: string;
|
|
141
|
+
expiresAt: Date;
|
|
142
|
+
tokenType: string;
|
|
143
|
+
}>;
|
|
144
|
+
/**
|
|
145
|
+
* Runs the Azure CLI to get an access token for the given scope.
|
|
146
|
+
* @param scope The resource scope
|
|
147
|
+
* @returns Promise resolving to CLI stdout and stderr
|
|
148
|
+
* @private
|
|
149
|
+
*/
|
|
150
|
+
private getCliToken;
|
|
151
|
+
/**
|
|
152
|
+
* Parses the raw CLI output and returns a token + expiry object.
|
|
153
|
+
* @param output The stdout from Azure CLI
|
|
154
|
+
* @returns An object with token and expiresAt
|
|
155
|
+
* @private
|
|
156
|
+
*/
|
|
157
|
+
private parseRawOutput;
|
|
158
|
+
/**
|
|
159
|
+
* Instantiates AzureCliCredential using the AZURE_TENANT_ID environment variable.
|
|
160
|
+
* @returns AzureCliCredential instance
|
|
161
|
+
*/
|
|
162
|
+
static fromEnv(): AzureCliCredential;
|
|
163
|
+
}
|
|
164
|
+
declare global {
|
|
165
|
+
namespace NodeJS {
|
|
166
|
+
interface ProcessEnv {
|
|
167
|
+
AZURE_TENANT_ID: string;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* DefaultChainedCredential tries multiple credential providers in order until one succeeds.
|
|
174
|
+
*
|
|
175
|
+
* The chain is: WorkloadIdentityCredential → ManagedIdentityCredential → ServicePrincipalCredential → AzureCliCredential.
|
|
176
|
+
* Useful for local dev, CI, and cloud environments with minimal config.
|
|
177
|
+
*/
|
|
178
|
+
declare class DefaultChainedCredential implements AzureCredential {
|
|
179
|
+
/**
|
|
180
|
+
* Attempts to get an Azure access token using the first available credential in the chain.
|
|
181
|
+
* @param scope The resource scope for the token
|
|
182
|
+
* @returns An object with token and expiresAt
|
|
183
|
+
* @throws If all credential providers fail
|
|
184
|
+
*/
|
|
185
|
+
getToken(scope: string): Promise<{
|
|
186
|
+
accessToken: string;
|
|
187
|
+
expiresAt: Date;
|
|
188
|
+
tokenType: string;
|
|
189
|
+
}>;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Options for configuring ManagedIdentityCredential.
|
|
194
|
+
*
|
|
195
|
+
* @property clientId - (Optional) The user-assigned managed identity client ID
|
|
196
|
+
*/
|
|
197
|
+
type ManagedIdentityCredentialOptions = {
|
|
198
|
+
clientId?: string;
|
|
199
|
+
};
|
|
200
|
+
/**
|
|
201
|
+
* AzureCredential implementation for Azure Managed Identity (MSI).
|
|
202
|
+
*
|
|
203
|
+
* Supports both system-assigned and user-assigned managed identities.
|
|
204
|
+
* Works on Azure VM, App Service, Container Apps, etc.
|
|
205
|
+
*/
|
|
206
|
+
declare class ManagedIdentityCredential implements AzureCredential {
|
|
207
|
+
options: ManagedIdentityCredentialOptions;
|
|
208
|
+
/**
|
|
209
|
+
* @param options Managed identity credential options
|
|
210
|
+
*/
|
|
211
|
+
constructor(options?: ManagedIdentityCredentialOptions);
|
|
212
|
+
/**
|
|
213
|
+
* Gets an Azure access token using the managed identity endpoint.
|
|
214
|
+
* @param scope The resource scope for the token
|
|
215
|
+
* @returns An object with token and expiresAt
|
|
216
|
+
* @throws If the endpoint is unavailable or token request fails
|
|
217
|
+
*/
|
|
218
|
+
getToken(scope: string): Promise<{
|
|
219
|
+
accessToken: string;
|
|
220
|
+
clientId: string | undefined;
|
|
221
|
+
expiresAt: Date;
|
|
222
|
+
tokenType: string;
|
|
223
|
+
}>;
|
|
224
|
+
/**
|
|
225
|
+
* Instantiates ManagedIdentityCredential using environment variables.
|
|
226
|
+
* @returns ManagedIdentityCredential instance
|
|
227
|
+
*/
|
|
228
|
+
static fromEnv(): ManagedIdentityCredential;
|
|
229
|
+
}
|
|
230
|
+
declare global {
|
|
231
|
+
namespace NodeJS {
|
|
232
|
+
interface ProcessEnv {
|
|
233
|
+
AZURE_MANAGED_IDENTITY_ENDPOINT?: string;
|
|
234
|
+
IDENTITY_ENDPOINT?: string;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* The OAuth2 token response returned by Azure AD and Managed Identity endpoints.
|
|
241
|
+
*
|
|
242
|
+
* @property access_token - The access token string
|
|
243
|
+
* @property client_id - The client/application ID (optional, present in MSI)
|
|
244
|
+
* @property expires_in - Seconds until token expiry
|
|
245
|
+
* @property expires_on - Expiry time (epoch seconds, as string)
|
|
246
|
+
* @property ext_expires_in - Extended expiry in seconds
|
|
247
|
+
* @property not_before - Not before time (epoch seconds, as string)
|
|
248
|
+
* @property resource - The resource for which the token is issued
|
|
249
|
+
* @property token_type - The type of token (usually 'Bearer')
|
|
250
|
+
*/
|
|
251
|
+
type OAuth2TokenResponse = {
|
|
252
|
+
access_token: string;
|
|
253
|
+
client_id?: string;
|
|
254
|
+
expires_in: number;
|
|
255
|
+
expires_on: string;
|
|
256
|
+
ext_expires_in: number;
|
|
257
|
+
not_before: string;
|
|
258
|
+
resource: string;
|
|
259
|
+
token_type: string;
|
|
260
|
+
};
|
|
261
|
+
/**
|
|
262
|
+
* Options for configuring ServicePrincipalCredential.
|
|
263
|
+
*
|
|
264
|
+
* @property clientId - The Azure AD application (client) ID
|
|
265
|
+
* @property clientSecret - The client secret or JWT assertion (for federated)
|
|
266
|
+
* @property tenantId - The Azure AD tenant ID
|
|
267
|
+
* @property authorityHost - (Optional) The Azure AD authority host
|
|
268
|
+
* @property federated - Whether to use federated (JWT) auth
|
|
269
|
+
*/
|
|
270
|
+
type ServicePrincipalCredentialOption = {
|
|
271
|
+
clientId: string;
|
|
272
|
+
clientSecret?: string;
|
|
273
|
+
tenantId: string;
|
|
274
|
+
authorityHost?: string;
|
|
275
|
+
federated: boolean;
|
|
276
|
+
};
|
|
277
|
+
/**
|
|
278
|
+
* AzureCredential implementation for authenticating with a Service Principal (client secret or federated/JWT).
|
|
279
|
+
*/
|
|
280
|
+
declare class ServicePrincipalCredential implements AzureCredential {
|
|
281
|
+
options: ServicePrincipalCredentialOption;
|
|
282
|
+
/**
|
|
283
|
+
* @param options Service principal credential options
|
|
284
|
+
*/
|
|
285
|
+
constructor(options: ServicePrincipalCredentialOption);
|
|
286
|
+
/**
|
|
287
|
+
* Gets an Azure access token using the service principal credentials.
|
|
288
|
+
* @param scope The resource scope for the token
|
|
289
|
+
* @returns An object with token and expiresAt
|
|
290
|
+
* @throws If client secret is missing or token request fails
|
|
291
|
+
*/
|
|
292
|
+
getToken(scope: string): Promise<{
|
|
293
|
+
accessToken: string;
|
|
294
|
+
clientId: string;
|
|
295
|
+
expiresAt: Date;
|
|
296
|
+
tokenType: string;
|
|
297
|
+
}>;
|
|
298
|
+
/**
|
|
299
|
+
* Instantiates ServicePrincipalCredential using environment variables.
|
|
300
|
+
* @returns ServicePrincipalCredential instance
|
|
301
|
+
*/
|
|
302
|
+
static fromEnv(): ServicePrincipalCredential;
|
|
303
|
+
}
|
|
304
|
+
declare global {
|
|
305
|
+
namespace NodeJS {
|
|
306
|
+
interface ProcessEnv {
|
|
307
|
+
AZURE_CLIENT_ID: string;
|
|
308
|
+
AZURE_CLIENT_SECRET: string;
|
|
309
|
+
AZURE_TENANT_ID: string;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* Options for configuring WorkloadIdentityCredential.
|
|
316
|
+
*
|
|
317
|
+
* @property clientId - The Azure AD application (client) ID
|
|
318
|
+
* @property federatedTokenFile - Path to the federated token file (OIDC/JWT)
|
|
319
|
+
* @property tenantId - The Azure AD tenant ID
|
|
320
|
+
* @property authorityHost - (Optional) The Azure AD authority host
|
|
321
|
+
*/
|
|
322
|
+
type WorkloadIdentityCredentialOption = {
|
|
323
|
+
clientId: string;
|
|
324
|
+
federatedTokenFile: string;
|
|
325
|
+
tenantId: string;
|
|
326
|
+
authorityHost?: string;
|
|
327
|
+
};
|
|
328
|
+
/**
|
|
329
|
+
* AzureCredential implementation for Azure Workload Identity (OIDC federated token).
|
|
330
|
+
*
|
|
331
|
+
* Reads a federated token from file and authenticates as a service principal using JWT assertion.
|
|
332
|
+
*/
|
|
333
|
+
declare class WorkloadIdentityCredential implements AzureCredential {
|
|
334
|
+
options: WorkloadIdentityCredentialOption;
|
|
335
|
+
/**
|
|
336
|
+
* @param options Workload identity credential options
|
|
337
|
+
*/
|
|
338
|
+
constructor(options: WorkloadIdentityCredentialOption);
|
|
339
|
+
/**
|
|
340
|
+
* Gets an Azure access token using the federated token file.
|
|
341
|
+
* @param scope The resource scope for the token
|
|
342
|
+
* @returns An object with token and expiresAt
|
|
343
|
+
* @throws If the federated token file does not exist
|
|
344
|
+
*/
|
|
345
|
+
getToken(scope: string): Promise<{
|
|
346
|
+
accessToken: string;
|
|
347
|
+
clientId: string;
|
|
348
|
+
expiresAt: Date;
|
|
349
|
+
tokenType: string;
|
|
350
|
+
}>;
|
|
351
|
+
/**
|
|
352
|
+
* Instantiates WorkloadIdentityCredential using environment variables.
|
|
353
|
+
* @returns WorkloadIdentityCredential instance
|
|
354
|
+
*/
|
|
355
|
+
static fromEnv(): WorkloadIdentityCredential;
|
|
356
|
+
}
|
|
357
|
+
declare global {
|
|
358
|
+
namespace NodeJS {
|
|
359
|
+
interface ProcessEnv {
|
|
360
|
+
AZURE_AUTHORITY_HOST: string;
|
|
361
|
+
AZURE_CLIENT_ID: string;
|
|
362
|
+
AZURE_FEDERATED_TOKEN_FILE: string;
|
|
363
|
+
AZURE_TENANT_ID: string;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
export { type AzureCLICredentialOptions, AzureCliCredential, AzureClient, type AzureClientOptions, AzureCredential, type CLITokenResponse, type Credential, DefaultChainedCredential, ManagedIdentityCredential, type ManagedIdentityCredentialOptions, type OAuth2TokenResponse, ServicePrincipalCredential, type ServicePrincipalCredentialOption, WorkloadIdentityCredential, type WorkloadIdentityCredentialOption };
|