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

Change login flow (experimental) #223

Merged
merged 4 commits into from
Jun 25, 2018
Merged

Change login flow (experimental) #223

merged 4 commits into from
Jun 25, 2018

Conversation

campionfellin
Copy link
Collaborator

This is the flow (more or less):

clasp login: logs you in using the default clasp credentials. Saves .clasprc.json to your ~ directory.
clasp login --creds: logs you in using the file credentials.json in the directory that you are running the command in. Saves .clasprc.json to your current directory.
clasp login --creds other_creds.json: logs you in using the credentials in other_creds.json. Saves .clasprc.json to your current directory.

Your credentials file should look like this:

{"installed":{"client_id":"239267426989-275aglft6htcfsdj7t5d3csdlogchamh.apps.googleusercontent.com","project_id":"project-id-xxxxxxxxx","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://accounts.google.com/o/oauth2/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"xxxxxxxx","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]}}

Signed-off-by: campionfellin campionfellin@gmail.com

Related to #204

  • npm run test succeeds.
  • npm run lint succeeds.
  • Appropriate changes to README are included in PR.

Signed-off-by: campionfellin <campionfellin@gmail.com>
@campionfellin
Copy link
Collaborator Author

Please test before merging.

@coveralls
Copy link

coveralls commented Jun 19, 2018

Coverage Status

Coverage decreased (-0.2%) to 15.507% when pulling 1c5f9fa on campionfellin:new-login into bd177b4 on google:master.

@campionfellin
Copy link
Collaborator Author

(also note that npm run docs did not work for some reason to update the docs with this)

@marcosscriven
Copy link

marcosscriven commented Jun 20, 2018

@campionfellin - would this help with #225? Would like to be able to use clasp to upload script from CI, without any interaction required.

@campionfellin
Copy link
Collaborator Author

For now, you still have to use the browser to confirm the login. What this does is allow you to authenticate with your own Oauth client, rather than the default for clasp. This helps with things like clasp run which need more permissions than the default.

@campionfellin
Copy link
Collaborator Author

@grant though this is related to #225 I think it belongs more with #204 and should be merged. We can continue discussion on #225 to see what to do next.

Copy link
Contributor

@grant grant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we update the README too? (Hand editing it is fine)
LGTM after nits and README.

src/auth.ts Outdated
oauth2ClientSettings.clientId = credentials.installed.client_id;
oauth2ClientSettings.clientSecret = credentials.installed.client_secret;
ownCreds = true;
console.log('Credentials found, using those to login.');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put all messages in LOG.

src/auth.ts Outdated
@@ -143,15 +154,15 @@ async function authorizeWithoutLocalhost() {
* Logs the user in. Saves the client credentials to an rc file.
* @param options the localhost and ownkey options from commander
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be cool if we documented the options.

* @param {object} options Login options
* @param {boolean} options.localhost ...
* @param {string} options.creds ...

src/auth.ts Outdated
@@ -53,10 +54,20 @@ export const drive = google.drive({
* @param {boolean} useLocalhost True if a local HTTP server should be run
* to handle the auth response. False if manual entry used.
*/
async function authorize(useLocalhost: boolean, writeToOwnKey: boolean) {
async function authorize(useLocalhost: boolean, creds: string) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we forgot to document @param {string} creds in a previous PR. Can we add it here?

src/auth.ts Outdated
let ownCreds = false;
try {
const credentials = JSON.parse(fs.readFileSync(creds, 'utf8'));
oauth2ClientSettings.clientId = credentials.installed.client_id;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably have some error handling to check if our credentials file is in the right format and has the right keys.

  • If credentials, credentials. installed, credentials. installed.client_id, credentials. installed.client_secret, then save them.
  • Otherwise, log an error and exit.

Signed-off-by: campionfellin <campionfellin@gmail.com>
@campionfellin
Copy link
Collaborator Author

Will update README in separate PR.

src/auth.ts Outdated
*/
async function authorize(useLocalhost: boolean, writeToOwnKey: boolean) {
async function authorize(useLocalhost: boolean, creds: any) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we never have any?
string|boolean?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typescript is not a fan 😔

[ts]
Argument of type 'string | false' is not assignable to parameter of type 'string | number | Buffer | URL'.
  Type 'false' is not assignable to type 'string | number | Buffer | URL'.

when trying to read the credentials file:

    const credentials = JSON.parse(fs.readFileSync(creds, 'utf8'));

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main reason for doing this is this line:

  creds = creds === true ? 'credentials.json' : creds;

and the reason for that was because if you put a default value in commander, it sets options.creds to that value whether or not the user entered in the --creds flag.

So I removed the default value from the commander side of things and use this line to set it to credentials.json by default.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you just do string and default creds outside the body (in the caller)?

There has to be some way to not use any.
This is an internal method too, so we have flexibility to our method signature.

Copy link
Contributor

@grant grant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't have any more any.
There's got some way to fix that, with stricter options, union, a generic or something.

src/auth.ts Outdated
async function authorize(useLocalhost: boolean, writeToOwnKey: boolean) {
async function authorize(useLocalhost: boolean, creds: any) {
let ownCreds = false;
creds = creds === true ? 'credentials.json' : creds;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never write === true
Just creds ? creds : '...';

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strings evaluate to true so when they enter --creds otherCredsFile.json then we would rename it to credentials.json unfortunately. I see the other comments and there is probably a way to do this without using this or any so I'll look closer at the commander documentation.

src/auth.ts Outdated
*/
async function authorize(useLocalhost: boolean, writeToOwnKey: boolean) {
async function authorize(useLocalhost: boolean, creds: any) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you just do string and default creds outside the body (in the caller)?

There has to be some way to not use any.
This is an internal method too, so we have flexibility to our method signature.

@campionfellin
Copy link
Collaborator Author

Ok, after some testing with it, I think the best solution is to remove the default value and make it so if the user uses --creds they must supply a credentials file. The reasoning is this:

If we default it to ./credentials.json, or anything really, when the user uses clasp login without specifying --creds, the options.creds variable is still set to ./credentials.json. Thus, if they have that file in their directory, but want to login globally with the default (old) clasp credentials, they can't.

@grant what do you think?

@grant
Copy link
Contributor

grant commented Jun 22, 2018

Agreed, that makes sense. Let's not default.

Signed-off-by: campionfellin <campionfellin@gmail.com>
@campionfellin
Copy link
Collaborator Author

Ok, now here is what happens:

clasp login : works as it used to, using the default clasp credentials.
clasp login --creds : gives error error: option '--creds <file>' argument missing
clasp login --creds creds.json (when creds.json exists and is formatted properly) : logs in user with those credentials, saves ./.clasprc.json
clasp login --creds creds.json (when creds.json doesn't exist): Credentials file not found.
clasp login --creds creds.json (when creds.json exists, but not right format) : Incorrect credentials file format.

@campionfellin
Copy link
Collaborator Author

Important to note when we update the readme:

If you use your own credentials from GCP, you must enable both the Apps Script API and Drive API.

@grant
Copy link
Contributor

grant commented Jun 22, 2018

It would be cool if you added @example with the comment you wrote above, so it lasts (here).

Signed-off-by: campionfellin <campionfellin@gmail.com>
@campionfellin
Copy link
Collaborator Author

Added simple examples, I think the more detailed "what happens if I do this..." should be in the README under troubleshooting.

@grant grant merged commit 0ce9d5d into google:master Jun 25, 2018
@grant
Copy link
Contributor

grant commented Jun 25, 2018

I'll try to release this week with these changes.

@campionfellin campionfellin deleted the new-login branch August 15, 2018 04:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants