Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(auth): obtain oauth authorization from command line #299

Merged
merged 2 commits into from
Apr 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions src/commands/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const express = require('express');
const inquirer = require('inquirer');
const path = require('path');
const ora = require('ora');
const http = require('http');
const { nanoid } = require('nanoid');

class OAuthLoginCommand extends BoxCommand {
Expand All @@ -26,7 +27,6 @@ class OAuthLoginCommand extends BoxCommand {
this.info(
chalk`{cyan If you are not using the quickstart guide to set up ({underline https://developer.box.com/guides/tooling/cli/quick-start/}) then go to the Box Developer console ({underline https://cloud.app.box.com/developers/console}) and:}`
);

this.info(
chalk`{cyan 1. Select an application with OAuth user authentication method. Create a new Custom App if needed.}`
);
Expand Down Expand Up @@ -134,14 +134,33 @@ class OAuthLoginCommand extends BoxCommand {
state,
redirect_uri: redirectUri,
});

open(authorizeUrl);

if (flags.code) {
this.info(
chalk`{green Please open ${authorizeUrl} to authorize the application and read parameters from the URL accessed after authorization.}`
);
this.info(
chalk`{yellow If you are not seeing your code, please make sure you have set up the Redirect URI.}`
);
const authInfo = await inquirer.prompt([
{
type: 'input',
name: 'code',
message: 'What is your auth code? (code=)',
},
{
type: 'input',
name: 'state',
message: 'What is your state code? (state=)',
},
]);
http.get("http://localhost:" + port + "/callback?state=" + authInfo.state + "&code=" + authInfo.code)
} else {
open(authorizeUrl);
this.info(
chalk`{yellow If you are redirect to files view, please make sure that your Redirect URI is set up correctly and restart the login command.}`
);
}
await new Promise((resolve) => setTimeout(resolve, 1000));

this.info(
chalk`{yellow If you are redirect to files view, please make sure that your Redirect URI is set up correctly and restart the login command.}`
);
}
}

Expand All @@ -152,6 +171,11 @@ OAuthLoginCommand.description = 'Sign in with OAuth and set a new environment';

OAuthLoginCommand.flags = {
...BoxCommand.minFlags,
code: flags.boolean({
char: 'c',
description: 'Manually visit authorize URL and input code',
default: false,
}),
name: flags.string({
char: 'n',
description: 'Set a name for the environment',
Expand Down