-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathdestination.ts
260 lines (234 loc) · 7.36 KB
/
destination.ts
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import { assoc } from '@sap-cloud-sdk/util';
import {
AuthenticationType,
Destination,
DestinationAuthToken,
DestinationCertificate,
DestinationNameAndJwt,
isDestinationNameAndJwt
} from './destination-service-types';
/**
* Takes an existing or a parsed destination and returns an SDK compatible destination object.
*
* @param destination - An object that adheres to the [[Destination]] interface.
* @returns An SDK compatible destination object.
*/
export function sanitizeDestination(
destination: Record<string, any>
): Destination {
validateDestinationInput(destination);
const destAuthToken = parseAuthTokens(destination);
let parsedDestination = parseCertificates(destAuthToken) as Destination;
parsedDestination = setDefaultAuthenticationFallback(parsedDestination);
parsedDestination = setTrustAll(parsedDestination);
parsedDestination = setOriginalProperties(parsedDestination);
return parsedDestination;
}
/**
* Takes a JSON object returned by any of the calls to the destination service and returns an SDK compatible destination object.
* This function only accepts destination configurations of type 'HTTP' and will error if no 'URL' is given.
*
* @param destinationJson - A JSON object returned by the destination service.
* @returns An SDK compatible destination object.
*/
export function parseDestination(
destinationJson: DestinationJson | DestinationConfiguration
): Destination {
const destinationConfig = getDestinationConfig(destinationJson);
validateDestinationConfig(destinationConfig);
const destination = Object.entries(destinationConfig).reduce(
(dest, [originalKey, value]) => {
if (originalKey in configMapping) {
dest[configMapping[originalKey]] = value;
}
return dest;
},
{
originalProperties: destinationJson,
authTokens: destinationJson['authTokens'] || [],
certificates: destinationJson['certificates'] || []
}
);
return sanitizeDestination(destination);
}
function getDestinationConfig(
destinationJson: DestinationJson | DestinationConfiguration
): DestinationConfiguration {
return isDestinationJson(destinationJson)
? destinationJson.destinationConfiguration
: destinationJson;
}
function validateDestinationConfig(
destinationConfig: DestinationConfiguration
): void {
if (
isHttpDestination(destinationConfig) &&
typeof destinationConfig.URL === 'undefined'
) {
throw Error(
"Property 'URL' of destination configuration must not be undefined."
);
}
}
function validateDestinationInput(destinationInput: Record<string, any>): void {
if (
isHttpDestination(destinationInput) &&
typeof destinationInput.url === 'undefined'
) {
throw Error("Property 'url' of destination input must not be undefined.");
}
}
function isHttpDestination(destinationInput: Record<string, any>): boolean {
return (
destinationInput.Type === 'HTTP' ||
destinationInput.type === 'HTTP' ||
(typeof destinationInput.type === 'undefined' &&
typeof destinationInput.Type === 'undefined')
);
}
/* eslint-disable-next-line valid-jsdoc */
/**
* @hidden
*/
export function toDestinationNameUrl(
destination: Destination | DestinationNameAndJwt
): string {
return isDestinationNameAndJwt(destination)
? `name: ${destination.destinationName}`
: `name: ${destination.name}, url: ${destination.url}`;
}
function setOriginalProperties(destination: Destination): Destination {
const originalProperties = destination.originalProperties
? destination.originalProperties
: destination;
return assoc('originalProperties', originalProperties, destination);
}
function setDefaultAuthenticationFallback(
destination: Destination
): Destination {
return destination.authentication
? destination
: assoc('authentication', getAuthenticationType(destination), destination);
}
function parseCertificate(
certificate: Record<string, any>
): DestinationCertificate {
return {
name: certificate.Name || certificate.name,
content: certificate.Content || certificate.content,
type: certificate.Type || certificate.type
};
}
function parseCertificates(
destination: Record<string, any>
): Record<string, any> {
const certificates = destination.certificates
? destination.certificates.map(parseCertificate)
: [];
return assoc('certificates', certificates, destination);
}
function parseAuthToken(authToken: Record<string, any>): DestinationAuthToken {
return {
type: authToken.type,
value: authToken.value,
expiresIn: authToken.expires_in,
error: 'error' in authToken ? authToken.error : null,
http_header: authToken.http_header
};
}
function parseAuthTokens(
destination: Record<string, any>
): Record<string, any> {
const authTokens = destination.authTokens
? destination.authTokens.map(parseAuthToken)
: [];
return assoc('authTokens', authTokens, destination);
}
function setTrustAll(destination: Destination): Destination {
return assoc(
'isTrustingAllCertificates',
parseTrustAll(destination.isTrustingAllCertificates),
destination
);
}
function parseTrustAll(isTrustingAllCertificates?: string | boolean): boolean {
if (typeof isTrustingAllCertificates === 'string') {
return isTrustingAllCertificates.toLowerCase() === 'true';
}
return !!isTrustingAllCertificates;
}
function getAuthenticationType(destination: Destination): AuthenticationType {
return destination.authentication ||
(destination.username && destination.password)
? 'BasicAuthentication'
: 'NoAuthentication';
}
/**
* Destination configuration alongside authtokens and certificates.
*/
export interface DestinationJson {
[key: string]: any;
destinationConfiguration: DestinationConfiguration;
authTokens?: Record<string, string>[];
certificates?: Record<string, string>[];
}
/**
* Configuration of a destination as it is available through the destination service.
*/
export interface DestinationConfiguration {
[key: string]: any;
URL: string;
Name?: string;
ProxyType?: string;
'sap-client'?: string;
User?: string;
Password?: string;
Authentication?: AuthenticationType;
TrustAll?: string;
tokenServiceURL?: string;
tokenServiceUsername?: string;
tokenServicePass?: string;
clientId?: string;
clientSecret?: string;
SystemUser?: string;
Type?: 'HTTP' | 'LDAP' | 'MAIL' | 'RFC';
}
/* eslint-disable-next-line valid-jsdoc */
/**
* @hidden
*/
export function isDestinationConfiguration(
destination: any
): destination is DestinationConfiguration {
return destination.URL !== undefined;
}
/* eslint-disable-next-line valid-jsdoc */
/**
* @hidden
*/
export function isDestinationJson(
destination: any
): destination is DestinationJson {
return Object.keys(destination).includes('destinationConfiguration');
}
const configMapping: Record<string, keyof Destination> = {
URL: 'url',
Name: 'name',
User: 'username',
Password: 'password',
ProxyType: 'proxyType',
'sap-client': 'sapClient',
Authentication: 'authentication',
TrustAll: 'isTrustingAllCertificates',
Type: 'type',
tokenServiceURL: 'tokenServiceUrl',
clientId: 'clientId',
clientSecret: 'clientSecret',
tokenServiceUser: 'tokenServiceUser',
tokenServicePassword: 'tokenServicePassword',
CloudConnectorLocationId: 'cloudConnectorLocationId',
certificates: 'certificates',
KeyStoreLocation: 'keyStoreName',
KeyStorePassword: 'keyStorePassword',
SystemUser: 'systemUser'
};