-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation of the login page through gihub, the installation sele…
…ction page through github as well and of the webhook handler to update the mongo db on each commit to master
- Loading branch information
Pugnet
committed
Jun 29, 2019
1 parent
04663a7
commit bfff63c
Showing
32 changed files
with
760 additions
and
263 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,4 @@ __pycache__/ | |
node_modules | ||
.idea | ||
*.lock | ||
*.pyc | ||
*.pyc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,84 @@ | ||
from github import Github | ||
import hmac | ||
|
||
import requests | ||
from github import Github, GithubIntegration | ||
from hashlib import sha1 | ||
|
||
from github_interface.github_types.github_repository import GithubRepository | ||
from mongo.credentials import CredentialsManager | ||
from utils.constants import GITHUB_APP_IDENTIFIER | ||
|
||
|
||
class GithubInterface: | ||
|
||
def __init__(self, access_token): | ||
self.__github_account = Github(access_token) | ||
self.__repo_cache = {} | ||
__repo_cache = {} | ||
|
||
# TODO: use repo id and not repo name for the cache | ||
@staticmethod | ||
def get_repo(repo_name, is_user_access_token=False): | ||
github_account = Github(GithubInterface.__fetch_access_token_from_db(is_user_access_token)) | ||
|
||
def get_repo(self, repo_name): | ||
if repo_name in self.__repo_cache: | ||
repo = self.__repo_cache[repo_name] | ||
if repo_name in GithubInterface.__repo_cache: | ||
repo = GithubInterface.__repo_cache[repo_name] | ||
else: | ||
self.__repo_cache[repo_name] = GithubRepository(self.__github_account.get_repo(repo_name)) | ||
repo = self.__repo_cache[repo_name] | ||
GithubInterface.__repo_cache[repo_name] = GithubRepository(github_account.get_repo(repo_name)) | ||
repo = GithubInterface.__repo_cache[repo_name] | ||
return repo | ||
|
||
def get_repos(self): | ||
@staticmethod | ||
def get_repos(is_user_access_token=False): | ||
github_account = Github(GithubInterface.__fetch_access_token_from_db(is_user_access_token)) | ||
|
||
repos = [] | ||
for repo in self.__github_account.get_user().get_repos(): | ||
|
||
if is_user_access_token: | ||
raw_repos = github_account.get_user().get_repos() | ||
else: | ||
raw_repos = github_account.get_installation(-1).get_repos() | ||
|
||
for repo in raw_repos: | ||
repos.append(GithubRepository(repo)) | ||
return repos | ||
|
||
@staticmethod | ||
def get_user_access_token(client_id, client_secret, code, redirect_uri): | ||
params = { | ||
"client_id": client_id, | ||
"client_secret": client_secret, | ||
"code": code, | ||
"redirect_uri": redirect_uri | ||
} | ||
r = requests.get(url="https://github.com/login/oauth/access_token", params=params) | ||
content = r.content.decode("utf-8") | ||
user_access_token = content[content.find("access_token=") + 13: content.find("&")] | ||
|
||
return user_access_token | ||
|
||
@staticmethod | ||
def get_user_installations(): | ||
access_token = GithubInterface.__fetch_access_token_from_db(True) | ||
response = requests.get(url="https://api.github.com/user/installations", | ||
headers={ | ||
"Authorization": "token " + access_token, | ||
"Accept": "application/vnd.github.machine-man-preview+json" | ||
}) | ||
return response.json() | ||
|
||
@staticmethod | ||
def get_installation_access_token(installation_id, private_key): | ||
integration = GithubIntegration(str(GITHUB_APP_IDENTIFIER), private_key) | ||
return integration.get_access_token(installation_id).token | ||
|
||
@staticmethod | ||
def verify_signature(signature, body, github_webhook_secret): | ||
computed_signature = "sha1=" + hmac.new(str.encode(github_webhook_secret), body, sha1).hexdigest() | ||
return computed_signature == signature | ||
|
||
@staticmethod | ||
def __fetch_access_token_from_db(is_user_access_token): | ||
if is_user_access_token: | ||
return CredentialsManager.read_credentials()["user_access_token"] | ||
else: | ||
return CredentialsManager.read_credentials()["installation_access_token"] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import json | ||
|
||
|
||
class CredentialsManager: | ||
|
||
@staticmethod | ||
def write_credentials(user_access_token="", installation_access_token=""): | ||
credentials = CredentialsManager.read_credentials() | ||
|
||
user_access_token = credentials[ | ||
"user_access_token"] if "user_access_token" in credentials and not user_access_token else user_access_token | ||
installation_access_token = credentials[ | ||
"installation_access_token"] if "installation_access_token" in credentials and not installation_access_token else installation_access_token | ||
|
||
with open("backend/ressources/credentials.txt", "w") as file: | ||
credentials = "{\"installation_access_token\":\"" + str( | ||
installation_access_token) + "\",\"user_access_token\":\"" + str(user_access_token) + "\"}" | ||
file.write(credentials) | ||
file.close() | ||
|
||
@staticmethod | ||
def read_credentials(): | ||
with open("backend/ressources/credentials.txt", "r") as file: | ||
file_content = file.read() | ||
if file_content == "": | ||
file_content = "{}" | ||
file.close() | ||
|
||
return json.loads(file_content) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
bfff63c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This commit has affected the following documentation files: