diff options
| author | Arslaan Pathan <[email protected]> | 2026-07-09 12:49:22 +1200 |
|---|---|---|
| committer | Arslaan Pathan <[email protected]> | 2026-07-09 12:49:22 +1200 |
| commit | d9499138aeaf32faee65640e624719df83f465eb (patch) | |
| tree | 7e40a70858306a86b8cde5b8e329d2033893dc71 /src/auth.py | |
| parent | d9d6d7ee74781da95be8b319f84fe7f0a2ea7ee0 (diff) | |
| download | slack-botnet-d9499138aeaf32faee65640e624719df83f465eb.tar.xz slack-botnet-d9499138aeaf32faee65640e624719df83f465eb.zip | |
Make OAuth work and send a welcome message!
Diffstat (limited to 'src/auth.py')
| -rw-r--r-- | src/auth.py | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/auth.py b/src/auth.py new file mode 100644 index 0000000..3f8ff02 --- /dev/null +++ b/src/auth.py @@ -0,0 +1,44 @@ +from fastapi import FastAPI, Request, Query +from fastapi.responses import RedirectResponse +import httpx +import os +from .db import add_user +from dotenv import load_dotenv + +load_dotenv() + +app = FastAPI() + +CLIENT_ID = os.environ.get("SLAKC_CLIENT_ID") +CLIENT_SECRET = os.environ.get("SLAKC_CLIENT_SECRET") +REDIRECT_URI = os.environ.get("SLAKC_REDIRECT_URI") + [email protected]("/slack/oauth/install") +async def install(): + slack_auth_url = ( + "https://slack.com/oauth/v2/authorize" + f"?client_id={CLIENT_ID}" + f"&redirect_uri={REDIRECT_URI}" + "&scope=" + "&user_scope=channels:write,channels:history,channels:read,chat:write,im:history,users:read" + ) + return RedirectResponse(slack_auth_url) + [email protected]("/slack/oauth/redirect") +async def oauth_redirect(request: Request, code: str = Query(...), state: str = Query(None)): + async with httpx.AsyncClient() as client: + response = await client.post("https://slack.com/api/oauth.v2.access", data={"client_id": CLIENT_ID, "client_secret": CLIENT_SECRET, "code": code, "redirect_uri": REDIRECT_URI}) + data = response.json() + + if not data.get("ok"): + return {"error": data.get("error", "Unknown error")} + + user_id = data.get("authed_user", {}).get("id") + access_token = data.get("authed_user", {}).get("access_token") + + if not user_id or not access_token: + return {"error": "Could not get user info from OAuth response"} + + await add_user(user_id, access_token) + + return {"message": "Successfully joined Botnet!", "user_id": user_id} |
