Added api-key commands #162-3

This commit is contained in:
2023-02-09 20:22:46 +01:00
parent d03ea1d970
commit 7f14aff1bd
8 changed files with 174 additions and 10 deletions

View File

@@ -18,6 +18,10 @@ class ApiKeyRepositoryABC(ABC):
def get_api_key(self, identifier: str, key: str) -> ApiKey:
pass
@abstractmethod
def get_api_key_by_key(self, key: str) -> ApiKey:
pass
@abstractmethod
def add_api_key(self, api_key: ApiKey):
pass

View File

@@ -27,7 +27,8 @@ class ApiKeyMigration(MigrationABC):
`LastModifiedAt` DATETIME(6),
PRIMARY KEY(`Id`),
FOREIGN KEY (`CreatorId`) REFERENCES `Users`(`UserId`),
CONSTRAINT UC_Identifier_Key UNIQUE (`Identifier`,`Key`)
CONSTRAINT UC_Identifier_Key UNIQUE (`Identifier`,`Key`),
CONSTRAINT UC_Key UNIQUE (`Key`)
);
"""
)

View File

@@ -4,7 +4,7 @@ from bot_data.db_context import DBContext
class AutoRoleFix1Migration(MigrationABC):
name = "0.3.0_AutoRoleMigration"
name = "0.3.0_AutoRoleFixMigration"
def __init__(self, logger: DatabaseLogger, db: DBContext):
MigrationABC.__init__(self)

View File

@@ -54,6 +54,15 @@ class ApiKey(TableABC):
"""
)
@staticmethod
def get_select_by_key(key: str) -> str:
return str(
f"""
SELECT * FROM `ApiKeys`
WHERE `Key` = '{key}';
"""
)
@property
def insert_string(self) -> str:
return str(

View File

@@ -1,7 +1,6 @@
from typing import Optional
from cpl_core.database.context import DatabaseContextABC
from cpl_core.utils import CredentialManager
from cpl_query.extension import List
from bot_core.logging.database_logger import DatabaseLogger
@@ -32,10 +31,6 @@ class ApiKeyRepositoryService(ApiKeyRepositoryABC):
return value
def _api_key_from_result(self, sql_result: tuple) -> ApiKey:
code = self._get_value_from_result(sql_result[3])
if code is not None:
code = CredentialManager.decrypt(code)
api_key = ApiKey(
self._get_value_from_result(sql_result[1]),
self._get_value_from_result(sql_result[2]),
@@ -58,7 +53,11 @@ class ApiKeyRepositoryService(ApiKeyRepositoryABC):
def get_api_key(self, identifier: str, key: str) -> ApiKey:
self._logger.trace(__name__, f"Send SQL command: {ApiKey.get_select_string(identifier, key)}")
return self._get_value_from_result(self._context.select(ApiKey.get_select_string(identifier, key))[0])
return self._api_key_from_result(self._context.select(ApiKey.get_select_string(identifier, key))[0])
def get_api_key_by_key(self, key: str) -> ApiKey:
self._logger.trace(__name__, f"Send SQL command: {ApiKey.get_select_by_key(key)}")
return self._api_key_from_result(self._context.select(ApiKey.get_select_by_key(key))[0])
def add_api_key(self, api_key: ApiKey):
self._logger.trace(__name__, f"Send SQL command: {api_key.insert_string}")