backend/gettoken.py
Marcin Woźniak c8c8b62263
gettoken.py, README.md - update
Signed-off-by: Marcin Woźniak <y0rune@aol.com>
2021-01-06 17:29:18 +01:00

49 lines
1.4 KiB
Python
Executable File

#!/usr/bin/python
import webbrowser
from http.server import BaseHTTPRequestHandler, HTTPServer, SimpleHTTPRequestHandler
import urllib.parse as urlparse
from urllib.parse import parse_qs
import sys
import requests as r
no_browser = False
API_ADDRESS = "http://localhost:1285"
if len(sys.argv) > 1 :
if (sys.argv[1] == "no-browser" or sys.argv[2] == "no-browser"):
no_browser = True
else:
API_ADDRESS = "http://" + sys.argv[1] + ":1285"
PORT = 3000
class S(BaseHTTPRequestHandler):
def _set_response(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
parsed = urlparse.urlparse(str(self.path))
ticket = str(parse_qs(parsed.query)['ticket'][0])
print(ticket)
response = r.get(API_ADDRESS + "/token?ticket=" + ticket)
self._set_response()
self.wfile.write("Your token: {}".format(
response.text).encode('utf-8'))
def wait_for_request(server_class=HTTPServer,
handler_class=S):
server_address = ('localhost', PORT)
httpd = server_class(server_address, handler_class)
return httpd.handle_request()
url = 'https://cas.amu.edu.pl/cas/login?service=http://localhost:' + \
str(PORT) + '&locale=pl'
if no_browser == False:
webbrowser.open_new_tab(url)
wait_for_request()