diff --git a/package-lock.json b/package-lock.json index cab5bb3..b605317 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,7 @@ "dependencies": { "@adobe/aem-headless-client-js": "^3.1.1", "@adobe/aemcs-api-client-lib": "git+https://github.com/adobe/aemcs-api-client-lib.git#main", + "@adobe/aio-lib-core-config": "^2.0.1", "@adobe/aio-lib-core-logging": "^1.2.0", "@adobe/aio-lib-core-networking": "^2.0.0" }, diff --git a/package.json b/package.json index 6aaa738..f79f5c1 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "dependencies": { "@adobe/aem-headless-client-js": "^3.1.1", "@adobe/aemcs-api-client-lib": "git+https://github.com/adobe/aemcs-api-client-lib.git#main", + "@adobe/aio-lib-core-config": "^2.0.1", "@adobe/aio-lib-core-networking": "^2.0.0", "@adobe/aio-lib-core-logging": "^1.2.0" }, diff --git a/src/utils/auth.js b/src/utils/auth.js index 5d7ec84..fe5561d 100644 --- a/src/utils/auth.js +++ b/src/utils/auth.js @@ -9,37 +9,25 @@ OF ANY KIND, either express or implied. See the License for the specific languag governing permissions and limitations under the License. */ -const fs = require('fs') -const path = require('path') +const config = require('@adobe/aio-lib-core-config') const exchange = require('@adobe/aemcs-api-client-lib') const { ErrorCodes } = require('@adobe/aem-headless-client-js') -const { AUTH_FILE_READ_ERROR, AUTH_FILE_PARSE_ERROR, EXCHANGE_TOKEN_ERROR } = ErrorCodes +const { AUTH_FILE_PARSE_ERROR, EXCHANGE_TOKEN_ERROR } = ErrorCodes const loggerNamespace = 'aem-headless-client-nodejs' const logger = require('@adobe/aio-lib-core-logging')(loggerNamespace, { level: process.env.LOG_LEVEL }) /** * Returns a Promise that resolves with a credentials JSON data. * - * @param {string} credentialsFilePath - credentials config file path (serviceToken or devToken content) + * @param {string} aioConfigKey - aio config key * @returns {Promise} the response body wrapped inside a Promise */ -async function getToken (credentialsFilePath) { - let authFileContent = '' +async function getToken (aioConfigKey) { + const configString = config.get(aioConfigKey) + let serviceToken = null try { - const filePath = path.isAbsolute(credentialsFilePath) ? credentialsFilePath : path.join(process.cwd(), credentialsFilePath) - authFileContent = fs.readFileSync(filePath, 'utf8') - logger.debug('auth file read successfully') - } catch (error) { - logger.debug('auth file read error', error) - throw new AUTH_FILE_READ_ERROR({ - messageValues: error.message - }) - } - - let config = null - try { - config = JSON.parse(authFileContent) + serviceToken = JSON.parse(configString) logger.debug('auth file parsed successfully') } catch (error) { logger.debug('auth file parse error', error) @@ -48,16 +36,16 @@ async function getToken (credentialsFilePath) { }) } - if (config.accessToken) { - // If config has DEV token + if (!serviceToken) { + // Treat config as a DEV token return { - accessToken: config.accessToken, + accessToken: configString, type: 'Bearer', expires: 24 * 60 * 60 * 1000 } } - return exchange(config) + return exchange(serviceToken) .then(data => { logger.debug('exchange token success') return {