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

Fix 403 Bad Authentication with newer urllib3. #25

Merged
merged 2 commits into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
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
34 changes: 34 additions & 0 deletions gpsoauth/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import requests
import ssl
from urllib3.poolmanager import PoolManager
from urllib3.util.ssl_ import DEFAULT_CIPHERS

from ._version import __version__
from . import google
Expand All @@ -16,9 +19,40 @@
auth_url = 'https://android.clients.google.com/auth'
useragent = 'gpsoauth/' + __version__

# Blocking AESCCM in urllib3 > 1.26.3 causes Google to return 403 Bad
# Authentication.
CIPHERS = ":".join(
cipher
for cipher in DEFAULT_CIPHERS.split(":")
if cipher != "!AESCCM"
)

class SSLContext(ssl.SSLContext):
def set_alpn_protocols(self, protocols):
"""
ALPN headers cause Google to return 403 Bad Authentication.
"""
pass

class AuthHTTPAdapter(requests.adapters.HTTPAdapter):
def init_poolmanager(self, *args, **kwargs):
"""
Secure settings from ssl.create_default_context(), but without
ssl.OP_NO_TICKET which causes Google to return 403 Bad
Authentication.
"""
context = SSLContext()
context.set_ciphers(CIPHERS)
context.options |= ssl.OP_NO_COMPRESSION
context.options |= ssl.OP_NO_SSLv2
context.options |= ssl.OP_NO_SSLv3
context.post_handshake_auth = True
context.verify_mode = ssl.CERT_REQUIRED
self.poolmanager = PoolManager(*args, ssl_context=context, **kwargs)

def _perform_auth_request(data, proxy=None):
session = requests.session()
session.mount(auth_url, AuthHTTPAdapter())
if proxy:
session.proxies = proxy

Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
install_requires=[
'pycryptodomex >= 3.0',
'requests',
'urllib3 < 1.26.0', # newer urllib3 versions cause BadAuth: https://github.com/urllib3/urllib3/issues/2101
],
license='MIT',
zip_safe=False,
Expand Down