-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathturboGears.py
More file actions
109 lines (100 loc) · 3.72 KB
/
Copy pathturboGears.py
File metadata and controls
109 lines (100 loc) · 3.72 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
from tg import expose, TGController, request, response, AppConfig, minimal_wsgi
import jwt
from datetime import datetime, timedelta
import uuid
from passlib.hash import bcrypt
SECRET_KEY = 'your-secret-key'
users_db = {}
accounts = {}
def verify_token(token):
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=['HS256'])
username = payload.get('sub')
if not username or username not in users_db:
return None, 'Invalid token or user'
return users_db[username], None
except jwt.ExpiredSignatureError:
return None, 'Token expired'
except Exception:
return None, 'Invalid token'
def get_token():
auth = request.headers.get('Authorization')
if auth and auth.startswith('Bearer '):
return auth[7:]
return None
class RootController(TGController):
@expose('json')
def signup(self, **kw):
username = kw.get('username')
password = kw.get('password')
if not username or not password:
response.status = 400
return {'message': 'Username and password required'}
if username in users_db:
response.status = 400
return {'message': 'Username already exists'}
user_id = str(uuid.uuid4())
hashed_password = bcrypt.hash(password)
users_db[username] = {'username': username, 'hashed_password': hashed_password, 'id': user_id}
accounts[user_id] = 0.0
return {'message': 'User created', 'user_id': user_id}
@expose('json')
def login(self, **kw):
username = kw.get('username')
password = kw.get('password')
user = users_db.get(username)
if not user or not bcrypt.verify(password, user['hashed_password']):
response.status = 401
return {'message': 'Invalid username or password'}
token = jwt.encode({'sub': username, 'exp': datetime.utcnow() + timedelta(minutes=30)}, SECRET_KEY, algorithm='HS256')
return {'access_token': token, 'token_type': 'bearer'}
@expose('json')
def deposit(self, **kw):
token = get_token()
user, err = verify_token(token)
if err:
response.status = 401
return {'message': err}
try:
amount = float(kw.get('amount', 0))
except:
amount = 0
if amount <= 0:
response.status = 400
return {'message': 'Amount must be positive'}
accounts[user['id']] += amount
return {'message': 'Deposited', 'balance': accounts[user['id']]}
@expose('json')
def withdraw(self, **kw):
token = get_token()
user, err = verify_token(token)
if err:
response.status = 401
return {'message': err}
try:
amount = float(kw.get('amount', 0))
except:
amount = 0
if amount <= 0:
response.status = 400
return {'message': 'Amount must be positive'}
if amount > accounts[user['id']]:
response.status = 400
return {'message': 'Insufficient balance'}
accounts[user['id']] -= amount
return {'message': 'Withdrawn', 'balance': accounts[user['id']]}
@expose('json')
def balance(self):
token = get_token()
user, err = verify_token(token)
if err:
response.status = 401
return {'message': err}
return {'user_id': user['id'], 'balance': accounts[user['id']]}
if __name__ == '__main__':
config = AppConfig(minimal=True, root_controller=RootController())
app = config.make_wsgi_app()
from wsgiref.simple_server import make_server
server = make_server('0.0.0.0', 8080, app)
print("Serving on http://0.0.0.0:8080")
server.serve_forever()