From 530b448b34d04e4f5211710291c5f4249f6491e2 Mon Sep 17 00:00:00 2001 From: "raoha.rh" Date: Wed, 21 Aug 2024 17:02:47 +0800 Subject: [PATCH] fix: fix/authoriaztion-failed --- server/dao/repositoryConfigDAO.py | 3 +-- server/event_handler/issue.py | 2 +- server/event_handler/pull_request.py | 4 ++-- server/requirements.txt | 2 +- server/routers/github.py | 18 +++++++++++------- 5 files changed, 16 insertions(+), 13 deletions(-) diff --git a/server/dao/repositoryConfigDAO.py b/server/dao/repositoryConfigDAO.py index c292a296..ece2ea02 100644 --- a/server/dao/repositoryConfigDAO.py +++ b/server/dao/repositoryConfigDAO.py @@ -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 diff --git a/server/event_handler/issue.py b/server/event_handler/issue.py index 84d6b431..322c4689 100644 --- a/server/event_handler/issue.py +++ b/server/event_handler/issue.py @@ -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) diff --git a/server/event_handler/pull_request.py b/server/event_handler/pull_request.py index a9aab4c0..33f6e665 100644 --- a/server/event_handler/pull_request.py +++ b/server/event_handler/pull_request.py @@ -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"]) diff --git a/server/requirements.txt b/server/requirements.txt index 5e29c17d..a2c0e7c3 100644 --- a/server/requirements.txt +++ b/server/requirements.txt @@ -14,7 +14,7 @@ httpx[socks] load_dotenv supabase boto3>=1.34.84 -jwt +PyJWT pydantic>=2.7.0 unstructured[md] python-dotenv diff --git a/server/routers/github.py b/server/routers/github.py index cae15870..06b95661 100644 --- a/server/routers/github.py +++ b/server/routers/github.py @@ -4,7 +4,7 @@ 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 @@ -12,7 +12,10 @@ 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") @@ -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" @@ -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") @@ -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,