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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions hangups/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import logging
import platform
import urllib.parse
import webbrowser

import mechanicalsoup
import requests
Expand Down Expand Up @@ -47,6 +48,7 @@
FORM_SELECTOR = '#gaia_loginform'
EMAIL_SELECTOR = '#Email'
PASSWORD_SELECTOR = '#Passwd'
CAPTCHA_SELECTOR = '#logincaptcha'
VERIFICATION_FORM_SELECTOR = '#challenge'
TOTP_CHALLENGE_SELECTOR = '[action="/signin/challenge/totp/2"]'
PHONE_CHALLENGE_SELECTOR = '[action="/signin/challenge/ipp/4"]'
Expand Down Expand Up @@ -130,6 +132,27 @@ def get_authorization_code():
print(MANUAL_LOGIN_INSTRUCTIONS)
return input('Authorization code: ')

@staticmethod
def get_captcha_text(url):
"""Prompt for captcha text.

Args:
url (str): The captcha image URL.

Returns:
str: Captcha text.

This method automatically opens the captcha image URL in a web browser
using the :mod:`webbrowser` module (exceptions are ignored) and then
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.

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.

logger.warning('Failed to open captcha image in browser! (%s)', e)
return input('Captcha text: ')


class RefreshTokenCache(object):
"""File-based cache for refresh token.
Expand Down Expand Up @@ -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.

captcha_text = credentials_prompt.get_captcha_text(
image.attrs['src']
)
browser.submit_form(FORM_SELECTOR, {
CAPTCHA_SELECTOR: captcha_text,
PASSWORD_SELECTOR: password,
})
break
else:
logger.warning('Detected captcha but failed to extract image!')

if browser.has_selector(TOTP_CHALLENGE_SELECTOR):
browser.submit_form(TOTP_CHALLENGE_SELECTOR, {})
elif browser.has_selector(PHONE_CHALLENGE_SELECTOR):
Expand All @@ -331,9 +367,9 @@ def _get_authorization_code(session, credentials_prompt):
input_selector = PHONE_CODE_SELECTOR
else:
raise GoogleAuthError('Unknown verification code input')
verfification_code = credentials_prompt.get_verification_code()
verification_code = credentials_prompt.get_verification_code()
browser.submit_form(
VERIFICATION_FORM_SELECTOR, {input_selector: verfification_code}
VERIFICATION_FORM_SELECTOR, {input_selector: verification_code}
)

try:
Expand Down