From 6be808ffcada6df3de8a43b3c493f8ad61268054 Mon Sep 17 00:00:00 2001
From: Jonas Leder <jonas@jonasled.de>
Date: Sat, 9 Nov 2019 16:53:27 +0100
Subject: [PATCH] login is available via /login

---
 main.py | 33 +++++++++++++++++++++++++++++++--
 1 file changed, 31 insertions(+), 2 deletions(-)

diff --git a/main.py b/main.py
index 9e57fdf..727d687 100644
--- a/main.py
+++ b/main.py
@@ -1,16 +1,23 @@
 #!/usr/bin/env python3
 from waitress import serve #Used as webserver (Production)
-from flask import Flask, request, render_template, redirect, abort, Markup #Used to prepare the dynamic pages (The main site)
+from flask import Flask, request, render_template, redirect, abort, Markup, session, make_response #Used to prepare the dynamic pages (The main site)
 import sqlite3 #Used to store the Data
 import os #Used for getting the enviorement variables
 import qrcode #Used to generate the QR
 import base64 #Used to encode the generated QR as base64, to directly insert it into the HTML
-from requests import post #Used to validate recaptcha
+from requests import post, get #Used to validate recaptcha / oauth
 from io import BytesIO #Needed for base64 encoding of the image
 from PIL import Image #Needed for QR generation
+from flask_github import GitHub #github oauth library
+import json #used for github oauth
 
 app = Flask(__name__)
+app.config['GITHUB_CLIENT_ID'] = ''
+app.config['GITHUB_CLIENT_SECRET'] = ''
+github = GitHub(app)
+
 domain_to_index = {}
+user = ""
 
 try:
     domain = os.environ["domains"].split(";") #Get the domains from the enviorement variable. If no enviorement variable is set set it to 127.0.0.1:5000 (for testing)
@@ -119,6 +126,7 @@ def grecaptcha_verify(request): #This function is used to verify the google reca
     return response 
 
 
+
 @app.route('/', methods=['GET'])
 def home_get():
     return render_template('home.html', builddate=builddate, version=version, domain=domain_prepared, recaptchaPublicKey=recaptchaPublicKey, showDomainSelect=showDomainSelect) #return the default site to create a new shorten link
@@ -184,6 +192,27 @@ def redirect_short_url(short_url):
         abort(404)
 
 
+@app.route('/login')
+def login():
+    return github.authorize(scope="user")
+
+@app.route('/github-callback')
+@github.authorized_handler
+def authorized(oauth_token):
+    if oauth_token is None:
+        return "oauth failed, please try again"
+    
+    headers = {'Authorization': 'token ' + oauth_token,} #Useragent doesn't matters, but is set here
+    githubResponse = get("https://api.github.com/user", headers=headers).text
+    userID = str(json.loads(githubResponse)['id'])
+    username = str(json.loads(githubResponse)['login'])
+
+    resp = make_response("Hello " + username + "(" + userID + ")")
+    resp.set_cookie('userID', userID)
+    resp.set_cookie('username', username)
+    return resp
+    
+
 if __name__ == '__main__':
     table_check()# This code checks whether database table is created or not
     if production: #Check if production variable is set to true use the waitress webserver, else use the buildin flask webserver, with more debug output
-- 
GitLab