-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanage.py
More file actions
262 lines (215 loc) · 6.81 KB
/
manage.py
File metadata and controls
262 lines (215 loc) · 6.81 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import hashlib
import json
import os
import typing
from getpass import getpass
from json import JSONDecodeError
from typing import Dict
import click
import requests
from requests import HTTPError
class Client:
def __init__(self, url):
self.url = url
self.token_file = '/tmp/____client_token'
self._token = None
self._user_id = None
@property
def is_logged_in(self):
return self.has_token
@property
def user_id(self):
if not self._user_id:
self._get_credentials()
return self._user_id
def _clean_localstorage(self):
try:
os.remove(self.token_file)
except FileNotFoundError:
pass
@property
def has_token(self):
if not self._token:
self._get_credentials()
return bool(self._token)
def _get_credentials(self):
try:
if not self._token:
with open(self.token_file, 'r') as f:
d = json.load(f)
self._user_id = d['user_id']
self._token = d['token']
except FileNotFoundError:
pass
except JSONDecodeError:
self._clean_localstorage()
return self._token
@staticmethod
def _parse_token_from_cookies(cookies):
cookie = cookies.get('Authorization')
return cookie.replace('"', '').replace('Bearer ', '')
def _get_cookie_from_token(self):
return {'Authorization': 'Bearer {}'.format(self._get_credentials())}
def _store_credentials(self, token: str):
self._token = token
with open(self.token_file, 'w') as f:
return json.dump(
{
'user_id': self._user_id,
'token': self._token
},
f
)
def signup(self, payload: Dict):
res = requests.post(self.url + '/auth/signup', data=json.dumps(payload))
res.raise_for_status()
return res.content
def login(self, payload: Dict) -> typing.Tuple[str, str]:
res = requests.post(self.url + '/auth/login', data=json.dumps(payload))
res.raise_for_status()
self._user_id = res.json()['user_id']
self._store_credentials(self._parse_token_from_cookies(res.cookies))
return self._user_id, res.cookies
def logout(self):
try:
res = requests.post(self.url + '/auth/logout', cookies=self._get_cookie_from_token())
except:
pass
self._clean_localstorage()
return True
def get_details(self):
res = requests.get(
self.url + '/user',
cookies=self._get_cookie_from_token()
)
res.raise_for_status()
return res.json()
def get_characters(self):
res = requests.get(
self.url + '/user/character',
cookies=self._get_cookie_from_token()
)
res.raise_for_status()
return res.json()
def create_character(self, payload: Dict):
res = requests.post(
self.url + '/user/character',
cookies=self._get_cookie_from_token(),
data=json.dumps(payload)
)
res.raise_for_status()
return res.json()
def authenticate_character(self, character_id: str) -> typing.Tuple[str, str]:
payload = {
'entity_type': 'character',
'entity_id': character_id
}
res = requests.post(
self.url + '/auth/token',
data=json.dumps(payload),
cookies=self._get_cookie_from_token()
)
res.raise_for_status()
return res.json(), res.cookies
def _get_login_data():
email = input('Enter email: ')
password = getpass('Enter Password: ')
payload = {
'email': email,
'password': hashlib.sha256(password.encode()).hexdigest()
}
return payload
def get_client() -> Client:
def get_client_url():
try:
with open('/tmp/__pm_client_url', 'r') as f:
d = f.read()
click.echo('using url %s' % d)
except FileNotFoundError:
d = input('Enter projectm base URL (default http://localhost:60161) : ')
d = d or 'http://localhost:60161'
check_res = requests.get(d + '/auth/login')
try:
check_res.raise_for_status()
except HTTPError as e:
if e.response.status_code == 405:
pass
else:
print('Error checking Client URL: ', str(e))
exit(1)
with open('/tmp/__pm_client_url', 'w') as f:
f.write(d)
return d
url = get_client_url()
return Client(url)
@click.group(name='client')
def main():
pass
@main.group(name='user')
def user():
pass
@user.command()
def signup():
client = get_client()
if client.is_logged_in:
click.echo('Logged in. Logout first')
return
payload = _get_login_data()
response = client.signup(payload)
click.echo('Signup response: %s' % response)
@user.command()
def login():
client = get_client()
if client.is_logged_in:
click.echo('Already logged in. Logout first')
return
payload = _get_login_data()
response = client.login(payload)
click.echo('Login response: %s - Cookies: %s' % response)
@user.command()
def logout():
client = get_client()
if not client.is_logged_in:
click.echo('Not logged in')
return
response = client.logout()
click.echo('Logout response: %s' % response)
@user.command()
def details():
client = get_client()
if not client.is_logged_in:
click.echo('Not logged in')
return
response = client.get_details()
click.echo('Response:\n%s' % json.dumps(response, indent=2))
@main.group()
def character():
pass
@character.command()
def ls():
client = get_client()
if not client.is_logged_in:
click.echo('Not logged in')
return
response = client.get_characters()
click.echo('Response:\n%s' % json.dumps(response, indent=2))
@character.command()
def authenticate():
client = get_client()
if not client.is_logged_in:
click.echo('Not logged in')
return
by_name = {ch['name']: ch for ch in client.get_characters()['data']}
if not by_name:
click.echo('No characters created')
return
character_name = input('Enter your chacter name: ')
try:
character_id = by_name[character_name]['character_id']
except KeyError:
click.echo('No character with that name, available: [%s]' % ', '.join(list(by_name.keys())))
return
response = client.authenticate_character(character_id)
click.echo('Authenticate Character response:\n%s - Cookies: %s' % response)
if __name__ == '__main__':
main()