@@ -113,7 +113,13 @@ def do_OPTIONS(self):
113113
114114class HTTPSServer :
115115 def __init__ (self , port = 6234 , cert_file = "localhost.crt" , key_file = "localhost.key" ):
116- """Initialize HTTPS server with configurable parameters."""
116+ """Initialize HTTPS server with configurable parameters.
117+
118+ Args:
119+ port (int, optional): Port number to run the server on. Defaults to 6234.
120+ cert_file (str, optional): SSL certificate file. Defaults to "localhost.crt".
121+ key_file (str, optional): SSL key file. Defaults to "localhost.key".
122+ """
117123 self .current_path = os .path .dirname (os .path .abspath (__file__ ))
118124 self .port = port
119125 self .cert_file = os .path .join (self .current_path , "localhost.crt" )
@@ -123,17 +129,31 @@ def __init__(self, port=6234, cert_file="localhost.crt", key_file="localhost.key
123129 self .should_shutdown = False
124130
125131 def token_received_callback (self , token_data ):
126- """Callback for when a token is received."""
132+ """Callback for when a token is received.
133+
134+ Args:
135+ token_data (dict): The received token data.
136+ """
127137 self .token_data = token_data
128138 self .should_shutdown = True
129139
130140 def create_server (self , state , code_verifier , domain ):
131- """Create and configure the HTTPS server."""
141+ """Create and configure the HTTPS server.
142+
143+ Args:
144+ state (str): The OAuth state parameter.
145+ code_verifier (str): The PKCE code verifier.
146+ domain (str): The domain for authentication.
147+
148+ Returns:
149+ socketserver.TCPServer: The configured HTTPS server.
150+ """
132151 # Create SSL context
133152 context = ssl .SSLContext (ssl .PROTOCOL_TLS_SERVER )
134153 context .load_cert_chain (self .cert_file , self .key_file )
135154
136- # Create server
155+ # Create server with address reuse
156+ socketserver .TCPServer .allow_reuse_address = True
137157 handler = make_request_handler_class (
138158 state , code_verifier , self .token_received_callback , domain
139159 )
@@ -143,7 +163,16 @@ def create_server(self, state, code_verifier, domain):
143163 return self .httpd
144164
145165 def start (self , state , code_verifier , domain ):
146- """Start the server."""
166+ """Start the server.
167+
168+ Args:
169+ state (str): The OAuth state parameter.
170+ code_verifier (str): The PKCE code verifier.
171+ domain (str): The domain for authentication.
172+
173+ Returns:
174+ dict: The received token data or an empty dict if no token was received.
175+ """
147176 if not self .httpd :
148177 self .create_server (state , code_verifier , domain )
149178
@@ -159,7 +188,7 @@ def start(self, state, code_verifier, domain):
159188 return self .token_data if self .token_data else {}
160189
161190 def stop (self ):
162- """Stop the server gracefully."""
191+ """Stop the server gracefully and cleanup resources ."""
163192 if self .httpd :
164193 self .httpd .server_close ()
165194 self .httpd = None
0 commit comments