diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..367c07d --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +#all files relating to python vitual env +/.venv +#environment variables and cache +*.env +*/__pycache__/* diff --git a/__pycache__/convert_to_json.cpython-311.pyc b/__pycache__/convert_to_json.cpython-311.pyc new file mode 100644 index 0000000..82eb3d6 Binary files /dev/null and b/__pycache__/convert_to_json.cpython-311.pyc differ diff --git a/__pycache__/db.cpython-311.pyc b/__pycache__/db.cpython-311.pyc new file mode 100644 index 0000000..6396a75 Binary files /dev/null and b/__pycache__/db.cpython-311.pyc differ diff --git a/__pycache__/main.cpython-311.pyc b/__pycache__/main.cpython-311.pyc new file mode 100644 index 0000000..9391935 Binary files /dev/null and b/__pycache__/main.cpython-311.pyc differ diff --git a/db.py b/db.py new file mode 100644 index 0000000..31d8fb0 --- /dev/null +++ b/db.py @@ -0,0 +1,39 @@ +import os +from typing import Any +from supabase import create_client, Client +import dotenv + +dotenv.load_dotenv(".env") + +url: str = os.getenv("SUPABASE_URL") +key: str = os.getenv("SUPABASE_KEY") + +class SupabaseInterface: + def __init__(self, url, key) -> None: + self.supabase_url = url + self.supabase_key = key + self.client: Client = create_client(self.supabase_url, self.supabase_key) + + def add_user(self, userdata): + + data = self.client.table("users").insert(userdata).execute() + print(data.data) + return data + + def user_exists(self, discord_id): + data = self.client.table("users").select("*").eq("discord_id", discord_id).execute() + if len(data.data)>0: + return True + else: + return False + + + +tester = SupabaseInterface(url,key) +# tester.add_user({ +# "discord_id": 476285280811483140, +# "github_id": 74085496 + +# }) + +print(tester.user_exists(476285280811483141)) \ No newline at end of file diff --git a/main.py b/main.py index 4e2f79e..be8327c 100644 --- a/main.py +++ b/main.py @@ -1,22 +1,78 @@ -from flask import Flask +from flask import Flask, redirect, render_template import requests from flask import request +import dotenv +import os +from db import SupabaseInterface + +dotenv.load_dotenv(".env") app = Flask(__name__) +app.config['TESTING']= True +app.config['SECRET_KEY']=os.getenv("FLASK_SESSION_KEY") @app.route("/") def hello_world(): return "

Hello, World!

" -@app.route("/register/") -def get_data(discord_id): - +@app.route("/already_authenticated") +def isAuthenticated(): + return render_template('success.html'), {"Refresh": f'1; url=https://discord.com/channels/{os.getenv("DISCORD_SERVER_ID")}'} +@app.route("/authenticate/") +def authenticate(discord_userdata): + redirect_uri = f'{os.getenv("HOST")}/register/{discord_userdata}' + github_auth_url = f'https://github.com/login/oauth/authorize?client_id={os.getenv("GITHUB_CLIENT_ID")}&redirect_uri={redirect_uri}' + return redirect(github_auth_url) +#this is where github calls back to +@app.route("/register/") +def register(discord_userdata): + url = os.getenv("SUPABASE_URL") + key = os.getenv("SUPABASE_KEY") - + #Extrapolate discord data from callback + #$ sign is being used as separator + [discord_id, discord_username, role] = discord_userdata.split('$') + + #Check if the user is registered + supabase_client = SupabaseInterface(url=url, key=key) + if supabase_client.user_exists(discord_id=discord_id): + print('true') + authenticated_url = f'{os.getenv("HOST")}/already_authenticated' + return redirect(authenticated_url) + #get github ID + github_url_for_access_token = 'https://github.com/login/oauth/access_token' + data = { + "client_id": os.getenv("GITHUB_CLIENT_ID"), + "client_secret": os.getenv("GITHUB_CLIENT_SECRET"), + "code": request.args.get("code") + } + header = { + "Accept":"application/json" + } + r = requests.post(github_url_for_access_token, data=data, headers=header) + auth_token = r.json()["access_token"] + user = requests.get("https://api.github.com/user", headers={ + "Authorization": f"Bearer {auth_token}" + }) + print(user.json()) + + github_id = user.json()["id"] + github_username = user.json()["login"] + + + #adding to the database + supabase_client.add_user({ + "discord_id": int(discord_id), + "github_id": github_id, + "github_url": f"https://github.com/{github_username}", + "discord_username": discord_username, + "role": role + }) - return '' \ No newline at end of file + + return render_template('success.html'), {"Refresh": f'1; url=https://discord.com/channels/{os.getenv("DISCORD_SERVER_ID")}'} \ No newline at end of file diff --git a/samples/userAuthenticationResponse.json b/samples/userAuthenticationResponse.json new file mode 100644 index 0000000..1780498 --- /dev/null +++ b/samples/userAuthenticationResponse.json @@ -0,0 +1,34 @@ +{ + "login": "KDwevedi", + "id": 74085496, + "node_id": "MDQ6VXNlcjc0MDg1NDk2", + "avatar_url": "https://avatars.githubusercontent.com/u/74085496?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/KDwevedi", + "html_url": "https://github.com/KDwevedi", + "followers_url": "https://api.github.com/users/KDwevedi/followers", + "following_url": "https://api.github.com/users/KDwevedi/following{/other_user}", + "gists_url": "https://api.github.com/users/KDwevedi/gists{/gist_id}", + "starred_url": "https://api.github.com/users/KDwevedi/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/KDwevedi/subscriptions", + "organizations_url": "https://api.github.com/users/KDwevedi/orgs", + "repos_url": "https://api.github.com/users/KDwevedi/repos", + "events_url": "https://api.github.com/users/KDwevedi/events{/privacy}", + "received_events_url": "https://api.github.com/users/KDwevedi/received_events", + "type": "User", + "site_admin": false, + "name": null, + "company": null, + "blog": "", + "location": null, + "email": null, + "hireable": null, + "bio": null, + "twitter_username": null, + "public_repos": 15, + "public_gists": 1, + "followers": 0, + "following": 1, + "created_at": "2020-11-07T03:48:50Z", + "updated_at": "2023-05-20T09:13:17Z" +} \ No newline at end of file diff --git a/templates/success.html b/templates/success.html new file mode 100644 index 0000000..7c0b272 --- /dev/null +++ b/templates/success.html @@ -0,0 +1,10 @@ + + + + + Success! + + +

You have been authenticated! Redirecting...

+ + \ No newline at end of file diff --git a/vercel.json b/vercel.json deleted file mode 100644 index 76ecd73..0000000 --- a/vercel.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "builds": [ - { - "src": "api/main.py", - "use": "@vercel/python" - } - ], - "routes": [ - { - "src": "/(.*)", - "dest": "api/main.py" - } - ] -} \ No newline at end of file