@teamwork/get-bearer-token
Version:
CLI tool to obtain bearer tokens for Teamwork API using OAuth flow
167 lines (139 loc) ⢠5.63 kB
JavaScript
import { Command } from "commander";
import chalk from "chalk";
import { getCredentials, getPort } from "./config.js";
import { performOAuthFlow } from "./oauth.js";
const program = new Command();
program
.name("teamwork-auth")
.description("CLI tool to obtain bearer tokens for Teamwork API")
.version("1.0.0");
program
.command("auth")
.description("Authenticate with Teamwork and get a bearer token")
.option("-p, --port <port>", "Port for the callback server", "3000")
.option("--no-open", "Don't automatically open the browser")
.action(async (options) => {
try {
console.log(chalk.bold.blue("š Teamwork Bearer Token CLI\n"));
// Get port (from options or find available)
const port = parseInt(options.port) || (await getPort());
// Get credentials (passing the port for redirect URI suggestion)
const credentials = await getCredentials(port);
// Perform OAuth flow
const result = await performOAuthFlow(credentials, port);
// Display results
console.log(chalk.green.bold("\nš Authentication successful!\n"));
console.log(chalk.cyan("š¤ User Information:"));
console.log(
` Name: ${result.userInfo.given_name} ${result.userInfo.family_name}`
);
console.log(` Email: ${result.userInfo.email}`);
console.log(` Company: ${result.installation.company.name}`);
console.log(` Region: ${result.installation.region}\n`);
console.log(chalk.cyan("š API Information:"));
console.log(` API Endpoint: ${result.apiEndpoint}`);
console.log(` Installation ID: ${result.installation.id}\n`);
console.log(chalk.yellow.bold("š Your Bearer Token:"));
console.log(chalk.green(` ${result.accessToken}\n`));
console.log(chalk.cyan("š” Usage Example:"));
console.log(
chalk.gray(
' curl -H "Authorization: Bearer ' + result.accessToken + '" \\'
)
);
console.log(
chalk.gray(' "' + result.apiEndpoint + 'projects.json"\n')
);
} catch (error) {
if (error.isTtyError || error.message.includes("canceled")) {
console.log(chalk.yellow("\nā ļø Process cancelled by user."));
process.exit(0);
}
console.error(chalk.red.bold("\nā Authentication failed:"));
console.error(chalk.red(` ${error.message}\n`));
if (error.message.includes("EADDRINUSE")) {
console.log(
chalk.yellow("š” Try using a different port with --port option")
);
} else if (error.message.includes("timeout")) {
console.log(
chalk.yellow(
"š” The authentication process timed out. Please try again."
)
);
} else if (error.message.includes("Token exchange failed")) {
console.log(
chalk.yellow(
"š” Please check your client credentials in the Teamwork Developer Portal"
)
);
}
process.exit(1);
}
});
// If no command was provided, run the default auth flow
if (!process.argv[2]) {
(async () => {
try {
console.log(chalk.bold.blue("š Teamwork Bearer Token CLI\n"));
// Get available port first
const port = await getPort();
// Get credentials (passing the port for redirect URI suggestion)
const credentials = await getCredentials(port);
// Perform OAuth flow
const result = await performOAuthFlow(credentials, port);
// Display results
console.log(chalk.green.bold("\nš Authentication successful!\n"));
console.log(chalk.cyan("š¤ User Information:"));
console.log(
` Name: ${result.userInfo.given_name} ${result.userInfo.family_name}`
);
console.log(` Email: ${result.userInfo.email}`);
console.log(` Company: ${result.installation.company.name}`);
console.log(` Region: ${result.installation.region}\n`);
console.log(chalk.cyan("š API Information:"));
console.log(` API Endpoint: ${result.apiEndpoint}`);
console.log(` Installation ID: ${result.installation.id}\n`);
console.log(chalk.yellow.bold("š Your Bearer Token:"));
console.log(chalk.green(` ${result.accessToken}\n`));
console.log(chalk.cyan("š” Usage Example:"));
console.log(
chalk.gray(
' curl -H "Authorization: Bearer ' + result.accessToken + '" \\'
)
);
console.log(
chalk.gray(' "' + result.apiEndpoint + 'projects.json"\n')
);
} catch (error) {
if (error.isTtyError || error.message.includes("canceled")) {
console.log(chalk.yellow("\nā ļø Process cancelled by user."));
process.exit(0);
}
console.error(chalk.red.bold("\nā Authentication failed:"));
console.error(chalk.red(` ${error.message}\n`));
if (error.message.includes("EADDRINUSE")) {
console.log(
chalk.yellow("š” Try using a different port with --port option")
);
} else if (error.message.includes("timeout")) {
console.log(
chalk.yellow(
"š” The authentication process timed out. Please try again."
)
);
} else if (error.message.includes("Token exchange failed")) {
console.log(
chalk.yellow(
"š” Please check your client credentials in the Teamwork Developer Portal"
)
);
}
process.exit(1);
}
})();
} else {
// Parse command line arguments only if there are arguments
program.parse();
}