Skip to content

server

LoginServerHandler

Bases: BaseHTTPRequestHandler

Login server handler.

Source code in common/ayon_common/connection/ui/server.py
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
class LoginServerHandler(BaseHTTPRequestHandler):
    """Login server handler."""

    # Browsers may open speculative (preconnect) connections which never
    #   send a request. Do not let them block a request handler forever.
    timeout = 10

    def do_GET(self):
        """Override to handle requests ourselves."""
        access_token = None
        if self.path not in ("/index.css", "/favicon.ico"):
            tokens = parse_qs(urlparse(self.path).query).get("token")
            if tokens:
                access_token = tokens[0]

        try:
            self._send_response(access_token)
        finally:
            # Store the token after the response was sent (or failed to be
            #   sent), the login window may close the server right after it
            #   receives the token.
            # Never clear an already received token, browsers can send
            #   additional requests (e.g. icons or repeated page loads).
            if access_token:
                self.server.set_token(access_token)

    def _send_response(self, access_token):
        if self.path == "/index.css":
            filepath = get_resource_path("index.css")
            content_type = "text/css"
        elif self.path == "/favicon.ico":
            filepath = get_resource_path("favicon.ico")
            content_type = "image/x-icon"
        else:
            content_type = "text/html"
            if access_token:
                filepath = get_resource_path("success.html")
            else:
                filepath = get_resource_path("failed.html")

        with open(filepath, "rb") as stream:
            content = stream.read()

        # Set header with content type
        self.send_response(200)
        self.send_header("Content-type", content_type)
        self.end_headers()
        self.wfile.write(content)
        self.wfile.flush()

    def log_message(self, *args, **kwargs):
        # Callback URLs contain access tokens; never write them to stderr.
        pass

do_GET()

Override to handle requests ourselves.

Source code in common/ayon_common/connection/ui/server.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def do_GET(self):
    """Override to handle requests ourselves."""
    access_token = None
    if self.path not in ("/index.css", "/favicon.ico"):
        tokens = parse_qs(urlparse(self.path).query).get("token")
        if tokens:
            access_token = tokens[0]

    try:
        self._send_response(access_token)
    finally:
        # Store the token after the response was sent (or failed to be
        #   sent), the login window may close the server right after it
        #   receives the token.
        # Never clear an already received token, browsers can send
        #   additional requests (e.g. icons or repeated page loads).
        if access_token:
            self.server.set_token(access_token)