Skip to content

Commit

Permalink
auth endpoints and auth improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Francesco Cosentino committed Dec 15, 2023
1 parent 96c15f2 commit d5e26c3
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
47 changes: 47 additions & 0 deletions api/routers/auth.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Bridge Auth Router."""
import json
import os

from fastapi import APIRouter

Expand Down Expand Up @@ -62,3 +63,49 @@ async def telegram_auth(auth: TelegramAuthSchema):
error="",
)
)


@router.delete(
"/telegram",
name="Sign out from Telegram",
summary="Clears the Telegram authentication.",
description="Clears the Telegram authentication and session files.",
response_model=TelegramAuthResponseSchema,
)
async def telegram_deauth():
"""Clears the Telegram authentication"""
config = Config.get_instance()

try:
# Remove the Telegram auth file.
if os.path.isfile(config.api.telegram_auth_file):
os.remove(config.api.telegram_auth_file)

# Remove the session file.
if os.path.isfile(f"{config.application.name}.session"):
os.remove(f"{config.application.name}.session")

return TelegramAuthResponseSchema(
auth=TelegramAuthResponse(
status="success",
message="signed out from the Telegram API successfully.",
error="",
)
)

except OSError as ex:
return TelegramAuthResponseSchema(
auth=TelegramAuthResponse(
status="failed",
message="failed to sign out from the Telegram API.",
error=ex.strerror,
)
)
except Exception as ex: # pylint: disable=broad-except
return TelegramAuthResponseSchema(
auth=TelegramAuthResponse(
status="failed",
message="failed to sign out from the Telegram API.",
error=str(ex),
)
)
13 changes: 8 additions & 5 deletions bridge/telegram/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
from asyncio.events import AbstractEventLoop

from telethon import TelegramClient
from telethon.errors.rpcerrorlist import (FloodWaitError,
PhoneCodeInvalidError,
SessionPasswordNeededError,
SessionRevokedError)
from telethon.errors.rpcerrorlist import (
FloodWaitError,
PhoneCodeInvalidError,
SessionPasswordNeededError,
SessionRevokedError,
)

from bridge.config import Config
from bridge.events import EventDispatcher
Expand Down Expand Up @@ -207,7 +209,8 @@ def password_callback():
json.dump(auth_data, auth_file)
raise

# os.remove(config.telegram.auth_file)
if os.path.isfile(config.api.telegram_auth_file):
os.remove(config.api.telegram_auth_file)

bot_identity = await telegram_client.get_me(input_peer=False)
logger.info(
Expand Down
3 changes: 3 additions & 0 deletions tools/kill-server.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

kill -9 "$(ps aux | grep python | awk '{print $2}')"

0 comments on commit d5e26c3

Please sign in to comment.