Skip to content
This repository has been archived by the owner on Dec 15, 2018. It is now read-only.

Update everything (cleaned up) #221

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
91 changes: 41 additions & 50 deletions pgoapi/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,28 @@

from pgoapi.utilities import get_time, get_format_time_diff

class Auth:

class Auth:
def __init__(self):
self.log = logging.getLogger(__name__)

self._auth_provider = None

self._login = False

"""
oauth2 uses refresh tokens (which basically never expires)
"""
oauth2 uses refresh tokens (which basically never expires)
to get an access_token which is only valid for a certain time)
"""
self._refresh_token = None
self._access_token = None
self._access_token_expiry = 0
# TODO: can be removed
self._auth_token = None

"""
Pokemon Go uses internal tickets, like an internal
"""
Pokemon Go uses internal tickets, like an internal
session to keep a user logged in over a certain time (30 minutes)
"""
self._ticket_expire = None
self._ticket_expire = 0
self._ticket_start = None
self._ticket_end = None

Expand All @@ -66,68 +64,61 @@ def get_token(self):
return self._access_token

def has_ticket(self):
if self._ticket_expire and self._ticket_start and self._ticket_end:
return True
else:
return False
return (self._ticket_expire and self._ticket_start and self._ticket_end)

def set_ticket(self, params):
self._ticket_expire, self._ticket_start, self._ticket_end = params

def is_new_ticket(self, new_ticket_time_ms):
if self._ticket_expire is None or new_ticket_time_ms > self._ticket_expire:
return True
else:
return False
return (not self._ticket_expire or new_ticket_time_ms > self._ticket_expire)

def check_ticket(self):
if self.has_ticket():
now_ms = get_time(ms = True)
if now_ms < (self._ticket_expire - 10000):
h, m, s = get_format_time_diff(now_ms, self._ticket_expire, True)
self.log.debug('Session Ticket still valid for further %02d:%02d:%02d hours (%s < %s)', h, m, s, now_ms, self._ticket_expire)
return True
else:
self.log.debug('Removed expired Session Ticket (%s < %s)', now_ms, self._ticket_expire)
self._ticket_expire, self._ticket_start, self._ticket_end = (None, None, None)
return False
else:
if not self.has_ticket():
return False
now_ms = get_time(ms=True)
if now_ms < (self._ticket_expire + 10000):
h, m, s = get_format_time_diff(now_ms, self._ticket_expire, True)
self.log.debug(
'Session Ticket still valid for further %02d:%02d:%02d hours (%s < %s)', h, m, s, now_ms, self._ticket_expire)
return True

self.log.debug('Removed expired Session Ticket (%s < %s)',
now_ms, self._ticket_expire)
self._ticket_expire, self._ticket_start, self._ticket_end = (
0, None, None)
return False

def get_ticket(self):
if self.check_ticket():
return (self._ticket_expire, self._ticket_start, self._ticket_end)
else:
return False
return False

def user_login(self, username, password):
raise NotImplementedError()

def set_refresh_token(self, username, password):
raise NotImplementedError()

def get_access_token(self, force_refresh = False):
def get_access_token(self, force_refresh=False):
raise NotImplementedError()


def check_access_token(self):
"""
Add few seconds to now so the token get refreshed
before it invalidates in the middle of the request
"""
now_s = get_time() + 120

if self._access_token is not None:
if self._access_token_expiry == 0:
self.log.debug('No Access Token Expiry found - assuming it is still valid!')
return True
elif self._access_token_expiry > now_s:
h, m, s = get_format_time_diff(now_s, self._access_token_expiry, False)
self.log.debug('Access Token still valid for further %02d:%02d:%02d hours (%s < %s)', h, m, s, now_s, self._access_token_expiry)
return True
else:
self.log.info('Access Token expired!')
return False
else:
if self._access_token is None:
self.log.debug('No Access Token available!')
return False
return False

