balena-cli
Version:
The official balena Command Line Interface
166 lines (158 loc) • 6.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@oclif/core");
const lazy_1 = require("../../utils/lazy");
const errors_1 = require("../../errors");
class LoginCmd extends core_1.Command {
async run() {
const { flags: options, args: params } = await this.parse(LoginCmd);
const balena = (0, lazy_1.getBalenaSdk)();
const messages = await Promise.resolve().then(() => require('../../utils/messages'));
const balenaUrl = await balena.settings.get('balenaUrl');
if (options.user != null) {
options.email = options.user;
}
console.log(messages.balenaAsciiArt);
console.log(`\nLogging in to ${balenaUrl}`);
await this.doLogin(options, balenaUrl, params.token);
const whoamiResult = (await balena.auth.whoami());
if (whoamiResult.actorType !== 'user' && !options.hideExperimentalWarning) {
console.info((0, lazy_1.stripIndent) `
----------------------------------------------------------------------------------------
You are logging in with a ${whoamiResult.actorType} key.
This is an experimental feature and many features of the CLI might not work as expected.
We sure hope you know what you are doing.
----------------------------------------------------------------------------------------
`);
}
console.info(`Successfully logged in as: ${this.getLoggedInMessage(whoamiResult)}`);
console.info(`\
Find out about the available commands by running:
$ balena help
${messages.reachingOut}`);
}
getLoggedInMessage(whoami) {
if (whoami.actorType === 'user') {
return whoami.username;
}
const identifier = whoami.actorType === 'device' ? whoami.uuid : whoami.slug;
return `${whoami.actorType} ${identifier}`;
}
async doLogin(loginOptions, balenaUrl = 'balena-cloud.com', token) {
if (loginOptions.token) {
if (!token) {
token = await (0, lazy_1.getCliForm)().ask({
message: 'Session token or API key from the preferences page',
name: 'token',
type: 'input',
});
}
const balena = (0, lazy_1.getBalenaSdk)();
await balena.auth.loginWithToken(token);
try {
if (!(await balena.auth.whoami())) {
throw new errors_1.ExpectedError('Token authentication failed');
}
}
catch (err) {
if (process.env.DEBUG) {
console.error(`Get user info failed with: ${err.message}`);
}
throw new errors_1.ExpectedError('Token authentication failed');
}
return;
}
else if (loginOptions.credentials) {
const patterns = await Promise.resolve().then(() => require('../../utils/patterns'));
return patterns.authenticate(loginOptions);
}
else if (loginOptions.web) {
const auth = await Promise.resolve().then(() => require('../../auth'));
await auth.login({ port: loginOptions.port });
return;
}
else {
const patterns = await Promise.resolve().then(() => require('../../utils/patterns'));
const loginType = await patterns.askLoginType();
if (loginType === 'register') {
const open = await Promise.resolve().then(() => require('open'));
const signupUrl = `https://dashboard.${balenaUrl}/signup`;
await open(signupUrl, { wait: false });
throw new errors_1.ExpectedError(`Please sign up at ${signupUrl}`);
}
loginOptions[loginType] = true;
return this.doLogin(loginOptions);
}
}
}
LoginCmd.description = (0, lazy_1.stripIndent) `
Login to balena.
Login to your balena account.
This command will prompt you to login using the following login types:
- Web authorization: open your web browser and prompt to authorize the CLI
from the dashboard.
- Credentials: using email/password and 2FA.
- Token: using a session token or API key from the preferences page.
`;
LoginCmd.examples = [
'$ balena login',
'$ balena login --web',
'$ balena login --token "..."',
'$ balena login --credentials',
'$ balena login --credentials --email johndoe@gmail.com --password secret',
];
LoginCmd.args = {
token: core_1.Args.string({
hidden: true,
}),
};
LoginCmd.flags = {
web: core_1.Flags.boolean({
default: false,
char: 'w',
description: 'web-based login',
exclusive: ['token', 'credentials'],
}),
token: core_1.Flags.boolean({
default: false,
char: 't',
description: 'session token or API key',
exclusive: ['web', 'credentials'],
}),
credentials: core_1.Flags.boolean({
default: false,
char: 'c',
description: 'credential-based login',
exclusive: ['web', 'token'],
}),
email: core_1.Flags.string({
char: 'e',
description: 'email',
exclusive: ['user'],
dependsOn: ['credentials'],
}),
user: core_1.Flags.string({
char: 'u',
hidden: true,
exclusive: ['email'],
dependsOn: ['credentials'],
}),
password: core_1.Flags.string({
char: 'p',
description: 'password',
dependsOn: ['credentials'],
}),
port: core_1.Flags.integer({
char: 'P',
description: 'TCP port number of local HTTP login server (--web auth only)',
dependsOn: ['web'],
}),
hideExperimentalWarning: core_1.Flags.boolean({
char: 'H',
default: false,
description: 'Hides warning for experimental features',
}),
};
LoginCmd.primary = true;
exports.default = LoginCmd;
//# sourceMappingURL=index.js.map