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

@@ -0,0 +1,139 @@
import hashlib
import uuid
from typing import List as TList
import discord
from cpl_core.database.context import DatabaseContextABC
from cpl_discord.command import DiscordCommandABC
from cpl_discord.service import DiscordBotServiceABC
from cpl_translation import TranslatePipe
from discord import app_commands
from discord.ext import commands
from discord.ext.commands import Context
from bot_api.configuration.authentication_settings import AuthenticationSettings
from bot_core.abc.client_utils_abc import ClientUtilsABC
from bot_core.abc.message_service_abc import MessageServiceABC
from bot_core.helper.command_checks import CommandChecks
from bot_core.logging.command_logger import CommandLogger
from bot_data.abc.api_key_repository_abc import ApiKeyRepositoryABC
from bot_data.abc.server_repository_abc import ServerRepositoryABC
from bot_data.abc.user_repository_abc import UserRepositoryABC
from bot_data.model.api_key import ApiKey
from modules.permission.abc.permission_service_abc import PermissionServiceABC
class ApiKeyGroup(DiscordCommandABC):
def __init__(
self,
logger: CommandLogger,
auth_settings: AuthenticationSettings,
message_service: MessageServiceABC,
bot: DiscordBotServiceABC,
client_utils: ClientUtilsABC,
permission_service: PermissionServiceABC,
translate: TranslatePipe,
db: DatabaseContextABC,
servers: ServerRepositoryABC,
users: UserRepositoryABC,
api_keys: ApiKeyRepositoryABC,
):
DiscordCommandABC.__init__(self)
self._logger = logger
self._auth_settings = auth_settings
self._message_service = message_service
self._bot = bot
self._client_utils = client_utils
self._permissions = permission_service
self._t = translate
self._db = db
self._servers = servers
self._users = users
self._api_keys = api_keys
def _get_api_key_str(self, api_key: ApiKey) -> str:
return hashlib.sha256(
f"{api_key.identifier}:{api_key.key}+{self._auth_settings.secret_key}".encode("utf-8")
).hexdigest()
@commands.hybrid_group(name="api-key")
@commands.guild_only()
async def api_key(self, ctx: Context):
pass
@api_key.command()
@commands.guild_only()
@CommandChecks.check_is_ready()
@CommandChecks.check_is_member_technician()
async def get(self, ctx: Context, key: str, wait: int = None):
self._logger.debug(__name__, f"Received command api-key get {ctx}: {key},{wait}")
api_key = self._api_keys.get_api_key_by_key(key)
await self._message_service.send_ctx_msg(
ctx,
self._t.transform("modules.technician.api_key.get").format(
api_key.identifier, self._get_api_key_str(api_key)
),
)
self._logger.trace(__name__, f"Finished command api-key get")
@get.autocomplete("key")
async def get_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]:
keys = self._api_keys.get_api_keys()
return [
app_commands.Choice(name=f"{key.identifier}: {key.key}", value=key.key)
for key in self._client_utils.get_auto_complete_list(keys, current, lambda x: x.key)
]
@api_key.command()
@commands.guild_only()
@CommandChecks.check_is_ready()
@CommandChecks.check_is_member_moderator()
async def add(self, ctx: Context, identifier: str):
self._logger.debug(__name__, f"Received command api-key add {ctx}: {identifier}")
server = self._servers.get_server_by_discord_id(ctx.guild.id)
user = self._users.get_user_by_discord_id_and_server_id(ctx.author.id, server.server_id)
api_key = ApiKey(identifier, str(uuid.uuid4()), user)
self._api_keys.add_api_key(api_key)
self._db.save_changes()
await self._message_service.send_ctx_msg(
ctx,
self._t.transform("modules.technician.api_key.add.success").format(
identifier, self._get_api_key_str(api_key)
),
)
self._logger.trace(__name__, f"Finished command api-key add")
@api_key.command()
@commands.guild_only()
@CommandChecks.check_is_ready()
@CommandChecks.check_is_member_moderator()
async def remove(self, ctx: Context, key: str):
self._logger.debug(__name__, f"Received command api-key remove {ctx}: {key}")
keys = self._api_keys.get_api_keys().where(lambda x: x.key == key)
if keys.count() < 1:
await self._message_service.send_ctx_msg(
ctx,
self._t.transform("modules.technician.api_key.remove.not_found"),
)
api_key = keys.single()
self._api_keys.delete_api_key(api_key)
self._db.save_changes()
await self._message_service.send_ctx_msg(ctx, self._t.transform("modules.technician.api_key.remove.success"))
self._logger.trace(__name__, f"Finished command api-key remove")
@remove.autocomplete("key")
async def set_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]:
keys = self._api_keys.get_api_keys()
return [
app_commands.Choice(name=f"{key.identifier}: {key.key}", value=key.key)
for key in self._client_utils.get_auto_complete_list(keys, current, lambda x: x.key)
]

View File

@@ -6,10 +6,11 @@ from cpl_discord.service.discord_collection_abc import DiscordCollectionABC
from bot_core.abc.module_abc import ModuleABC
from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum
from modules.base.abc.base_helper_abc import BaseHelperABC
from modules.base.service.base_helper_service import BaseHelperService
from modules.technician.command.api_key_group import ApiKeyGroup
from modules.technician.command.log_command import LogCommand
from modules.technician.command.restart_command import RestartCommand
from modules.technician.command.shutdown_command import ShutdownCommand
from modules.base.service.base_helper_service import BaseHelperService
class TechnicianModule(ModuleABC):
@@ -25,4 +26,5 @@ class TechnicianModule(ModuleABC):
self._dc.add_command(RestartCommand)
self._dc.add_command(ShutdownCommand)
self._dc.add_command(LogCommand)
self._dc.add_command(ApiKeyGroup)
# events