Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(github): Add Github OAuth for the bot. #92

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions bot/github/oauth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import json

import requests
from bottle import redirect, request

client_id = '9cfb35031c71d9f6e013'
client_secret = '2e7e793b001390e9f2c9dd2e0033747bff890896'
redirect_uri = 'http://localhost:5556/github/oauth/redirect'
scopes = 'admin:repo_hook'
auth_url = 'https://github.com/login/oauth/authorize/?client_id=' + client_id + '&redirect_uri=' + redirect_uri + '&scope=' + scopes
get_token_url = 'https://github.com/login/oauth/access_token'


def redirector() -> None:
redirect(auth_url)


def authorizer() -> str:
auth_code: str = request.query.get('code')
if auth_code:
token: str = exchanger(auth_code)
return f"Access-token = {token}"
else:
return "Authorization failed!"


def exchanger(code: str) -> str:
parameters = {
'code': code,
'client_id': client_id,
'client_secret': client_secret,
'redirect_uri': redirect_uri
}
payload: str = json.dumps(parameters)
header: dict[str, str] = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}

r = requests.post(get_token_url, data=payload, headers=header)

if r.status_code != 200:
return "Failed to get Access-Token"
else:
data = r.json()
token = data['access_token']
return token
12 changes: 12 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from sentry_sdk.integrations.bottle import BottleIntegration

from bot.github.github_parsers import GitHubPayloadParser
from bot.github.oauth import authorizer, redirector
from bot.models.github.event import GitHubEvent
from bot.slack import SlackBot
from bot.utils.log import Logger
Expand Down Expand Up @@ -79,6 +80,17 @@ def manage_slack_commands() -> dict | None:
return response


@get("/github/oauth")
def initiate_oauth() -> None:
redirector()


@get("/github/oauth/redirect")
def get_access_token() -> str:
token = authorizer()
return token


if __name__ == "__main__":
load_dotenv(Path(".") / ".env")
debug: bool = os.environ["DEBUG"] == "1"
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
bottle==0.12.20
python-dotenv==0.19.2
requests==2.28.1
sentry-sdk[bottle]==0.16.2
slackclient==2.9.3