-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
210 lines (190 loc) · 7.34 KB
/
db.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
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
from functools import wraps
import motor.motor_asyncio
import secrets
import bcrypt
from tinkof import FetchPortfolios
from utility import Random128Hex, CalculateNominalPortfolio, CalculateMarketPortfolio
import os
class DBHandler():
def __init__(self, connString=None, dbName=None):
if (connString==None):
connString = os.getenv('MONGODB_CONNSTRING')
if (dbName==None):
dbName = os.getenv('MONGODB_DATABASE')
self.db = motor.motor_asyncio.AsyncIOMotorClient(connString, serverSelectionTimeoutMS=5000)[dbName]
def WrongTempCode(self):
return {'status': False, 'error': 'wrong_temp_code'}
def WrongPassword(self):
return {'status': False, 'error': 'wrong_password'}
def WrongTempKey(self):
return {'status': False, 'error': 'wrong_temp_key'}
def StatusTrue(self):
return {'status': True}
def CheckPassword(self, p1, p2):
return bcrypt.checkpw(bytes(p1, encoding='utf8'), p2)
def HashPassword(self, password):
return bcrypt.hashpw(bytes(password, encoding='utf8'), bcrypt.gensalt(10))
async def FindUser(self, tempKey=None, telegramId=None):
if tempKey:
return await self.db.users.find_one({'temp-key': tempKey})
else:
return await self.db.users.find_one({'telegram-id': telegramId})
async def GetByFigis(self, figis):
return await self.db.bonds.find({
'figi': {
'$in': figis
}
}).to_list(5000)
async def CreateTempKey(self, userId):
tempKey = Random128Hex()
await self.db.users.update_one(
{
"_id":userId
},
{
"$set": {
"temp-key": tempKey
}
})
return tempKey
async def CreateUser(self, user):
tempKey = Random128Hex();
userObj = {
'telegram-id': user['id'],
'tinkoff-token': '',
'temp-key': tempKey,
'confirmation-code': Random128Hex(),
'portfolios': []
}
inserted = await self.db.users.insert_one(userObj)
return {'status': True, 'temp_key': await self.CreateTempKey(inserted.inserted_id)}
async def LoginUser(self, user):
foundUser = await self.FindUser(telegramId=user['id'])
if foundUser:
return {'status': True, 'temp_key': await self.CreateTempKey(foundUser['_id'])}
else:
return await self.CreateUser(user)
def authorized(func):
@wraps(func)
async def wrapper(self, tempKey, *args, **kwargs):
if foundUser := await self.FindUser(tempKey=tempKey):
return await func(self, tempKey, foundUser, *args, **kwargs)
else:
return self.WrongTempKey()
return wrapper
@authorized
async def SetConfCode(self, tempKey, foundUser, confCode):
await self.db.users.update_one(
{
"temp-key": tempKey
},
{
'$set': {
"confirmation-code": confCode
}
})
return self.StatusTrue()
@authorized
async def UpdateToken(self, tempKey, foundUser, confCode, newToken):
if foundUser['confirmation-code']==confCode:
await self.db.users.update_one(
{
"temp-key": tempKey
},
{
'$set': {
'confirmation-code': secrets.token_hex(128),
'tinkoff-token': newToken
}
})
await self.UpdatePortfolios(tempKey)
return self.StatusTrue()
else:
return self.WrongTempCode()
@authorized
async def GetPortfolios(self, tempKey, foundUser):
portfolios = foundUser['portfolios']
for portfolio in portfolios:
bonds = await self.GetByFigis(list(portfolio['bonds'].keys()))
for bond in bonds:
bond['count'] = portfolio['bonds'][bond['figi']]
portfolio['bonds'] = bonds
return {'status': True, 'portfolios': portfolios}
@authorized
async def UpdatePortfolios(self, tempKey, foundUser):
if tinkoffToken := foundUser['tinkoff-token']:
portfolios = await FetchPortfolios(tinkoffToken)
await self.db.users.update_one(
{
"temp-key": tempKey
},
{
'$set': {
'portfolios': portfolios
}
})
return self.StatusTrue()
else:
return {'status': False, 'error':'token_not_initialized'}
@authorized
async def GetPortfolioSums(self, tempKey, foundUser):
portfolios = (await self.GetPortfolios(tempKey))['portfolios']
return {'status': True, 'market_sum': CalculateMarketPortfolio(portfolios), 'nominal_sum': CalculateNominalPortfolio(portfolios)}
async def GetAllBonds(self):
allBonds = await self.db.bonds.find().to_list(length=5000)
return {'status': True, 'bonds': allBonds}
"""
async def create_user(self, username, password):
user_dict = {
"t_username": username,
"telegram_id": '',
"tinkoff_token": "",
"pass_hash": bcrypt.hashpw(bytes(password, encoding='utf8'), bcrypt.gensalt(10)),
"temp_key": ''
}
inserted = await self.db.users.insert_one(user_dict);
return {'status': True, 'temp_key': self.create_temp_key(inserted.inserted_id)}
async def login_user(self, username, password):
found_user = await self.find_user(username=username)
if found_user:
if self.check_password(password, found_user['pass_hash']):
return {'status': True, 'temp_key': await self.create_temp_key(found_user['_id'])}
else:
return self.wrong_password()
else:
return self.create_user(username, password)
async def update_token(self, temp_key, password, new_token):
found_user = await self.find_user(temp_key=temp_key)
if found_user:
if self.check_password(password, found_user['pass_hash']):
await self.db.users.update_one(
{
"_id": found_user['_id']
},
{
'$set': {
"tinkoff_token": new_token
}
})
return self.status_true()
else:
return self.wrong_password()
else:
return self.wrong_temp_key()
async def update_password(self, temp_key, old_password, new_password):
found_user = await self.find_user(temp_key=temp_key)
if found_user:
if self.check_password(old_password, found_user['pass_hash']):
await self.db.users.update_one(
{
"_id": found_user['_id']
},
{
'$set': {
'pass_hash': self.hash_password(new_password)
}
})
return self.status_true()
else:
return self.wrong_temp_key()
"""