Skip to content

Commit 3d135eb

Browse files
committed
Store tonce for each user by redis
- Tonce for each user - Change login_type from `m.login.vaccount` to `com.bitorbit.login.vaccount`
1 parent d8def0e commit 3d135eb

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

synapse/handlers/vaccount_auth/auth_provider.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from solana.publickey import PublicKey
2727
from nacl.signing import VerifyKey
2828
from nacl.exceptions import BadSignatureError
29+
from redis import Redis
2930

3031
import logging
3132

@@ -47,12 +48,12 @@ class VaccountAuthProvider:
4748
def __init__(self, config, account_handler: ModuleApi):
4849
self.account_handler = account_handler
4950
self.store: DataStore = account_handler._hs.get_datastore()
50-
self.last_tonce = int(account_handler._hs.get_clock().time())
51+
self.redis = Redis()
5152

5253
@staticmethod
5354
def get_supported_login_types():
5455
supported_login_types = {
55-
'm.login.vaccount': (
56+
'com.bitorbit.login.vaccount': (
5657
'vaccount_address',
5758
'signature',
5859
'signer',
@@ -69,7 +70,7 @@ async def check_auth(self, evm_vaccount_address, login_type, login_dict):
6970
Args:
7071
evm_vaccount_address: ethereum based interpretation of the Vaccount address
7172
login_type: type of authentication
72-
login_dict: authentication parameters `supported_login_types
73+
login_dict: authentication parameters `supported_login_types`
7374
7475
Returns:
7576
Canonical user ID if authentication was successful
@@ -84,9 +85,6 @@ async def check_auth(self, evm_vaccount_address, login_type, login_dict):
8485
if not signature or not signer_key or not signed_timestamp or not vaccount_address or not signer_type:
8586
return False
8687

87-
if not self._is_valid_sign_timestamp(signed_timestamp):
88-
return False
89-
9088
if evm_vaccount_address.startswith("@") and ":" in evm_vaccount_address:
9189
# username is of the form @V4Bw2..:bar.com
9290
evm_vaccount_address = evm_vaccount_address.split(":", 1)[0][1:]
@@ -115,6 +113,9 @@ async def check_auth(self, evm_vaccount_address, login_type, login_dict):
115113
if not is_valid_signature or not is_active_vaccount or not is_valid_evm_address:
116114
return False
117115

116+
if not self._is_valid_sign_timestamp(evm_vaccount_address, signed_timestamp):
117+
return False
118+
118119
user_id = self.account_handler.get_qualified_user_id(username=evm_vaccount_address)
119120

120121
if await self.account_handler.check_user_exists(user_id):
@@ -151,7 +152,7 @@ async def check_auth(self, evm_vaccount_address, login_type, login_dict):
151152
# await self.store.set_e2e_cross_signing_key(
152153
# user_id, "master", vaccount_signing_key
153154
# )
154-
self.last_tonce = signed_timestamp
155+
self._commit_last_sign_timestamp(evm_vaccount_address, signed_timestamp)
155156

156157
return user_id
157158

@@ -167,16 +168,19 @@ def _is_valid_signature(signature, signer_key, signed_msg) -> bool:
167168

168169
return True
169170

170-
def _is_valid_sign_timestamp(self, signed_timestamp: int):
171+
def _is_valid_sign_timestamp(self, evm_vaccount_address: str, signed_timestamp: int):
171172
"""Check if signed timestamp is valid
172173
Args:
173-
signed_tonce: signed timestamp
174+
evm_vaccount_address: ethereum representing of the VA address
175+
signed_timestamp: last signed timestamp by VA key
174176
Returns:
175177
True if timestamp is greater than last signed timestamp
176178
"""
177179
current_timestamp = int(self.account_handler._hs.get_clock().time())
178-
ts_window = current_timestamp - signed_timestamp
179-
if signed_timestamp >= self.last_tonce and ts_window <= SIGN_TIMESTAMP_TOLERANCE:
180+
ts_window = current_timestamp - signed_timestamp
181+
last_signed_timestamp = self.redis.get(evm_vaccount_address) or signed_timestamp
182+
183+
if signed_timestamp >= int(last_signed_timestamp) and ts_window <= SIGN_TIMESTAMP_TOLERANCE:
180184
return True
181185

182186
return False
@@ -201,7 +205,7 @@ async def register_user(self, localpart, displayname):
201205
displayname=displayname,
202206
)
203207

204-
logger.info(f"Registration was successful: {user_id}, timestamp: {self.last_tonce}")
208+
logger.info(f"Registration was successful: {user_id}")
205209
return user_id
206210

207211
async def _is_active_vaccount(self, vaccount_address: PublicKey, signer: PublicKey, signer_type: str) -> bool:
@@ -251,6 +255,15 @@ async def _get_parsed_account_info(self, account_address):
251255

252256
return account_data
253257

258+
def _commit_last_sign_timestamp(self, evm_vaccount_address, last_timestamp):
259+
is_commit = self.redis.set(
260+
name=evm_vaccount_address,
261+
value=last_timestamp,
262+
# ex=SIGN_TIMESTAMP_TOLERANCE,
263+
)
264+
265+
return is_commit
266+
254267
@staticmethod
255268
def parse_config(config):
256269
return config

synapse/handlers/vaccount_auth/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def register_account(homeserver_api_url):
4646
evm_address = get_vaccount_evm_address(vaccount)
4747

4848
payload = {
49-
'type': 'm.login.vaccount',
49+
'type': 'com.bitorbit.login.vaccount',
5050
'user': '@' + evm_address + ':my.domain.name',
5151
'vaccount_address': vaccount.to_base58().decode(),
5252
'signature': signature.signature.hex(),

synapse/python_dependencies.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
"netaddr>=0.7.18",
8484
"Jinja2==3.0.3",
8585
"solana==0.20.0",
86+
"redis>=4.3.3",
8687
"borsh-construct==0.1.0",
8788
"pysha3==1.0.2",
8889
"bleach>=1.4.3",

0 commit comments

Comments
 (0)