Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
except requests.RequestException as e:
raise GoogleAuthError('OAuthLogin request failed: {}'.format(e))
uberauth = r.text
try:
r = session.get(('https://accounts.google.com/MergeSession?'
'service=mail&'
'continue=http://www.google.com&uberauth={}')
.format(uberauth), headers=headers)
r.raise_for_status()
except requests.RequestException as e:
raise GoogleAuthError('MergeSession request failed: {}'.format(e))
cookies = session.cookies.get_dict(domain='.google.com')
if cookies == {}:
raise GoogleAuthError('Failed to find session cookies')
return cookies
password = credentials_prompt.get_password()
browser.submit_form(FORM_SELECTOR, {PASSWORD_SELECTOR: password})
if browser.has_selector(TOTP_CHALLENGE_SELECTOR):
browser.submit_form(TOTP_CHALLENGE_SELECTOR, {})
elif browser.has_selector(PHONE_CHALLENGE_SELECTOR):
browser.submit_form(PHONE_CHALLENGE_SELECTOR, {})
if browser.has_selector(VERIFICATION_FORM_SELECTOR):
if browser.has_selector(TOTP_CODE_SELECTOR):
input_selector = TOTP_CODE_SELECTOR
elif browser.has_selector(PHONE_CODE_SELECTOR):
input_selector = PHONE_CODE_SELECTOR
else:
raise GoogleAuthError('Unknown verification code input')
verfification_code = credentials_prompt.get_verification_code()
browser.submit_form(
VERIFICATION_FORM_SELECTOR, {input_selector: verfification_code}
)
try:
return browser.get_cookie('oauth_code')
except KeyError:
raise GoogleAuthError('Authorization code cookie not found')
if browser.has_selector(VERIFICATION_FORM_SELECTOR):
if browser.has_selector(TOTP_CODE_SELECTOR):
input_selector = TOTP_CODE_SELECTOR
elif browser.has_selector(PHONE_CODE_SELECTOR):
input_selector = PHONE_CODE_SELECTOR
else:
raise GoogleAuthError('Unknown verification code input')
verfification_code = credentials_prompt.get_verification_code()
browser.submit_form(
VERIFICATION_FORM_SELECTOR, {input_selector: verfification_code}
)
try:
return browser.get_cookie('oauth_code')
except KeyError:
raise GoogleAuthError('Authorization code cookie not found')
Returns:
dict: Google session cookies.
Raises:
GoogleAuthError: If authentication with Google fails.
"""
with requests.Session() as session:
session.headers = {'user-agent': USER_AGENT}
try:
logger.info('Authenticating with refresh token')
refresh_token = refresh_token_cache.get()
if refresh_token is None:
raise GoogleAuthError("Refresh token not found")
access_token = _auth_with_refresh_token(session, refresh_token)
except GoogleAuthError as e:
logger.info('Failed to authenticate using refresh token: %s', e)
logger.info('Authenticating with credentials')
if manual_login:
authorization_code = (
credentials_prompt.get_authorization_code()
)
else:
authorization_code = _get_authorization_code(
session, credentials_prompt
)
access_token, refresh_token = _auth_with_code(
session, authorization_code
)
refresh_token_cache.set(refresh_token)
logger.info('Authentication successful')