Skip to content

Commit 0aaf75e

Browse files
committed
CRED-2148: Add PAT auth support to TypeScript v2 API client
1 parent b88dc59 commit 0aaf75e

3 files changed

Lines changed: 38 additions & 0 deletions

File tree

packages/datadog-api-client/src/auth.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,26 @@ export class AppKeyAuthAuthentication implements SecurityAuthentication {
8181
}
8282
}
8383

84+
/**
85+
* Applies bearer token authentication to the request context.
86+
*/
87+
export class BearerAuthAuthentication implements SecurityAuthentication {
88+
public constructor(private token: string) {}
89+
90+
public getName(): string {
91+
return "bearerAuth";
92+
}
93+
94+
public applySecurityAuthentication(context: RequestContext): void {
95+
context.setHeaderParam("Authorization", "Bearer " + this.token);
96+
}
97+
}
98+
8499
export type AuthMethods = {
85100
AuthZ?: SecurityAuthentication;
86101
apiKeyAuth?: SecurityAuthentication;
87102
appKeyAuth?: SecurityAuthentication;
103+
bearerAuth?: SecurityAuthentication;
88104
};
89105

90106
export type ApiKeyConfiguration = string;
@@ -96,6 +112,7 @@ export type AuthMethodsConfiguration = {
96112
AuthZ?: OAuth2Configuration;
97113
apiKeyAuth?: ApiKeyConfiguration;
98114
appKeyAuth?: ApiKeyConfiguration;
115+
bearerAuth?: ApiKeyConfiguration;
99116
};
100117

101118
/**
@@ -129,5 +146,11 @@ export function configureAuthMethods(
129146
);
130147
}
131148

149+
if (config["bearerAuth"]) {
150+
authMethods["bearerAuth"] = new BearerAuthAuthentication(
151+
config["bearerAuth"],
152+
);
153+
}
154+
132155
return authMethods;
133156
}

packages/datadog-api-client/src/configuration.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,14 @@ export function createConfiguration(
218218
) {
219219
authMethods["appKeyAuth"] = process.env.DD_APP_KEY;
220220
}
221+
if (
222+
!("bearerAuth" in authMethods) &&
223+
typeof process !== "undefined" &&
224+
process.env &&
225+
process.env.DD_BEARER_TOKEN
226+
) {
227+
authMethods["bearerAuth"] = process.env.DD_BEARER_TOKEN;
228+
}
221229

222230
const configuration = new Configuration(
223231
conf.baseServer,
@@ -254,6 +262,10 @@ export function applySecurityAuthentication<
254262
requestContext: RequestContext,
255263
authMethods: AuthMethodKey[],
256264
): void {
265+
if (conf.authMethods["bearerAuth"]) {
266+
conf.authMethods["bearerAuth"].applySecurityAuthentication(requestContext);
267+
}
268+
257269
for (const authMethodName of authMethods) {
258270
const authMethod = conf.authMethods[authMethodName];
259271
if (authMethod) {

packages/datadog-api-client/src/http/isomorphic-fetch.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
180180
"x",
181181
);
182182
}
183+
if (headers["Authorization"]) {
184+
headers["Authorization"] = headers["Authorization"].replace(/./g, "x");
185+
}
183186

184187
const headersJSON = JSON.stringify(headers, null, 2).replace(/\n/g, "\n\t");
185188
const method = request.getHttpMethod().toString();

0 commit comments

Comments
 (0)