diff --git a/.gitignore b/.gitignore index b6e4761..b97131e 100644 --- a/.gitignore +++ b/.gitignore @@ -127,3 +127,5 @@ dmypy.json # Pyre type checker .pyre/ + +.idea/ diff --git a/main.py b/main.py new file mode 100644 index 0000000..fe49599 --- /dev/null +++ b/main.py @@ -0,0 +1,32 @@ +import uuid +from fastapi import FastAPI +from starlette.responses import RedirectResponse + +from utils import gen_url, get_auth + +app = FastAPI() + + +@app.get('/gen') +async def gen( + *, + username: str, + host: str, + back_host: str, +): + session = str(uuid.uuid4()) + return RedirectResponse(gen_url(username, host, back_host, session)) + + +@app.get("/{username}/{host}") +async def back_to_telegram( + *, + username: str, + host: str, + session: str, +): + data = await get_auth(host, session) + if data.get("ok", False): + return RedirectResponse(f"https://t.me/{username}?start={data['token']}") + else: + return data diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..d3864c6 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +httpx==0.23.3 +fastapi==0.89.1 +starlette==0.23.1 +uvicorn==0.20.0 diff --git a/utils.py b/utils.py new file mode 100644 index 0000000..60aa137 --- /dev/null +++ b/utils.py @@ -0,0 +1,27 @@ +import urllib.parse + +from httpx import AsyncClient + +client = AsyncClient() + + +async def get_auth(host: str, session: str): + url = f"https://{host}/api/miauth/{session}/check" + response = await client.post(url) + return response.json() + + +def callback_url(username: str, host: str, back_host: str): + return f"https://{back_host}/{username}/{host}" + + +def gen_url(username: str, host: str, back_host: str, session: str): + return f"https://{host}/miauth/{session}?name=Misskey2Telegram&callback=" \ + f"{urllib.parse.quote(callback_url(username, host, back_host))}" \ + f"&permission=read:account,write:account,read:blocks,write:blocks,read:drive," \ + f"write:drive,read:favorites,write:favorites,read:following,write:following," \ + f"read:messaging,write:messaging,read:mutes,write:mutes,write:notes,read:notifications," \ + f"write:notifications,read:reactions,write:reactions,write:votes,read:pages," \ + f"write:pages,write:page-likes,read:page-likes,read:user-groups,write:user-groups," \ + f"read:channels,write:channels,read:gallery,write:gallery,read:gallery-likes," \ + f"write:gallery-likes" diff --git a/vercel.json b/vercel.json new file mode 100644 index 0000000..9414fa7 --- /dev/null +++ b/vercel.json @@ -0,0 +1,4 @@ +{ + "builds": [{ "src": "main.py", "use": "@vercel/python" }], + "routes": [{ "src": "/(.*)", "dest": "main.py" }] +} \ No newline at end of file