Skip to content

Commit 0563f08

Browse files
committed
refactor(Cluster): refactor auth page not to use templates
1 parent 2cd21ac commit 0563f08

File tree

1 file changed

+51
-45
lines changed

1 file changed

+51
-45
lines changed

silverback/cluster/auth.py

Lines changed: 51 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import webbrowser
1414
from collections.abc import Mapping
1515
from enum import Enum
16-
from string import Template
1716
from typing import Any, Optional, TypedDict, Union
1817
from urllib.parse import urlencode, urlsplit, urlunsplit
1918

@@ -587,14 +586,7 @@ class CallbackHTTPRequestHandler(http.server.BaseHTTPRequestHandler):
587586
</svg>"""
588587
"""SVG to display error icon"""
589588

590-
PAGE_TEMPLATE = Template(
591-
"""
592-
<html lang="$lang">
593-
<head>
594-
<title>$title</title>
595-
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
596-
<meta name="charset" content="utf-8">
597-
<style>
589+
CSS_STYLE = """
598590
* {
599591
margin: 0;
600592
padding: 0;
@@ -678,19 +670,40 @@ class CallbackHTTPRequestHandler(http.server.BaseHTTPRequestHandler):
678670
-webkit-animation: colored-circle 0.6s ease-in-out 0.7s backwards;
679671
animation: colored-circle 0.6s ease-in-out 0.7s backwards;
680672
}
681-
</style>
673+
"""
674+
675+
# NOTE: Rest of this code borrowed from `fief-python[cli]`
676+
# https://github.com/fief-dev/fief-python/blob/main/fief_client/integrations/cli.py
677+
def __init__(
678+
self,
679+
*args,
680+
queue: "queue.Queue[str]",
681+
**kwargs,
682+
) -> None:
683+
self.queue = queue
684+
super().__init__(*args, **kwargs)
685+
686+
def log_message(self, format: str, *args: typing.Any) -> None:
687+
pass
688+
689+
def render_success_page(self, lang="en") -> str:
690+
return f"""
691+
<html lang="{lang}">
692+
<head>
693+
<title>Authentication Success</title>
694+
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
695+
<meta name="charset" content="utf-8">
696+
<style>{self.CSS_STYLE}</style>
682697
</head>
683698
684699
<body>
685700
<div class="message">
686701
<div class="animation-ctn">
687-
<div class="icon">
688-
$svg
689-
</div>
702+
<div class="icon">{self.SUCCESS_SVG}</div>
690703
</div>
691704
692-
<h1>$title</h1>
693-
<p>$message</p>
705+
<h1>Authentication Successful</h1>
706+
<p>Done! You can go back to your terminal! This page will auto-close in 5 secs.</p>
694707
</div>
695708
</body>
696709
<script>
@@ -702,39 +715,32 @@ class CallbackHTTPRequestHandler(http.server.BaseHTTPRequestHandler):
702715
</script>
703716
</html>
704717
"""
705-
)
706-
"""Template for callback HTML page"""
707718

708-
# NOTE: Rest of this code borrowed from `fief-python[cli]`
709-
# https://github.com/fief-dev/fief-python/blob/main/fief_client/integrations/cli.py
710-
def __init__(
711-
self,
712-
*args,
713-
queue: "queue.Queue[str]",
714-
**kwargs,
715-
) -> None:
716-
self.queue = queue
717-
super().__init__(*args, **kwargs)
718-
719-
def log_message(self, format: str, *args: typing.Any) -> None:
720-
pass
719+
def render_error_page(self, query_params: dict[str, typing.Any], lang="en") -> str:
720+
return f"""
721+
<html lang="{lang}">
722+
<head>
723+
<title>Authentication Failed</title>
724+
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
725+
<meta name="charset" content="utf-8">
726+
<style>{self.CSS_STYLE}</style>
727+
</head>
721728
722-
def render_success_page(self, lang="en") -> str:
723-
return self.PAGE_TEMPLATE.substitute(
724-
lang=lang,
725-
title="Authentication Successful",
726-
message="Done! You can go back to your terminal! This page will auto-close in 5 secs.",
727-
svg=self.SUCCESS_SVG,
728-
)
729+
<body>
730+
<div class="message">
731+
<div class="animation-ctn">
732+
<div class="icon">{self.ERROR_SVG}</div>
733+
</div>
729734
730-
def render_error_page(self, query_params: dict[str, typing.Any], lang="en") -> str:
731-
return self.PAGE_TEMPLATE.substitute(
732-
lang=lang,
733-
title="Authentication Failed",
734-
message=f"""Something went wrong trying to authenticate your. Please try again.
735-
Error detail: {json.dumps(query_params)}""",
736-
svg=self.ERROR_SVG,
737-
)
735+
<h1>Authentication Failed</h1>
736+
<p>
737+
Something went wrong trying to authenticate you.
738+
Please try again. Error detail: {json.dumps(query_params)}
739+
</p>
740+
</div>
741+
</body>
742+
</html>
743+
"""
738744

739745
def do_GET(self):
740746
parsed_url = urllib.parse.urlparse(self.path)

0 commit comments

Comments
 (0)