diff --git a/querybook/config/querybook_default_config.yaml b/querybook/config/querybook_default_config.yaml index 42bd611c2..268a12424 100644 --- a/querybook/config/querybook_default_config.yaml +++ b/querybook/config/querybook_default_config.yaml @@ -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 diff --git a/querybook/server/datasources/github.py b/querybook/server/datasources/github.py index 82420604c..08601cdf5 100644 --- a/querybook/server/datasources/github.py +++ b/querybook/server/datasources/github.py @@ -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} diff --git a/querybook/server/env.py b/querybook/server/env.py index 2128717a3..2eedb83d9 100644 --- a/querybook/server/env.py +++ b/querybook/server/env.py @@ -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" diff --git a/querybook/server/lib/github_integration/__init__.py b/querybook/server/lib/github/__init__.py similarity index 100% rename from querybook/server/lib/github_integration/__init__.py rename to querybook/server/lib/github/__init__.py diff --git a/querybook/server/lib/github_integration/github_integration.py b/querybook/server/lib/github/github.py similarity index 65% rename from querybook/server/lib/github_integration/github_integration.py rename to querybook/server/lib/github/github.py index 4cced5956..ad3b46232 100644 --- a/querybook/server/lib/github_integration/github_integration.py +++ b/querybook/server/lib/github/github.py @@ -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 @@ -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 @@ -70,41 +81,13 @@ def error_response(self, error_message: str) -> str: """ -def get_github_manager() -> GitHubIntegrationManager: - return GitHubIntegrationManager(additional_scopes=["repo"]) +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()