fjkit 0.1.3__tar.gz → 0.2.0__tar.gz

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.
Files changed (170) hide show
  1. {fjkit-0.1.3 → fjkit-0.2.0}/PKG-INFO +1 -1
  2. {fjkit-0.1.3 → fjkit-0.2.0}/docs/workbench/assets/components.js +12 -1
  3. {fjkit-0.1.3 → fjkit-0.2.0}/docs/workbench/build.py +2 -0
  4. {fjkit-0.1.3 → fjkit-0.2.0}/docs/workbench/build_data.py +4 -0
  5. {fjkit-0.1.3 → fjkit-0.2.0}/docs/workbench/cheatsheet.py +26 -7
  6. {fjkit-0.1.3 → fjkit-0.2.0}/docs/workbench/templates/components.html +19 -1
  7. {fjkit-0.1.3 → fjkit-0.2.0}/docs/workbench/templates/learn.html +11 -0
  8. {fjkit-0.1.3 → fjkit-0.2.0}/docs/workbench/templates/plugins.html +13 -4
  9. {fjkit-0.1.3 → fjkit-0.2.0}/docs/workbench/templates/zh/components.html +16 -1
  10. {fjkit-0.1.3 → fjkit-0.2.0}/docs/workbench/templates/zh/learn.html +10 -0
  11. {fjkit-0.1.3 → fjkit-0.2.0}/docs/workbench/templates/zh/plugins.html +11 -4
  12. {fjkit-0.1.3 → fjkit-0.2.0}/pyproject.toml +1 -1
  13. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/__init__.py +19 -1
  14. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/auth/__init__.py +10 -1
  15. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/auth/csrf.py +20 -0
  16. fjkit-0.2.0/src/fjkit/auth/errors.py +69 -0
  17. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/auth/plugin.py +276 -41
  18. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/auth/stores.py +12 -0
  19. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/auth/types.py +25 -4
  20. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/cli/check.py +127 -17
  21. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/cli/eject.py +153 -32
  22. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/cli/ejected.py +2 -2
  23. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/cli/vocabulary.py +33 -5
  24. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/config.py +39 -0
  25. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/errors.py +81 -13
  26. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/messages.py +31 -16
  27. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/rendering.py +34 -30
  28. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/signing.py +11 -11
  29. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/dist/fjkit-luma.css +1 -1
  30. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/dist/fjkit-lyra.css +1 -1
  31. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/dist/fjkit-maia.css +1 -1
  32. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/dist/fjkit-mira.css +1 -1
  33. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/dist/fjkit-nova.css +1 -1
  34. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/dist/fjkit-rhea.css +1 -1
  35. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/dist/fjkit-sera.css +1 -1
  36. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/dist/fjkit-vega.css +1 -1
  37. fjkit-0.2.0/src/fjkit/static/js/resize.js +198 -0
  38. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/src/fjkit.css +104 -1
  39. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/templates/errors/page.html +14 -13
  40. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/templates/ui/attrs.html +5 -1
  41. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/templates/ui/data.html +11 -7
  42. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/templates/ui/feedback.html +13 -2
  43. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/templates/ui/form.html +7 -4
  44. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/templates/ui/nav.html +5 -0
  45. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/templates/ui/overlay.html +83 -62
  46. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/templates/ui/shell.html +6 -2
  47. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/templates/ui/sidebar.html +5 -1
  48. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/templates/ui/table.html +56 -5
  49. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/templates/ui/tabs.html +5 -2
  50. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/templating.py +44 -2
  51. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/vendored.py +4 -3
  52. fjkit-0.2.0/tests/test_auth.py +1019 -0
  53. fjkit-0.2.0/tests/test_color_scheme.py +102 -0
  54. {fjkit-0.1.3 → fjkit-0.2.0}/tests/test_components.py +364 -8
  55. {fjkit-0.1.3 → fjkit-0.2.0}/tests/test_errors.py +181 -1
  56. {fjkit-0.1.3 → fjkit-0.2.0}/tests/test_flash.py +36 -0
  57. {fjkit-0.1.3 → fjkit-0.2.0}/tests/test_plugins.py +28 -0
  58. {fjkit-0.1.3 → fjkit-0.2.0}/tests/test_rendering.py +18 -0
  59. {fjkit-0.1.3 → fjkit-0.2.0}/tests/test_vocabulary.py +321 -1
  60. fjkit-0.1.3/src/fjkit/auth/errors.py +0 -31
  61. fjkit-0.1.3/tests/test_auth.py +0 -540
  62. {fjkit-0.1.3 → fjkit-0.2.0}/.gitignore +0 -0
  63. {fjkit-0.1.3 → fjkit-0.2.0}/LICENSE +0 -0
  64. {fjkit-0.1.3 → fjkit-0.2.0}/README.md +0 -0
  65. {fjkit-0.1.3 → fjkit-0.2.0}/docs/README.md +0 -0
  66. {fjkit-0.1.3 → fjkit-0.2.0}/docs/workbench/assets/brand.css +0 -0
  67. {fjkit-0.1.3 → fjkit-0.2.0}/docs/workbench/assets/common.js +0 -0
  68. {fjkit-0.1.3 → fjkit-0.2.0}/docs/workbench/assets/learn.js +0 -0
  69. {fjkit-0.1.3 → fjkit-0.2.0}/docs/workbench/assets/mock-server.js +0 -0
  70. {fjkit-0.1.3 → fjkit-0.2.0}/docs/workbench/templates/_diagram.html +0 -0
  71. {fjkit-0.1.3 → fjkit-0.2.0}/docs/workbench/templates/_parts.html +0 -0
  72. {fjkit-0.1.3 → fjkit-0.2.0}/docs/workbench/templates/_shell_diagram.html +0 -0
  73. {fjkit-0.1.3 → fjkit-0.2.0}/docs/workbench/templates/base.html +0 -0
  74. {fjkit-0.1.3 → fjkit-0.2.0}/docs/workbench/templates/cheatsheet.html +0 -0
  75. {fjkit-0.1.3 → fjkit-0.2.0}/docs/workbench/templates/introduction.html +0 -0
  76. {fjkit-0.1.3 → fjkit-0.2.0}/docs/workbench/templates/zh/cheatsheet.html +0 -0
  77. {fjkit-0.1.3 → fjkit-0.2.0}/docs/workbench/templates/zh/introduction.html +0 -0
  78. {fjkit-0.1.3 → fjkit-0.2.0}/scripts/vendor_icons.py +0 -0
  79. {fjkit-0.1.3 → fjkit-0.2.0}/scripts/vendor_ui.py +0 -0
  80. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/auth/sources.py +0 -0
  81. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/cli/__init__.py +0 -0
  82. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/cli/build_css.py +0 -0
  83. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/flash.py +0 -0
  84. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/forms.py +0 -0
  85. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/htmx.py +0 -0
  86. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/icons.py +0 -0
  87. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/mounting.py +0 -0
  88. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/plugins.py +0 -0
  89. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/py.typed +0 -0
  90. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/brand/torii-bolt.png +0 -0
  91. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/icons/lucide.json +0 -0
  92. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/js/errors.js +0 -0
  93. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/js/multiselect.js +0 -0
  94. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/js/reveal.js +0 -0
  95. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/js/select.js +0 -0
  96. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/base/base.css +0 -0
  97. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/basecoat-base.css +0 -0
  98. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/basecoat-components.css +0 -0
  99. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/basecoat-luma.css +0 -0
  100. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/basecoat-lyra.css +0 -0
  101. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/basecoat-maia.css +0 -0
  102. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/basecoat-mira.css +0 -0
  103. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/basecoat-nova.css +0 -0
  104. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/basecoat-rhea.css +0 -0
  105. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/basecoat-sera.css +0 -0
  106. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/basecoat-vega.css +0 -0
  107. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/basecoat.all.css +0 -0
  108. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/basecoat.css +0 -0
  109. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/accordion.css +0 -0
  110. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/alert.css +0 -0
  111. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/avatar.css +0 -0
  112. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/badge.css +0 -0
  113. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/breadcrumb.css +0 -0
  114. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/button-group.css +0 -0
  115. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/button.css +0 -0
  116. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/card.css +0 -0
  117. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/chart.css +0 -0
  118. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/checkbox.css +0 -0
  119. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/collapsible.css +0 -0
  120. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/combobox.css +0 -0
  121. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/command.css +0 -0
  122. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/dialog.css +0 -0
  123. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/drawer.css +0 -0
  124. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/dropdown-menu.css +0 -0
  125. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/empty.css +0 -0
  126. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/field.css +0 -0
  127. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/form.css +0 -0
  128. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/input-group.css +0 -0
  129. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/input.css +0 -0
  130. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/item.css +0 -0
  131. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/kbd.css +0 -0
  132. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/label.css +0 -0
  133. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/native-select.css +0 -0
  134. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/popover.css +0 -0
  135. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/progress.css +0 -0
  136. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/radio.css +0 -0
  137. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/range.css +0 -0
  138. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/scrollbar.css +0 -0
  139. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/select.css +0 -0
  140. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/sidebar.css +0 -0
  141. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/skeleton.css +0 -0
  142. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/switch.css +0 -0
  143. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/table.css +0 -0
  144. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/tabs.css +0 -0
  145. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/textarea.css +0 -0
  146. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/toast.css +0 -0
  147. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/components/tooltip.css +0 -0
  148. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/js/all.min.js +0 -0
  149. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/styles/luma.css +0 -0
  150. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/styles/lyra.css +0 -0
  151. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/styles/maia.css +0 -0
  152. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/styles/mira.css +0 -0
  153. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/styles/nova.css +0 -0
  154. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/styles/rhea.css +0 -0
  155. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/styles/sera.css +0 -0
  156. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/basecoat/styles/vega.css +0 -0
  157. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/htmx/htmx.min.js +0 -0
  158. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/htmx/idiomorph-ext.min.js +0 -0
  159. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/static/vendor/htmx/json-enc.js +0 -0
  160. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/styles.py +0 -0
  161. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/templates/ui/button.html +0 -0
  162. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/templates/ui/disclosure.html +0 -0
  163. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/templates/ui/icon.html +0 -0
  164. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/templates/ui/layout.html +0 -0
  165. {fjkit-0.1.3 → fjkit-0.2.0}/src/fjkit/templates/ui/swap.html +0 -0
  166. {fjkit-0.1.3 → fjkit-0.2.0}/tests/conftest.py +0 -0
  167. {fjkit-0.1.3 → fjkit-0.2.0}/tests/test_docs_site.py +0 -0
  168. {fjkit-0.1.3 → fjkit-0.2.0}/tests/test_messages.py +0 -0
  169. {fjkit-0.1.3 → fjkit-0.2.0}/tests/test_mounting.py +0 -0
  170. {fjkit-0.1.3 → fjkit-0.2.0}/tests/test_style_packs.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: fjkit