now_s = get_time()
if self._access_token_expiry == 0:
self.log.debug(
'No Access Token Expiry found - assuming it is still valid!')
return True
elif self._access_token_expiry > now_s:
h, m, s = get_format_time_diff(
now_s, self._access_token_expiry, False)
self.log.debug(
'Access Token still valid for further %02d:%02d:%02d hours (%s < %s)',
h, m, s, now_s, self._access_token_expiry)
return True

self.log.info('Access Token expired!')
return False
33 changes: 24 additions & 9 deletions pgoapi/auth_google.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@
from gpsoauth import perform_master_login, perform_oauth
from six import string_types


class AuthGoogle(Auth):

GOOGLE_LOGIN_ANDROID_ID = '9774d56d682e549c'
GOOGLE_LOGIN_SERVICE= 'audience:server:client_id:848232511240-7so421jotr2609rmqakceuu1luuq0ptb.apps.googleusercontent.com'
GOOGLE_LOGIN_SERVICE = 'audience:server:client_id:848232511240-7so421jotr2609rmqakceuu1luuq0ptb.apps.googleusercontent.com'
GOOGLE_LOGIN_APP = 'com.nianticlabs.pokemongo'
GOOGLE_LOGIN_CLIENT_SIG = '321187995bc7cdc2b5fc91b11a96e2baa8602c62'

Expand All @@ -52,13 +53,20 @@ def set_proxy(self, proxy_config):
def user_login(self, username, password):
self.log.info('Google User Login for: {}'.format(username))

if not isinstance(username, string_types) or not isinstance(password, string_types):
raise InvalidCredentialsException("Username/password not correctly specified")
if not isinstance(username, string_types) or not isinstance(
password, string_types):
raise InvalidCredentialsException(
"Username/password not correctly specified")

user_login = perform_master_login(username, password, self.GOOGLE_LOGIN_ANDROID_ID, proxy=self._proxy)
user_login = perform_master_login(
username,
password,
self.GOOGLE_LOGIN_ANDROID_ID,
proxy=self._proxy)

if user_login and user_login.get('Error', None) == 'NeedsBrowser':
raise AuthGoogleTwoFactorRequiredException(user_login['Url'], user_login['ErrorDetail'])
raise AuthGoogleTwoFactorRequiredException(
user_login['Url'], user_login['ErrorDetail'])

try:
refresh_token = user_login.get('Token', None)
Expand All @@ -79,7 +87,7 @@ def set_refresh_token(self, refresh_token):
self.log.info('Google Refresh Token provided by user')
self._refresh_token = refresh_token

def get_access_token(self, force_refresh = False):
def get_access_token(self, force_refresh=False):
token_validity = self.check_access_token()

if token_validity is True and force_refresh is False:
Expand All @@ -91,8 +99,14 @@ def get_access_token(self, force_refresh = False):
else:
self.log.info('Request Google Access Token...')

token_data = perform_oauth(None, self._refresh_token, self.GOOGLE_LOGIN_ANDROID_ID, self.GOOGLE_LOGIN_SERVICE, self.GOOGLE_LOGIN_APP,
self.GOOGLE_LOGIN_CLIENT_SIG, proxy=self._proxy)
token_data = perform_oauth(
None,
self._refresh_token,
self.GOOGLE_LOGIN_ANDROID_ID,
self.GOOGLE_LOGIN_SERVICE,
self.GOOGLE_LOGIN_APP,
self.GOOGLE_LOGIN_CLIENT_SIG,
proxy=self._proxy)

access_token = token_data.get('Auth', None)
if access_token is not None:
Expand All @@ -101,7 +115,8 @@ def get_access_token(self, force_refresh = False):
self._login = True

self.log.info('Google Access Token successfully received.')
self.log.debug('Google Access Token: %s...', self._access_token[:25])
self.log.debug('Google Access Token: %s...',
self._access_token[:25])
return self._access_token
else:
self._access_token = None
Expand Down
Loading