From 06a7f9e440e00104fae13de570b766b2db4ca77d Mon Sep 17 00:00:00 2001 From: David Pauli Date: Sat, 30 Jul 2016 18:03:05 +0200 Subject: [PATCH] Add certificate for auth and RPC calls. --- pgoapi/auth_ptc.py | 4 ++-- pgoapi/pgoapi.py | 23 +++++++++++++++-------- pgoapi/rpc_api.py | 5 +++-- pokecli.py | 3 ++- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/pgoapi/auth_ptc.py b/pgoapi/auth_ptc.py index 8190dbeb..abd08161 100755 --- a/pgoapi/auth_ptc.py +++ b/pgoapi/auth_ptc.py @@ -46,12 +46,12 @@ def __init__(self): self._session = requests.session() self._session.verify = True - def login(self, username, password): + def login(self, username, password, cert): self.log.info('Login for: %s', username) head = {'User-Agent': 'niantic'} - r = self._session.get(self.PTC_LOGIN_URL, headers=head) + r = self._session.get(self.PTC_LOGIN_URL, headers=head, verify=cert) try: jdata = json.loads(r.content.decode('utf-8')) diff --git a/pgoapi/pgoapi.py b/pgoapi/pgoapi.py index 11f1127f..e7bbd4b6 100755 --- a/pgoapi/pgoapi.py +++ b/pgoapi/pgoapi.py @@ -54,6 +54,8 @@ def __init__(self): self._position_lng = None self._position_alt = None + self._certificate = None + self.log.info('%s v%s - %s', __title__, __version__, __copyright__ ) def set_logger(self, logger = None): @@ -71,9 +73,9 @@ def set_position(self, lat, lng, alt): self._position_lat = lat self._position_lng = lng self._position_alt = alt - - def create_request(self): - request = PGoApiRequest(self._api_endpoint, self._auth_provider, self._position_lat, self._position_lng, self._position_alt) + + def create_request(self): + request = PGoApiRequest(self._api_endpoint, self._auth_provider, self._position_lat, self._position_lng, self._position_alt, self._certificate) return request def __getattr__(self, func): @@ -87,14 +89,17 @@ def function(**kwargs): return function else: raise AttributeError - - def login(self, provider, username, password, lat = None, lng = None, alt = None, app_simulation = True): + + def login(self, provider, username, password, lat = None, lng = None, alt = None, app_simulation = True, cert = None): if lat and lng and alt: self._position_lat = lat self._position_lng = lng self._position_alt = alt + if cert: + self._certificate = cert + if not isinstance(username, six.string_types) or not isinstance(password, six.string_types): raise AuthException("Username/password not correctly specified") @@ -107,7 +112,7 @@ def login(self, provider, username, password, lat = None, lng = None, alt = None self.log.debug('Auth provider: %s', provider) - if not self._auth_provider.login(username, password): + if not self._auth_provider.login(username, password, self._certificate): self.log.info('Login process failed') return False @@ -150,7 +155,7 @@ def login(self, provider, username, password, lat = None, lng = None, alt = None class PGoApiRequest: - def __init__(self, api_endpoint, auth_provider, position_lat, position_lng, position_alt): + def __init__(self, api_endpoint, auth_provider, position_lat, position_lng, position_alt, certificate): self.log = logging.getLogger(__name__) """ Inherit necessary parameters """ @@ -161,6 +166,8 @@ def __init__(self, api_endpoint, auth_provider, position_lat, position_lng, posi self._position_lng = position_lng self._position_alt = position_alt + self._certificate = certificate + self._req_method_list = [] def call(self): @@ -174,7 +181,7 @@ def call(self): self.log.info('Not logged in') return NotLoggedInException() - request = RpcApi(self._auth_provider) + request = RpcApi(self._auth_provider, self._certificate) self.log.info('Execution of RPC') response = None diff --git a/pgoapi/rpc_api.py b/pgoapi/rpc_api.py index c0af539d..f6cd3cae 100755 --- a/pgoapi/rpc_api.py +++ b/pgoapi/rpc_api.py @@ -49,7 +49,7 @@ class RpcApi: RPC_ID = 0 - def __init__(self, auth_provider): + def __init__(self, auth_provider, certificate): self.log = logging.getLogger(__name__) @@ -58,6 +58,7 @@ def __init__(self, auth_provider): self._session.verify = True self._auth_provider = auth_provider + self._certificate = certificate if RpcApi.RPC_ID == 0: RpcApi.RPC_ID = int(random.random() * 10 ** 18) @@ -89,7 +90,7 @@ def _make_rpc(self, endpoint, request_proto_plain): request_proto_serialized = request_proto_plain.SerializeToString() try: - http_response = self._session.post(endpoint, data=request_proto_serialized) + http_response = self._session.post(endpoint, data=request_proto_serialized, verify=self._certificate) except requests.exceptions.ConnectionError as e: raise ServerBusyOrOfflineException diff --git a/pokecli.py b/pokecli.py index 0750b95a..780ccba4 100755 --- a/pokecli.py +++ b/pokecli.py @@ -62,6 +62,7 @@ def init_config(): parser.add_argument("-l", "--location", help="Location", required=required("location")) parser.add_argument("-d", "--debug", help="Debug Mode", action='store_true') parser.add_argument("-t", "--test", help="Only parse the specified location", action='store_true') + parser.add_argument("-c", "--cert", help="Certificate") parser.set_defaults(DEBUG=False, TEST=False) config = parser.parse_args() @@ -116,7 +117,7 @@ def main(): # set player position on the earth api.set_position(*position) - if not api.login(config.auth_service, config.username, config.password, app_simulation = True): + if not api.login(config.auth_service, config.username, config.password, app_simulation = True, cert = config.cert): return # get player profile call (single command example)