-
Notifications
You must be signed in to change notification settings - Fork 438
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
Conversation
Signed-off-by: campionfellin <[email protected]>
Please test before merging. |
(also note that |
@campionfellin - would this help with #225? Would like to be able to use clasp to upload script from CI, without any interaction required. |
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 |
There was a problem hiding this 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.'); |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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 <[email protected]>
Will update README in separate PR. |
src/auth.ts
Outdated
*/ | ||
async function authorize(useLocalhost: boolean, writeToOwnKey: boolean) { | ||
async function authorize(useLocalhost: boolean, creds: any) { |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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'));
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this 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; |
There was a problem hiding this comment.
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 : '...'
;
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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.
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 If we default it to @grant what do you think? |
Agreed, that makes sense. Let's not default. |
Signed-off-by: campionfellin <[email protected]>
Ok, now here is what happens:
|
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. |
It would be cool if you added |
Signed-off-by: campionfellin <[email protected]>
Added simple examples, I think the more detailed "what happens if I do this..." should be in the README under troubleshooting. |
I'll try to release this week with these changes. |
This is the flow (more or less):
clasp login
: logs you in using the defaultclasp
credentials. Saves.clasprc.json
to your~
directory.clasp login --creds
: logs you in using the filecredentials.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 inother_creds.json
. Saves.clasprc.json
to your current directory.Your credentials file should look like this:
Signed-off-by: campionfellin [email protected]
Related to #204
npm run test
succeeds.npm run lint
succeeds.