@api-client/core 0.8.15 → 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/HttpProject.js +19 -4
- package/build/src/models/HttpProject.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/HttpProject.ts +18 -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
|
@@ -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,8BAA8B,CAAC;AAEvD;;;GAGG;AACH,MAAM,OAAO,6BAA6B;IAExC;;;;;;;;;;;OAWG;IACH,MAAM,CAAC,gBAAgB,CAAC,OAAqB,EAAE,aAAuC;QACpF,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,aAAa,CAAC,IAAI,CAAC,aAAa,CAAC,MAAM,EAAE;YAC1D,OAAO,OAAO,CAAC;SAChB;QAED,KAAK,MAAM,IAAI,IAAI,aAAa,EAAE;YAChC,IAAI,IAAI,CAAC,OAAO,KAAK,KAAK,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE;gBAC1C,SAAS;aACV;YACD,QAAQ,IAAI,CAAC,IAAI,EAAE;gBACjB,KAAK,OAAO;oBAAE,IAAI,CAAC,gBAAgB,CAAC,OAAO,EAAE,IAAI,CAAC,MAA6B,CAAC,CAAC;oBAAC,MAAM;gBACxF,KAAK,SAAS;oBAAE,IAAI,CAAC,aAAa,CAAC,OAAO,EAAE,IAAI,CAAC,MAA8B,CAAC,CAAC;oBAAC,MAAM;gB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QACvC,OAAO,CAAC,OAAO,GAAG,OAAO,CAAC,QAAQ,EAAE,CAAC;IACvC,CAAC;IAED;;;;;;;;OAQG;IACO,MAAM,CAAC,yBAAyB,CAAC,OAAqB,EAAE,IAAyB;QACzF,MAAM,EAAE,QAAQ,GAAC,EAAE,EAAE,QAAQ,GAAC,EAAE,EAAE,GAAG,IAAI,CAAC;QAC1C,MAAM,OAAO,GAAG,IAAI,OAAO,CAAC,OAAO,CAAC,OAAO,IAAI,EAAE,CAAC,CAAC;QACnD,IAAI,IAAY,CAAC;QACjB,MAAM,OAAO,GAAG,GAAG,QAAQ,IAAI,QAAQ,EAAE,CAAC;QAC1C,IAAI,OAAO,MAAM,KAAK,UAAU,IAAI,OAAO,MAAM,CAAC,IAAI,KAAK,UAAU,EAAE;YACrE,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;SAChD;aAAM;YACL,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,CAAC;SACtB;QACD,OAAO,CAAC,GAAG,CAAC,eAAe,EAAE,SAAS,IAAI,EAAE,CAAC,CAAC;QAC9C,OAAO,CAAC,OAAO,GAAG,OAAO,CAAC,QAAQ,EAAE,CAAC;IACvC,CAAC;CACF"}
|
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
import { IHttpRequest } from '../../models/HttpRequest.js';
|
|
2
|
-
import { IRequestAuthorization } from '../../models/RequestAuthorization.js';
|
|
3
|
-
import { IBearerAuthorization, IBasicAuthorization, IOidcAuthorization, IOAuth2Authorization, ICCAuthorization } from '../../models/Authorization.js';
|
|
4
|
-
import { HttpCertificate } from '../../models/ClientCertificate.js';
|
|
5
|
-
import { Headers } from '../../lib/headers/Headers.js';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Applies authorization data to the HttpRequest from
|
|
9
|
-
* request authorization configuration.
|
|
10
|
-
*/
|
|
11
|
-
export class RequestAuthorizationProcessor {
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Applies the auth data from the authorization config.
|
|
15
|
-
*
|
|
16
|
-
* Note, this mutates the original request. Make a copy of you don't want to change
|
|
17
|
-
* the values in the source request.
|
|
18
|
-
*
|
|
19
|
-
* Note, this does not process client certificates. Use the `#readCertificates()` method to
|
|
20
|
-
* get a certificate to use with the HTTP request,
|
|
21
|
-
*
|
|
22
|
-
* @param request The request to apply the authorization to.
|
|
23
|
-
* @returns The same request (a reference)
|
|
24
|
-
*/
|
|
25
|
-
static setAuthorization(request: IHttpRequest, authorization?: IRequestAuthorization[]): IHttpRequest {
|
|
26
|
-
if (!Array.isArray(authorization) || !authorization.length) {
|
|
27
|
-
return request;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
for (const auth of authorization) {
|
|
31
|
-
if (auth.enabled === false || !auth.config) {
|
|
32
|
-
continue;
|
|
33
|
-
}
|
|
34
|
-
switch (auth.type) {
|
|
35
|
-
case 'basic': this.processBasicAuth(request, auth.config as IBasicAuthorization); break;
|
|
36
|
-
case 'oauth 2': this.processOAuth2(request, auth.config as IOAuth2Authorization); break;
|
|
37
|
-
case 'open id': this.processOpenId(request, auth.config as IOidcAuthorization); break;
|
|
38
|
-
case 'bearer': this.processBearer(request, auth.config as IBearerAuthorization); break;
|
|
39
|
-
default:
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
return request;
|
|
43
|
-
}
|
|
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?: IRequestAuthorization[]): HttpCertificate | undefined {
|
|
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 as ICCAuthorization;
|
|
60
|
-
return init.certificate;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Injects basic auth header into the request headers.
|
|
65
|
-
*/
|
|
66
|
-
protected static processBasicAuth(request: IHttpRequest, config: IBasicAuthorization): void {
|
|
67
|
-
const { username } = config;
|
|
68
|
-
if (!username) {
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
this.applyRequestBasicAuthData(request, config);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Injects oauth 2 auth header into the request headers.
|
|
76
|
-
*/
|
|
77
|
-
protected static processOAuth2(request: IHttpRequest, config: IOAuth2Authorization): void {
|
|
78
|
-
const { accessToken, tokenType='Bearer', deliveryMethod='header', deliveryName='authorization' } = config;
|
|
79
|
-
if (!accessToken) {
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
82
|
-
const value = `${tokenType} ${accessToken}`;
|
|
83
|
-
if (deliveryMethod === 'header') {
|
|
84
|
-
const headers = new Headers(request.headers || '');
|
|
85
|
-
headers.append(deliveryName, value);
|
|
86
|
-
request.headers = headers.toString();
|
|
87
|
-
} else if (deliveryMethod === 'query') {
|
|
88
|
-
const { url } = request;
|
|
89
|
-
try {
|
|
90
|
-
const parsed = new URL(url);
|
|
91
|
-
parsed.searchParams.append(deliveryName, value);
|
|
92
|
-
request.url = parsed.toString();
|
|
93
|
-
} catch (e) {
|
|
94
|
-
// ...
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Injects OpenID Connect auth header into the request headers.
|
|
101
|
-
*/
|
|
102
|
-
protected static processOpenId(request: IHttpRequest, config: IOidcAuthorization): void {
|
|
103
|
-
const { accessToken } = config;
|
|
104
|
-
if (accessToken) {
|
|
105
|
-
this.processOAuth2(request, config);
|
|
106
|
-
}
|
|
107
|
-
// todo - if AT is missing find the current token from the tokens list in the passed configuration.
|
|
108
|
-
// Currently the authorization method UI sets the token when the requests is generated so it's not as much important.
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Injects bearer auth header into the request headers.
|
|
113
|
-
*/
|
|
114
|
-
protected static processBearer(request: IHttpRequest, config: IBearerAuthorization): void {
|
|
115
|
-
const { token } = config;
|
|
116
|
-
const value = `Bearer ${token}`;
|
|
117
|
-
const headers = new Headers(request.headers || '');
|
|
118
|
-
headers.append('authorization', value);
|
|
119
|
-
request.headers = headers.toString();
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Applies the basic authorization data to the request.
|
|
124
|
-
*
|
|
125
|
-
* If the header value have changed then it fires `request-headers-changed` custom event.
|
|
126
|
-
* It sets computed value of the readers to the event's detail object.
|
|
127
|
-
*
|
|
128
|
-
* @param request The event's detail object. Changes made here will be propagated to the event.
|
|
129
|
-
* @param data The authorization data to apply.
|
|
130
|
-
*/
|
|
131
|
-
protected static applyRequestBasicAuthData(request: IHttpRequest, data: IBasicAuthorization): void {
|
|
132
|
-
const { username='', password='' } = data;
|
|
133
|
-
const headers = new Headers(request.headers || '');
|
|
134
|
-
let hash: string;
|
|
135
|
-
const decoded = `${username}:${password}`;
|
|
136
|
-
if (typeof Buffer === 'function' && typeof Buffer.from === 'function') {
|
|
137
|
-
hash = Buffer.from(decoded).toString('base64');
|
|
138
|
-
} else {
|
|
139
|
-
hash = btoa(decoded);
|
|
140
|
-
}
|
|
141
|
-
headers.set('authorization', `Basic ${hash}`);
|
|
142
|
-
request.headers = headers.toString();
|
|
143
|
-
}
|
|
144
|
-
}
|