@sap/cli-core
Version:
Command-Line Interface (CLI) Core Module
31 lines (30 loc) • 1.56 kB
JavaScript
import { get as getConfig } from "../../../../../config/index.js";
import { AuthenticationMethod, OPTION_ACCESS_TOKEN, } from "../../../../../constants.js";
import { get } from "../../../../../logger/index.js";
import { getOptionValueFromConfigGracefully } from "../../../../../utils/options.js";
import { checkOptions } from "../../../options/utils.js";
import { setAuthenticationMethod, setTargetHost } from "../../../utils.js";
import { setAuthorization } from "../../utils.js";
import { SecretsStorageSingleton } from "../../../../../cache/secrets/SecretsStorageSingleton.js";
export const updateAuthorization = async () => {
const { info: logInfo, debug } = get("commands.handler.authentication.oauth.tokenProvider.setAuthorization");
logInfo("setting authorization using access token");
const config = getConfig();
const secrets = await SecretsStorageSingleton.SINGLETON.getDefaultSecret();
const accessToken = secrets.access_token ||
getOptionValueFromConfigGracefully(OPTION_ACCESS_TOKEN);
if (accessToken) {
debug("access token found");
setAuthorization({ authorization: `Bearer ${accessToken}` });
setTargetHost(`${config.tenantUrl}`);
setAuthenticationMethod(AuthenticationMethod.oauth);
}
else {
debug("no access token found");
throw new Error("cannot set authorization: no access token found in configuration");
}
};
export const create = () => async (command) => {
await checkOptions(OPTION_ACCESS_TOKEN, command);
return updateAuthorization;
};