forked from jekirl/poketrainer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth_ptc.py
More file actions
executable file
·98 lines (75 loc) · 3.47 KB
/
auth_ptc.py
File metadata and controls
executable file
·98 lines (75 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""
pgoapi - Pokemon Go API
Copyright (c) 2016 tjado <https://github.com/tejado>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.
Author: tjado <https://github.com/tejado>
"""
from __future__ import absolute_import
import json
import re
import requests
from pgoapi.auth import Auth
class AuthPtc(Auth):
PTC_LOGIN_URL = 'https://sso.pokemon.com/sso/login?service=https%3A%2F%2Fsso.pokemon.com%2Fsso%2Foauth2.0%2FcallbackAuthorize'
PTC_LOGIN_OAUTH = 'https://sso.pokemon.com/sso/oauth2.0/accessToken'
PTC_LOGIN_CLIENT_SECRET = 'w8ScCUXJQc6kXKw8FiOhd8Fixzht18Dq3PEVkUCP5ZPxtgyWsbTvWHFLm2wNY0JR'
def __init__(self):
Auth.__init__(self)
self._auth_provider = 'ptc'
self._session = requests.session()
self._session.verify = True
def login(self, username, password):
self.log.info('Login for: %s', username)
head = {'User-Agent': 'niantic'}
r = self._session.get(self.PTC_LOGIN_URL, headers=head)
jdata = json.loads(r.content.decode('utf-8'))
data = {
'lt': jdata['lt'],
'execution': jdata['execution'],
'_eventId': 'submit',
'username': username,
'password': password,
}
r1 = self._session.post(self.PTC_LOGIN_URL, data=data, headers=head)
ticket = None
try:
ticket = re.sub('.*ticket=', '', r1.history[0].headers['Location'])
except Exception as e:
try:
self.log.error('Could not retrieve token: %s', r1.json()['errors'][0])
except Exception as e:
self.log.error('Could not retrieve token! (%s)', str(e))
return False
data1 = {
'client_id': 'mobile-app_pokemon-go',
'redirect_uri': 'https://www.nianticlabs.com/pokemongo/error',
'client_secret': self.PTC_LOGIN_CLIENT_SECRET,
'grant_type': 'refresh_token',
'code': ticket,
}
r2 = self._session.post(self.PTC_LOGIN_OAUTH, data=data1)
access_token = re.sub('&expires.*', '', r2.content.decode('utf-8'))
access_token = re.sub('.*access_token=', '', access_token)
if '-sso.pokemon.com' in access_token:
self.log.info('PTC Login successful')
self.log.debug('PTC Session Token: %s', access_token[:25])
self._auth_token = access_token
else:
self.log.info('Seems not to be a PTC Session Token... login failed :(')
return False
self._login = True
return True