|
| 1 | +/* |
| 2 | +Copyright 2019 Adobe Inc. All rights reserved. |
| 3 | +This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. You may obtain a copy |
| 5 | +of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +
|
| 7 | +Unless required by applicable law or agreed to in writing, software distributed under |
| 8 | +the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 9 | +OF ANY KIND, either express or implied. See the License for the specific language |
| 10 | +governing permissions and limitations under the License. |
| 11 | +*/ |
| 12 | + |
| 13 | +const { Flags } = require('@oclif/core') |
| 14 | + |
| 15 | +const { PropertyDefault } = require('./properties') |
| 16 | +const runtimeLib = require('@adobe/aio-lib-runtime') |
| 17 | +const { getToken, context, CLI } = require('@adobe/aio-lib-ims') |
| 18 | +const { getCliEnv } = require('@adobe/aio-lib-env') |
| 19 | +const RuntimeBaseCommand = require('./RuntimeBaseCommand') |
| 20 | + |
| 21 | +class DeployServiceCommand extends RuntimeBaseCommand { |
| 22 | +/** |
| 23 | + * Retrieves an access token for Adobe I/O CLI authentication. |
| 24 | + * This function handles both CLI and custom contexts, setting up the appropriate |
| 25 | + * authentication context and retrieving the corresponding access token. |
| 26 | + * |
| 27 | + * @async |
| 28 | + * @function getAccessToken |
| 29 | + * @param {object} [options] - Options for token retrieval |
| 30 | + * @param {string} [options.env] - The environment to use (e.g. 'prod', 'stage') |
| 31 | + * @param {boolean} [options.useCachedToken] - Whether to use a cached token instead of requesting a new one |
| 32 | + * @returns {Promise<{accessToken: string|null, env: string}>} An object containing: |
| 33 | + * - accessToken: The retrieved access token for authentication, or null if token retrieval failed |
| 34 | + * - env: The current CLI environment |
| 35 | + * @throws {Error} If token retrieval fails or context setup fails |
| 36 | + */ |
| 37 | + async getAccessToken ({ env = getCliEnv(), useCachedToken = false } = {}) { |
| 38 | + let contextName = CLI // default |
| 39 | + const currentContext = await context.getCurrent() // potential override |
| 40 | + |
| 41 | + if (currentContext !== CLI) { |
| 42 | + contextName = currentContext |
| 43 | + } else { |
| 44 | + await context.setCli({ 'cli.bare-output': true }, false) // set this globally |
| 45 | + } |
| 46 | + |
| 47 | + let accessToken = null |
| 48 | + if (useCachedToken) { |
| 49 | + const contextConfig = await context.get(contextName) |
| 50 | + accessToken = contextConfig?.access_token?.token |
| 51 | + } else { |
| 52 | + accessToken = await getToken(contextName) |
| 53 | + } |
| 54 | + |
| 55 | + return { accessToken, env } |
| 56 | + } |
| 57 | + |
| 58 | + getAuthHandler () { |
| 59 | + const env = getCliEnv() |
| 60 | + return { |
| 61 | + getAuthHeader: async () => { |
| 62 | + this.debug(`Retrieving CLI Token using env=${env}`) |
| 63 | + const { accessToken } = await this.getAccessToken({ env }) |
| 64 | + |
| 65 | + return `Bearer ${accessToken}` |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + async setRuntimeApiHostAndAuthHandler (options) { |
| 71 | + let _options = structuredClone(options) |
| 72 | + if (!_options?.['use-runtime-auth']) { |
| 73 | + const endpoint = process.env.AIO_DEPLOY_SERVICE_URL ?? PropertyDefault.DEPLOYSERVICEURL |
| 74 | + _options = _options ?? {} |
| 75 | + _options.apihost = `${endpoint}/runtime` |
| 76 | + _options.auth_handler = this.getAuthHandler() |
| 77 | + } |
| 78 | + |
| 79 | + return _options |
| 80 | + } |
| 81 | + |
| 82 | + async wsk (options) { |
| 83 | + let _options = structuredClone(options) |
| 84 | + if (!_options) { |
| 85 | + _options = await super.getOptions() |
| 86 | + _options = await this.setRuntimeApiHostAndAuthHandler(_options) |
| 87 | + } |
| 88 | + return runtimeLib.init(_options) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +DeployServiceCommand.flags = { |
| 93 | + ...RuntimeBaseCommand.flags, |
| 94 | + 'use-runtime-auth': Flags.boolean({ char: 'r', description: 'use Runtime auth [default: false]', default: false }) |
| 95 | +} |
| 96 | + |
| 97 | +module.exports = DeployServiceCommand |
0 commit comments