import asyncio import logging from services.agenthandler import selectAgents from services.API import AirlockAPIWrapper from utils.Selector import Selector from utils.utils import colorText, get_sanitized_input logger = logging.getLogger(__name__) async def generate(api: AirlockAPIWrapper): otp_dict = {} agents = await selectAgents(api) print(colorText("Would you like to continue with these devices?", "white")) for agent in agents: print(agent.hostname) confirm = await Selector.confirm() if agents and confirm: requester = await get_sanitized_input("Who is requesting the OTP: ") because = await get_sanitized_input("Why/What work are they doing?: ") purpose = f"Requester: {requester} - for : {because}" possible_durations = [15, 60, 360, 1440, 10080] print(colorText("Please select a duration in minutes: ", "white")) print(colorText("15 mins, 60 mins, 360 mins(6 Hours), 1440 mins (24 Hours), 10080 mins (7 Days):", "white")) duration_selected = await Selector.select_int(possible_durations) if duration_selected: async def generate_otp(agent): otp_code = await api.otp_generate(agent.agentid, duration_selected, purpose) # pyright: ignore[reportArgumentType] logger.info(f"Generated OTP for {agent.hostname}: {otp_code}") return agent.hostname, otp_code results = await asyncio.gather(*(generate_otp(agent) for agent in agents)) otp_dict = dict(results) return otp_dict async def otp_activities_by_agent(api: AirlockAPIWrapper): agents = await selectAgents(api) otp_dict = {} for agent in agents: otp_info = await api.otp_find_by_agent(agent.agentid) otp_dict[agent.hostname] = otp_info return otp_dict async def revoke(api: AirlockAPIWrapper): otp_dict = await otp_activities_by_agent(api) list_to_revoke = [entry["otpid"] for entry in otp_dict.values() if entry] if otp_dict and list_to_revoke: async def revoke_otp(otpid): await api.otp_revoke(otpid) await asyncio.gather(*(revoke_otp(otpid) for otpid in list_to_revoke))