diff options
-rw-r--r-- | game/python-extra/tmw/__init__.py | 3 | ||||
-rw-r--r-- | game/python-extra/tmw/google.py | 71 |
2 files changed, 74 insertions, 0 deletions
diff --git a/game/python-extra/tmw/__init__.py b/game/python-extra/tmw/__init__.py new file mode 100644 index 0000000..9c76689 --- /dev/null +++ b/game/python-extra/tmw/__init__.py @@ -0,0 +1,3 @@ +# TMW Plugin, both built-in client as Google Auth workaround + + diff --git a/game/python-extra/tmw/google.py b/game/python-extra/tmw/google.py new file mode 100644 index 0000000..3f240ee --- /dev/null +++ b/game/python-extra/tmw/google.py @@ -0,0 +1,71 @@ +import BaseHTTPServer, json, webbrowser, threading, time, traceback +import urlparse as parse +#from urllib.parse import urlparse + +REPLY = False +CODE = "" +PORT = 64004 # FIXME ask OS for a random port + +class GHandler(BaseHTTPServer.BaseHTTPRequestHandler): + def _set_headers(self, code=200): + self.send_response(code) + self.send_header('Content-type', 'text/html; charset=UTF-8') + self.end_headers() + + # We only need the GET method + def do_GET(self): + global REPLY, CODE + try: + # Snippet Source: StackOverflow/21584545 + post_data = dict(parse.parse_qsl(parse.urlsplit(self.path).query)) + # End Snippet + # Handle data... + + ## Verify for consent failure + if "error" in post_data: + print("ERROR DETECTED") + REPLY=True + raise Exception("Declined") + + ## Validate UUID + # assert post_data["uuid"] == UUID + # assert post_data["token"] == TOKEN + assert post_data["scope"] == "email%20https://www.googleapis.com/auth/userinfo.email%20openid" + CODE = post_data["code"] + # We're done + REPLY = True + self.send_response(200) + self.end_headers() + self.wfile.write("""<!DOCTYPE html><html><head><title>Mana Launcher</title></head><body><h1>Thanks</h1><br/>You can close this window now.</body></html>""") + except: + traceback.print_exc() + # Garbage, disregard + self.send_error(403) + return + +# Wait for reply +def __callbacks__(): + global REPLY + server_address = ('', 64004) + httpd = BaseHTTPServer.HTTPServer(server_address, GHandler) + while not REPLY: + httpd.handle_request() + # Reply obtained, propagate + # FIXME + +def __request__(UUID, TOKEN): + global PORT, REPLY, CODE + # Fire loopback server + lo=threading.Thread(target=__callbacks__) + lo.daemon=True + lo.start() + + # OAuth Consent Screen + webbrowser.open_new("https://accounts.google.com/o/oauth2/v2/auth?scope=email&response_type=code&state=uuid%3D"+str(UUID)+"%26token%3D"+str(TOKEN)+"&redirect_uri=http%3A//127.0.0.1%3A"+str(PORT)+"&client_id=160252006716-mp7sgnncdtd4c9quv9sbtem3bkccn72q.apps.googleusercontent.com") + + # Wait until we get a reply + while not REPLY: + time.sleep(0.5) + + return CODE + |