2023-08-12 17:49:31 +02:00
|
|
|
"""This module contains functions for authenticating with providers."""
|
|
|
|
|
2023-09-02 21:15:55 +02:00
|
|
|
import os
|
2023-08-07 23:28:24 +02:00
|
|
|
import asyncio
|
|
|
|
|
2023-09-02 21:15:55 +02:00
|
|
|
from dotenv import load_dotenv
|
|
|
|
from dhooks import Webhook, Embed
|
|
|
|
|
|
|
|
load_dotenv()
|
|
|
|
|
|
|
|
async def invalidation_webhook(provider_and_key: str) -> None:
|
|
|
|
"""Runs when a new user is created."""
|
|
|
|
|
|
|
|
dhook = Webhook(os.environ['DISCORD_WEBHOOK__API_ISSUE'])
|
|
|
|
|
|
|
|
embed = Embed(
|
|
|
|
description='Key Invalidated',
|
|
|
|
color=0xffee90,
|
|
|
|
)
|
|
|
|
|
|
|
|
embed.add_field(name='Provider', value=provider_and_key.split('>')[0])
|
|
|
|
embed.add_field(name='Key (censored)', value=f'||{provider_and_key.split(">")[1][:10]}...||', inline=False)
|
|
|
|
|
|
|
|
dhook.send(embed=embed)
|
|
|
|
|
2023-08-12 18:20:18 +02:00
|
|
|
async def invalidate_key(provider_and_key: str) -> None:
|
2023-08-12 18:05:53 +02:00
|
|
|
"""
|
2023-08-12 18:20:18 +02:00
|
|
|
|
2023-08-12 18:05:53 +02:00
|
|
|
Invalidates a key stored in the secret/ folder by storing it in the associated .invalid.txt file.
|
|
|
|
The schmea in which <provider_and_key> should be passed is:
|
|
|
|
<provider_name><key>, e.g.
|
2023-08-18 21:23:00 +02:00
|
|
|
closed4>cd-...
|
2023-08-12 18:05:53 +02:00
|
|
|
|
|
|
|
"""
|
2023-08-12 17:49:31 +02:00
|
|
|
|
2023-08-07 23:28:24 +02:00
|
|
|
if not provider_and_key:
|
|
|
|
return
|
|
|
|
|
|
|
|
provider = provider_and_key.split('>')[0]
|
|
|
|
provider_file = f'secret/{provider}.txt'
|
|
|
|
key = provider_and_key.split('>')[1]
|
|
|
|
|
|
|
|
with open(provider_file, encoding='utf8') as f_in:
|
|
|
|
text = f_in.read()
|
|
|
|
|
|
|
|
with open(provider_file, 'w', encoding='utf8') as f_out:
|
|
|
|
f_out.write(text.replace(key, ''))
|
|
|
|
|
|
|
|
with open(f'secret/{provider}.invalid.txt', 'a', encoding='utf8') as f:
|
|
|
|
f.write(key + '\n')
|
|
|
|
|
2023-10-02 21:09:39 +02:00
|
|
|
# await invalidation_webhook(provider_and_key)
|
2023-09-02 21:15:55 +02:00
|
|
|
|
2023-08-07 23:28:24 +02:00
|
|
|
if __name__ == '__main__':
|
2023-09-02 21:15:55 +02:00
|
|
|
asyncio.run(invalidate_key('closed>demo-...'))
|