@@ -12,52 +12,77 @@ governing permissions and limitations under the License.
1212const { getToken, context } = require ( '@adobe/aio-lib-ims' )
1313const { CLI } = require ( '@adobe/aio-lib-ims/src/context' )
1414const { getCliEnv } = require ( '@adobe/aio-lib-env' )
15- const defaultRuntimeUrl = 'https://adobeioruntime.net'
15+ const defaultDeployServiceUrl = 'https://deploy-service.app-builder.adp.adobe.io'
16+ const aioLogger = require ( '@adobe/aio-lib-core-logging' ) ( '@adobe/aio-cli-plugin-app:auth-helper' , { provider : 'debug' } )
17+
18+ /**
19+ * Retrieves an access token for Adobe I/O CLI authentication.
20+ * This function handles both CLI and custom contexts, setting up the appropriate
21+ * authentication context and retrieving the corresponding access token.
22+ *
23+ * @async
24+ * @function getAccessToken
25+ * @param {object } [options] - Options for token retrieval
26+ * @param {boolean } [options.useCachedToken=false] - Whether to use a cached token instead of requesting a new one
27+ * @returns {Promise<{accessToken: string|null, env: string}> } An object containing:
28+ * - accessToken: The retrieved access token for authentication, or null if token retrieval failed
29+ * - env: The current CLI environment (e.g. 'prod', 'stage')
30+ * @throws {Error } If token retrieval fails or context setup fails
31+ */
32+ async function getAccessToken ( { useCachedToken = false } = { } ) {
33+ const env = getCliEnv ( )
34+ aioLogger . debug ( `Retrieving CLI Token using env=${ env } ` )
35+
36+ let contextName = CLI // default
37+ const currentContext = await context . getCurrent ( ) // potential override
38+ if ( currentContext !== CLI ) {
39+ contextName = currentContext
40+ } else {
41+ await context . setCli ( { 'cli.bare-output' : true } , false ) // set this globally
42+ }
43+
44+ let accessToken = null
45+ if ( useCachedToken ) {
46+ const contextConfig = await context . get ( contextName )
47+ accessToken = contextConfig ?. access_token ?. token
48+ } else {
49+ accessToken = await getToken ( contextName )
50+ }
51+
52+ return { accessToken, env }
53+ }
1654
1755/**
1856 * For use with the openwhisk client js library to send a bearer token instead of basic
1957 * auth to the openwhisk service. Set this to the auth_handler option when initializing
2058 */
2159const bearerAuthHandler = {
2260 getAuthHeader : async ( ) => {
23- await context . setCli ( { 'cli.bare-output' : true } , false ) // set this globally
24-
25- const env = getCliEnv ( )
26-
27- console . debug ( `Retrieving CLI Token using env=${ env } ` )
28- const accessToken = await getToken ( CLI )
61+ const { accessToken } = await getAccessToken ( )
2962
3063 return `Bearer ${ accessToken } `
3164 }
3265}
3366
3467const setRuntimeApiHostAndAuthHandler = ( config ) => {
35- // TODO: remove this check once the deploy service is enabled by default
36- if ( process . env . IS_DEPLOY_SERVICE_ENABLED === 'true' ) {
37- const aioConfig = ( config && 'runtime' in config ) ? config : null
38- if ( aioConfig ) {
39- aioConfig . runtime . apihost = process . env . AIO_RUNTIME_APIHOST ?? defaultRuntimeUrl
40- aioConfig . runtime . auth_handler = bearerAuthHandler
41- return aioConfig
42- }
43- const owConfig = ( config && 'ow' in config ) ? config : null
44- if ( owConfig ) {
45- owConfig . ow . apihost = process . env . AIO_RUNTIME_APIHOST ?? defaultRuntimeUrl
46- owConfig . ow . auth_handler = bearerAuthHandler
47- return owConfig
48- }
49- } else {
50- if ( config && config . runtime ) {
51- config . runtime . apihost = process . env . AIO_RUNTIME_APIHOST ?? defaultRuntimeUrl
52- }
53- if ( config && config . ow ) {
54- config . ow . apihost = process . env . AIO_RUNTIME_APIHOST ?? defaultRuntimeUrl
55- }
68+ const aioConfig = ( config && 'runtime' in config ) ? config : null
69+ if ( aioConfig ) {
70+ const apiEndpoint = process . env . AIO_DEPLOY_SERVICE_URL ?? defaultDeployServiceUrl
71+ aioConfig . runtime . apihost = `${ apiEndpoint } /runtime`
72+ aioConfig . runtime . auth_handler = bearerAuthHandler
73+ return aioConfig
74+ }
75+ const owConfig = ( config && 'ow' in config ) ? config : null
76+ if ( owConfig ) {
77+ const apiEndpoint = process . env . AIO_DEPLOY_SERVICE_URL ?? defaultDeployServiceUrl
78+ owConfig . ow . apihost = `${ apiEndpoint } /runtime`
79+ owConfig . ow . auth_handler = bearerAuthHandler
80+ return owConfig
5681 }
57- return config
5882}
5983
6084module . exports = {
85+ getAccessToken,
6186 bearerAuthHandler,
6287 setRuntimeApiHostAndAuthHandler
6388}
0 commit comments