Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangvi7 committed Oct 29, 2024
1 parent 5a6db84 commit 0ac6e33
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 41 deletions.
4 changes: 4 additions & 0 deletions querybook/config/querybook_default_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ OAUTH_AUTHORIZATION_URL: ~
OAUTH_TOKEN_URL: ~
OAUTH_USER_PROFILE: ~

# --------------- GitHub Integration ---------------
GITHUB_CLIENT_ID: ~
GITHUB_CLIENT_SECRET: ~

# LDAP
LDAP_CONN: ~
LDAP_USER_DN: uid={},dc=example,dc=com
Expand Down
4 changes: 1 addition & 3 deletions querybook/server/datasources/github.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
from app.datasource import register
from lib.github_integration.github_integration import get_github_manager
from lib.github.github import github_manager
from typing import Dict


@register("/github/auth/", methods=["GET"])
def connect_github() -> Dict[str, str]:
github_manager = get_github_manager()
return github_manager.initiate_github_integration()


@register("/github/is_authenticated/", methods=["GET"])
def is_github_authenticated() -> str:
github_manager = get_github_manager()
is_authenticated = github_manager.get_github_token() is not None
return {"is_authenticated": is_authenticated}
4 changes: 4 additions & 0 deletions querybook/server/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ class QuerybookSettings(object):
OAUTH_USER_PROFILE = get_env_config("OAUTH_USER_PROFILE")
AZURE_TENANT_ID = get_env_config("AZURE_TENANT_ID")

# GitHub App settings for feature integration
GITHUB_CLIENT_ID = os.getenv("GITHUB_CLIENT_ID")
GITHUB_CLIENT_SECRET = os.getenv("GITHUB_CLIENT_SECRET")

LDAP_CONN = get_env_config("LDAP_CONN")
LDAP_USE_TLS = str(get_env_config("LDAP_USE_TLS")).lower() == "true"
LDAP_USE_BIND_USER = str(get_env_config("LDAP_USE_BIND_USER")).lower() == "true"
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@

LOG = get_logger(__file__)


GITHUB_OAUTH_CALLBACK = "/github/oauth2callback"
GITHUB_ACCESS_TOKEN = "github_access_token"


class GitHubIntegrationManager(GitHubLoginManager):
def __init__(self, additional_scopes: Optional[list] = None):
class GitHubManager(GitHubLoginManager):
def __init__(
self,
additional_scopes: Optional[list] = None,
client_id: Optional[str] = None,
client_secret: Optional[str] = None,
):
self.additional_scopes = additional_scopes or []
self._client_id = client_id
self._client_secret = client_secret
super().__init__()

@property
Expand All @@ -24,14 +31,18 @@ def oauth_config(self) -> Dict[str, Any]:
config[
"callback_url"
] = f"{QuerybookSettings.PUBLIC_URL}{GITHUB_OAUTH_CALLBACK}"
if self._client_id:
config["client_id"] = self._client_id
if self._client_secret:
config["client_secret"] = self._client_secret
return config

def save_github_token(self, token: str) -> None:
flask_session["github_access_token"] = token
flask_session[GITHUB_ACCESS_TOKEN] = token
LOG.debug("Saved GitHub token to session")

def get_github_token(self) -> Optional[str]:
return flask_session.get("github_access_token")
return flask_session.get(GITHUB_ACCESS_TOKEN)

def initiate_github_integration(self) -> Dict[str, str]:
github = self.oauth_session
Expand Down Expand Up @@ -70,41 +81,14 @@ def error_response(self, error_message: str) -> str:
"""


def get_github_manager() -> GitHubIntegrationManager:
return GitHubIntegrationManager(additional_scopes=["repo"])
# Create a module-level instance with dynamic client_id and client_secret
github_manager = GitHubManager(
additional_scopes=["repo"],
client_id=QuerybookSettings.GITHUB_CLIENT_ID,
client_secret=QuerybookSettings.GITHUB_CLIENT_SECRET,
)


@flask_app.route(GITHUB_OAUTH_CALLBACK)
def github_callback() -> str:
github_manager = get_github_manager()
return github_manager.github_integration_callback()


# Test GitHub OAuth Flow
def main():
github_manager = GitHubIntegrationManager()
oauth_config = github_manager.oauth_config
client_id = oauth_config["client_id"]
client_secret = oauth_config["client_secret"]

from requests_oauthlib import OAuth2Session

github = OAuth2Session(client_id)
authorization_url, state = github.authorization_url(
oauth_config["authorization_url"]
)
print("Please go here and authorize,", authorization_url)

redirect_response = input("Paste the full redirect URL here:")
github.fetch_token(
oauth_config["token_url"],
client_secret=client_secret,
authorization_response=redirect_response,
)

user_profile = github.get(oauth_config["profile_url"]).json()
print(user_profile)


if __name__ == "__main__":
main()

0 comments on commit 0ac6e33

Please sign in to comment.