diff options
author | Muriithi Frederick Muriuki | 2018-01-12 18:07:13 +0300 |
---|---|---|
committer | Pjotr Prins | 2018-03-26 09:24:34 +0000 |
commit | 2524691b03cb629eb79e4d11c8ce8e451a5cd870 (patch) | |
tree | 85a3a33e642227dbc1bd768b0154831deeb61892 /wqflask | |
parent | d0f071a3871a2bcbb2c5170996a4afb145c21f9c (diff) | |
download | genenetwork2-2524691b03cb629eb79e4d11c8ce8e451a5cd870.tar.gz |
Add functions to help handle github login
* Add functions to help with the github OAuth2 login process
Diffstat (limited to 'wqflask')
-rw-r--r-- | wqflask/wqflask/user_manager.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/wqflask/wqflask/user_manager.py b/wqflask/wqflask/user_manager.py index 25833464..9012c842 100644 --- a/wqflask/wqflask/user_manager.py +++ b/wqflask/wqflask/user_manager.py @@ -494,6 +494,37 @@ def login(): lu = LoginUser() return lu.standard_login() +@app.route("/n/login/github_oauth2", methods=('GET', 'POST')) +def github_oauth2(): + from utility.tools import GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET + from utility.elasticsearch_tools import get_user_by_unique_column + import requests + code = request.args.get("code") + data = { + "client_id": GITHUB_CLIENT_ID, + "client_secret": GITHUB_CLIENT_SECRET, + "code": code + } + result = requests.post("https://github.com/login/oauth/access_token", json=data) + result_dict = {arr[0]:arr[1] for arr in [tok.split("=") for tok in [token.encode("utf-8") for token in result.text.split("&")]]} + + github_user = get_github_user_details(result_dict["access_token"]) + user_details = get_user_by_unique_column("github_id", github_user["id"]) + if user_details == None: + user_details = { + "user_id": str(uuid4()) + , "name": github_user["name"] + , "github_id": github_user["id"] + , "user_url": github_user["html_url"] + , "login_type": "github" + } + url = "/n/login?type=github" + return redirect(url) + +def get_github_user_details(access_token): + from utility.tools import GITHUB_API_URL + result = requests.get(GITHUB_API_URL, params={"access_token":access_token}) + return result.json() class LoginUser(object): remember_time = 60 * 60 * 24 * 30 # One month in seconds |