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

Fix issue where CLI commands making more than one request to rubygems.org needing an OTP code would crash or ask for the code twice #4162

Merged
merged 2 commits into from
Dec 17, 2020

Conversation

sonalkr132
Copy link
Member

What was the end-user or developer problem that led to this PR?

Fixes failed commands with access update or fist invocation (no local API key):

$ gem yank somegem -v 0.0.1
Don't have an account yet? Create one at http://localhost:3000/sign_up
   Email:   some@rubygems.org
  Password:   
You have enabled multi-factor authentication. Please enter OTP code.
Code:   320292
Added yank_rubygem scope to the existing API key
You have enabled multifactor authentication but no OTP code provided.
Please fill it and retry.

What is your fix for the problem, implemented in this PR?

reuse otp passed by the user in api request retry

Make sure the following tasks are checked

Fixes failed commands with access updates:
```
You have enabled multi-factor authentication. Please enter OTP code.
Code:   320292
Added yank_rubygem scope to the existing API key
You have enabled multifactor authentication but no OTP code provided.
Please fill it and retry.
```
@sonalkr132
Copy link
Member Author

The first invocation has one more quirk, which exists in previous versions as well:

Don't have an account yet? Create one at http://localhost:3000/sign_up
   Email:   some@rubygems.org
Password:   

You have enabled multi-factor authentication. Please enter OTP code.
Code:   569043
Signed in.
Pushing gem to http://localhost:3000...
You have enabled multi-factor authentication. Please enter OTP code.
Code:   569043
Successfully registered gem: sometsomeee (3.10.50.15)

It asks for OTP twice because sign_in and real execution are separate invocations(1). Is it okay to use options[:otp] as an instance variable, which will be set in sign_in?

@deivid-rodriguez
Copy link
Member

deivid-rodriguez commented Dec 16, 2020

It asks for OTP twice because sign_in and real execution are separate invocations(1). Is it okay to use options[:otp] as an instance variable, which will be set in sign_in?

It seems ok to me!

Something I don't understand is how the scope is getting updated in the current version if it needs an otp code too?

@sonalkr132
Copy link
Member Author

update scope was promoting for otp, through rubygems_api_request. This version has one fewer prompt (and request) for update scope case.

@deivid-rodriguez
Copy link
Member

Aahhhh right, sorry, I get it now.

Copy link
Member

@deivid-rodriguez deivid-rodriguez left a comment

Choose a reason for hiding this comment

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

This looks good to me!

Another quirk I see (I haven't tried it but I think it happes from reading the code) is that if --opt is given on the command line, the given code will be used for sign_in, but then other requests will still ask for an otp code, right?

If we implement your instance variable suggestion, we're it's filled up lazily from either options[:otp] or asking the user, I think both cases would be fixed?

In any case, this PR keeps existing behaviour and fixes an issue, so if you want to merge this now and fix the other cases later, I'm good with it!

@deivid-rodriguez
Copy link
Member

@sonalkr132 Not sure if you're still there, but if you give me the OK now, I'll sneak this into the 3.2.2 release.

@sonalkr132
Copy link
Member Author

the given code will be used for sign_in, but then other requests will still ask for an otp code, right?

not really. for them the otp is passed from the command file itself (1). if mfa_unauthorized?(response) is basically fallback.

RUBYGEMS_HOST="https://staging.rubygems.org" gem push sometsomeee-3.10.50.15.gem  --otp 487877
Enter your https://staging.rubygems.org credentials.
Don't have an account yet? Create one at https://staging.rubygems.org/sign_up
   Email:   aditya.prakash132@gmail.com
Password:   

Signed in.
Pushing gem to https://staging.rubygems.org...
Successfully registered gem: sometsomeee (3.10.50.15)

options[:otp] gets shared between sigin_in and actual invocation
of the command, which previously prompting user twice to enter otp.

we can't use `request.add_field` for OTP as it appends values.
assert_match "Password:", @ui.output
assert_match "Signed in with API key:", @ui.output
assert_match response_success, @ui.output
assert_equal '11111', @fetcher.last_request['OTP']
Copy link
Member Author

Choose a reason for hiding this comment

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

this assert fails with nil without b09e932

Copy link
Member

@deivid-rodriguez deivid-rodriguez left a comment

Choose a reason for hiding this comment

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

Looks good to me @sonalkr132!

@deivid-rodriguez
Copy link
Member

deivid-rodriguez commented Dec 17, 2020

I'd like to rename the PR to something more close to the user problem being fixed, maybe: "Fix issue where CLI commands making more than one request to rubygems.org needing an OTP code would crash or ask for the code twice"?

@deivid-rodriguez
Copy link
Member

Anyways, merging.

Thanks for fixing this and improving the code all around!

@deivid-rodriguez deivid-rodriguez merged commit ac12b74 into rubygems:master Dec 17, 2020
@sonalkr132 sonalkr132 deleted the reuse-otp branch December 17, 2020 10:25
@deivid-rodriguez
Copy link
Member

To clarify why I'd like to rename the PR, our scripts use the PR title to generate the changelog, so the title of the PR is what the users will see when checking the changelog. So the idea would be that PR titles are end user focused. No big deal though!

deivid-rodriguez added a commit that referenced this pull request Dec 17, 2020
Reuse otp passed by user in api request retry

(cherry picked from commit ac12b74)
@sonalkr132 sonalkr132 changed the title Reuse otp passed by user in api request retry Fix issue where CLI commands making more than one request to rubygems.org needing an OTP code would crash or ask for the code twice Dec 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants