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

Add certificate for auth and RPC calls. #152

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions pgoapi/auth_ptc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
23 changes: 15 additions & 8 deletions pgoapi/pgoapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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")

Expand All @@ -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

Expand Down Expand Up @@ -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 """
Expand All @@ -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):
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions pgoapi/rpc_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -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)
Expand Down Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion pokecli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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)
Expand Down