-
Notifications
You must be signed in to change notification settings - Fork 189
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
base: master
Are you sure you want to change the base?
Add captcha support #446
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
import logging | ||
import platform | ||
import urllib.parse | ||
import webbrowser | ||
|
||
import mechanicalsoup | ||
import requests | ||
|
@@ -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"]' | ||
|
@@ -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) | ||
webbrowser.open(url) | ||
except Exception as e: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like |
||
logger.warning('Failed to open captcha image in browser! (%s)', e) | ||
return input('Captcha text: ') | ||
|
||
|
||
class RefreshTokenCache(object): | ||
"""File-based cache for refresh token. | ||
|
@@ -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'): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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): | ||
|
@@ -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: | ||
|
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 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.