Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 4 additions & 2 deletions src/commands/app/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const {
const rtLib = require('@adobe/aio-lib-runtime')
const LogForwarding = require('../../lib/log-forwarding')
const { sendAppAssetsDeployedAuditLog, sendAppDeployAuditLog } = require('../../lib/audit-logger')
const { setRuntimeApiHostAndAuthHandler, getAccessToken } = require('../../lib/auth-helper')
const { setRuntimeApiHostAndAuthHandler, getAccessToken, getTokenData } = require('../../lib/auth-helper')
const logActions = require('../../lib/log-actions')

const PRE_DEPLOY_EVENT_REG = 'pre-deploy-event-reg'
Expand Down Expand Up @@ -68,6 +68,8 @@ class Deploy extends BuildCommand {

if (cliDetails?.accessToken) {
try {
// store user id from token data for cdn deploy audit metadata
Comment thread
purplecabbage marked this conversation as resolved.
appInfo.auditUserId = getTokenData(cliDetails.accessToken)?.user_id
// send audit log at start (don't wait for deployment to finish)
await sendAppDeployAuditLog({
accessToken: cliDetails?.accessToken,
Expand Down Expand Up @@ -131,7 +133,7 @@ class Deploy extends BuildCommand {
for (let i = 0; i < keys.length; ++i) {
const k = keys[i]
const v = setRuntimeApiHostAndAuthHandler(values[i])

v.auditUserId = appInfo.auditUserId
Copy link

Copilot AI Oct 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The auditUserId property is being added directly to the config object returned by setRuntimeApiHostAndAuthHandler, which modifies the cloned config structure. Consider adding this property within setRuntimeApiHostAndAuthHandler by passing auditUserId as a parameter, or document this side-effect clearly to maintain consistency in how config objects are modified.

Copilot uses AI. Check for mistakes.
await this.deploySingleConfig({ name: k, config: v, originalConfig: values[i], flags, spinner })
if (cliDetails?.accessToken && v.app.hasFrontend && flags['web-assets']) {
const opItems = getFilesCountWithExtension(v.web.distProd)
Expand Down
14 changes: 14 additions & 0 deletions src/lib/auth-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,22 @@ const setRuntimeApiHostAndAuthHandler = (_config) => {
}
}

/**
* Decodes a JWT token and returns its payload as a JavaScript object.
*
* @function getTokenData
* @param {string} token - The JWT token to decode
* @returns {object} The decoded payload of the JWT token
* @throws
*/
const getTokenData = (token) => {
const [, payload] = token.split('.', 3)
Comment thread
purplecabbage marked this conversation as resolved.
Outdated
return JSON.parse(Buffer.from(payload, 'base64'))
Comment thread
purplecabbage marked this conversation as resolved.
Outdated
Comment thread
purplecabbage marked this conversation as resolved.
Outdated
Comment thread
purplecabbage marked this conversation as resolved.
Outdated
}

module.exports = {
getAccessToken,
getTokenData,
bearerAuthHandler,
setRuntimeApiHostAndAuthHandler
}
13 changes: 12 additions & 1 deletion test/commands/lib/auth-helper.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { getAccessToken, bearerAuthHandler, setRuntimeApiHostAndAuthHandler } = require('../../../src/lib/auth-helper')
const { getAccessToken, bearerAuthHandler, setRuntimeApiHostAndAuthHandler, getTokenData } = require('../../../src/lib/auth-helper')
const { getToken, context } = require('@adobe/aio-lib-ims')
const { CLI } = require('@adobe/aio-lib-ims/src/context')
const { getCliEnv } = require('@adobe/aio-lib-env')
Expand Down Expand Up @@ -57,6 +57,17 @@ describe('getAccessToken', () => {
})
})

describe('getTokenData', () => {
test('should decode JWT token and return payload', () => {
// Example JWT token with payload: {"user_id":"12345","name":"Test User"}
const exampleToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiMTIzNDUiLCJuYW1lIjoiVGVzdCBVc2VyIn0.sflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c'

const result = getTokenData(exampleToken)

expect(result).toEqual({ user_id: '12345', name: 'Test User' })
})
})

describe('bearerAuthHandler', () => {
beforeEach(() => {
jest.clearAllMocks()
Expand Down