Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
34 changes: 11 additions & 23 deletions src/utils/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>} 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)
Expand All @@ -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 {
Expand Down