Skip to content

Commit

Permalink
Fix 403 Bad Authentication with newer urllib3.
Browse files Browse the repository at this point in the history
Closes #24.
  • Loading branch information
jkuebart committed Feb 6, 2021
1 parent 403ac2e commit b8252d2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
43 changes: 43 additions & 0 deletions gpsoauth/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import requests
import ssl
from urllib3.poolmanager import PoolManager

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

# Certain ciphers cause Google to return 403 Bad Authentication.
CIPHERS = ":".join(
[
"ECDHE+AESGCM",
"ECDHE+CHACHA20",
"DHE+AESGCM",
"DHE+CHACHA20",
"ECDH+AES",
"DH+AES",
"RSA+AESGCM",
"RSA+AES",
"!aNULL",
"!eNULL",
"!MD5",
"!DSS",
]
)

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

0 comments on commit b8252d2

Please sign in to comment.