11/**
22 * @license
3- * Copyright (c) 2021, 2024 , Oracle and/or its affiliates.
3+ * Copyright (c) 2021, 2023 , Oracle and/or its affiliates.
44 * Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
55 */
66const CredentialEncryptor = require ( './credentialEncryptor' ) ;
@@ -9,6 +9,7 @@ const { getLogger } = require('./wktLogging');
99
1010const DECRYPTION_FAILED_STRING = 'Unsupported state or unable to authenticate data' ;
1111
12+ /* global Buffer */
1213class CredentialManager {
1314 constructor ( credentialStorePolicy ) {
1415 this . credentialStorePolicy = credentialStorePolicy ;
@@ -56,17 +57,63 @@ class CredentialManager {
5657 refObj . reference = { } ;
5758 }
5859 const cipherText = refObj . reference [ refObj . field ] ;
59- if ( cipherText ) {
60- refObj . reference [ refObj . field ] = await this . manager . loadCredential ( jsonPath , cipherText ) ;
61- } else {
62- logger . debug ( 'Failed to load credential from %s so skipping it' , jsonPath ) ;
63- }
60+ refObj . reference [ refObj . field ] = await this . manager . loadCredential ( jsonPath , cipherText ) ;
6461 }
6562 }
6663 return Promise . resolve ( loadedProject ) ;
6764 }
6865}
6966
67+ class CredentialStoreManager extends CredentialManager {
68+ constructor ( projectGuid ) {
69+ super ( 'native' ) ;
70+ const CredentialStore = require ( './credentialStore' ) ;
71+ 72+ this . projectGuid = projectGuid ;
73+ this . credentialStore = new CredentialStore ( ) ;
74+ super . credentialManager = this ;
75+ }
76+ 77+ get credentialStoreType ( ) {
78+ return super . credentialStoreType ;
79+ }
80+ 81+ async storeCredentials ( project ) {
82+ return super . storeCredentials ( project ) ;
83+ }
84+ 85+ async loadCredentials ( project ) {
86+ return super . loadCredentials ( project ) ;
87+ }
88+ 89+ async storeCredential ( jsonPath , clearText ) {
90+ return new Promise ( ( resolve , reject ) => {
91+ if ( clearText ) {
92+ this . credentialStore . storeCredential ( this . _getCredentialName ( jsonPath ) , clearText )
93+ . then ( ( ) => resolve ( ) )
94+ . catch ( err => reject ( new Error ( `Failed to save credential for ${ jsonPath } : ${ err } ` ) ) ) ;
95+ } else {
96+ this . credentialStore . deleteCredential ( this . _getCredentialName ( jsonPath ) )
97+ . then ( ( ) => resolve ( ) )
98+ . catch ( err => reject ( new Error ( `Failed to delete empty credential for ${ jsonPath } : ${ err } ` ) ) ) ;
99+ }
100+ } ) ;
101+ }
102+ 103+ // eslint-disable-next-line no-unused-vars
104+ async loadCredential ( jsonPath , cipherText ) {
105+ return new Promise ( ( resolve , reject ) => {
106+ this . credentialStore . getCredential ( this . _getCredentialName ( jsonPath ) )
107+ . then ( clearText => resolve ( clearText ) )
108+ . catch ( err => reject ( new Error ( `Failed to load credential for ${ jsonPath } : ${ err } ` ) ) ) ;
109+ } ) ;
110+ }
111+ 112+ _getCredentialName ( jsonPath ) {
113+ return Buffer . from ( `${ this . projectGuid } :${ jsonPath } ` , 'utf8' ) . toString ( 'base64' ) ;
114+ }
115+ }
116+ 70117class EncryptedCredentialManager extends CredentialManager {
71118 static BAD_PASSPHRASE_KEY = 'Incorrect passphrase' ;
72119
@@ -152,5 +199,6 @@ class CredentialNoStoreManager extends CredentialManager {
152199
153200module . exports = {
154201 CredentialNoStoreManager,
202+ CredentialStoreManager,
155203 EncryptedCredentialManager
156204} ;
0 commit comments