Skip to content

Commit d955bda

Browse files
ruchevitsmrin9
authored andcommitted
persist auth, if enabled
1 parent 39d4de8 commit d955bda

File tree

5 files changed

+51
-1
lines changed

5 files changed

+51
-1
lines changed

docs/specs/temp.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ paths:
1515
type: object
1616
$ref: "#/components/schemas/person"
1717
components:
18+
securitySchemes:
19+
JWT:
20+
type: http
21+
scheme: bearer
22+
bearerFormat: JWT
23+
ApiKey:
24+
type: apiKey
25+
in: header
26+
name: API_TOKEN
1827
schemas:
1928
person:
2029
type: object

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
allow-authentication = "true"
4949
update-route="false"
5050
match-type="regex"
51+
persist-auth="true"
5152
></rapi-doc>
5253
<!--
5354
<rapi-doc-mini

src/rapidoc-mini.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export default class RapiDocMini extends LitElement {
4949
responseAreaHeight: { type: String, attribute: 'response-area-height' },
5050
showSummaryWhenCollapsed: { type: String, attribute: 'show-summary-when-collapsed' },
5151
fillRequestFieldsWithExample: { type: String, attribute: 'fill-request-fields-with-example' },
52+
persistAuth: { type: String, attribute: 'persist-auth' },
5253

5354
// Schema Styles
5455
schemaStyle: { type: String, attribute: 'schema-style' },
@@ -166,6 +167,7 @@ export default class RapiDocMini extends LitElement {
166167
if (!this.schemaExpandLevel || this.schemaExpandLevel < 1) { this.schemaExpandLevel = 99999; }
167168
if (!this.schemaDescriptionExpanded || !'true, false,'.includes(`${this.schemaDescriptionExpanded},`)) { this.schemaDescriptionExpanded = 'false'; }
168169
if (!this.fillRequestFieldsWithExample || !'true, false,'.includes(`${this.fillRequestFieldsWithExample},`)) { this.fillRequestFieldsWithExample = 'true'; }
170+
if (!this.persistAuth || !'true, false,'.includes(`${this.persistAuth},`)) { this.persistAuth = 'false'; }
169171
if (!this.responseAreaHeight) { this.responseAreaHeight = '300px'; }
170172

171173
if (!this.allowTry || !'true, false,'.includes(`${this.allowTry},`)) { this.allowTry = 'true'; }

src/rapidoc.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export default class RapiDoc extends LitElement {
6363
defaultSchemaTab: { type: String, attribute: 'default-schema-tab' },
6464
responseAreaHeight: { type: String, attribute: 'response-area-height' },
6565
fillRequestFieldsWithExample: { type: String, attribute: 'fill-request-fields-with-example' },
66+
persistAuth: { type: String, attribute: 'persist-auth' },
6667
onNavTagClick: { type: String, attribute: 'on-nav-tag-click' },
6768

6869
// Schema Styles
@@ -434,6 +435,7 @@ export default class RapiDoc extends LitElement {
434435
this.schemaHideReadOnly += ['get', 'head', 'delete', 'options'];
435436
this.schemaHideWriteOnly = this.schemaHideWriteOnly !== 'never';
436437
if (!this.fillRequestFieldsWithExample || !'true, false,'.includes(`${this.fillRequestFieldsWithExample},`)) { this.fillRequestFieldsWithExample = 'true'; }
438+
if (!this.persistAuth || !'true, false,'.includes(`${this.persistAuth},`)) { this.persistAuth = 'false'; }
437439
if (!this.onNavTagClick || !'expand-collapse, show-description,'.includes(`${this.onNavTagClick},`)) { this.onNavTagClick = 'expand-collapse'; }
438440
if (!this.responseAreaHeight) {
439441
this.responseAreaHeight = '300px';

src/templates/security-scheme-template.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { marked } from 'marked';
66
const codeVerifier = '731DB1C3F7EA533B85E29492D26AA-1234567890-1234567890';
77
const codeChallenge = '4FatVDBJKPAo4JgLLaaQFMUcQPn5CrPRvLlaob9PTYc'; // Base64 encoded SHA-256
88

9+
const localStorageKey = 'rapidoc';
10+
911
export function applyApiKey(securitySchemeId, username = '', password = '', providedApikeyVal = '') {
1012
const securityObj = this.resolvedSpec.securitySchemes?.find((v) => (v.securitySchemeId === securitySchemeId));
1113
if (!securityObj) {
@@ -17,6 +19,7 @@ export function applyApiKey(securitySchemeId, username = '', password = '', prov
1719
finalApiKeyValue = `Basic ${btoa(`${username}:${password}`)}`;
1820
}
1921
} else if (providedApikeyVal) {
22+
securityObj.value = providedApikeyVal;
2023
finalApiKeyValue = `${securityObj.scheme?.toLowerCase() === 'bearer' ? 'Bearer ' : ''}${providedApikeyVal}`;
2124
}
2225
if (finalApiKeyValue) {
@@ -37,6 +40,14 @@ export function onClearAllApiKeys() {
3740
this.requestUpdate();
3841
}
3942

43+
function getPersistedApiKeys() {
44+
return JSON.parse(localStorage.getItem(localStorageKey)) || {};
45+
}
46+
47+
function setPersistedApiKeys(obj) {
48+
localStorage.setItem(localStorageKey, JSON.stringify(obj));
49+
}
50+
4051
function onApiKeyChange(securitySchemeId) {
4152
let apiKeyValue = '';
4253
const securityObj = this.resolvedSpec.securitySchemes.find((v) => (v.securitySchemeId === securitySchemeId));
@@ -51,6 +62,11 @@ function onApiKeyChange(securitySchemeId) {
5162
apiKeyValue = trEl.querySelector('.api-key-input').value.trim();
5263
applyApiKey.call(this, securitySchemeId, '', '', apiKeyValue);
5364
}
65+
if (this.persistAuth === 'true') {
66+
const rapidocLs = getPersistedApiKeys.call(this);
67+
rapidocLs[securitySchemeId] = securityObj;
68+
setPersistedApiKeys.call(this, rapidocLs);
69+
}
5470
}
5571
}
5672
}
@@ -339,8 +355,28 @@ function oAuthFlowTemplate(flowName, clientId, clientSecret, securitySchemeId, a
339355
`;
340356
}
341357

358+
function removeApiKey(securitySchemeId) {
359+
const securityObj = this.resolvedSpec.securitySchemes?.find((v) => (v.securitySchemeId === securitySchemeId));
360+
securityObj.user = '';
361+
securityObj.password = '';
362+
securityObj.value = '';
363+
securityObj.finalKeyValue = '';
364+
if (this.persistAuth === 'true') {
365+
const rapidocLs = getPersistedApiKeys.call(this);
366+
delete rapidocLs[securityObj.securitySchemeId];
367+
setPersistedApiKeys.call(this, rapidocLs);
368+
}
369+
this.requestUpdate();
370+
}
371+
342372
export default function securitySchemeTemplate() {
343373
if (!this.resolvedSpec) { return ''; }
374+
if (this.persistAuth === 'true') {
375+
const rapidocLs = getPersistedApiKeys.call(this);
376+
Object.values(rapidocLs).forEach((p) => {
377+
applyApiKey.call(this, p.securitySchemeId, p.username, p.password, p.value);
378+
});
379+
}
344380
const providedApiKeys = this.resolvedSpec.securitySchemes?.filter((v) => (v.finalKeyValue));
345381
if (!providedApiKeys) {
346382
return;
@@ -369,7 +405,7 @@ export default function securitySchemeTemplate() {
369405
${v.finalKeyValue
370406
? html`
371407
<span class='blue-text'> ${v.finalKeyValue ? 'Key Applied' : ''} </span>
372-
<button class="m-btn thin-border small" part="btn btn-outline" @click=${() => { v.finalKeyValue = ''; this.requestUpdate(); }}>REMOVE</button>
408+
<button class="m-btn thin-border small" part="btn btn-outline" @click=${() => { removeApiKey.call(this, v.securitySchemeId); }}>REMOVE</button>
373409
`
374410
: ''
375411
}

0 commit comments

Comments
 (0)