-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
192 lines (177 loc) · 5.26 KB
/
api.py
File metadata and controls
192 lines (177 loc) · 5.26 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
from datetime import datetime
from flask_api import FlaskAPI
import random
import string
import config
import db
import crypto_ecdsa
import crypto
import codecs
app = FlaskAPI(__name__)
@app.route('/')
def index():
return {
'response': 'OK'
}
@app.route('/get_proof/<int:telegram_id>/<int:minimal_score>/<data>')
def get_proof(telegram_id, minimal_score, data):
conn = db.get_connection(config.db_url)
cur = conn.cursor()
cur.execute(
'SELECT score from users WHERE user_id = %s;',
(telegram_id,))
score = cur.fetchone()
if score:
if score[0] >= minimal_score:
proof, timestamp = crypto.process(data)
cur.close()
db.close_connection(conn)
return {
'response': 'OK',
'timestamp': timestamp,
'proof': proof
}
else:
cur.close()
db.close_connection(conn)
return {
'response': 'Low score'
}
else:
cur.close()
db.close_connection(conn)
return {
'response': 'User not found'
}
@app.route('/get_score/<token>/<int:telegram_id>')
def get_score(token, telegram_id):
conn = db.get_connection(config.db_url)
cur = conn.cursor()
cur.execute(
'SELECT quota from api WHERE token = %s;',
(token,))
quota = cur.fetchone()
if quota:
if quota[0] >= 1:
cur.execute(
'SELECT score from users WHERE user_id = %s;',
(telegram_id,))
score = cur.fetchone()
if score:
time_now = datetime.now()
message = f'{telegram_id},{score[0]},{time_now}'
signature = crypto_ecdsa.sign(message, crypto_ecdsa.load_key(config.api_ecdsa_private_key))
cur.execute(
'UPDATE api SET quota = quota - 1 WHERE token = %s;',
(token,))
cur.close()
db.close_connection(conn)
return {
'response': 'OK',
'telegram_id': telegram_id,
'score': score[0],
'time': time_now,
'message': message,
'signature': codecs.encode(signature[0], 'hex_codec').decode('ascii')
}
else:
cur.close()
db.close_connection(conn)
return {
'response': 'User not found'
}
else:
cur.close()
db.close_connection(conn)
return {
'response': 'Quota exceeded'
}
else:
cur.close()
db.close_connection(conn)
return {
'response': 'Invalid token'
}
@app.route('/get_quota/<token>')
def get_quota(token):
conn = db.get_connection(config.db_url)
cur = conn.cursor()
cur.execute(
'SELECT quota from api WHERE token = %s;',
(token,))
quota = cur.fetchone()
cur.close()
db.close_connection(conn)
if quota:
return {
'response': 'OK',
'token': token,
'quota': quota[0]
}
else:
return {
'response': 'Invalid token'
}
@app.route('/generate_token/<master_key>/<int:quota>')
def generate_token(master_key, quota):
if master_key == config.api_master_key:
try:
quota_num = int(quota)
except:
quota_num = -1
if quota_num > 0:
token = ''.join(random.choices(string.ascii_lowercase + string.digits, k=32))
conn = db.get_connection(config.db_url)
conn.cursor().execute(
'INSERT INTO api (token, quota) VALUES (%s, %s);',
(token, quota))
db.close_connection(conn)
return {
'response': 'OK',
'token': token,
'quota': quota
}
else:
return {
'response': 'Quota must be a positive integer'
}
else:
return {
'response': 'Wrong master key'
}
@app.route('/add_quota/<master_key>/<token>/<int:quota>')
def add_quota(master_key, token, quota):
if master_key == config.api_master_key:
try:
quota_num = int(quota)
except:
quota_num = 0
if quota_num != 0:
conn = db.get_connection(config.db_url)
cur = conn.cursor()
cur.execute(
'UPDATE api SET quota = quota + %s WHERE token = %s RETURNING quota;',
(quota, token))
new_quota = cur.fetchone()
cur.close()
db.close_connection(conn)
if new_quota:
return {
'response': 'OK',
'token': token,
'quota': new_quota[0]
}
else:
return {
'response': 'Invalid token'
}
else:
return {
'response': 'Quota must be a non-zero integer'
}
else:
return {
'response': 'Wrong master key'
}
if __name__ == '__main__':
app.run(debug=True)