-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathauth.ts
More file actions
156 lines (131 loc) · 3.89 KB
/
Copy pathauth.ts
File metadata and controls
156 lines (131 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { RequestContext } from "./http/http";
/**
* Interface authentication schemes.
*/
export interface SecurityAuthentication {
/*
* @return returns the name of the security authentication as specified in OAI
*/
getName(): string;
/**
* Applies the authentication scheme to the request context
*
* @params context the request context which should use this authentication scheme
*/
applySecurityAuthentication(context: RequestContext): void;
}
export interface TokenProvider {
getToken(): Promise<string> | string;
}
/**
* Applies oauth2 authentication to the request context.
*/
export class AuthZAuthentication implements SecurityAuthentication {
/**
* Configures OAuth2 with the necessary properties
*
* @param accessToken: The access token to be used for every request
*/
public constructor(public accessToken: string) {}
public getName(): string {
return "AuthZ";
}
public applySecurityAuthentication(context: RequestContext): void {
context.setHeaderParam("Authorization", "Bearer " + this.accessToken);
}
}
/**
* Applies apiKey authentication to the request context.
*/
export class ApiKeyAuthAuthentication implements SecurityAuthentication {
/**
* Configures this api key authentication with the necessary properties
*
* @param apiKey: The api key to be used for every request
*/
public constructor(public apiKey: string) {}
public getName(): string {
return "apiKeyAuth";
}
public applySecurityAuthentication(context: RequestContext): void {
context.setHeaderParam("DD-API-KEY", this.apiKey);
}
}
/**
* Applies apiKey authentication to the request context.
*/
export class AppKeyAuthAuthentication implements SecurityAuthentication {
/**
* Configures this api key authentication with the necessary properties
*
* @param apiKey: The api key to be used for every request
*/
public constructor(public apiKey: string) {}
public getName(): string {
return "appKeyAuth";
}
public applySecurityAuthentication(context: RequestContext): void {
context.setHeaderParam("DD-APPLICATION-KEY", this.apiKey);
}
}
/**
* Applies bearer token authentication to the request context.
*/
export class BearerAuthAuthentication implements SecurityAuthentication {
public constructor(private token: string) {}
public getName(): string {
return "bearerAuth";
}
public applySecurityAuthentication(context: RequestContext): void {
context.setHeaderParam("Authorization", "Bearer " + this.token);
}
}
export type AuthMethods = {
AuthZ?: SecurityAuthentication;
apiKeyAuth?: SecurityAuthentication;
appKeyAuth?: SecurityAuthentication;
bearerAuth?: SecurityAuthentication;
};
export type ApiKeyConfiguration = string;
export type HttpBasicConfiguration = { username: string; password: string };
export type HttpBearerConfiguration = { tokenProvider: TokenProvider };
export type OAuth2Configuration = { accessToken: string };
export type AuthMethodsConfiguration = {
AuthZ?: OAuth2Configuration;
apiKeyAuth?: ApiKeyConfiguration;
appKeyAuth?: ApiKeyConfiguration;
bearerAuth?: ApiKeyConfiguration;
};
/**
* Creates the authentication methods from a swagger description.
*
*/
export function configureAuthMethods(
config: AuthMethodsConfiguration | undefined,
): AuthMethods {
let authMethods: AuthMethods = {};
if (!config) {
return authMethods;
}
if (config["AuthZ"]) {
authMethods["AuthZ"] = new AuthZAuthentication(
config["AuthZ"]["accessToken"],
);
}
if (config["apiKeyAuth"]) {
authMethods["apiKeyAuth"] = new ApiKeyAuthAuthentication(
config["apiKeyAuth"],
);
}
if (config["appKeyAuth"]) {
authMethods["appKeyAuth"] = new AppKeyAuthAuthentication(
config["appKeyAuth"],
);
}
if (config["bearerAuth"]) {
authMethods["bearerAuth"] = new BearerAuthAuthentication(
config["bearerAuth"],
);
}
return authMethods;
}