@@ -6,6 +6,8 @@ import { marked } from 'marked';
6
6
const codeVerifier = '731DB1C3F7EA533B85E29492D26AA-1234567890-1234567890' ;
7
7
const codeChallenge = '4FatVDBJKPAo4JgLLaaQFMUcQPn5CrPRvLlaob9PTYc' ; // Base64 encoded SHA-256
8
8
9
+ const localStorageKey = 'rapidoc' ;
10
+
9
11
export function applyApiKey ( securitySchemeId , username = '' , password = '' , providedApikeyVal = '' ) {
10
12
const securityObj = this . resolvedSpec . securitySchemes ?. find ( ( v ) => ( v . securitySchemeId === securitySchemeId ) ) ;
11
13
if ( ! securityObj ) {
@@ -17,6 +19,7 @@ export function applyApiKey(securitySchemeId, username = '', password = '', prov
17
19
finalApiKeyValue = `Basic ${ btoa ( `${ username } :${ password } ` ) } ` ;
18
20
}
19
21
} else if ( providedApikeyVal ) {
22
+ securityObj . value = providedApikeyVal ;
20
23
finalApiKeyValue = `${ securityObj . scheme ?. toLowerCase ( ) === 'bearer' ? 'Bearer ' : '' } ${ providedApikeyVal } ` ;
21
24
}
22
25
if ( finalApiKeyValue ) {
@@ -37,6 +40,14 @@ export function onClearAllApiKeys() {
37
40
this . requestUpdate ( ) ;
38
41
}
39
42
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
+
40
51
function onApiKeyChange ( securitySchemeId ) {
41
52
let apiKeyValue = '' ;
42
53
const securityObj = this . resolvedSpec . securitySchemes . find ( ( v ) => ( v . securitySchemeId === securitySchemeId ) ) ;
@@ -51,6 +62,11 @@ function onApiKeyChange(securitySchemeId) {
51
62
apiKeyValue = trEl . querySelector ( '.api-key-input' ) . value . trim ( ) ;
52
63
applyApiKey . call ( this , securitySchemeId , '' , '' , apiKeyValue ) ;
53
64
}
65
+ if ( this . persistAuth === 'true' ) {
66
+ const rapidocLs = getPersistedApiKeys . call ( this ) ;
67
+ rapidocLs [ securitySchemeId ] = securityObj ;
68
+ setPersistedApiKeys . call ( this , rapidocLs ) ;
69
+ }
54
70
}
55
71
}
56
72
}
@@ -339,8 +355,28 @@ function oAuthFlowTemplate(flowName, clientId, clientSecret, securitySchemeId, a
339
355
` ;
340
356
}
341
357
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
+
342
372
export default function securitySchemeTemplate ( ) {
343
373
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
+ }
344
380
const providedApiKeys = this . resolvedSpec . securitySchemes ?. filter ( ( v ) => ( v . finalKeyValue ) ) ;
345
381
if ( ! providedApiKeys ) {
346
382
return ;
@@ -369,7 +405,7 @@ export default function securitySchemeTemplate() {
369
405
${ v . finalKeyValue
370
406
? html `
371
407
< 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 >
373
409
`
374
410
: ''
375
411
}
0 commit comments