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

Do not convert to bytes the refresh_token #3273

Merged
merged 6 commits into from
Nov 20, 2017

Conversation

humitos
Copy link
Member

@humitos humitos commented Nov 15, 2017

When the body is encoded, a mix of bytes and unicode happens and it
fails at this line

https://github.com/requests/requests-oauthlib/blob/c472e6bf0468f1bd52b70a64e9aac92bee41c491/requests_oauthlib/oauth2_session.py#L285

(Pdb) body
u'grant_type=refresh_token&allow_redirects=True&refresh_token=b%27z9BSRKqf2QD4pcDhqj%27&client_id=CLIENT_ID&client_secret=CLIENT_SECRET'

(note those weird %27 in the string)

Closes #2993
Closes #2992

When the body is encoded, a mix of bytes and unicode happens and it
fails at this line

https://github.com/requests/requests-oauthlib/blob/c472e6bf0468f1bd52b70a64e9aac92bee41c491/requests_oauthlib/oauth2_session.py#L285

(Pdb) body
u'grant_type=refresh_token&allow_redirects=True&refresh_token=b%27z9BSRKqf2QD4pcDhqj%27&client_id=CLIENT_ID&client_secret=CLIENT_SECRET'

(note those weird `%27` in the string)
@ericholscher
Copy link
Member

Can we add some logging around this to catch failures better in the future?

@humitos
Copy link
Member Author

humitos commented Nov 15, 2017

I think we should connect the logs from this module, since it's already logging the body: https://github.com/requests/requests-oauthlib/blob/c472e6bf0468f1bd52b70a64e9aac92bee41c491/requests_oauthlib/oauth2_session.py#L287

@ericholscher
Copy link
Member

I mean something that would catch the exception/error that would presumably happen on invalid refresh token? The full body isn't gonna help us catch errors, just spam logs :)

@agjohnson
Copy link
Contributor

Ideally, we should still solve the issue of a failure here not updating the GitHub repos that were successfully returned.

@humitos
Copy link
Member Author

humitos commented Nov 16, 2017

I mean something that would catch the exception/error that would presumably happen on invalid refresh token?

I think I will need more directions here. I mean, I can catch the exception as you say but then?

I updated this PR with the solution that I imagine you want :)

The self.get_session is used in a couple of places (sync repos, setup and update webhooks) but in the other cases it seems that the execption is properly handled. So, this is the one that was missing.

Ideally, we should still solve the issue of a failure here not updating the GitHub repos that were successfully returned.

I need to take a look in deep at this (each sync is ran in a separate celery task or the same?). I suppose that it's the same, otherwise it shouldn't break the other service. If this is the case, I think I solved it by returning [] if something fails. Let me know, since I will need to apply the same logic at https://github.com/rtfd/readthedocs.org/blob/f5d3a8aae4a5338b02899e2b9f6ca90df738cd75/readthedocs/oauth/services/github.py#L151-L163 (maybe I can create a comon paginate method that handle this and a _paginate in each subclass)

@agjohnson
Copy link
Contributor

Correct, all providers are synced in the same task. I am confused why catching these exceptions was even needed now though. Most of the calls in the oauth services are themselves wrapped in try catch, and ultimately raise an exception to the user.

That is, we want to raise Exception('You should reconnect your account'), which would raise the error to the user -- at least in theory. I haven't tested this.

`paginate` method is shared between both services and each service
implement `get_next_url_to_paginate` and `get_paginated_results`.
@humitos
Copy link
Member Author

humitos commented Nov 20, 2017

I just refactored the code to handle the exception in both services. For that I created a common paginate method in the base class and implement two small methods in each of the subclasses.

If something goes wrong, it returns [] as results so other services requests are not affected by the one that it's failing.

@agjohnson can you take a look again at this PR?

@agjohnson
Copy link
Contributor

This looks great 👍

The abstraction for paginate is a lot nicer now. We'll want to watch for more errors here, but I think the error reporting back should be fine here.

@agjohnson agjohnson merged commit e737d78 into master Nov 20, 2017
@agjohnson agjohnson deleted the humitos/bitbucket/refresh_token branch November 20, 2017 17:00
kakulukia added a commit to kakulukia/readthedocs.org that referenced this pull request Nov 23, 2017
* 'master' of github.com:rtfd/readthedocs.org:
  Add GLOBAL_PIP_CACHE setting (readthedocs#3299)
  Remove invalid attribute `exception` (readthedocs#3298)
  Update build from database at __exit__ (readthedocs#3292)
  Extra way to check OOM of a command ran inside Docker (readthedocs#3294)
  Add supervisord contrib example (readthedocs#3262)
  Allow promos to be placed in the docs footer (readthedocs#3267)
  Do not convert to bytes the `refresh_token` (readthedocs#3273)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants