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

Add captcha support #446

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Add captcha support #446

wants to merge 3 commits into from

Conversation

xolox
Copy link
Contributor

@xolox xolox commented Dec 30, 2018

Context: As explained in #445 I integrated hangups in my chat-archive program a few months ago. Back then (August 2018) hangups worked fine for me, but a few weeks ago the authentication started failing and try as I might I could not get it to work again 🙁.

Disclaimer: Initially I had assumed I'd done something stupid when integrating hangups (even though it had worked before) but using python -m hangups.auth helped to confirm that my authentication problems had nothing to do with the integration between hangups and chat-archive.

Analysis:

  • Tonight I decided to dive in and find out what was going wrong, which ended up with me in an interactive debugger stepping through _get_authorization_code() and dumping the response HTML to a temporary file so I could see what was going on 😇.
  • I found out that after the password submission a captcha challenge was being presented. Because hangups didn't handle this response it failed to select the mandatory TOTP challenge and as a result the exception hangups.auth.GoogleAuthError: Authorization code cookie not found was raised.

Resolution:

  • When I found out that the captcha challenge was to blame for my problems I resolved to create a pull request for hangups that would at the very least inform the user via logging that authentication was failing due to missing captcha support.
  • A quick pdb session convinced me that it wouldn't be that hard to actually add support for captcha images instead of just logging a message, because I didn't see another reasonable[1] way to get hangups to successfully connect again.

[1] When I say "reasonable" I'm thinking about the potential audience of my chat-archive program, whom I'm not comfortable asking to open a pdb prompt or use the "Web Developer Tools" to extract a cookie from a browser session.

Expectation management:

I expect that hangups is used in a lot of different contexts (CLI, GUI, headless?) and I guess the webbrowser.open() call might be deemed inappropriate, however:

  1. The webbrowser.open() call is located inside CredentialsPrompt so that this behavior can easily be overridden by extending.

  2. The captcha image URL is logged so that the user can manually open it if the use of webbrowser.open() doesn't work.

  3. AFAICT when the captcha challenge is presented there was formerly (before this pull request) no way for hangups to ever finish successfully, so in that sense backwards compatibility shouldn't be a concern.

If there's problems with my current implementation that need to be resolved before this can be merged, feel free to let me know, because I'd love to see this get merged. While this new functionality is likely to be fragile, right now it's definitely an added value for me 🙂.

Copy link
Owner

@tdryer tdryer left a comment

Choose a reason for hiding this comment

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

Can you extend the tests in test_auth.py to cover this?

Google previously announced that they're going to require JavaScript for logins, so at some point in the future this is going to break.

try:
logger.info('Detected captcha, opening image in browser: %s', url)
webbrowser.open(url)
except Exception as e:
Copy link
Owner

Choose a reason for hiding this comment

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

Looks like webbrowser.open will just return False if it can't open a browser.

prompts the user to enter the captcha text.
"""
try:
logger.info('Detected captcha, opening image in browser: %s', url)
Copy link
Owner

Choose a reason for hiding this comment

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

It might be better to print this instead of logging it, so the URL appears on the screen in case we can't open a browser.

@@ -319,6 +342,19 @@ def _get_authorization_code(session, credentials_prompt):
password = credentials_prompt.get_password()
browser.submit_form(FORM_SELECTOR, {PASSWORD_SELECTOR: password})

if browser.has_selector(CAPTCHA_SELECTOR):
for image in browser._page.soup.select('div.captcha-img img'):
Copy link
Owner

Choose a reason for hiding this comment

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

Static analysis is complaining about this private attribute access. We could add a Browser.select method instead.

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.

2 participants