-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy path_auth_server.py
More file actions
194 lines (156 loc) · 6.98 KB
/
_auth_server.py
File metadata and controls
194 lines (156 loc) · 6.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import http.server
import json
import os
import socketserver
import ssl
import time
from dotenv import load_dotenv
from ._oidc_utils import get_auth_config
load_dotenv()
# Server port
PORT = 6234
# Custom exception for token received
class TokenReceivedSignal(Exception):
"""Exception raised when a token is successfully received."""
def __init__(self, token_data):
self.token_data = token_data
super().__init__("Token received successfully")
def make_request_handler_class(state, code_verifier, token_callback, domain):
class SimpleHTTPSRequestHandler(http.server.SimpleHTTPRequestHandler):
"""Simple HTTPS request handler that serves static files."""
def log_message(self, format, *args) -> None:
# do nothing
pass
def do_POST(self):
"""Handle POST requests to /set_token."""
if self.path == "/set_token":
content_length = int(self.headers["Content-Length"])
post_data = self.rfile.read(content_length)
token_data = json.loads(post_data.decode("utf-8"))
print("Received authentication information")
self.send_response(200)
self.end_headers()
self.wfile.write(b"Token received")
time.sleep(1)
token_callback(token_data)
elif self.path == "/log":
content_length = int(self.headers["Content-Length"])
post_data = self.rfile.read(content_length)
logs = json.loads(post_data.decode("utf-8"))
# Write logs to .uipath/.error_log file
uipath_dir = os.path.join(os.getcwd(), ".uipath")
os.makedirs(uipath_dir, exist_ok=True)
error_log_path = os.path.join(uipath_dir, ".error_log")
with open(error_log_path, "a", encoding="utf-8") as f:
f.write(
f"\n--- Authentication Error Log {time.strftime('%Y-%m-%d %H:%M:%S')} ---\n"
)
json.dump(logs, f, indent=2)
f.write("\n")
print(logs)
print("Received log data")
self.send_response(200)
self.end_headers()
self.wfile.write(b"Log received")
else:
self.send_error(404, "Path not found")
def do_GET(self):
"""Handle GET requests by serving index.html."""
# Always serve index.html regardless of the path
try:
index_path = os.path.join(os.path.dirname(__file__), "index.html")
with open(index_path, "r") as f:
content = f.read()
# Get the redirect URI from auth config
auth_config = get_auth_config()
redirect_uri = auth_config["redirect_uri"]
content = content.replace("__PY_REPLACE_EXPECTED_STATE__", state)
content = content.replace("__PY_REPLACE_CODE_VERIFIER__", code_verifier)
content = content.replace("__PY_REPLACE_REDIRECT_URI__", redirect_uri)
content = content.replace(
"__PY_REPLACE_CLIENT_ID__", auth_config["client_id"]
)
content = content.replace("__PY_REPLACE_DOMAIN__", domain)
self.send_response(200)
self.send_header("Content-Type", "text/html")
self.send_header("Content-Length", str(len(content)))
self.end_headers()
self.wfile.write(content.encode("utf-8"))
except FileNotFoundError:
self.send_error(404, "File not found")
def end_headers(self):
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
self.send_header("Access-Control-Allow-Headers", "Content-Type")
super().end_headers()
def do_OPTIONS(self):
self.send_response(200)
self.end_headers()
return SimpleHTTPSRequestHandler
class HTTPSServer:
def __init__(self, port=6234, cert_file="localhost.crt", key_file="localhost.key"):
"""Initialize HTTPS server with configurable parameters.
Args:
port (int, optional): Port number to run the server on. Defaults to 6234.
cert_file (str, optional): SSL certificate file. Defaults to "localhost.crt".
key_file (str, optional): SSL key file. Defaults to "localhost.key".
"""
self.current_path = os.path.dirname(os.path.abspath(__file__))
self.port = port
self.cert_file = os.path.join(self.current_path, "localhost.crt")
self.key_file = os.path.join(self.current_path, "localhost.key")
self.httpd = None
self.token_data = None
self.should_shutdown = False
def token_received_callback(self, token_data):
"""Callback for when a token is received.
Args:
token_data (dict): The received token data.
"""
self.token_data = token_data
self.should_shutdown = True
def create_server(self, state, code_verifier, domain):
"""Create and configure the HTTPS server.
Args:
state (str): The OAuth state parameter.
code_verifier (str): The PKCE code verifier.
domain (str): The domain for authentication.
Returns:
socketserver.TCPServer: The configured HTTPS server.
"""
# Create SSL context
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
context.load_cert_chain(self.cert_file, self.key_file)
# Create server with address reuse
socketserver.TCPServer.allow_reuse_address = True
handler = make_request_handler_class(
state, code_verifier, self.token_received_callback, domain
)
self.httpd = socketserver.TCPServer(("", self.port), handler)
self.httpd.socket = context.wrap_socket(self.httpd.socket, server_side=True)
return self.httpd
def start(self, state, code_verifier, domain):
"""Start the server.
Args:
state (str): The OAuth state parameter.
code_verifier (str): The PKCE code verifier.
domain (str): The domain for authentication.
Returns:
dict: The received token data or an empty dict if no token was received.
"""
if not self.httpd:
self.create_server(state, code_verifier, domain)
try:
if self.httpd:
while not self.should_shutdown:
self.httpd.handle_request()
except KeyboardInterrupt:
print("Process interrupted by user")
finally:
self.stop()
return self.token_data if self.token_data else {}
def stop(self):
"""Stop the server gracefully and cleanup resources."""
if self.httpd:
self.httpd.server_close()
self.httpd = None