Skip to content

Commit

Permalink
fix: fix/authoriaztion-failed (#241)
Browse files Browse the repository at this point in the history
- authoriaztion-failed
  • Loading branch information
RaoHai authored Aug 21, 2024
2 parents 2b2e2ec + 530b448 commit aa797d2
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 13 deletions.
3 changes: 1 addition & 2 deletions server/dao/repositoryConfigDAO.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

import json
from dao.BaseDAO import BaseDAO
from models.repository import RepositoryConfig
from supabase.client import Client, create_client
from supabase.client import Client

from petercat_utils.db.client.supabase import get_client

Expand Down
2 changes: 1 addition & 1 deletion server/event_handler/issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class IssueEventHandler:
auth: Auth.AppAuth
g: Github

def __init__(self, payload: Any, auth: Auth.AppAuth) -> None:
def __init__(self, payload: Any, auth: Auth.AppAuth, installation_id: int) -> None:
self.event: Any = payload
self.auth: Auth.AppAuth = auth
self.g: Github = Github(auth=auth)
Expand Down
4 changes: 2 additions & 2 deletions server/event_handler/pull_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ class PullRequestEventHandler():
auth: Auth.AppAuth
g: Github

def __init__(self, payload: Any, auth: Auth.AppAuth) -> None:
def __init__(self, payload: Any, auth: Auth.AppAuth, installation_id: int) -> None:
self.event: Any = payload
self.auth: Auth.AppAuth = auth
self.g: Github = Github(auth=auth)

def execute(self):
async def execute(self):
try:
if self.event['action'] == 'opened':
repo = self.g.get_repo(self.event['repository']["full_name"])
Expand Down
2 changes: 1 addition & 1 deletion server/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ httpx[socks]
load_dotenv
supabase
boto3>=1.34.84
jwt
PyJWT
pydantic>=2.7.0
unstructured[md]
python-dotenv
Expand Down
18 changes: 11 additions & 7 deletions server/routers/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
from fastapi.responses import RedirectResponse
import requests
import time
from github import Auth, Github, Organization
from github import Auth, Github
from auth.get_user_info import get_user_access_token
from dao.authorizationDAO import AuthorizationDAO
from dao.repositoryConfigDAO import RepositoryConfigDAO
from models.repository import RepositoryConfig
from models.authorization import Authorization
from utils.github import get_handler, get_private_key
from petercat_utils import get_env_variable
from jwt import JWT, jwk_from_pem

import jwt
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.backends import default_backend

APP_ID = get_env_variable("X_GITHUB_APP_ID")
WEB_URL = get_env_variable("WEB_URL")
Expand All @@ -37,11 +40,10 @@ def get_jwt():
}

pem = get_private_key()
signing_key = jwk_from_pem(pem.encode("utf-8"))

print(pem)
jwt_instance = JWT()
return jwt_instance.encode(payload, signing_key, alg='RS256')
private_key = serialization.load_pem_private_key(
pem.encode("utf-8"), password=None, backend=default_backend()
)
return jwt.encode(payload, private_key, algorithm='RS256')

def get_app_installations_access_token(installation_id: str, jwt: str):
url = f"https://api.github.com/app/installations/{installation_id}/access_tokens"
Expand All @@ -65,6 +67,7 @@ def get_installation_repositories(access_token: str):
'Authorization': f"Bearer {access_token}"
})
return resp.json()


# https://github.com/login/oauth/authorize?client_id=Iv1.c2e88b429e541264
@router.get("/app/installation/callback")
Expand All @@ -78,6 +81,7 @@ def github_app_callback(code: str, installation_id: str, setup_action: str):
else:
jwt = get_jwt()
access_token = get_app_installations_access_token(installation_id=installation_id, jwt=jwt)
print(f"get_app_installations_access_token: {access_token}")
authorization = Authorization(
**access_token,
code=code,
Expand Down

0 comments on commit aa797d2

Please sign in to comment.