Skip to content

Commit 5310710

Browse files
committed
fixes #482 - allow scope for non oauth security schemes
1 parent 4eaa864 commit 5310710

File tree

2 files changed

+30
-25
lines changed

2 files changed

+30
-25
lines changed

src/components/api-request.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,6 @@ export default class ApiRequest extends LitElement {
10571057
fetchUrl = this.path;
10581058
const fetchOptions = {
10591059
method: this.method.toUpperCase(),
1060-
headers: {},
10611060
};
10621061
// Generate URL using Path Params
10631062
pathParamEls.map((el) => {
@@ -1154,28 +1153,28 @@ export default class ApiRequest extends LitElement {
11541153
curlUrl = fetchUrl;
11551154
}
11561155
curl = `curl -X ${this.method.toUpperCase()} "${curlUrl}" \\\n`;
1157-
1156+
const reqHeaders = new Headers();
11581157
if (acceptHeader) {
11591158
// Uses the acceptHeader from Response panel
1160-
fetchOptions.headers.Accept = acceptHeader;
1159+
reqHeaders.append('Accept', acceptHeader);
11611160
curlHeaders += ` -H "Accept: ${acceptHeader}" \\\n`;
11621161
} else if (this.accept) {
1163-
fetchOptions.headers.Accept = this.accept;
1162+
reqHeaders.append('Accept', this.accept);
11641163
curlHeaders += ` -H "Accept: ${this.accept}" \\\n`;
11651164
}
11661165

11671166
// Add Authentication Header if provided
11681167
this.api_keys
11691168
.filter((v) => (v.in === 'header'))
11701169
.forEach((v) => {
1171-
fetchOptions.headers[v.name] = v.finalKeyValue;
1170+
reqHeaders.append(v.name, v.finalKeyValue);
11721171
curlHeaders += ` -H "${v.name}: ${v.finalKeyValue}" \\\n`;
11731172
});
11741173

11751174
// Add Header Params
11761175
headerParamEls.map((el) => {
11771176
if (el.value) {
1178-
fetchOptions.headers[el.dataset.pname] = el.value;
1177+
reqHeaders.append(el.dataset.pname, el.value);
11791178
curlHeaders += ` -H "${el.dataset.pname}: ${el.value}" \\\n`;
11801179
}
11811180
});
@@ -1272,7 +1271,7 @@ export default class ApiRequest extends LitElement {
12721271
// Common for all request-body
12731272
if (!requestBodyType.includes('form-data')) {
12741273
// For multipart/form-data dont set the content-type to allow creation of browser generated part boundaries
1275-
fetchOptions.headers['Content-Type'] = requestBodyType;
1274+
reqHeaders.append('Content-Type', requestBodyType);
12761275
}
12771276
curlHeaders += ` -H "Content-Type: ${requestBodyType}" \\\n`;
12781277
}
@@ -1291,6 +1290,7 @@ export default class ApiRequest extends LitElement {
12911290
if (this.fetchCredentials) {
12921291
fetchOptions.credentials = this.fetchCredentials;
12931292
}
1293+
fetchOptions.headers = reqHeaders;
12941294
const fetchRequest = new Request(fetchUrl, fetchOptions);
12951295
this.dispatchEvent(new CustomEvent('before-try', {
12961296
bubbles: true,

src/templates/security-scheme-template.js

+23-18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable arrow-body-style */
12
import { html } from 'lit-element';
23
import { unsafeHTML } from 'lit-html/directives/unsafe-html';
34
import marked from 'marked';
@@ -449,19 +450,18 @@ export function pathSecurityTemplate(pathSecurity) {
449450
pathSecurity.forEach((pSecurity) => {
450451
const andSecurityKeys1 = [];
451452
const andKeyTypes = [];
452-
let pathScopes = '';
453453
Object.keys(pSecurity).forEach((pathSecurityKey) => {
454+
let pathScopes = '';
454455
const s = this.resolvedSpec.securitySchemes.find((ss) => ss.securitySchemeId === pathSecurityKey);
455-
if (!pathScopes && Array.isArray(pSecurity[pathSecurityKey])) {
456+
if (pSecurity[pathSecurityKey] && Array.isArray(pSecurity[pathSecurityKey])) {
456457
pathScopes = pSecurity[pathSecurityKey].join(', ');
457458
}
458459
if (s) {
459460
andKeyTypes.push(s.typeDisplay);
460-
andSecurityKeys1.push(s);
461+
andSecurityKeys1.push({ ...s, ...({ scopes: pathScopes }) });
461462
}
462463
});
463464
orSecurityKeys1.push({
464-
pathScopes,
465465
securityTypes: andKeyTypes.length > 1 ? `${andKeyTypes[0]} + ${andKeyTypes.length - 1} more` : andKeyTypes[0],
466466
securityDefs: andSecurityKeys1,
467467
});
@@ -485,7 +485,20 @@ export function pathSecurityTemplate(pathSecurity) {
485485
<div class="tooltip-text" style="position:absolute; color: var(--fg); top:26px; right:0; border:1px solid var(--border-color);padding:2px 4px; display:block;">
486486
${orSecurityItem1.securityDefs.length > 1 ? html`<div>Requires <b>all</b> of the following </div>` : ''}
487487
<div style="padding-left: 8px">
488-
${orSecurityItem1.securityDefs.map((andSecurityItem, j) => html`
488+
${orSecurityItem1.securityDefs.map((andSecurityItem, j) => {
489+
const scopeHtml = html`${andSecurityItem.scopes !== ''
490+
? html`
491+
<div>
492+
<b>Required scopes:</b>
493+
<br/>
494+
<div style="margin-left:8px">
495+
${andSecurityItem.scopes.split(',').map((scope, cnt) => html`${cnt === 0 ? '' : '┃'}<span>${scope}</span>`)}
496+
</div>
497+
</div>`
498+
: ''
499+
}`;
500+
501+
return html`
489502
${andSecurityItem.type === 'oauth2'
490503
? html`
491504
<div>
@@ -494,31 +507,23 @@ export function pathSecurityTemplate(pathSecurity) {
494507
: 'Needs'
495508
}
496509
OAuth Token <span style="font-family:var(--font-mono); color:var(--primary-color);">${andSecurityItem.securitySchemeId}</span> in <b>Authorization header</b>
497-
${orSecurityItem1.pathScopes !== ''
498-
? html`
499-
<div>
500-
<b>Required scopes:</b>
501-
<br/>
502-
<div style="margin-left:8px">
503-
${orSecurityItem1.pathScopes.split(',').map((scope, cnt) => html`${cnt === 0 ? '' : '┃'}<span>${scope}</span>`)}
504-
</div>
505-
</div>`
506-
: ''
507-
}
510+
${scopeHtml}
508511
</div>`
509512
: andSecurityItem.type === 'http'
510513
? html`
511514
<div>
512515
${orSecurityItem1.securityDefs.length > 1 ? html`<b>${j + 1}.</b> &nbsp;` : html`Requires`}
513516
${andSecurityItem.scheme === 'basic' ? 'Base 64 encoded username:password' : 'Bearer Token'} in <b>Authorization header</b>
517+
${scopeHtml}
514518
</div>`
515519
: html`
516520
<div>
517521
${orSecurityItem1.securityDefs.length > 1 ? html`<b>${j + 1}.</b> &nbsp;` : html`Requires`}
518522
Token in <b>${andSecurityItem.name} ${andSecurityItem.in}</b>
523+
${scopeHtml}
519524
</div>`
520-
}
521-
`)}
525+
}`;
526+
})}
522527
</div>
523528
</div>
524529
</div>

0 commit comments

Comments
 (0)