tools/lib/auth.py update oauth redirect url

albatross
Willem Melching 2021-05-17 14:51:29 +02:00
parent 2a05701f47
commit 0252072729
1 changed files with 7 additions and 6 deletions

View File

@ -7,12 +7,14 @@ from tools.lib.api import CommaApi, APIError
from tools.lib.auth_config import set_token from tools.lib.auth_config import set_token
from typing import Dict, Any from typing import Dict, Any
PORT = 3000
class ClientRedirectServer(HTTPServer): class ClientRedirectServer(HTTPServer):
query_params: Dict[str, Any] = {} query_params: Dict[str, Any] = {}
class ClientRedirectHandler(BaseHTTPRequestHandler): class ClientRedirectHandler(BaseHTTPRequestHandler):
def do_GET(self): def do_GET(self):
if not self.path.startswith('/auth_redirect'): if not self.path.startswith('/auth/g/redirect'):
self.send_response(204) self.send_response(204)
return return
@ -28,8 +30,8 @@ class ClientRedirectHandler(BaseHTTPRequestHandler):
def log_message(self, format, *args): # pylint: disable=redefined-builtin def log_message(self, format, *args): # pylint: disable=redefined-builtin
pass # this prevent http server from dumping messages to stdout pass # this prevent http server from dumping messages to stdout
def auth_redirect_link(port): def auth_redirect_link():
redirect_uri = f'http://localhost:{port}/auth_redirect' redirect_uri = f'http://localhost:{PORT}/auth/g/redirect'
params = { params = {
'type': 'web_server', 'type': 'web_server',
'client_id': '45471411055-ornt4svd2miog6dnopve7qtmh5mnu6id.apps.googleusercontent.com', 'client_id': '45471411055-ornt4svd2miog6dnopve7qtmh5mnu6id.apps.googleusercontent.com',
@ -42,10 +44,9 @@ def auth_redirect_link(port):
return (redirect_uri, 'https://accounts.google.com/o/oauth2/auth?' + urlencode(params)) return (redirect_uri, 'https://accounts.google.com/o/oauth2/auth?' + urlencode(params))
def login(): def login():
port = 9090 redirect_uri, oauth_uri = auth_redirect_link()
redirect_uri, oauth_uri = auth_redirect_link(port)
web_server = ClientRedirectServer(('localhost', port), ClientRedirectHandler) web_server = ClientRedirectServer(('localhost', PORT), ClientRedirectHandler)
print(f'To sign in, use your browser and navigate to {oauth_uri}') print(f'To sign in, use your browser and navigate to {oauth_uri}')
webbrowser.open(oauth_uri, new=2) webbrowser.open(oauth_uri, new=2)