fjkit 0.1.0.dev0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fjkit/__init__.py +49 -0
- fjkit/auth/__init__.py +54 -0
- fjkit/auth/csrf.py +88 -0
- fjkit/auth/errors.py +31 -0
- fjkit/auth/plugin.py +496 -0
- fjkit/auth/sources.py +24 -0
- fjkit/auth/stores.py +141 -0
- fjkit/auth/types.py +141 -0
- fjkit/cli/__init__.py +67 -0
- fjkit/cli/build_css.py +135 -0
- fjkit/cli/check.py +193 -0
- fjkit/cli/eject.py +562 -0
- fjkit/cli/ejected.py +202 -0
- fjkit/cli/vocabulary.py +85 -0
- fjkit/config.py +130 -0
- fjkit/errors.py +156 -0
- fjkit/flash.py +189 -0
- fjkit/forms.py +145 -0
- fjkit/htmx.py +76 -0
- fjkit/icons.py +60 -0
- fjkit/messages.py +227 -0
- fjkit/mounting.py +72 -0
- fjkit/plugins.py +223 -0
- fjkit/py.typed +0 -0
- fjkit/rendering.py +554 -0
- fjkit/signing.py +63 -0
- fjkit/static/brand/torii-bolt.png +0 -0
- fjkit/static/dist/fjkit-luma.css +2 -0
- fjkit/static/dist/fjkit-lyra.css +2 -0
- fjkit/static/dist/fjkit-maia.css +2 -0
- fjkit/static/dist/fjkit-mira.css +2 -0
- fjkit/static/dist/fjkit-nova.css +2 -0
- fjkit/static/dist/fjkit-rhea.css +2 -0
- fjkit/static/dist/fjkit-sera.css +2 -0
- fjkit/static/dist/fjkit-vega.css +2 -0
- fjkit/static/icons/lucide.json +1 -0
- fjkit/static/js/errors.js +117 -0
- fjkit/static/js/multiselect.js +126 -0
- fjkit/static/js/reveal.js +51 -0
- fjkit/static/js/select.js +113 -0
- fjkit/static/src/fjkit.css +385 -0
- fjkit/static/vendor/basecoat/base/base.css +136 -0
- fjkit/static/vendor/basecoat/basecoat-base.css +2 -0
- fjkit/static/vendor/basecoat/basecoat-components.css +39 -0
- fjkit/static/vendor/basecoat/basecoat-luma.css +2 -0
- fjkit/static/vendor/basecoat/basecoat-lyra.css +2 -0
- fjkit/static/vendor/basecoat/basecoat-maia.css +2 -0
- fjkit/static/vendor/basecoat/basecoat-mira.css +2 -0
- fjkit/static/vendor/basecoat/basecoat-nova.css +2 -0
- fjkit/static/vendor/basecoat/basecoat-rhea.css +2 -0
- fjkit/static/vendor/basecoat/basecoat-sera.css +2 -0
- fjkit/static/vendor/basecoat/basecoat-vega.css +2 -0
- fjkit/static/vendor/basecoat/basecoat.all.css +1 -0
- fjkit/static/vendor/basecoat/basecoat.css +1 -0
- fjkit/static/vendor/basecoat/components/accordion.css +27 -0
- fjkit/static/vendor/basecoat/components/alert.css +38 -0
- fjkit/static/vendor/basecoat/components/avatar.css +47 -0
- fjkit/static/vendor/basecoat/components/badge.css +6 -0
- fjkit/static/vendor/basecoat/components/breadcrumb.css +30 -0
- fjkit/static/vendor/basecoat/components/button-group.css +54 -0
- fjkit/static/vendor/basecoat/components/button.css +6 -0
- fjkit/static/vendor/basecoat/components/card.css +26 -0
- fjkit/static/vendor/basecoat/components/chart.css +56 -0
- fjkit/static/vendor/basecoat/components/checkbox.css +12 -0
- fjkit/static/vendor/basecoat/components/collapsible.css +21 -0
- fjkit/static/vendor/basecoat/components/combobox.css +99 -0
- fjkit/static/vendor/basecoat/components/command.css +140 -0
- fjkit/static/vendor/basecoat/components/dialog.css +101 -0
- fjkit/static/vendor/basecoat/components/drawer.css +89 -0
- fjkit/static/vendor/basecoat/components/dropdown-menu.css +56 -0
- fjkit/static/vendor/basecoat/components/empty.css +26 -0
- fjkit/static/vendor/basecoat/components/field.css +82 -0
- fjkit/static/vendor/basecoat/components/form.css +9 -0
- fjkit/static/vendor/basecoat/components/input-group.css +74 -0
- fjkit/static/vendor/basecoat/components/input.css +32 -0
- fjkit/static/vendor/basecoat/components/item.css +50 -0
- fjkit/static/vendor/basecoat/components/kbd.css +6 -0
- fjkit/static/vendor/basecoat/components/label.css +13 -0
- fjkit/static/vendor/basecoat/components/native-select.css +12 -0
- fjkit/static/vendor/basecoat/components/popover.css +107 -0
- fjkit/static/vendor/basecoat/components/progress.css +14 -0
- fjkit/static/vendor/basecoat/components/radio.css +11 -0
- fjkit/static/vendor/basecoat/components/range.css +50 -0
- fjkit/static/vendor/basecoat/components/scrollbar.css +46 -0
- fjkit/static/vendor/basecoat/components/select.css +64 -0
- fjkit/static/vendor/basecoat/components/sidebar.css +101 -0
- fjkit/static/vendor/basecoat/components/skeleton.css +6 -0
- fjkit/static/vendor/basecoat/components/switch.css +8 -0
- fjkit/static/vendor/basecoat/components/table.css +34 -0
- fjkit/static/vendor/basecoat/components/tabs.css +31 -0
- fjkit/static/vendor/basecoat/components/textarea.css +7 -0
- fjkit/static/vendor/basecoat/components/toast.css +57 -0
- fjkit/static/vendor/basecoat/components/tooltip.css +82 -0
- fjkit/static/vendor/basecoat/js/all.min.js +1 -0
- fjkit/static/vendor/basecoat/styles/luma.css +1376 -0
- fjkit/static/vendor/basecoat/styles/lyra.css +1379 -0
- fjkit/static/vendor/basecoat/styles/maia.css +1364 -0
- fjkit/static/vendor/basecoat/styles/mira.css +1373 -0
- fjkit/static/vendor/basecoat/styles/nova.css +1387 -0
- fjkit/static/vendor/basecoat/styles/rhea.css +1381 -0
- fjkit/static/vendor/basecoat/styles/sera.css +1397 -0
- fjkit/static/vendor/basecoat/styles/vega.css +1386 -0
- fjkit/static/vendor/htmx/htmx.min.js +1 -0
- fjkit/static/vendor/htmx/json-enc.js +38 -0
- fjkit/styles.py +30 -0
- fjkit/templates/errors/page.html +92 -0
- fjkit/templates/ui/attrs.html +18 -0
- fjkit/templates/ui/button.html +64 -0
- fjkit/templates/ui/data.html +362 -0
- fjkit/templates/ui/disclosure.html +89 -0
- fjkit/templates/ui/feedback.html +313 -0
- fjkit/templates/ui/form.html +595 -0
- fjkit/templates/ui/icon.html +13 -0
- fjkit/templates/ui/layout.html +189 -0
- fjkit/templates/ui/nav.html +110 -0
- fjkit/templates/ui/overlay.html +624 -0
- fjkit/templates/ui/shell.html +170 -0
- fjkit/templates/ui/sidebar.html +170 -0
- fjkit/templates/ui/table.html +427 -0
- fjkit/templates/ui/tabs.html +133 -0
- fjkit/templating.py +365 -0
- fjkit/vendored.py +46 -0
- fjkit-0.1.0.dev0.dist-info/METADATA +119 -0
- fjkit-0.1.0.dev0.dist-info/RECORD +127 -0
- fjkit-0.1.0.dev0.dist-info/WHEEL +4 -0
- fjkit-0.1.0.dev0.dist-info/entry_points.txt +2 -0
- fjkit-0.1.0.dev0.dist-info/licenses/LICENSE +21 -0
fjkit/__init__.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"""fjkit — a server-rendered UI kit for FastAPI.
|
|
2
|
+
|
|
3
|
+
Jinja2 for templates, Basecoat for component CSS, htmx for interactivity. The
|
|
4
|
+
stylesheet is built when fjkit is released, not when the app runs: no Tailwind,
|
|
5
|
+
no `package.json`, no build step in the app.
|
|
6
|
+
|
|
7
|
+
from fastapi import FastAPI
|
|
8
|
+
from fjkit import FjkitConfig, mount_fjkit
|
|
9
|
+
|
|
10
|
+
config = FjkitConfig(template_dir=Path(__file__).parent / "templates")
|
|
11
|
+
app = FastAPI()
|
|
12
|
+
mount_fjkit(app, config)
|
|
13
|
+
|
|
14
|
+
Routes name their template on a decorator and return data:
|
|
15
|
+
|
|
16
|
+
@router.get("/tasks")
|
|
17
|
+
@render("tasks/page.html", partial="tasks/_board.html")
|
|
18
|
+
def tasks_page(service: ServiceDep) -> BoardResponse:
|
|
19
|
+
return BoardResponse(...)
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
from __future__ import annotations
|
|
23
|
+
|
|
24
|
+
from fjkit import messages
|
|
25
|
+
from fjkit.config import FjkitConfig, RenderMode
|
|
26
|
+
from fjkit.flash import FlashMessage, FlashPlugin
|
|
27
|
+
from fjkit.messages import Message
|
|
28
|
+
from fjkit.mounting import mount_fjkit
|
|
29
|
+
from fjkit.plugins import AppSetup, EnvSetup, Plugin, PluginWarning
|
|
30
|
+
from fjkit.rendering import render
|
|
31
|
+
from fjkit.templating import Templates, build_environment, get_templates
|
|
32
|
+
|
|
33
|
+
__all__ = [
|
|
34
|
+
"AppSetup",
|
|
35
|
+
"EnvSetup",
|
|
36
|
+
"FjkitConfig",
|
|
37
|
+
"FlashMessage",
|
|
38
|
+
"FlashPlugin",
|
|
39
|
+
"Message",
|
|
40
|
+
"Plugin",
|
|
41
|
+
"PluginWarning",
|
|
42
|
+
"RenderMode",
|
|
43
|
+
"Templates",
|
|
44
|
+
"build_environment",
|
|
45
|
+
"get_templates",
|
|
46
|
+
"messages",
|
|
47
|
+
"mount_fjkit",
|
|
48
|
+
"render",
|
|
49
|
+
]
|
fjkit/auth/__init__.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"""Sessions for a fjkit app: a cookie in the browser, the token on the server.
|
|
2
|
+
|
|
3
|
+
from fjkit import FjkitConfig, mount_fjkit
|
|
4
|
+
from fjkit.auth import AuthPlugin, RedisStore
|
|
5
|
+
|
|
6
|
+
auth = AuthPlugin(
|
|
7
|
+
secret=os.environ["FJKIT_SECRET"],
|
|
8
|
+
store=RedisStore(redis.from_url(REDIS_URL)),
|
|
9
|
+
trusted_origins=["https://app.example.com"],
|
|
10
|
+
)
|
|
11
|
+
mount_fjkit(app, FjkitConfig(template_dir=..., plugins=(auth,)))
|
|
12
|
+
|
|
13
|
+
Three independent seams: where sessions live (`SessionStore`), how credentials
|
|
14
|
+
become tokens (`TokenSource`), and how a write proves it came from this site
|
|
15
|
+
(`Csrf`). Replacing one touches no route and no template.
|
|
16
|
+
|
|
17
|
+
`fjkit.auth.plugin` documents what the cookie holds and why; `fjkit.auth.csrf`
|
|
18
|
+
documents the CSRF cost a cookie brings back.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
from __future__ import annotations
|
|
22
|
+
|
|
23
|
+
from fjkit.auth.csrf import NoCsrf, OriginCsrf
|
|
24
|
+
from fjkit.auth.errors import AuthError, CsrfRejected, NotAuthenticated, RefreshFailed
|
|
25
|
+
from fjkit.auth.plugin import AuthPlugin, CookieSpec
|
|
26
|
+
from fjkit.auth.sources import LocalSource
|
|
27
|
+
from fjkit.auth.stores import MemoryStore, RedisStore, SyncStore
|
|
28
|
+
from fjkit.auth.types import (
|
|
29
|
+
Csrf,
|
|
30
|
+
RefreshableTokenSource,
|
|
31
|
+
Session,
|
|
32
|
+
SessionStore,
|
|
33
|
+
TokenSource,
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
__all__ = [
|
|
37
|
+
"AuthError",
|
|
38
|
+
"AuthPlugin",
|
|
39
|
+
"CookieSpec",
|
|
40
|
+
"Csrf",
|
|
41
|
+
"CsrfRejected",
|
|
42
|
+
"LocalSource",
|
|
43
|
+
"MemoryStore",
|
|
44
|
+
"NoCsrf",
|
|
45
|
+
"NotAuthenticated",
|
|
46
|
+
"OriginCsrf",
|
|
47
|
+
"RedisStore",
|
|
48
|
+
"RefreshFailed",
|
|
49
|
+
"RefreshableTokenSource",
|
|
50
|
+
"Session",
|
|
51
|
+
"SessionStore",
|
|
52
|
+
"SyncStore",
|
|
53
|
+
"TokenSource",
|
|
54
|
+
]
|
fjkit/auth/csrf.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"""Cross-site request forgery (CSRF) defence, in two layers.
|
|
2
|
+
|
|
3
|
+
A cookie is an ambient credential: the browser attaches it to a cross-site
|
|
4
|
+
request unasked. That is the vulnerability, and a `Bearer` header does not have
|
|
5
|
+
it.
|
|
6
|
+
|
|
7
|
+
The two layers cover different gaps. `SameSite=Lax` on the cookie (see
|
|
8
|
+
`CookieSpec`) stops the browser sending it on a cross-*site* write, which is
|
|
9
|
+
nearly every classic CSRF. It misses same-site cross-origin: `evil.example.com`
|
|
10
|
+
and `app.example.com` share a registrable domain, so Lax treats them as one site
|
|
11
|
+
and sends the cookie. An `Origin` comparison is exact — scheme, host and port —
|
|
12
|
+
so it catches exactly what Lax misses.
|
|
13
|
+
|
|
14
|
+
This version ships no token strategy. A token has to reach every form and every
|
|
15
|
+
htmx request, so the form macro and the shell both need request-scoped data;
|
|
16
|
+
shipping a token means shipping a templating feature with it. `Csrf.on_issue` is
|
|
17
|
+
the plug-in point when that cost is worth paying.
|
|
18
|
+
|
|
19
|
+
Two cases this cannot cover:
|
|
20
|
+
|
|
21
|
+
* A GET that changes state. No header check helps; fix it in the routing.
|
|
22
|
+
* Non-browser clients on cookie-authenticated writes. They send no `Origin` and
|
|
23
|
+
are refused. Those callers should use a bearer token instead, which is the
|
|
24
|
+
same route in `"auto"` mode answering JSON.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
from __future__ import annotations
|
|
28
|
+
|
|
29
|
+
from collections.abc import Sequence
|
|
30
|
+
|
|
31
|
+
from fastapi import Request, Response
|
|
32
|
+
|
|
33
|
+
from fjkit.auth.errors import CsrfRejected
|
|
34
|
+
|
|
35
|
+
__all__ = ["NoCsrf", "OriginCsrf"]
|
|
36
|
+
|
|
37
|
+
#: Methods that do not change state, so no CSRF risk justifies refusing them.
|
|
38
|
+
#: `OPTIONS` is here because refusing a preflight breaks the very requests that
|
|
39
|
+
#: would have been checked properly a moment later.
|
|
40
|
+
SAFE_METHODS = frozenset({"GET", "HEAD", "OPTIONS", "TRACE"})
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class OriginCsrf:
|
|
44
|
+
"""Refuse a cookie-authenticated write whose `Origin` is not ours.
|
|
45
|
+
|
|
46
|
+
Pass `trusted_origins` explicitly; never derive it from the `Host` header.
|
|
47
|
+
An attacker controls `Host`, and a check that compares a request against a
|
|
48
|
+
value the request supplied is not a check.
|
|
49
|
+
"""
|
|
50
|
+
|
|
51
|
+
__slots__ = ("_trusted",)
|
|
52
|
+
|
|
53
|
+
def __init__(self, trusted_origins: Sequence[str]) -> None:
|
|
54
|
+
self._trusted = frozenset(o.rstrip("/") for o in trusted_origins)
|
|
55
|
+
|
|
56
|
+
async def verify(self, request: Request) -> None:
|
|
57
|
+
if request.method in SAFE_METHODS:
|
|
58
|
+
return
|
|
59
|
+
|
|
60
|
+
origin = request.headers.get("origin")
|
|
61
|
+
if origin is None:
|
|
62
|
+
# Every browser sends `Origin` on a write, so a request without one
|
|
63
|
+
# carries no automatically attached cookie. That reasoning rests on
|
|
64
|
+
# clients we do not control, so refuse rather than infer.
|
|
65
|
+
raise CsrfRejected(f"{request.method} {request.url.path} carried a session cookie but no Origin header")
|
|
66
|
+
|
|
67
|
+
if origin.rstrip("/") not in self._trusted:
|
|
68
|
+
raise CsrfRejected(f"Origin {origin!r} is not one of this app's trusted origins")
|
|
69
|
+
|
|
70
|
+
def on_issue(self, response: Response, sid: str) -> None:
|
|
71
|
+
"""Plant nothing: the check reads a header the browser already sends."""
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class NoCsrf:
|
|
75
|
+
"""Check nothing.
|
|
76
|
+
|
|
77
|
+
For an app that needs no protection of its own: an internal tool behind a
|
|
78
|
+
gateway that already checks. Explicit rather than `csrf=None`, so the
|
|
79
|
+
decision appears in the app's wiring and stays findable.
|
|
80
|
+
"""
|
|
81
|
+
|
|
82
|
+
__slots__ = ()
|
|
83
|
+
|
|
84
|
+
async def verify(self, request: Request) -> None:
|
|
85
|
+
return
|
|
86
|
+
|
|
87
|
+
def on_issue(self, response: Response, sid: str) -> None:
|
|
88
|
+
return
|
fjkit/auth/errors.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"""The failures this plugin raises, and the response each one implies.
|
|
2
|
+
|
|
3
|
+
Two shapes rather than one. `NotAuthenticated` sends the browser to the login
|
|
4
|
+
screen. `CsrfRejected` leaves the browser where it is and answers 403, because
|
|
5
|
+
the request may come from an attacker and its victim has no login to do.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
__all__ = ["AuthError", "CsrfRejected", "NotAuthenticated", "RefreshFailed"]
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class AuthError(Exception):
|
|
14
|
+
"""Base for everything this plugin raises."""
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class NotAuthenticated(AuthError):
|
|
18
|
+
"""No usable session: no cookie, an unknown sid, or an expired store entry."""
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class RefreshFailed(NotAuthenticated):
|
|
22
|
+
"""The upstream refused to renew the token.
|
|
23
|
+
|
|
24
|
+
A subclass rather than a sibling, because a failed refresh also means the
|
|
25
|
+
user logs in again: one handler covers both, and no caller has to remember
|
|
26
|
+
the second name.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class CsrfRejected(AuthError):
|
|
31
|
+
"""A cookie-authenticated write arrived without a trusted `Origin`."""
|