-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
143 lines (108 loc) · 3.48 KB
/
server.py
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
#!/usr/bin/python3
import cherrypy
import json
import answers
from people import users_map, api_keys
cherrypy.config.update({'server.socket_port': 5050,})
cherrypy.config.update({'request.show_tracebacks': False})
use_led = False
if use_led:
import controller
serial_connector = controller.SerialConnector()
class TaskServer(object):
def _cp_dispatch(self, vpath):
cherrypy.request.params['login'] = vpath.pop(0)
if len(vpath) == 0:
return self
return self
@cherrypy.expose
def index(self, login=None):
if login is None:
raise cherrypy.HTTPError(404)
return 'Generic help for {}'.format(login)
@cherrypy.expose
def github(self, login, **kwargs):
return GithubTask.handle(login, **kwargs)
@cherrypy.expose
@cherrypy.tools.json_in()
def krs(self, login, **kwargs):
return KrsTask.handle(login, **kwargs)
@cherrypy.expose
@cherrypy.tools.json_in()
def wiki(self, login, **kwargs):
return WikiTask.handle(login, **kwargs)
class Task(object):
color = '000000'
password = None
previous = None
@classmethod
def handle(cls, login, **kwargs):
cls.check_login(login)
cls.verify_secret(login, **kwargs)
cls.verify_previous(login, **kwargs)
if cls.verify_success(login, **kwargs):
cls.do_lamp(login)
return cls.output_success()
else:
return cls.output_failure()
@classmethod
def verify_success(cls, login, **kwargs):
raise NotImplemented()
@classmethod
def check_login(cls, login):
if login not in users_map:
raise cherrypy.HTTPError(404)
@classmethod
def verify_secret(cls, login, **kwargs):
if 'api_key' not in kwargs:
raise cherrypy.HTTPError(403)
if not api_keys[login] == kwargs['api_key']:
raise cherrypy.HTTPError(403)
@classmethod
def verify_previous(cls, login, **kwargs):
body = cherrypy.request.json
if not cls.previous:
return
if 'prev' not in body:
raise cherrypy.HTTPError(403)
if cls.previous.password != body['prev']:
raise cherrypy.HTTPError(403)
@classmethod
def do_lamp(cls, login):
if use_led:
serial_connector.set_hex_color(users_map[login], cls.color)
@classmethod
def output_success(cls):
result = {'result': 'Success', 'info': 'Gratulacje!'}
if cls.password:
result['password'] = cls.password
return json.dumps(result)
@classmethod
def output_failure(cls):
return json.dumps({'result': 'Failure', 'info': 'Nie udało się'})
class GithubTask(Task):
color = '00ff00'
@classmethod
def verify_success(cls, login, **kwargs):
return True
class KrsTask(Task):
color = 'ff0000'
password = 'wybitnie_trudny_klucz'
@classmethod
def verify_success(cls, login, **kwargs):
body = cherrypy.request.json
if 'postal' not in body or body['postal'] != answers.postal:
return False
return True
class WikiTask(Task):
color = '0000ff'
previous = KrsTask
@classmethod
def verify_success(cls, login, **kwargs):
body = cherrypy.request.json
print(body)
if 'battle_url' not in body or body['battle_url'] != answers.battle_url:
return False
return True
if __name__ == '__main__':
cherrypy.quickstart(TaskServer())