@api-client/core 0.8.16 → 0.8.17
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/build/browser.d.ts +3 -1
- package/build/browser.js.map +1 -1
- package/build/index.d.ts +3 -1
- package/build/index.js.map +1 -1
- package/build/src/authorization/lib/SecurityProcessor.d.ts +9 -1
- package/build/src/authorization/lib/SecurityProcessor.js +19 -2
- package/build/src/authorization/lib/SecurityProcessor.js.map +1 -1
- package/build/src/models/ProjectRequest.d.ts +40 -0
- package/build/src/models/ProjectRequest.js +134 -0
- package/build/src/models/ProjectRequest.js.map +1 -1
- package/build/src/runtime/http-runner/HttpRequestRunner.js +3 -3
- package/build/src/runtime/http-runner/HttpRequestRunner.js.map +1 -1
- package/build/src/runtime/node/ProjectRequestRunner.js +6 -1
- package/build/src/runtime/node/ProjectRequestRunner.js.map +1 -1
- package/data/model.js +4 -3
- package/package.json +1 -1
- package/src/authorization/lib/SecurityProcessor.ts +23 -4
- package/src/models/ProjectRequest.ts +166 -0
- package/src/runtime/http-runner/HttpRequestRunner.ts +3 -3
- package/src/runtime/node/ProjectRequestRunner.ts +6 -1
- package/build/src/runtime/http-runner/RequestAuthorizationProcessor.d.ts +0 -56
- package/build/src/runtime/http-runner/RequestAuthorizationProcessor.js +0 -143
- package/build/src/runtime/http-runner/RequestAuthorizationProcessor.js.map +0 -1
- package/src/runtime/http-runner/RequestAuthorizationProcessor.ts +0 -144
package/data/model.js
CHANGED
|
@@ -52,8 +52,8 @@ config.set('APIC-483/APIC-483.raml', { type: "RAML 1.0" });
|
|
|
52
52
|
config.set('oas-3-api/oas-3-api.yaml', { type: "OAS 3.0" });
|
|
53
53
|
config.set('secured-api/secured-api.raml', { type: "RAML 1.0" });
|
|
54
54
|
|
|
55
|
-
const srcFolder =
|
|
56
|
-
const descFolder =
|
|
55
|
+
const srcFolder = 'data/apis/';
|
|
56
|
+
const descFolder = 'data/models/';
|
|
57
57
|
|
|
58
58
|
class ApiParser {
|
|
59
59
|
/**
|
|
@@ -89,7 +89,7 @@ class ApiParser {
|
|
|
89
89
|
if (destFile.indexOf('/') !== -1) {
|
|
90
90
|
destFile = destFile.substring(destFile.lastIndexOf('/'));
|
|
91
91
|
}
|
|
92
|
-
const location =
|
|
92
|
+
const location = `${srcFolder}/${file}`;
|
|
93
93
|
const content = await this.parse(vendor, location);
|
|
94
94
|
const destination = path.join(descFolder, destFile);
|
|
95
95
|
await writeFile(destination, content);
|
|
@@ -105,6 +105,7 @@ class ApiParser {
|
|
|
105
105
|
// const ro = new RenderOptions().withoutAmfJsonLdSerialization().withoutCompactUris().withoutCompactedEmission().withPrettyPrint();
|
|
106
106
|
const apiConfiguration = this.getConfiguration(vendor).withRenderOptions(ro);
|
|
107
107
|
const client = apiConfiguration.baseUnitClient();
|
|
108
|
+
console.log(`file://${location}`);
|
|
108
109
|
const result = await client.parse(`file://${location}`);
|
|
109
110
|
const transformed = client.transform(result.baseUnit, PipelineId.Editing);
|
|
110
111
|
return client.render(transformed.baseUnit, 'application/ld+json');
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { IApiKeyAuthorization, IBasicAuthorization, IBearerAuthorization, IOAuth2Authorization, IOidcAuthorization, IOidcTokenInfo, IPassThroughAuthorization, IRamlCustomAuthorization } from "../../models/Authorization.js";
|
|
1
|
+
import { IApiKeyAuthorization, IBasicAuthorization, IBearerAuthorization, ICCAuthorization, IOAuth2Authorization, IOidcAuthorization, IOidcTokenInfo, IPassThroughAuthorization, IRamlCustomAuthorization } from "../../models/Authorization.js";
|
|
2
2
|
import { IRequestAuthorization, RequestAuthorization } from "../../models/RequestAuthorization.js";
|
|
3
3
|
import { IHttpRequest } from "../../models/HttpRequest.js";
|
|
4
4
|
import { IProperty } from "../../models/Property.js";
|
|
5
5
|
import { Headers } from '../../lib/headers/Headers.js';
|
|
6
|
+
import { HttpCertificate } from "../../models/ClientCertificate.js";
|
|
6
7
|
import { UrlProcessor } from "../../lib/parsers/UrlProcessor.js";
|
|
7
8
|
import { UrlEncoder } from "../../lib/parsers/UrlEncoder.js";
|
|
8
9
|
import { hasBuffer } from '../../Platform.js';
|
|
@@ -30,7 +31,7 @@ export class SecurityProcessor {
|
|
|
30
31
|
/**
|
|
31
32
|
* Applies authorization configuration to the request object.
|
|
32
33
|
*/
|
|
33
|
-
static applyAuthorization(request: IHttpRequest, authorization
|
|
34
|
+
static applyAuthorization(request: IHttpRequest, authorization?: (IRequestAuthorization | RequestAuthorization)[], opts: IAuthApplyOptions = {}): void {
|
|
34
35
|
if (!Array.isArray(authorization) || !authorization.length) {
|
|
35
36
|
return;
|
|
36
37
|
}
|
|
@@ -92,11 +93,11 @@ export class SecurityProcessor {
|
|
|
92
93
|
* Injects basic auth header into the request headers.
|
|
93
94
|
*/
|
|
94
95
|
static applyBasicAuth(request: IHttpRequest, config: IBasicAuthorization): void {
|
|
95
|
-
const { username, password } = config;
|
|
96
|
+
const { username, password = '' } = config;
|
|
96
97
|
if (!username) {
|
|
97
98
|
return;
|
|
98
99
|
}
|
|
99
|
-
const hash = `${username}:${password
|
|
100
|
+
const hash = `${username}:${password}`;
|
|
100
101
|
const value = hasBuffer ? Buffer.from(hash).toString('base64') : btoa(hash);
|
|
101
102
|
this.applyHeader(request, 'authorization', `Basic ${value}`);
|
|
102
103
|
}
|
|
@@ -254,4 +255,22 @@ export class SecurityProcessor {
|
|
|
254
255
|
request.headers = parser.toString();
|
|
255
256
|
}
|
|
256
257
|
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Reads the client certificate from the authorization configuration.
|
|
261
|
+
*
|
|
262
|
+
* @param authorization The HTTP request authorization configuration.
|
|
263
|
+
* @returns The certificate to use with the HTTP request or undefined when not configured.
|
|
264
|
+
*/
|
|
265
|
+
static readCertificate(authorization?: IRequestAuthorization[]): HttpCertificate | undefined {
|
|
266
|
+
if (!Array.isArray(authorization) || !authorization.length) {
|
|
267
|
+
return undefined;
|
|
268
|
+
}
|
|
269
|
+
const item = authorization.find(i => i.enabled !== false && i.type === 'client certificate');
|
|
270
|
+
if (!item || !item.config) {
|
|
271
|
+
return undefined;
|
|
272
|
+
}
|
|
273
|
+
const init = item.config as ICCAuthorization;
|
|
274
|
+
return init.certificate;
|
|
275
|
+
}
|
|
257
276
|
}
|
|
@@ -7,9 +7,29 @@ import v4 from '../lib/uuid.js';
|
|
|
7
7
|
import { IRequest, Request } from './Request.js';
|
|
8
8
|
import { IAppProjectRequest } from './AppProject.js';
|
|
9
9
|
import { Environment } from './Environment.js';
|
|
10
|
+
import { Property } from './Property.js';
|
|
11
|
+
import { Server } from './Server.js';
|
|
12
|
+
import { UriTemplate } from '../lib/parsers/UriTemplate.js';
|
|
10
13
|
|
|
11
14
|
export const Kind = 'Core#ProjectRequest';
|
|
12
15
|
|
|
16
|
+
export interface RequestUrlReadOptions {
|
|
17
|
+
/**
|
|
18
|
+
* When set it won't apply variables to the URL.
|
|
19
|
+
*/
|
|
20
|
+
noVariables?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* When set is returns the only the base URL defined in the environment without
|
|
23
|
+
* adding the `url` defined on the request object. *
|
|
24
|
+
*/
|
|
25
|
+
baseUrl?: boolean;
|
|
26
|
+
/**
|
|
27
|
+
* When set it uses these variables instead one the onces defined on the project.
|
|
28
|
+
* When this is defined then `noVariables` has no effect.
|
|
29
|
+
*/
|
|
30
|
+
variables?: Record<string, string>;
|
|
31
|
+
}
|
|
32
|
+
|
|
13
33
|
export interface IProjectRequest extends IProjectDefinitionProperty, IRequest {
|
|
14
34
|
kind: typeof Kind;
|
|
15
35
|
/**
|
|
@@ -18,6 +38,8 @@ export interface IProjectRequest extends IProjectDefinitionProperty, IRequest {
|
|
|
18
38
|
key: string;
|
|
19
39
|
/**
|
|
20
40
|
* The key of an environment defined on the project to apply to this request.
|
|
41
|
+
* When not set it inherits environments from parents.
|
|
42
|
+
* However, when this is set, it makes sure that server/variables from this environment are always applied.
|
|
21
43
|
*/
|
|
22
44
|
environment?: string;
|
|
23
45
|
}
|
|
@@ -58,6 +80,8 @@ export class ProjectRequest extends Request implements ProjectDefinitionProperty
|
|
|
58
80
|
|
|
59
81
|
/**
|
|
60
82
|
* The key of an environment defined on the project to apply to this request.
|
|
83
|
+
* When not set it inherits environments from parents.
|
|
84
|
+
* However, when this is set, it makes sure that server/variables from this environment are always applied.
|
|
61
85
|
*/
|
|
62
86
|
environment?: string;
|
|
63
87
|
|
|
@@ -320,4 +344,146 @@ export class ProjectRequest extends Request implements ProjectDefinitionProperty
|
|
|
320
344
|
}
|
|
321
345
|
return this.project.readEnvironments({ parent: parent?.key });
|
|
322
346
|
}
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* It reads the URL from an environment defined for the request..
|
|
350
|
+
* The environment servers and variables are applies for all environments defined on each folder down to this request.
|
|
351
|
+
* This respects the `encapsulate` flag defined on an environment.
|
|
352
|
+
*
|
|
353
|
+
* By default it applies variables collected from the environments to the url. You can configure this behavior
|
|
354
|
+
* by passing a configuration option.
|
|
355
|
+
*
|
|
356
|
+
* Note on performance.
|
|
357
|
+
* This is a heave computing operation so use it sparingly. For best performance only compute the URL when
|
|
358
|
+
* environments, environment selection or variables change.
|
|
359
|
+
*
|
|
360
|
+
* @param opts Read URL options
|
|
361
|
+
* @returns the read URL or undefined when the URL/environments are not defined.
|
|
362
|
+
*/
|
|
363
|
+
readUrl(opts: RequestUrlReadOptions = {}): string | undefined {
|
|
364
|
+
const { environment } = this;
|
|
365
|
+
const expects = this.getExpects();
|
|
366
|
+
// note, this list respects the `encapsulate` flag.
|
|
367
|
+
const environments = this.readEnvironments();
|
|
368
|
+
|
|
369
|
+
let requestUrl = opts.baseUrl ? undefined : expects.url;
|
|
370
|
+
if (!environments) {
|
|
371
|
+
if (requestUrl && opts.variables) {
|
|
372
|
+
return this.evaluateUri(requestUrl, opts.variables);
|
|
373
|
+
}
|
|
374
|
+
return requestUrl || undefined;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// we assume that absolute URL has a scheme with a delimiter (://). However, the scheme value can be an URL.
|
|
378
|
+
// When the request starts with `/` is obviously a relative (no domain) URL
|
|
379
|
+
// The edge case is `{` because it may mean a variable with a domain or a path to be appended to the base URI
|
|
380
|
+
// defined in an environment. However, we choose to use `/` as the indicator of being a "relative" URL.
|
|
381
|
+
const isAbsolute = !!requestUrl && !requestUrl.startsWith('/') && requestUrl.includes('://');
|
|
382
|
+
if (isAbsolute && opts.noVariables) {
|
|
383
|
+
// we know the URL exists, it is absolute (server wouldn't be applied anyway), and we don't
|
|
384
|
+
// want to apply variables. We return what we have so far.
|
|
385
|
+
return requestUrl;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
const flatProperties: Property[] = [];
|
|
389
|
+
let effectiveServer: Server | undefined;
|
|
390
|
+
for (const env of environments) {
|
|
391
|
+
if (env.server && env.server.uri) {
|
|
392
|
+
effectiveServer = env.server;
|
|
393
|
+
}
|
|
394
|
+
if (!opts.noVariables && !opts.variables && env.variables) {
|
|
395
|
+
for (const prop of env.variables) {
|
|
396
|
+
// environments are listed from the project's root down to the folder where the request
|
|
397
|
+
// exists. When a folder has a variable that was already defined it is replaced.
|
|
398
|
+
this.addProperty(prop, flatProperties)
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
if (env.key === environment) {
|
|
402
|
+
break;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
if (!requestUrl) {
|
|
407
|
+
// we return whatever we have in the server.
|
|
408
|
+
if (effectiveServer) {
|
|
409
|
+
// flatProperties is be empty if `noVariables` option is passed.
|
|
410
|
+
const uri = effectiveServer.readUri();
|
|
411
|
+
if (opts.variables) {
|
|
412
|
+
return this.evaluateUri(uri, opts.variables);
|
|
413
|
+
}
|
|
414
|
+
return this.evaluateUri(uri, flatProperties);
|
|
415
|
+
}
|
|
416
|
+
return undefined;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
if (isAbsolute) {
|
|
420
|
+
// ignore server reading and just apply variables to the request URL
|
|
421
|
+
if (opts.variables) {
|
|
422
|
+
return this.evaluateUri(requestUrl, opts.variables);
|
|
423
|
+
}
|
|
424
|
+
return this.evaluateUri(requestUrl, flatProperties);
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
let serverUrl = '';
|
|
428
|
+
if (effectiveServer) {
|
|
429
|
+
serverUrl = effectiveServer.readUri();
|
|
430
|
+
}
|
|
431
|
+
if (!serverUrl.endsWith('/')) {
|
|
432
|
+
serverUrl += '/';
|
|
433
|
+
}
|
|
434
|
+
if (requestUrl.startsWith('/')) {
|
|
435
|
+
requestUrl = requestUrl.substring(1);
|
|
436
|
+
}
|
|
437
|
+
const url = serverUrl + requestUrl;
|
|
438
|
+
if (opts.variables) {
|
|
439
|
+
return this.evaluateUri(url, opts.variables);
|
|
440
|
+
}
|
|
441
|
+
return this.evaluateUri(url, flatProperties);
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
protected addProperty(item: Property, list: Property[]): void {
|
|
445
|
+
const index = list.findIndex(i => i.name === item.name);
|
|
446
|
+
if (index >= 0) {
|
|
447
|
+
list.splice(index, 1);
|
|
448
|
+
}
|
|
449
|
+
list.push(item);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
protected evaluateUri(uri: string, variables?: Property[]): string;
|
|
453
|
+
|
|
454
|
+
protected evaluateUri(uri: string, variables?: Record<string, string>): string;
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
* Evaluates the URI against variables.
|
|
458
|
+
*
|
|
459
|
+
* Note, this doesn't throw errors. When error occurs it returns the input string.
|
|
460
|
+
*
|
|
461
|
+
* @param uri The URI to process
|
|
462
|
+
* @param variables The list of variables to use
|
|
463
|
+
* @returns Expanded URI.
|
|
464
|
+
*/
|
|
465
|
+
protected evaluateUri(uri: string, variables?: Property[] | Record<string, string>): string {
|
|
466
|
+
if (!variables) {
|
|
467
|
+
return uri;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
let vars: Record<string, string> | undefined;
|
|
471
|
+
if (Array.isArray(variables)) {
|
|
472
|
+
if (variables.length) {
|
|
473
|
+
vars = Property.toMap(variables);
|
|
474
|
+
}
|
|
475
|
+
} else {
|
|
476
|
+
vars = variables;
|
|
477
|
+
}
|
|
478
|
+
if (!vars) {
|
|
479
|
+
return uri;
|
|
480
|
+
}
|
|
481
|
+
let result = uri;
|
|
482
|
+
try {
|
|
483
|
+
result = new UriTemplate(uri).expand(vars, { ignoreMissing: true });
|
|
484
|
+
} catch (e) {
|
|
485
|
+
//
|
|
486
|
+
}
|
|
487
|
+
return result;
|
|
488
|
+
}
|
|
323
489
|
}
|
|
@@ -9,7 +9,7 @@ import { HttpEngineOptions } from '../http-engine/HttpEngine.js';
|
|
|
9
9
|
import { CoreEngine } from '../http-engine/CoreEngine.js';
|
|
10
10
|
import { Logger } from '../../lib/logging/Logger.js';
|
|
11
11
|
import { CookieJar } from '../../cookies/CookieJar.js';
|
|
12
|
-
import {
|
|
12
|
+
import { SecurityProcessor } from '../../authorization/lib/SecurityProcessor.js';
|
|
13
13
|
import { RequestCookiesProcessor } from './RequestCookiesProcessor.js';
|
|
14
14
|
import { HttpFlowRunner } from './HttpFlowRunner.js';
|
|
15
15
|
import { ISentRequest } from "../../models/SentRequest.js";
|
|
@@ -174,7 +174,7 @@ export class HttpRequestRunner {
|
|
|
174
174
|
|
|
175
175
|
async applyAuthorization(request: IHttpRequest): Promise<IHttpRequest> {
|
|
176
176
|
const auth = await this.readAuthorization();
|
|
177
|
-
|
|
177
|
+
SecurityProcessor.applyAuthorization(request, auth);
|
|
178
178
|
return request;
|
|
179
179
|
}
|
|
180
180
|
|
|
@@ -264,7 +264,7 @@ export class HttpRequestRunner {
|
|
|
264
264
|
const { logger, signal } = this;
|
|
265
265
|
const auth = await this.readAuthorization();
|
|
266
266
|
const config = await this.readConfig();
|
|
267
|
-
const cert =
|
|
267
|
+
const cert = SecurityProcessor.readCertificate(auth);
|
|
268
268
|
const opts: HttpEngineOptions = {};
|
|
269
269
|
if (cert) {
|
|
270
270
|
opts.certificates = [cert];
|
|
@@ -251,12 +251,17 @@ export class ProjectRequestRunner extends EventEmitter {
|
|
|
251
251
|
const info: IRunResult = {
|
|
252
252
|
key: request.key,
|
|
253
253
|
};
|
|
254
|
+
let url: string | undefined;
|
|
255
|
+
const typedProject = request as ProjectRequest;
|
|
256
|
+
if (typeof typedProject.readUrl === 'function') {
|
|
257
|
+
url = typedProject.readUrl({ variables });
|
|
258
|
+
}
|
|
254
259
|
const requestData = request.expects.toJSON();
|
|
255
260
|
|
|
256
261
|
try {
|
|
257
262
|
// Below replaces the single call to the `run()` function of the factory to
|
|
258
263
|
// report via the events a request object that has evaluated with the Jexl library.
|
|
259
|
-
requestData.url = factory.prepareRequestUrl(requestData.url, variables);
|
|
264
|
+
requestData.url = url || factory.prepareRequestUrl(requestData.url, variables);
|
|
260
265
|
const requestCopy = await factory.applyVariables(requestData);
|
|
261
266
|
await factory.applyAuthorization(requestCopy);
|
|
262
267
|
await factory.applyCookies(requestCopy);
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { IHttpRequest } from '../../models/HttpRequest.js';
|
|
2
|
-
import { IRequestAuthorization } from '../../models/RequestAuthorization.js';
|
|
3
|
-
import { IBearerAuthorization, IBasicAuthorization, IOidcAuthorization, IOAuth2Authorization } from '../../models/Authorization.js';
|
|
4
|
-
import { HttpCertificate } from '../../models/ClientCertificate.js';
|
|
5
|
-
/**
|
|
6
|
-
* Applies authorization data to the HttpRequest from
|
|
7
|
-
* request authorization configuration.
|
|
8
|
-
*/
|
|
9
|
-
export declare class RequestAuthorizationProcessor {
|
|
10
|
-
/**
|
|
11
|
-
* Applies the auth data from the authorization config.
|
|
12
|
-
*
|
|
13
|
-
* Note, this mutates the original request. Make a copy of you don't want to change
|
|
14
|
-
* the values in the source request.
|
|
15
|
-
*
|
|
16
|
-
* Note, this does not process client certificates. Use the `#readCertificates()` method to
|
|
17
|
-
* get a certificate to use with the HTTP request,
|
|
18
|
-
*
|
|
19
|
-
* @param request The request to apply the authorization to.
|
|
20
|
-
* @returns The same request (a reference)
|
|
21
|
-
*/
|
|
22
|
-
static setAuthorization(request: IHttpRequest, authorization?: IRequestAuthorization[]): IHttpRequest;
|
|
23
|
-
/**
|
|
24
|
-
* Reads the client certificate from the authorization configuration.
|
|
25
|
-
*
|
|
26
|
-
* @param authorization The HTTP request authorization configuration.
|
|
27
|
-
* @returns The certificate to use with the HTTP request or undefined when not configured.
|
|
28
|
-
*/
|
|
29
|
-
static readCertificate(authorization?: IRequestAuthorization[]): HttpCertificate | undefined;
|
|
30
|
-
/**
|
|
31
|
-
* Injects basic auth header into the request headers.
|
|
32
|
-
*/
|
|
33
|
-
protected static processBasicAuth(request: IHttpRequest, config: IBasicAuthorization): void;
|
|
34
|
-
/**
|
|
35
|
-
* Injects oauth 2 auth header into the request headers.
|
|
36
|
-
*/
|
|
37
|
-
protected static processOAuth2(request: IHttpRequest, config: IOAuth2Authorization): void;
|
|
38
|
-
/**
|
|
39
|
-
* Injects OpenID Connect auth header into the request headers.
|
|
40
|
-
*/
|
|
41
|
-
protected static processOpenId(request: IHttpRequest, config: IOidcAuthorization): void;
|
|
42
|
-
/**
|
|
43
|
-
* Injects bearer auth header into the request headers.
|
|
44
|
-
*/
|
|
45
|
-
protected static processBearer(request: IHttpRequest, config: IBearerAuthorization): void;
|
|
46
|
-
/**
|
|
47
|
-
* Applies the basic authorization data to the request.
|
|
48
|
-
*
|
|
49
|
-
* If the header value have changed then it fires `request-headers-changed` custom event.
|
|
50
|
-
* It sets computed value of the readers to the event's detail object.
|
|
51
|
-
*
|
|
52
|
-
* @param request The event's detail object. Changes made here will be propagated to the event.
|
|
53
|
-
* @param data The authorization data to apply.
|
|
54
|
-
*/
|
|
55
|
-
protected static applyRequestBasicAuthData(request: IHttpRequest, data: IBasicAuthorization): void;
|
|
56
|
-
}
|
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
import { Headers } from '../../lib/headers/Headers.js';
|
|
2
|
-
/**
|
|
3
|
-
* Applies authorization data to the HttpRequest from
|
|
4
|
-
* request authorization configuration.
|
|
5
|
-
*/
|
|
6
|
-
export class RequestAuthorizationProcessor {
|
|
7
|
-
/**
|
|
8
|
-
* Applies the auth data from the authorization config.
|
|
9
|
-
*
|
|
10
|
-
* Note, this mutates the original request. Make a copy of you don't want to change
|
|
11
|
-
* the values in the source request.
|
|
12
|
-
*
|
|
13
|
-
* Note, this does not process client certificates. Use the `#readCertificates()` method to
|
|
14
|
-
* get a certificate to use with the HTTP request,
|
|
15
|
-
*
|
|
16
|
-
* @param request The request to apply the authorization to.
|
|
17
|
-
* @returns The same request (a reference)
|
|
18
|
-
*/
|
|
19
|
-
static setAuthorization(request, authorization) {
|
|
20
|
-
if (!Array.isArray(authorization) || !authorization.length) {
|
|
21
|
-
return request;
|
|
22
|
-
}
|
|
23
|
-
for (const auth of authorization) {
|
|
24
|
-
if (auth.enabled === false || !auth.config) {
|
|
25
|
-
continue;
|
|
26
|
-
}
|
|
27
|
-
switch (auth.type) {
|
|
28
|
-
case 'basic':
|
|
29
|
-
this.processBasicAuth(request, auth.config);
|
|
30
|
-
break;
|
|
31
|
-
case 'oauth 2':
|
|
32
|
-
this.processOAuth2(request, auth.config);
|
|
33
|
-
break;
|
|
34
|
-
case 'open id':
|
|
35
|
-
this.processOpenId(request, auth.config);
|
|
36
|
-
break;
|
|
37
|
-
case 'bearer':
|
|
38
|
-
this.processBearer(request, auth.config);
|
|
39
|
-
break;
|
|
40
|
-
default:
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
return request;
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Reads the client certificate from the authorization configuration.
|
|
47
|
-
*
|
|
48
|
-
* @param authorization The HTTP request authorization configuration.
|
|
49
|
-
* @returns The certificate to use with the HTTP request or undefined when not configured.
|
|
50
|
-
*/
|
|
51
|
-
static readCertificate(authorization) {
|
|
52
|
-
if (!Array.isArray(authorization) || !authorization.length) {
|
|
53
|
-
return undefined;
|
|
54
|
-
}
|
|
55
|
-
const item = authorization.find(i => i.enabled !== false && i.type === 'client certificate');
|
|
56
|
-
if (!item || !item.config) {
|
|
57
|
-
return undefined;
|
|
58
|
-
}
|
|
59
|
-
const init = item.config;
|
|
60
|
-
return init.certificate;
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* Injects basic auth header into the request headers.
|
|
64
|
-
*/
|
|
65
|
-
static processBasicAuth(request, config) {
|
|
66
|
-
const { username } = config;
|
|
67
|
-
if (!username) {
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
this.applyRequestBasicAuthData(request, config);
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Injects oauth 2 auth header into the request headers.
|
|
74
|
-
*/
|
|
75
|
-
static processOAuth2(request, config) {
|
|
76
|
-
const { accessToken, tokenType = 'Bearer', deliveryMethod = 'header', deliveryName = 'authorization' } = config;
|
|
77
|
-
if (!accessToken) {
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
const value = `${tokenType} ${accessToken}`;
|
|
81
|
-
if (deliveryMethod === 'header') {
|
|
82
|
-
const headers = new Headers(request.headers || '');
|
|
83
|
-
headers.append(deliveryName, value);
|
|
84
|
-
request.headers = headers.toString();
|
|
85
|
-
}
|
|
86
|
-
else if (deliveryMethod === 'query') {
|
|
87
|
-
const { url } = request;
|
|
88
|
-
try {
|
|
89
|
-
const parsed = new URL(url);
|
|
90
|
-
parsed.searchParams.append(deliveryName, value);
|
|
91
|
-
request.url = parsed.toString();
|
|
92
|
-
}
|
|
93
|
-
catch (e) {
|
|
94
|
-
// ...
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
/**
|
|
99
|
-
* Injects OpenID Connect auth header into the request headers.
|
|
100
|
-
*/
|
|
101
|
-
static processOpenId(request, config) {
|
|
102
|
-
const { accessToken } = config;
|
|
103
|
-
if (accessToken) {
|
|
104
|
-
this.processOAuth2(request, config);
|
|
105
|
-
}
|
|
106
|
-
// todo - if AT is missing find the current token from the tokens list in the passed configuration.
|
|
107
|
-
// Currently the authorization method UI sets the token when the requests is generated so it's not as much important.
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* Injects bearer auth header into the request headers.
|
|
111
|
-
*/
|
|
112
|
-
static processBearer(request, config) {
|
|
113
|
-
const { token } = config;
|
|
114
|
-
const value = `Bearer ${token}`;
|
|
115
|
-
const headers = new Headers(request.headers || '');
|
|
116
|
-
headers.append('authorization', value);
|
|
117
|
-
request.headers = headers.toString();
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* Applies the basic authorization data to the request.
|
|
121
|
-
*
|
|
122
|
-
* If the header value have changed then it fires `request-headers-changed` custom event.
|
|
123
|
-
* It sets computed value of the readers to the event's detail object.
|
|
124
|
-
*
|
|
125
|
-
* @param request The event's detail object. Changes made here will be propagated to the event.
|
|
126
|
-
* @param data The authorization data to apply.
|
|
127
|
-
*/
|
|
128
|
-
static applyRequestBasicAuthData(request, data) {
|
|
129
|
-
const { username = '', password = '' } = data;
|
|
130
|
-
const headers = new Headers(request.headers || '');
|
|
131
|
-
let hash;
|
|
132
|
-
const decoded = `${username}:${password}`;
|
|
133
|
-
if (typeof Buffer === 'function' && typeof Buffer.from === 'function') {
|
|
134
|
-
hash = Buffer.from(decoded).toString('base64');
|
|
135
|
-
}
|
|
136
|
-
else {
|
|
137
|
-
hash = btoa(decoded);
|
|
138
|
-
}
|
|
139
|
-
headers.set('authorization', `Basic ${hash}`);
|
|
140
|
-
request.headers = headers.toString();
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
//# sourceMappingURL=RequestAuthorizationProcessor.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"RequestAuthorizationProcessor.js","sourceRoot":"","sources":["../../../../src/runtime/http-runner/RequestAuthorizationProcessor.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,OAAO,EAAE,MAAM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