3
- Version: 0.1.3
3
+ Version: 0.2.0
4
4
  Summary: Server-rendered UI kit for FastAPI — Jinja2 + Basecoat + htmx, no front-end build
5
5
  Project-URL: Homepage, https://liweicheng00.github.io/fjkit/
6
6
  Project-URL: Documentation, https://liweicheng00.github.io/fjkit/
@@ -96,6 +96,7 @@ const TABLE_STATE = {
96
96
  empty: "empty",
97
97
  sortable: "sorted",
98
98
  "batch select": "select",
99
+ resizable: "resizable",
99
100
  };
100
101
 
101
102
  const TABLE_JINJA = {
@@ -142,6 +143,15 @@ const TABLE_JINJA = {
142
143
  {% endcall %}
143
144
 
144
145
  {% block scripts %}{{ select_scripts() }}{% endblock %}`,
146
+
147
+ resizable: `{% from "ui/table.html" import table, resize_scripts %}
148
+
149
+ {# the id is the key the widths are stored under #}
150
+ {% call table(TASK_COLUMNS, rows=tasks, resizable=true, id="tasks-table") %}
151
+ {% for task in tasks %}{{ task_row(request, task) }}{% endfor %}
152
+ {% endcall %}
153
+
154
+ {% block scripts %}{{ resize_scripts() }}{% endblock %}`,
145
155
  };
146
156
 
147
157
  const TABLE_CAPTION = {
@@ -149,6 +159,7 @@ const TABLE_CAPTION = {
149
159
  empty: "The same call with nothing to show. The empty state is inside the macro, so a page cannot ship a table that renders as a lone header row.",
150
160
  sortable: "A column becomes sortable by carrying a sort_url, and sort says which way it is going now. The arrow and aria-sort are keyed off that one value, so they cannot disagree. Every header is an href before it is a swap: sorting works with JavaScript off, and a sorted view is a URL you can send someone.",
151
161
  "batch select": "{\"select\": true} is the header box, select_cell is the row box, and the name they share is the field the selection posts under — selected=3&selected=7, which is what a checkbox column has always sent. The action collects it with hx-include; nothing maintains a list. select_scripts() is what ticks the column, shows the partial state and tints the picked rows.",
162
+ resizable: "Drag the edge of a heading, or focus it with Tab and press the arrow keys. Home or a double-click resets the column. The widths are stored in localStorage under the table's id and come back after a reload and after an htmx swap. Without resize_scripts() the handles stay hidden and the table is the ordinary one.",
152
163
  };
153
164
 
154
165
  const MACROS = {
@@ -272,7 +283,7 @@ const MACROS = {
272
283
  table: {
273
284
  label: "table",
274
285
  controls: [
275
- { key: "state", type: "select", label: "state", options: ["3 rows", "empty", "sortable", "batch select"], value: "3 rows" },
286
+ { key: "state", type: "select", label: "state", options: ["3 rows", "empty", "sortable", "batch select", "resizable"], value: "3 rows" },
276
287
  ],
277
288
  render: (p) => DATA.tables[TABLE_STATE[p.state]],
278
289
  jinja: (p) => TABLE_JINJA[p.state],
@@ -85,6 +85,7 @@ HTMX = STATIC / "vendor" / "htmx" / "htmx.min.js"
85
85
  BASECOAT = STATIC / "vendor" / "basecoat" / "js" / "all.min.js"
86
86
  ERRORS_JS = STATIC / "js" / "errors.js"
87
87
  REVEAL_JS = STATIC / "js" / "reveal.js"
88
+ RESIZE_JS = STATIC / "js" / "resize.js"
88
89
  MARK = STATIC / "brand" / "torii-bolt.png"
89
90
 
90
91
  OUT = REPO / "docs"
@@ -362,6 +363,7 @@ VENDORED = [
362
363
  # The Components page calls `reveal_scripts()`, so the reveal showcase is a
363
364
  # control a reader can press rather than a picture of one.
364
365
  (REVEAL_JS, "js/reveal.js", "git restore packages/fjkit/src/fjkit/static/js/reveal.js"),
366
+ (RESIZE_JS, "js/resize.js", "git restore packages/fjkit/src/fjkit/static/js/resize.js"),
365
367
  (MARK, "brand/torii-bolt.png", "git restore packages/fjkit/src/fjkit/static/brand/torii-bolt.png"),
366
368
  ]
367
369
 
@@ -265,6 +265,10 @@ tables = {
265
265
  f"{{% call table({SELECT_COLUMNS}, rows=[1, 2, 3]) %}}"
266
266
  f"{select_row_src}{{% endcall %}}"
267
267
  ),
268
+ "resizable": render(
269
+ f"{TABLE}{DATA}{BUTTON}"
270
+ f'{{% call table({COLUMNS}, rows=[1, 2, 3], resizable=true, id="docs-resizable") %}}{row_src}{{% endcall %}}'
271
+ ),
268
272
  }
269
273
 
270
274
  # ---------------------------------------------------------------- pagination
@@ -357,16 +357,19 @@ GROUPS = [
357
357
  'table(columns, rows=none, empty_title="Nothing here",\n'
358
358
  ' empty_description=none, empty_icon="list", target=none,\n'
359
359
  ' swap="outerHTML", push_url=true, select_name="selected",\n'
360
- ' select_label="Select all rows")',
360
+ ' select_label="Select all rows", resizable=false)',
361
361
  block="rows",
362
362
  en="columns is a list of dicts — label, and optionally align, width, "
363
363
  "sort, sort_url or select. Passing rows lets the macro own the empty "
364
364
  "case; pass rows=none to always render the body yourself. A column with "
365
365
  "a sort_url becomes a sortable header, and target turns those links into "
366
- "htmx swaps.",
366
+ "htmx swaps. resizable=true puts a drag handle on each labelled column "
367
+ "and needs an id=, the key the widths are stored under.",
367
368
  zh="columns 是 dict 的清單——label,可選 align、width、sort、sort_url 或 select。"
368
369
  "傳了 rows,空資料的情況就歸 macro 管;傳 rows=none 則永遠自己渲染 body。"
369
- "有 sort_url 的欄位會變成可排序表頭,給了 target 那些連結就同時是 htmx swap。",
370
+ "有 sort_url 的欄位會變成可排序表頭,給了 target 那些連結就同時是 htmx swap。"
371
+ "resizable=true 會在每個有 label 的欄位放一個拖曳把手,並且必須給 id=,"
372
+ "欄寬就存在這個 id 底下。",
370
373
  ),
371
374
  _m(
372
375
  "cell(value=none, tone=none, numeric=false, align=none)",
@@ -406,6 +409,14 @@ GROUPS = [
406
409
  zh="載入 js/select.js:從表頭的核取方塊勾選整欄、顯示部分選取狀態、"
407
410
  "把選到的列上色。逐頁載入,不進 shell。",
408
411
  ),
412
+ _m(
413
+ "resize_scripts()",
414
+ en="Loads js/resize.js, which shows the handles of a resizable table, "
415
+ "follows a drag or the arrow keys, and keeps the widths in localStorage. "
416
+ "Home or a double-click resets a column. Per page, never the shell.",
417
+ zh="載入 js/resize.js:顯示可調寬表格的把手,跟著拖曳或方向鍵改變欄寬,"
418
+ "並把欄寬存在 localStorage。Home 鍵或雙擊把欄寬還原。逐頁載入,不進 shell。",
419
+ ),
409
420
  _m(
410
421
  'page_size(url, per_page, options=(10, 25, 50, 100),\n'
411
422
  ' param="per_page", keep=none, target=none,\n'
@@ -584,8 +595,9 @@ GROUPS = [
584
595
  _m(
585
596
  "theme_toggle()",
586
597
  en="The shell renders one already; call it only when you have replaced "
587
- "header_actions.",
588
- zh="外殼已經放了一顆;只有在你換掉 header_actions 時才需要自己呼叫。",
598
+ "header_actions. It renders nothing under a fixed color_scheme.",
599
+ zh="外殼已經放了一顆;只有在你換掉 header_actions 時才需要自己呼叫。"
600
+ "color_scheme 固定時它不輸出任何東西。",
589
601
  ),
590
602
  _m(
591
603
  'breadcrumb(trail, separator="chevron", label="Breadcrumb")',
@@ -865,8 +877,10 @@ SHELL_BLOCKS = [
865
877
  },
866
878
  {
867
879
  "name": "header_actions",
868
- "en": "The right-hand end of the header. Defaults to theme_toggle().",
869
- "zh": "標頭右端。預設是 theme_toggle()。",
880
+ "en": "The right-hand end of the header. Defaults to theme_toggle() under "
881
+ 'color_scheme="system", and to nothing under "light" or "dark".',
882
+ "zh": '標頭右端。color_scheme="system" 時預設是 theme_toggle(),'
883
+ '"light" 或 "dark" 時預設是空的。',
870
884
  },
871
885
  {
872
886
  "name": "sidebar_trigger",
@@ -921,6 +935,11 @@ GLOBALS = [
921
935
  "en": "The versions in the footer: fjkit, fastapi, jinja2, basecoat, htmx.",
922
936
  "zh": "頁尾那幾個版本號:fjkit、fastapi、jinja2、basecoat、htmx。",
923
937
  },
938
+ {
939
+ "name": "fjkit_color_scheme",
940
+ "en": "What FjkitConfig.color_scheme renders: .prepaint_script, .html_class and .toggle.",
941
+ "zh": "FjkitConfig.color_scheme 的渲染結果:.prepaint_script、.html_class、.toggle。",
942
+ },
924
943
  {
925
944
  "name": "fjkit_icon_path(name)",
926
945
  "en": "The raw SVG path data for a Lucide name. icon() is what a template calls.",
@@ -4,6 +4,7 @@
4
4
  {% from "ui/tabs.html" import tab_panel, tabs %}
5
5
  {% from "ui/layout.html" import grid, page_header, stack %}
6
6
  {% from "ui/form.html" import reveal_scripts %}
7
+ {% from "ui/table.html" import resize_scripts %}
7
8
 
8
9
  {#
9
10
  One section per file in `src/fjkit/templates/ui/`, in the order `ls` prints
@@ -376,6 +377,13 @@
376
377
  <code>labels: list[str] = Form([])</code>. Without the script the field posts one JSON
377
378
  string, and the route answers 422.
378
379
  </p>
380
+ <p>
381
+ A multiple <code>combobox</code> is laid out for a narrow column: the filter box takes the
382
+ first line on its own, the chips wrap below it, the control fills the column and the list is
383
+ anchored to the box rather than to the bottom of the chips, so picking several options in a
384
+ row never moves the option under the pointer. Long labels wrap in the list and truncate in a
385
+ chip.
386
+ </p>
379
387
  <p>
380
388
  Up to about a dozen options, a checkbox group inside a <code>fieldset</code> posts the same
381
389
  thing and needs no script.
@@ -472,6 +480,15 @@
472
480
  the rest. It posts as the repeated field a checkbox column has always sent, and
473
481
  <code>select_scripts()</code> is what ticks the whole column from the header box.
474
482
  </p>
483
+ <p>
484
+ <code>resizable=true</code> puts a handle on the trailing edge of each labelled column. A
485
+ visitor drags it, or focuses it and presses the arrow keys; Home or a double-click gives the
486
+ column back its natural width. The widths are kept in <code>localStorage</code> under the
487
+ table's <code>id</code>, which is why a resizable table must have one, and they come back
488
+ after a reload and after an htmx swap. The page loads <code>resize_scripts()</code>; without
489
+ it the handles stay hidden and the table is the ordinary one. CSS cannot do this:
490
+ <code>resize</code> works only on a block box with overflow, and a table cell is not one.
491
+ </p>
475
492
  <p>
476
493
  <code>page_size</code> is a <code>&lt;form method="get"&gt;</code>, because a bare
477
494
  <code>&lt;select&gt;</code> cannot submit without a script. Its <code>url</code> must carry no
@@ -538,6 +555,7 @@
538
555
  {% block page_scripts %}
539
556
  <script defer src="{{ fjkit_static('components.js') }}"></script>
540
557
  {# The reveal showcase is a working control, so the page opts into its script
541
- the same way an app's sign-in page does. #}
558
+ the same way an app's sign-in page does. The resizable table is the same. #}
542
559
  {{ reveal_scripts() }}
560
+ {{ resize_scripts() }}
543
561
  {% endblock %}
@@ -1088,6 +1088,7 @@ def create_task(service: ServiceDep, payload: TaskCreate) -> BoardResponse:
1088
1088
  ("a ValidationError your own code raised", model_error),
1089
1089
  ("catch_unexpected_errors", unexpected),
1090
1090
  ("unexpected_error", "the words that toast says. Leave it and the kit says <em>Something went wrong</em>; set a <code>Message</code> to change them once, or a callable <code>(request, exc) -&gt; Message</code> to pick them per exception. Choose the words from the exception — never put <code>str(exc)</code> in them, that is the inside of your app"),
1091
+ ("a 404", "an unknown address, or a route that raised <code>HTTPException(404)</code>. A navigation gets errors/page.html, which says the page may have moved. An htmx swap gets an empty 404 and a toast, so the page it came from stays as it was. A JSON client gets FastAPI's own reply. Every other <code>HTTPException</code> is FastAPI's reply, headers included. Register your own <code>HTTPException</code> handler after <code>mount_fjkit()</code> and it replaces this one"),
1091
1092
  ("errors/page.html", "the page a failed navigation lands on. Replace it by writing a file at the same path in your own template directory — the same shadowing fjkit eject uses, so there is no separate custom-error-page mechanism to learn"),
1092
1093
  ("js/errors.js", "the third and last file every page loads, after htmx and Basecoat. It listens to htmx:beforeSwap, so a 422 that is not FastAPI's list — a route answering with markup of its own — is left alone"),
1093
1094
  ]) }}
@@ -1107,6 +1108,16 @@ def create_task(service: ServiceDep, payload: TaskCreate) -> BoardResponse:
1107
1108
  Watch the status badges. They do not move. Status colour is deliberately not wired to the
1108
1109
  brand, because "green means done" has to survive a rebrand.
1109
1110
  </p>
1111
+ <p>
1112
+ The Scheme buttons are the visitor's choice, and by default the visitor keeps it:
1113
+ <code>FjkitConfig(color_scheme="system")</code> follows their saved choice, then their OS,
1114
+ and puts a toggle in the header. An app that is only ever light or only ever dark says
1115
+ <code>color_scheme="light"</code> or <code>"dark"</code>. The server then renders the scheme
1116
+ on <code>&lt;html&gt;</code>, the toggle and the pre-paint script are left out, and a choice
1117
+ a visitor saved earlier has nothing left to read it. <code>theme_toggle()</code> renders
1118
+ nothing under a fixed scheme, so an app that fills <code>header_actions</code> itself
1119
+ calls it as before.
1120
+ </p>
1110
1121
  {% endcall %}
1111
1122
 
1112
1123
  {#
@@ -65,8 +65,17 @@ mount_fjkit(app, FjkitConfig(template_dir=…, plugins=(auth,)))", label="app/ma
65
65
  {% call prose() %}
66
66
  <p>
67
67
  Routes see none of it. Put <code>Depends(auth.required)</code> on a router and its handlers
68
- keep the signatures they had; the session is on <code>request.state.auth</code>, and in
69
- every template as <code>session</code>.
68
+ keep the signatures they had. A handler that wants the session takes it as a parameter,
69
+ <code>session: Session = Depends(auth.required)</code>, or <code>Depends(auth.optional)</code>
70
+ on a page that is also public; code that holds the plugin and a request calls
71
+ <code>auth.session(request)</code>; every template has it as <code>session</code>.
72
+ </p>
73
+ <p>
74
+ Those are the places to read it, and the reason is an outage. While the identity provider
75
+ is unreachable, a request whose token has already expired is answered 503 by whatever asks
76
+ for the session, and a handler that asks for nothing, such as a logout, runs. The
77
+ middleware's own <code>request.state.auth</code> is <code>None</code> on that request, so a
78
+ handler reading it directly would take a signed-in user for an anonymous one.
70
79
  </p>
71
80
  {% endcall %}
72
81
 
@@ -530,7 +539,7 @@ mount_fjkit(app, config)", label="app/main.py") }}
530
539
 
531
540
  {{ code("def extend(self, setup: EnvSetup) -> None:
532
541
  setup.add_context_processor(
533
- lambda request: {\"session\": getattr(request.state, \"auth\", None)},
542
+ lambda request: {\"session\": self.session(request)},
534
543
  provides=[\"session\"],
535
544
  )", label="the auth plugin's whole extend()") }}
536
545
 
@@ -539,7 +548,7 @@ mount_fjkit(app, config)", label="app/main.py") }}
539
548
  Two constraints, both deliberate. A processor is <strong>synchronous</strong>: it runs inside
540
549
  <code>Templates.page()</code>, which a <code>def</code> handler reaches from the threadpool. A
541
550
  plugin needing I/O does it in middleware and leaves the result on
542
- <code>request.state</code>, which is what the line above reads.
551
+ <code>request.state</code>, which is what <code>session()</code> reads for the line above.
543
552
  </p>
544
553
  <p>
545
554
  <code>provides=</code> is required rather than inferred. Without it, two plugins fighting over
@@ -4,6 +4,7 @@
4
4
  {% from "ui/tabs.html" import tab_panel, tabs %}
5
5
  {% from "ui/layout.html" import grid, page_header, stack %}
6
6
  {% from "ui/form.html" import reveal_scripts %}
7
+ {% from "ui/table.html" import resize_scripts %}
7
8
 
8
9
  {#
9
10
  Components 的中文版。結構、id、macro 呼叫與腳本掛點必須跟英文版逐字對應——
@@ -353,6 +354,11 @@
353
354
  <code>labels=bug&amp;labels=ui</code>。路由宣告 <code>labels: list[str] = Form([])</code>。
354
355
  沒有載入這支腳本時,欄位會送出一整串 JSON 字串,路由回應 422。
355
356
  </p>
357
+ <p>
358
+ multiple 的 <code>combobox</code> 是為窄欄排版的:篩選框自己佔第一行,chips 在它下方換行,
359
+ 控制項撐滿整欄,清單錨定在篩選框而不是 chips 的底部,所以連續選取幾個選項時,
360
+ 游標下的選項不會移動。過長的標籤在清單裡換行,在 chip 裡截斷。
361
+ </p>
356
362
  <p>
357
363
  選項在十來個以內時,放在 <code>fieldset</code> 裡的一組 checkbox 送出相同的內容,而且不需要腳本。
358
364
  </p>
@@ -442,6 +448,14 @@
442
448
  它送出的就是核取方塊欄位一直以來送出的那種重複欄位,
443
449
  而 <code>select_scripts()</code> 負責讓表頭那一格勾選整欄。
444
450
  </p>
451
+ <p>
452
+ <code>resizable=true</code> 會在每個有 label 的欄位右緣放一個把手。訪客可以拖曳它,
453
+ 或是聚焦後按方向鍵;按 Home 或雙擊則把欄寬還原為自然寬度。
454
+ 欄寬存在 <code>localStorage</code>,以表格的 <code>id</code> 為鍵,所以可調寬的表格必須有
455
+ <code>id</code>;重新載入或經過 htmx swap 後,欄寬都會還原。頁面要載入
456
+ <code>resize_scripts()</code>;沒有載入時把手不會出現,表格就是一般的表格。
457
+ CSS 做不到這件事:<code>resize</code> 只作用於帶 overflow 的區塊盒,而表格儲存格不是。
458
+ </p>
445
459
  <p>
446
460
  <code>page_size</code> 是一個 <code>&lt;form method="get"&gt;</code>,因為單獨一個
447
461
  <code>&lt;select&gt;</code> 沒有腳本就無法送出。它的 <code>url</code> 不能帶 query string:
@@ -507,6 +521,7 @@
507
521
  {% block page_scripts %}
508
522
  <script defer src="{{ fjkit_static('components.js') }}"></script>
509
523
  {# The reveal showcase is a working control, so the page opts into its script
510
- the same way an app's sign-in page does. #}
524
+ the same way an app's sign-in page does. The resizable table is the same. #}
511
525
  {{ reveal_scripts() }}
526
+ {{ resize_scripts() }}
512
527
  {% endblock %}
@@ -1051,6 +1051,7 @@ def create_task(service: ServiceDep, payload: TaskCreate) -> BoardResponse:
1051
1051
  ("你自己的程式碼丟出的 ValidationError", model_error),
1052
1052
  ("catch_unexpected_errors", unexpected),
1053
1053
  ("unexpected_error", "那則 toast 的內容。不設就是 kit 自己的一句 <em>Something went wrong</em>;給一個 <code>Message</code> 就整批換掉,給一個 <code>(request, exc) -&gt; Message</code> 的函式就能依例外種類挑句子。用例外挑句子,不要把 <code>str(exc)</code> 放進去,那是你 app 的內部"),
1054
+ ("404", "不存在的網址,或路由丟出的 <code>HTTPException(404)</code>。導覽會拿到 errors/page.html,告訴訪客這一頁可能已經移走。htmx swap 拿到一個空的 404 加一則 toast,原本的頁面保持原樣。JSON client 拿到 FastAPI 自己的回應。其他狀態碼的 <code>HTTPException</code> 一律是 FastAPI 的回應,header 也原樣保留。在 <code>mount_fjkit()</code> 之後註冊自己的 <code>HTTPException</code> handler,就會取代這一個"),
1054
1055
  ("errors/page.html", "導覽失敗時落到的那一頁。在自己的模板目錄放一個同路徑的檔案就能換掉,用的是與 fjkit eject 相同的遮蔽機制,所以沒有另一套「自訂錯誤頁」要學"),
1055
1056
  ("js/errors.js", "每一頁載入的第三個、也是最後一個檔案,排在 htmx 與 Basecoat 之後。它聽 htmx:beforeSwap,所以不是 FastAPI 清單的 422——某條路由自己回了 markup——會被放過"),
1056
1057
  ]) }}
@@ -1069,6 +1070,15 @@ def create_task(service: ServiceDep, payload: TaskCreate) -> BoardResponse:
1069
1070
  狀態標籤不動。狀態顏色刻意不接到品牌 token 上,
1070
1071
  因為「綠色代表完成」必須撐過一次改版。
1071
1072
  </p>
1073
+ <p>
1074
+ 「配色」那排按鈕是訪客的選擇,預設也交給訪客:
1075
+ <code>FjkitConfig(color_scheme="system")</code> 先看訪客存過的選擇,再看作業系統,
1076
+ 並在標頭放一顆切換鈕。只用淺色或只用深色的 app 設成 <code>color_scheme="light"</code>
1077
+ 或 <code>"dark"</code>,伺服器就直接把配色寫在 <code>&lt;html&gt;</code> 上,
1078
+ 切換鈕與首次繪製前的腳本都不輸出,訪客先前存過的選擇也不再有人讀取。
1079
+ 配色固定時 <code>theme_toggle()</code> 不輸出任何東西,
1080
+ 所以自己填 <code>header_actions</code> 的 app 照舊呼叫即可。
1081
+ </p>
1072
1082
  {% endcall %}
1073
1083
 
1074
1084
  {#
@@ -63,8 +63,15 @@ mount_fjkit(app, FjkitConfig(template_dir=…, plugins=(auth,)))", label="app/ma
63
63
  {% call prose() %}
64
64
  <p>
65
65
  路由看不到這些。在 router 上掛 <code>Depends(auth.required)</code>,底下的 handler
66
- 簽名不用改。session 在 <code>request.state.auth</code> 上,在每個模板裡叫
67
- <code>session</code>。
66
+ 簽名不用改。需要 session 的 handler 把它當參數拿:<code>session: Session = Depends(auth.required)</code>,
67
+ 公開頁面則用 <code>Depends(auth.optional)</code>;手上有 plugin 和 request 的程式碼呼叫
68
+ <code>auth.session(request)</code>;每個模板裡叫 <code>session</code>。
69
+ </p>
70
+ <p>
71
+ 只從這幾個地方讀,原因是斷線。身分提供者連不上時,token 已經過期的請求會由任何索取 session
72
+ 的地方回 503,什麼都不索取的 handler,例如登出,照常執行。middleware 自己的
73
+ <code>request.state.auth</code> 在那次請求裡是 <code>None</code>,直接讀它的 handler
74
+ 會把已登入的使用者當成匿名。
68
75
  </p>
69
76
  {% endcall %}
70
77
 
@@ -507,7 +514,7 @@ mount_fjkit(app, config)", label="app/main.py") }}
507
514
 
508
515
  {{ code("def extend(self, setup: EnvSetup) -> None:
509
516
  setup.add_context_processor(
510
- lambda request: {\"session\": getattr(request.state, \"auth\", None)},
517
+ lambda request: {\"session\": self.session(request)},
511
518
  provides=[\"session\"],
512
519
  )", label="auth plugin 的 extend(),全部") }}
513
520
 
@@ -516,7 +523,7 @@ mount_fjkit(app, config)", label="app/main.py") }}
516
523
  兩個限制都是刻意的。processor 必須<strong>同步</strong>:它在
517
524
  <code>Templates.page()</code> 裡執行,而那是 <code>def</code> handler 從 threadpool 進來的。
518
525
  需要 I/O 的 plugin 在 middleware 裡做完,把結果留在 <code>request.state</code> 上,
519
- 上面那一行讀的正是這個。
526
+ 上面那一行的 <code>session()</code> 讀的正是這個。
520
527
  </p>
521
528
  <p>
522
529
  <code>provides=</code> 是強制宣告,不是自動推得。沒有它,兩個 plugin 會默默搶同一個
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "fjkit"
3
- version = "0.1.3"
3
+ version = "0.2.0"
4
4
  description = "Server-rendered UI kit for FastAPI — Jinja2 + Basecoat + htmx, no front-end build"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.13"
@@ -21,15 +21,33 @@ Routes name their template on a decorator and return data:
21
21
 
22
22
  from __future__ import annotations
23
23
 
24
+ from typing import TYPE_CHECKING, Any
25
+
24
26
  from fjkit import messages
25
27
  from fjkit.config import FjkitConfig, RenderMode
26
- from fjkit.flash import FlashMessage, FlashPlugin
27
28
  from fjkit.messages import Message
28
29
  from fjkit.mounting import mount_fjkit
29
30
  from fjkit.plugins import AppSetup, EnvSetup, Plugin, PluginWarning
30
31
  from fjkit.rendering import render
31
32
  from fjkit.templating import Templates, build_environment, get_templates
32
33
 
34
+ if TYPE_CHECKING:
35
+ from fjkit.flash import FlashMessage, FlashPlugin
36
+
37
+ #: The names resolved on first use rather than at import. Flash is a plugin
38
+ #: (CHARTER §2.3): an app that does not register it imports nothing of it, and
39
+ #: `fjkit.flash` brings `fjkit.signing` with it.
40
+ _LAZY = {"FlashMessage", "FlashPlugin"}
41
+
42
+
43
+ def __getattr__(name: str) -> Any:
44
+ if name in _LAZY:
45
+ from fjkit import flash
46
+
47
+ return getattr(flash, name)
48
+ raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
49
+
50
+
33
51
  __all__ = [
34
52
  "AppSetup",
35
53
  "EnvSetup",
@@ -21,7 +21,14 @@ documents the CSRF cost a cookie brings back.
21
21
  from __future__ import annotations
22
22
 
23
23
  from fjkit.auth.csrf import NoCsrf, OriginCsrf
24
- from fjkit.auth.errors import AuthError, CsrfRejected, NotAuthenticated, RefreshFailed
24
+ from fjkit.auth.errors import (
25
+ AuthError,
26
+ CsrfRejected,
27
+ NotAuthenticated,
28
+ RefreshFailed,
29
+ RefreshRefused,
30
+ RefreshUnavailable,
31
+ )
25
32
  from fjkit.auth.plugin import AuthPlugin, CookieSpec
26
33
  from fjkit.auth.sources import LocalSource
27
34
  from fjkit.auth.stores import MemoryStore, RedisStore, SyncStore
@@ -46,6 +53,8 @@ __all__ = [
46
53
  "OriginCsrf",
47
54
  "RedisStore",
48
55
  "RefreshFailed",
56
+ "RefreshRefused",
57
+ "RefreshUnavailable",
49
58
  "RefreshableTokenSource",
50
59
  "Session",
51
60
  "SessionStore",
@@ -11,11 +11,31 @@ and `app.example.com` share a registrable domain, so Lax treats them as one site
11
11
  and sends the cookie. An `Origin` comparison is exact — scheme, host and port —
12
12
  so it catches exactly what Lax misses.
13
13
 
14
+ Login is the one anonymous write that is checked. A cross-site `POST /login`
15
+ carries no cookie, so the middleware has nothing to verify, yet it is the
16
+ request that sets one: a foreign page that submits the attacker's credentials
17
+ logs the visitor in as the attacker, and whatever they type next is recorded
18
+ under that account (login CSRF). `AuthPlugin.issue` therefore runs `verify`
19
+ whenever the request carries an `Origin`. A login without one comes from a
20
+ non-browser client, which has no visitor to plant a session in, and is let
21
+ through. Other anonymous writes stay unchecked, because an endpoint that is
22
+ open to anyone has nothing for a forged request to steal.
23
+
14
24
  This version ships no token strategy. A token has to reach every form and every
15
25
  htmx request, so the form macro and the shell both need request-scoped data;
16
26
  shipping a token means shipping a templating feature with it. `Csrf.on_issue` is
17
27
  the plug-in point when that cost is worth paying.
18
28
 
29
+ A login completed on a GET, which is what an OpenID Connect or OAuth callback
30
+ is (`GET /callback?code=…`), gets none of this. A browser sends no `Origin` on
31
+ a navigation and `verify` passes safe methods anyway, so a visitor sent to
32
+ `/callback?code=<attacker's code>` would be logged in as the attacker. The
33
+ `state` parameter closes it, and `AuthPlugin` carries it: `start_login` sets a
34
+ signed nonce cookie and returns the `state` for the authorization URL, and
35
+ `check_state` in the callback refuses the request unless the provider echoed
36
+ that browser's nonce back. The app calls them in its own two routes, because
37
+ the plugin owns neither the redirect out nor the callback in.
38
+
19
39
  Two cases this cannot cover:
20
40
 
21
41
  * A GET that changes state. No header check helps; fix it in the routing.
@@ -0,0 +1,69 @@
1
+ """The failures this plugin raises, and the response each one implies.
2
+
3
+ Three 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
+ `RefreshUnavailable` leaves the session in place and answers 503, because the
7
+ request was fine and the identity provider was not, and the token has expired
8
+ so there is nothing left to serve the request on.
9
+
10
+ `RefreshRefused` is the one exception here a `TokenSource` raises rather than
11
+ the plugin: it is how `refresh` says the upstream said no.
12
+ """
13
+
14
+ from __future__ import annotations
15
+
16
+ __all__ = [
17
+ "AuthError",
18
+ "CsrfRejected",
19
+ "NotAuthenticated",
20
+ "RefreshFailed",
21
+ "RefreshRefused",
22
+ "RefreshUnavailable",
23
+ ]
24
+
25
+
26
+ class AuthError(Exception):
27
+ """Base for everything this plugin raises."""
28
+
29
+
30
+ class NotAuthenticated(AuthError):
31
+ """No usable session: no cookie, an unknown sid, or an expired store entry."""
32
+
33
+
34
+ class RefreshFailed(NotAuthenticated):
35
+ """The upstream refused to renew the token, and the session is gone.
36
+
37
+ A subclass rather than a sibling, because a failed refresh also means the
38
+ user logs in again: one handler covers both, and no caller has to remember
39
+ the second name.
40
+ """
41
+
42
+
43
+ class RefreshUnavailable(AuthError):
44
+ """The upstream could not be asked to renew the token, and the session is kept.
45
+
46
+ A sibling of `NotAuthenticated` rather than a subclass: sending the user to
47
+ the login page would be wrong, because they are still logged in and the
48
+ login page needs the same upstream. The request is answered 503 with a
49
+ `Retry-After`, and the next request tries the refresh again.
50
+
51
+ Raised only once the token has expired. Renewal starts `refresh_leeway`
52
+ ahead of that, and a request inside the leeway is served on the token it
53
+ still has. Logout never raises it: the plugin defers the refusal to
54
+ whatever asks for the session, and a logout asks for nothing.
55
+ """
56
+
57
+
58
+ class RefreshRefused(Exception):
59
+ """Raised by `TokenSource.refresh` when the upstream said no.
60
+
61
+ The one definitive answer: the refresh token is revoked, expired or
62
+ unknown, and no retry will change that. Anything else `refresh` raises (a
63
+ connection error, a timeout, an upstream 5xx) is read as transient. Not an
64
+ `AuthError`, because it is the source's word to the plugin, not a response.
65
+ """
66
+
67
+
68
+ class CsrfRejected(AuthError):
69
+ """A cookie-authenticated write arrived without a trusted `Origin`."""