backend/gettoken.py

43 lines
1.2 KiB
Python
Raw Normal View History

2020-10-27 18:21:45 +01:00
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
if len(sys.argv) > 1 :
API_ADDRESS = "http://" + sys.argv[1] + ":1285"
else:
API_ADDRESS = "http://localhost:1285"
PORT = 3000
2020-10-27 18:21:45 +01:00
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)
2020-10-27 18:21:45 +01:00
self._set_response()
self.wfile.write("Your token: {}".format(
response.text).encode('utf-8'))
2020-10-27 18:21:45 +01:00
def wait_for_request(server_class=HTTPServer,
handler_class=S):
server_address = ('localhost', PORT)
2020-10-27 18:21:45 +01:00
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'
2020-10-27 18:21:45 +01:00
webbrowser.open_new_tab(url)
wait_for_request()