Every app's .env gets CORE_ADMIN_EMAIL (admin@<domain>, the Core's own admin, as Federated's apps use). The skill says apps make that account an admin on each Core sign-in, trusting only Authelia's email, instead of a setup link; the sso example apps show the check. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
102 lines
3.4 KiB
Python
102 lines
3.4 KiB
Python
"""{{TITLE}}: signs people in with the Core's Authelia.
|
|
|
|
The sign-in itself is in oidc.py. Python's standard library only.
|
|
"""
|
|
|
|
import html
|
|
import urllib.error
|
|
import urllib.parse
|
|
from http.cookies import SimpleCookie
|
|
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
|
|
|
import oidc
|
|
|
|
|
|
class Handler(BaseHTTPRequestHandler):
|
|
def do_GET(self):
|
|
url = urllib.parse.urlsplit(self.path)
|
|
query = dict(urllib.parse.parse_qsl(url.query))
|
|
routes = {
|
|
"/": self.home,
|
|
"/login": self.login,
|
|
"/auth/callback": self.callback,
|
|
"/logout": self.logout,
|
|
"/health": lambda q: self.reply(200, "ok\n", "text/plain"),
|
|
}
|
|
route = routes.get(url.path)
|
|
if route is None:
|
|
self.reply(404, "Not found\n", "text/plain")
|
|
return
|
|
try:
|
|
route(query)
|
|
except oidc.SignInError as error:
|
|
self.reply(400, f"Sign-in failed: {error}\n", "text/plain")
|
|
except (urllib.error.URLError, KeyError, ValueError) as error:
|
|
self.reply(502, f"Sign-in failed: {error}\n", "text/plain")
|
|
|
|
def cookie(self, name):
|
|
cookies = SimpleCookie(self.headers.get("Cookie", ""))
|
|
return cookies[name].value if name in cookies else None
|
|
|
|
def home(self, query):
|
|
user = oidc.read_session(self.cookie("session"))
|
|
if user:
|
|
groups = ", ".join(user.get("groups") or []) or "none"
|
|
admin = ", the app's admin" if user.get("admin") else ""
|
|
body = (
|
|
f"<p>Signed in as <strong id=\"user\">{html.escape(user['name'])}</strong>"
|
|
f" ({html.escape(user['email'])}), groups: {html.escape(groups)}"
|
|
f"{admin}.</p>"
|
|
'<p><a href="/logout">Sign out</a></p>'
|
|
)
|
|
else:
|
|
body = '<p><a href="/login">Sign in with your Core account</a></p>'
|
|
self.page(body)
|
|
|
|
def login(self, query):
|
|
location, flow = oidc.start_login()
|
|
self.redirect(location, [("flow", flow, oidc.FLOW_SECONDS)])
|
|
|
|
def callback(self, query):
|
|
user = oidc.finish_login(query, self.cookie("flow"))
|
|
self.redirect("/", [("session", oidc.make_session(user), oidc.SESSION_SECONDS),
|
|
("flow", "", 0)])
|
|
|
|
def logout(self, query):
|
|
self.redirect("/", [("session", "", 0)])
|
|
|
|
def page(self, body):
|
|
self.reply(200, f"""<!doctype html>
|
|
<html lang="en">
|
|
<head><meta charset="utf-8"><title>{{TITLE}}</title></head>
|
|
<body>
|
|
<h1>{{TITLE}}</h1>
|
|
<p>Running on Federated Core, deployed with coreapp-dev.</p>
|
|
{body}
|
|
</body>
|
|
</html>
|
|
""", "text/html; charset=utf-8")
|
|
|
|
def redirect(self, location, cookies=()):
|
|
self.send_response(302)
|
|
self.send_header("Location", location)
|
|
for name, value, max_age in cookies:
|
|
self.send_header(
|
|
"Set-Cookie",
|
|
f"{name}={value}; Max-Age={max_age}; Path=/; Secure; HttpOnly; SameSite=Lax")
|
|
self.send_header("Content-Length", "0")
|
|
self.end_headers()
|
|
|
|
def reply(self, status, body, content_type):
|
|
data = body.encode()
|
|
self.send_response(status)
|
|
self.send_header("Content-Type", content_type)
|
|
self.send_header("Content-Length", str(len(data)))
|
|
self.end_headers()
|
|
self.wfile.write(data)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print("Listening on port 8080", flush=True)
|
|
ThreadingHTTPServer(("", 8080), Handler).serve_forever()
|