user_pk/client_pk -> user_id and cli/client -> user (when it does not reffer to the software)

This commit is contained in:
Sergi Delgado Segura
2020-04-21 16:42:43 +02:00
parent 0e5a99b778
commit 5f7a909804
8 changed files with 168 additions and 196 deletions

View File

@@ -120,14 +120,14 @@ class API:
logger.info("Received invalid register request", from_addr="{}".format(remote_addr))
return jsonify({"error": str(e)}), HTTP_BAD_REQUEST
client_pk = request_data.get("public_key")
user_id = request_data.get("public_key")
if client_pk:
if user_id:
try:
rcode = HTTP_OK
available_slots, subscription_expiry = self.watcher.gatekeeper.add_update_user(client_pk)
available_slots, subscription_expiry = self.watcher.gatekeeper.add_update_user(user_id)
response = {
"public_key": client_pk,
"public_key": user_id,
"available_slots": available_slots,
"subscription_expiry": subscription_expiry,
}
@@ -234,10 +234,10 @@ class API:
message = "get appointment {}".format(locator).encode()
signature = request_data.get("signature")
user_pk = self.watcher.gatekeeper.authenticate_user(message, signature)
user_id = self.watcher.gatekeeper.authenticate_user(message, signature)
triggered_appointments = self.watcher.db_manager.load_all_triggered_flags()
uuid = hash_160("{}{}".format(locator, user_pk))
uuid = hash_160("{}{}".format(locator, user_id))
# If the appointment has been triggered, it should be in the locator (default else just in case).
if uuid in triggered_appointments:

View File

@@ -76,12 +76,12 @@ class Gatekeeper:
}
self.lock = Lock()
def add_update_user(self, user_pk):
def add_update_user(self, user_id):
"""
Adds a new user or updates the subscription of an existing one, by adding additional slots.
Args:
user_pk(:obj:`str`): the public key that identifies the user (33-bytes hex str).
user_id(:obj:`str`): the public key that identifies the user (33-bytes hex str).
Returns:
:obj:`tuple`: a tuple with the number of available slots in the user subscription and the subscription
@@ -91,23 +91,23 @@ class Gatekeeper:
:obj:`InvalidParameter`: if the user_pk does not match the expected format.
"""
if not is_compressed_pk(user_pk):
if not is_compressed_pk(user_id):
raise InvalidParameter("Provided public key does not match expected format (33-byte hex string)")
if user_pk not in self.registered_users:
self.registered_users[user_pk] = UserInfo(
if user_id not in self.registered_users:
self.registered_users[user_id] = UserInfo(
self.default_slots, self.block_processor.get_block_count() + self.default_subscription_duration
)
else:
# FIXME: For now new calls to register add default_slots to the current count and reset the expiry time
self.registered_users[user_pk].available_slots += self.default_slots
self.registered_users[user_pk].subscription_expiry = (
self.registered_users[user_id].available_slots += self.default_slots
self.registered_users[user_id].subscription_expiry = (
self.block_processor.get_block_count() + self.default_subscription_duration
)
self.user_db.store_user(user_pk, self.registered_users[user_pk].to_dict())
self.user_db.store_user(user_id, self.registered_users[user_id].to_dict())
return self.registered_users[user_pk].available_slots, self.registered_users[user_pk].subscription_expiry
return self.registered_users[user_id].available_slots, self.registered_users[user_id].subscription_expiry
def authenticate_user(self, message, signature):
"""
@@ -126,10 +126,10 @@ class Gatekeeper:
try:
rpk = Cryptographer.recover_pk(message, signature)
compressed_pk = Cryptographer.get_compressed_pk(rpk)
user_id = Cryptographer.get_compressed_pk(rpk)
if compressed_pk in self.registered_users:
return compressed_pk
if user_id in self.registered_users:
return user_id
else:
raise AuthenticationFailure("User not found.")

View File

@@ -37,42 +37,42 @@ class UsersDBM(DBManager):
raise e
def store_user(self, user_pk, user_data):
def store_user(self, user_id, user_data):
"""
Stores a user record to the database. ``user_pk`` is used as identifier.
Args:
user_pk (:obj:`str`): a 33-byte hex-encoded string identifying the user.
user_id (:obj:`str`): a 33-byte hex-encoded string identifying the user.
user_data (:obj:`dict`): the user associated data, as a dictionary.
Returns:
:obj:`bool`: True if the user was stored in the database, False otherwise.
"""
if is_compressed_pk(user_pk):
if is_compressed_pk(user_id):
try:
self.create_entry(user_pk, json.dumps(user_data))
logger.info("Adding user to Gatekeeper's db", user_pk=user_pk)
self.create_entry(user_id, json.dumps(user_data))
logger.info("Adding user to Gatekeeper's db", user_id=user_id)
return True
except json.JSONDecodeError:
logger.info("Could't add user to db. Wrong user data format", user_pk=user_pk, user_data=user_data)
logger.info("Could't add user to db. Wrong user data format", user_id=user_id, user_data=user_data)
return False
except TypeError:
logger.info("Could't add user to db", user_pk=user_pk, user_data=user_data)
logger.info("Could't add user to db", user_id=user_id, user_data=user_data)
return False
else:
logger.info("Could't add user to db. Wrong pk format", user_pk=user_pk, user_data=user_data)
logger.info("Could't add user to db. Wrong pk format", user_id=user_id, user_data=user_data)
return False
def load_user(self, user_pk):
def load_user(self, user_id):
"""
Loads a user record from the database using the ``user_pk`` as identifier.
Args:
user_pk (:obj:`str`): a 33-byte hex-encoded string identifying the user.
user_id (:obj:`str`): a 33-byte hex-encoded string identifying the user.
Returns:
:obj:`dict`: A dictionary containing the user data if the ``key`` is found.
@@ -81,31 +81,31 @@ class UsersDBM(DBManager):
"""
try:
data = self.load_entry(user_pk)
data = self.load_entry(user_id)
data = json.loads(data)
except (TypeError, json.decoder.JSONDecodeError):
data = None
return data
def delete_user(self, user_pk):
def delete_user(self, user_id):
"""
Deletes a user record from the database.
Args:
user_pk (:obj:`str`): a 33-byte hex-encoded string identifying the user.
user_id (:obj:`str`): a 33-byte hex-encoded string identifying the user.
Returns:
:obj:`bool`: True if the user was deleted from the database or it was non-existent, False otherwise.
"""
try:
self.delete_entry(user_pk)
logger.info("Deleting user from Gatekeeper's db", uuid=user_pk)
self.delete_entry(user_id)
logger.info("Deleting user from Gatekeeper's db", uuid=user_id)
return True
except TypeError:
logger.info("Cannot delete user from db, user key has wrong type", uuid=user_pk)
logger.info("Cannot delete user from db, user key has wrong type", uuid=user_id)
return False
def load_all_users(self):
@@ -122,7 +122,7 @@ class UsersDBM(DBManager):
for k, v in self.db.iterator():
# Get uuid and appointment_data from the db
user_pk = k.decode("utf-8")
data[user_pk] = json.loads(v)
user_id = k.decode("utf-8")
data[user_id] = json.loads(v)
return data