hyper-admin 1.0.0__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.
- hyper_admin-1.0.0.dist-info/METADATA +366 -0
- hyper_admin-1.0.0.dist-info/RECORD +442 -0
- hyper_admin-1.0.0.dist-info/WHEEL +4 -0
- hyper_admin-1.0.0.dist-info/entry_points.txt +2 -0
- hyper_admin-1.0.0.dist-info/licenses/LICENSE +21 -0
- hyperadmin/__init__.py +52 -0
- hyperadmin/__main__.py +21 -0
- hyperadmin/adapters/__init__.py +28 -0
- hyperadmin/adapters/_estimate.py +33 -0
- hyperadmin/adapters/_filter_clause.py +9 -0
- hyperadmin/adapters/_occ.py +44 -0
- hyperadmin/adapters/_relation_loading.py +23 -0
- hyperadmin/adapters/_search.py +84 -0
- hyperadmin/adapters/filtering.py +86 -0
- hyperadmin/adapters/introspection.py +239 -0
- hyperadmin/adapters/registry.py +37 -0
- hyperadmin/adapters/schema.py +239 -0
- hyperadmin/adapters/sqlalchemy.py +423 -0
- hyperadmin/adapters/sqlmodel.py +650 -0
- hyperadmin/auth/__init__.py +156 -0
- hyperadmin/auth/_types.py +52 -0
- hyperadmin/auth/backend.py +37 -0
- hyperadmin/auth/bridge.py +351 -0
- hyperadmin/auth/exceptions.py +124 -0
- hyperadmin/auth/middleware.py +287 -0
- hyperadmin/auth/models.py +245 -0
- hyperadmin/auth/oauth/__init__.py +81 -0
- hyperadmin/auth/oauth/backend.py +611 -0
- hyperadmin/auth/oauth/providers.py +214 -0
- hyperadmin/auth/oauth/refresh.py +126 -0
- hyperadmin/auth/oauth/session.py +96 -0
- hyperadmin/auth/oauth/tokens.py +114 -0
- hyperadmin/auth/oauth/views.py +306 -0
- hyperadmin/auth/oauth/wiring.py +131 -0
- hyperadmin/auth/otp.py +189 -0
- hyperadmin/auth/permissions.py +232 -0
- hyperadmin/auth/session.py +81 -0
- hyperadmin/auth/session_middleware.py +164 -0
- hyperadmin/auth/token_handoff.py +452 -0
- hyperadmin/auth/user_cache.py +222 -0
- hyperadmin/auth/views.py +581 -0
- hyperadmin/core/__init__.py +76 -0
- hyperadmin/core/actions.py +151 -0
- hyperadmin/core/adapters.py +883 -0
- hyperadmin/core/app.py +1437 -0
- hyperadmin/core/auth.py +97 -0
- hyperadmin/core/bulk_results.py +28 -0
- hyperadmin/core/cache.py +68 -0
- hyperadmin/core/choices.py +38 -0
- hyperadmin/core/concurrency.py +107 -0
- hyperadmin/core/csrf.py +108 -0
- hyperadmin/core/discovery.py +161 -0
- hyperadmin/core/display.py +172 -0
- hyperadmin/core/fields.py +158 -0
- hyperadmin/core/fieldsets.py +22 -0
- hyperadmin/core/filtering.py +403 -0
- hyperadmin/core/filters_compat.py +78 -0
- hyperadmin/core/inlines.py +64 -0
- hyperadmin/core/introspection.py +280 -0
- hyperadmin/core/layouts.py +17 -0
- hyperadmin/core/lifecycle.py +278 -0
- hyperadmin/core/model.py +189 -0
- hyperadmin/core/navigation.py +354 -0
- hyperadmin/core/options.py +391 -0
- hyperadmin/core/palette.py +94 -0
- hyperadmin/core/panels.py +227 -0
- hyperadmin/core/plugin_wiring.py +205 -0
- hyperadmin/core/plugins.py +588 -0
- hyperadmin/core/primary_key.py +118 -0
- hyperadmin/core/rate_limit.py +90 -0
- hyperadmin/core/registry.py +129 -0
- hyperadmin/core/relation_loading.py +25 -0
- hyperadmin/core/schema.py +83 -0
- hyperadmin/core/sensitive.py +134 -0
- hyperadmin/core/settings.py +377 -0
- hyperadmin/core/storage_paths.py +54 -0
- hyperadmin/core/timezones.py +137 -0
- hyperadmin/core/uploads.py +16 -0
- hyperadmin/db.py +160 -0
- hyperadmin/discover.py +54 -0
- hyperadmin/filters/__init__.py +39 -0
- hyperadmin/filters/base.py +246 -0
- hyperadmin/filters/boolean.py +105 -0
- hyperadmin/filters/choice.py +74 -0
- hyperadmin/filters/date.py +265 -0
- hyperadmin/filters/relation.py +69 -0
- hyperadmin/filters/saved_views.py +203 -0
- hyperadmin/i18n/__init__.py +50 -0
- hyperadmin/i18n/loader.py +150 -0
- hyperadmin/i18n/middleware.py +159 -0
- hyperadmin/i18n/validation_messages.py +264 -0
- hyperadmin/loadtest/__init__.py +28 -0
- hyperadmin/loadtest/batch.py +263 -0
- hyperadmin/loadtest/checkpoint.py +168 -0
- hyperadmin/loadtest/generators/__init__.py +29 -0
- hyperadmin/loadtest/generators/auth.py +86 -0
- hyperadmin/loadtest/generators/erp.py +177 -0
- hyperadmin/loadtest/plan.py +189 -0
- hyperadmin/loadtest/pool.py +113 -0
- hyperadmin/loadtest/progress.py +150 -0
- hyperadmin/locale/ar/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/ar/LC_MESSAGES/messages.po +2139 -0
- hyperadmin/locale/de/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/de/LC_MESSAGES/messages.po +2114 -0
- hyperadmin/locale/en/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/en/LC_MESSAGES/messages.po +1780 -0
- hyperadmin/locale/es/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/es/LC_MESSAGES/messages.po +2094 -0
- hyperadmin/locale/fr/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/fr/LC_MESSAGES/messages.po +2100 -0
- hyperadmin/locale/he/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/he/LC_MESSAGES/messages.po +2076 -0
- hyperadmin/locale/hi/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/hi/LC_MESSAGES/messages.po +2084 -0
- hyperadmin/locale/id/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/id/LC_MESSAGES/messages.po +2071 -0
- hyperadmin/locale/it/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/it/LC_MESSAGES/messages.po +2098 -0
- hyperadmin/locale/ja/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/ja/LC_MESSAGES/messages.po +2049 -0
- hyperadmin/locale/ko/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/ko/LC_MESSAGES/messages.po +2049 -0
- hyperadmin/locale/nl/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/nl/LC_MESSAGES/messages.po +2100 -0
- hyperadmin/locale/pl/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/pl/LC_MESSAGES/messages.po +2112 -0
- hyperadmin/locale/pt_BR/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/pt_BR/LC_MESSAGES/messages.po +2098 -0
- hyperadmin/locale/ru/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/ru/LC_MESSAGES/messages.po +2112 -0
- hyperadmin/locale/th/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/th/LC_MESSAGES/messages.po +2065 -0
- hyperadmin/locale/tr/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/tr/LC_MESSAGES/messages.po +2084 -0
- hyperadmin/locale/uk/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/uk/LC_MESSAGES/messages.po +1889 -0
- hyperadmin/locale/vi/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/vi/LC_MESSAGES/messages.po +2063 -0
- hyperadmin/locale/zh_CN/LC_MESSAGES/messages.mo +0 -0
- hyperadmin/locale/zh_CN/LC_MESSAGES/messages.po +2049 -0
- hyperadmin/main.py +3 -0
- hyperadmin/management/__init__.py +0 -0
- hyperadmin/management/commands/__init__.py +0 -0
- hyperadmin/management/commands/createsuperuser.py +114 -0
- hyperadmin/management/commands/initdb.py +76 -0
- hyperadmin/management/commands/plugins.py +31 -0
- hyperadmin/management/commands/seed.py +125 -0
- hyperadmin/navigation/__init__.py +9 -0
- hyperadmin/navigation/pins.py +153 -0
- hyperadmin/panels/__init__.py +10 -0
- hyperadmin/panels/pdf.py +151 -0
- hyperadmin/permissions/__init__.py +59 -0
- hyperadmin/permissions/audit.py +109 -0
- hyperadmin/permissions/matrix.py +343 -0
- hyperadmin/permissions/object_rules.py +143 -0
- hyperadmin/realtime/__init__.py +51 -0
- hyperadmin/realtime/_debug.py +77 -0
- hyperadmin/realtime/config.py +124 -0
- hyperadmin/realtime/emitting.py +143 -0
- hyperadmin/realtime/events.py +107 -0
- hyperadmin/realtime/presence.py +193 -0
- hyperadmin/realtime/presence_redis.py +142 -0
- hyperadmin/realtime/pubsub.py +129 -0
- hyperadmin/realtime/pubsub_redis.py +159 -0
- hyperadmin/realtime/registry.py +102 -0
- hyperadmin/realtime/sse.py +82 -0
- hyperadmin/realtime/ws.py +477 -0
- hyperadmin/routing.py +680 -0
- hyperadmin/static/css/_accessibility.css +149 -0
- hyperadmin/static/css/_alerts.css +147 -0
- hyperadmin/static/css/_autocomplete.css +39 -0
- hyperadmin/static/css/_banner.css +118 -0
- hyperadmin/static/css/_buttons.css +241 -0
- hyperadmin/static/css/_card.css +86 -0
- hyperadmin/static/css/_dark-mode.css +232 -0
- hyperadmin/static/css/_fieldsets.css +71 -0
- hyperadmin/static/css/_filter.css +203 -0
- hyperadmin/static/css/_fonts.css +25 -0
- hyperadmin/static/css/_forms.css +862 -0
- hyperadmin/static/css/_inline-cell.css +222 -0
- hyperadmin/static/css/_inlines.css +160 -0
- hyperadmin/static/css/_layout.css +62 -0
- hyperadmin/static/css/_login.css +112 -0
- hyperadmin/static/css/_menu.css +156 -0
- hyperadmin/static/css/_navbar.css +299 -0
- hyperadmin/static/css/_page-header.css +139 -0
- hyperadmin/static/css/_pagination.css +125 -0
- hyperadmin/static/css/_palette.css +304 -0
- hyperadmin/static/css/_panels.css +174 -0
- hyperadmin/static/css/_permissions.css +547 -0
- hyperadmin/static/css/_pill.css +123 -0
- hyperadmin/static/css/_popover.css +170 -0
- hyperadmin/static/css/_popup.css +65 -0
- hyperadmin/static/css/_progress.css +161 -0
- hyperadmin/static/css/_realtime.css +78 -0
- hyperadmin/static/css/_reset.css +48 -0
- hyperadmin/static/css/_responsive.css +51 -0
- hyperadmin/static/css/_rtl.css +23 -0
- hyperadmin/static/css/_search.css +67 -0
- hyperadmin/static/css/_seg.css +95 -0
- hyperadmin/static/css/_sidebar.css +446 -0
- hyperadmin/static/css/_sticky.css +54 -0
- hyperadmin/static/css/_table.css +776 -0
- hyperadmin/static/css/_theme-terminal-type.css +92 -0
- hyperadmin/static/css/_theme-terminal.css +330 -0
- hyperadmin/static/css/_theme-toggle.css +46 -0
- hyperadmin/static/css/_tokens.css +232 -0
- hyperadmin/static/css/_utilities.css +133 -0
- hyperadmin/static/css/hyperadmin.css +58 -0
- hyperadmin/static/fonts/Inter-OFL.txt +93 -0
- hyperadmin/static/fonts/InterVariable.woff2 +0 -0
- hyperadmin/static/fonts/JetBrainsMono-OFL.txt +93 -0
- hyperadmin/static/fonts/JetBrainsMonoVariable.woff2 +0 -0
- hyperadmin/static/icons/arrow-down.svg +1 -0
- hyperadmin/static/icons/arrow-up.svg +1 -0
- hyperadmin/static/icons/bar-chart-3.svg +1 -0
- hyperadmin/static/icons/bell.svg +1 -0
- hyperadmin/static/icons/bookmark.svg +1 -0
- hyperadmin/static/icons/briefcase.svg +1 -0
- hyperadmin/static/icons/building-2.svg +1 -0
- hyperadmin/static/icons/calendar.svg +1 -0
- hyperadmin/static/icons/check.svg +1 -0
- hyperadmin/static/icons/chevron-down.svg +1 -0
- hyperadmin/static/icons/chevron-left.svg +1 -0
- hyperadmin/static/icons/chevron-right.svg +1 -0
- hyperadmin/static/icons/circle-check.svg +1 -0
- hyperadmin/static/icons/circle-help.svg +1 -0
- hyperadmin/static/icons/circle-x.svg +1 -0
- hyperadmin/static/icons/clock.svg +1 -0
- hyperadmin/static/icons/columns-3.svg +1 -0
- hyperadmin/static/icons/command.svg +1 -0
- hyperadmin/static/icons/copy.svg +1 -0
- hyperadmin/static/icons/credit-card.svg +1 -0
- hyperadmin/static/icons/database.svg +1 -0
- hyperadmin/static/icons/download.svg +1 -0
- hyperadmin/static/icons/ellipsis.svg +1 -0
- hyperadmin/static/icons/external-link.svg +1 -0
- hyperadmin/static/icons/eye.svg +1 -0
- hyperadmin/static/icons/file-text.svg +1 -0
- hyperadmin/static/icons/filter.svg +1 -0
- hyperadmin/static/icons/folder.svg +1 -0
- hyperadmin/static/icons/globe.svg +1 -0
- hyperadmin/static/icons/history.svg +1 -0
- hyperadmin/static/icons/home.svg +1 -0
- hyperadmin/static/icons/image.svg +1 -0
- hyperadmin/static/icons/info.svg +1 -0
- hyperadmin/static/icons/key-round.svg +1 -0
- hyperadmin/static/icons/layers.svg +1 -0
- hyperadmin/static/icons/layout-grid.svg +1 -0
- hyperadmin/static/icons/list-checks.svg +1 -0
- hyperadmin/static/icons/lock.svg +1 -0
- hyperadmin/static/icons/log-out.svg +1 -0
- hyperadmin/static/icons/mail.svg +1 -0
- hyperadmin/static/icons/map-pin.svg +1 -0
- hyperadmin/static/icons/menu.svg +1 -0
- hyperadmin/static/icons/moon.svg +1 -0
- hyperadmin/static/icons/package.svg +1 -0
- hyperadmin/static/icons/panel-left-close.svg +1 -0
- hyperadmin/static/icons/panel-left-open.svg +1 -0
- hyperadmin/static/icons/pencil.svg +1 -0
- hyperadmin/static/icons/pin-off.svg +1 -0
- hyperadmin/static/icons/pin.svg +1 -0
- hyperadmin/static/icons/plus.svg +1 -0
- hyperadmin/static/icons/receipt.svg +1 -0
- hyperadmin/static/icons/refresh-cw.svg +1 -0
- hyperadmin/static/icons/rotate-ccw.svg +1 -0
- hyperadmin/static/icons/search.svg +1 -0
- hyperadmin/static/icons/settings.svg +1 -0
- hyperadmin/static/icons/shield-check.svg +1 -0
- hyperadmin/static/icons/shopping-cart.svg +1 -0
- hyperadmin/static/icons/sliders-horizontal.svg +1 -0
- hyperadmin/static/icons/sparkles.svg +1 -0
- hyperadmin/static/icons/store.svg +1 -0
- hyperadmin/static/icons/sun.svg +1 -0
- hyperadmin/static/icons/tag.svg +1 -0
- hyperadmin/static/icons/trash-2.svg +1 -0
- hyperadmin/static/icons/triangle-alert.svg +1 -0
- hyperadmin/static/icons/truck.svg +1 -0
- hyperadmin/static/icons/upload.svg +1 -0
- hyperadmin/static/icons/user.svg +1 -0
- hyperadmin/static/icons/users.svg +1 -0
- hyperadmin/static/icons/wallet.svg +1 -0
- hyperadmin/static/icons/x.svg +1 -0
- hyperadmin/static/img/hyperadmin-favicon.svg +1 -0
- hyperadmin/static/img/hyperadmin-mark-mono.svg +1 -0
- hyperadmin/static/img/hyperadmin-mark.svg +1 -0
- hyperadmin/static/img/hyperadmin-wordmark-dark.svg +1 -0
- hyperadmin/static/img/hyperadmin-wordmark.svg +1 -0
- hyperadmin/static/img/terminal/hyperadmin-favicon-16.svg +1 -0
- hyperadmin/static/img/terminal/hyperadmin-favicon.svg +1 -0
- hyperadmin/static/img/terminal/hyperadmin-mark.svg +1 -0
- hyperadmin/static/img/terminal/hyperadmin-wordmark-dark.svg +1 -0
- hyperadmin/static/img/terminal/hyperadmin-wordmark.svg +1 -0
- hyperadmin/static/js/autocomplete.js +475 -0
- hyperadmin/static/js/confirm_delete.js +113 -0
- hyperadmin/static/js/error_summary.js +37 -0
- hyperadmin/static/js/form_guard.js +77 -0
- hyperadmin/static/js/inline_edit.js +97 -0
- hyperadmin/static/js/palette.js +292 -0
- hyperadmin/static/js/primitives.js +205 -0
- hyperadmin/static/js/realtime-status.js +515 -0
- hyperadmin/static/js/request_errors.js +85 -0
- hyperadmin/static/js/shortcuts.js +175 -0
- hyperadmin/static/js/sidebar.js +83 -0
- hyperadmin/static/js/vendor/LICENSE-alpinejs.txt +21 -0
- hyperadmin/static/js/vendor/LICENSE-htmx.txt +25 -0
- hyperadmin/static/js/vendor/alpinejs-3.17.4.min.js +21 -0
- hyperadmin/static/js/vendor/alpinejs-focus-3.17.4.min.js +18 -0
- hyperadmin/static/js/vendor/htmx-1.9.10.min.js +1 -0
- hyperadmin/templates/_base.html +161 -0
- hyperadmin/templates/_messages.html +11 -0
- hyperadmin/templates/_navbar.html +126 -0
- hyperadmin/templates/_sidebar.html +80 -0
- hyperadmin/templates/auth/mfa_challenge.html +75 -0
- hyperadmin/templates/auth/mfa_settings.html +110 -0
- hyperadmin/templates/auth/oauth_link_required.html +24 -0
- hyperadmin/templates/auth/oauth_settings.html +55 -0
- hyperadmin/templates/base.html +16 -0
- hyperadmin/templates/bulk_form.html +18 -0
- hyperadmin/templates/bulk_result.html +20 -0
- hyperadmin/templates/components/_icons.html +86 -0
- hyperadmin/templates/components/_macros.html +27 -0
- hyperadmin/templates/components/_nav.html +55 -0
- hyperadmin/templates/components/_row_access.html +26 -0
- hyperadmin/templates/components/banner.html +77 -0
- hyperadmin/templates/components/bulk_form.html +81 -0
- hyperadmin/templates/components/bulk_result.html +133 -0
- hyperadmin/templates/components/bulk_toolbar.html +63 -0
- hyperadmin/templates/components/command_palette.html +59 -0
- hyperadmin/templates/components/confirm_delete_dialog.html +26 -0
- hyperadmin/templates/components/conflict_dialog.html +42 -0
- hyperadmin/templates/components/csrf_reload_banner.html +20 -0
- hyperadmin/templates/components/error_summary.html +39 -0
- hyperadmin/templates/components/error_toast.html +18 -0
- hyperadmin/templates/components/fieldset.html +38 -0
- hyperadmin/templates/components/filter_bar.html +50 -0
- hyperadmin/templates/components/filter_sidebar.html +31 -0
- hyperadmin/templates/components/filters/boolean.html +10 -0
- hyperadmin/templates/components/filters/current_period.html +8 -0
- hyperadmin/templates/components/filters/date_range.html +26 -0
- hyperadmin/templates/components/filters/is_owner.html +8 -0
- hyperadmin/templates/components/filters/multi_choice.html +10 -0
- hyperadmin/templates/components/filters/multi_relation.html +20 -0
- hyperadmin/templates/components/filters/select.html +11 -0
- hyperadmin/templates/components/inline_cell.html +23 -0
- hyperadmin/templates/components/inline_cell_error.html +51 -0
- hyperadmin/templates/components/inline_cell_stale.html +20 -0
- hyperadmin/templates/components/inline_editor.html +85 -0
- hyperadmin/templates/components/inline_formset.html +34 -0
- hyperadmin/templates/components/inline_row.html +49 -0
- hyperadmin/templates/components/list_error.html +12 -0
- hyperadmin/templates/components/menu.html +74 -0
- hyperadmin/templates/components/nav_pinned.html +24 -0
- hyperadmin/templates/components/pagination.html +87 -0
- hyperadmin/templates/components/panel_document.html +19 -0
- hyperadmin/templates/components/panel_states.html +35 -0
- hyperadmin/templates/components/panel_tab_strip.html +31 -0
- hyperadmin/templates/components/pill.html +15 -0
- hyperadmin/templates/components/popover.html +59 -0
- hyperadmin/templates/components/presence_banner.html +28 -0
- hyperadmin/templates/components/progress.html +72 -0
- hyperadmin/templates/components/saved_views.html +61 -0
- hyperadmin/templates/components/search_input.html +19 -0
- hyperadmin/templates/components/seg.html +38 -0
- hyperadmin/templates/components/shortcuts_overlay.html +45 -0
- hyperadmin/templates/components/sortable_header.html +23 -0
- hyperadmin/templates/components/table.html +45 -0
- hyperadmin/templates/components/table_body.html +86 -0
- hyperadmin/templates/components/thumbnail.html +8 -0
- hyperadmin/templates/components/toast.html +36 -0
- hyperadmin/templates/components/tooltip.html +20 -0
- hyperadmin/templates/create.html +43 -0
- hyperadmin/templates/dashboard.html +8 -0
- hyperadmin/templates/detail.html +120 -0
- hyperadmin/templates/detail_layout.html +15 -0
- hyperadmin/templates/error.html +24 -0
- hyperadmin/templates/form_layout.html +26 -0
- hyperadmin/templates/list.html +5 -0
- hyperadmin/templates/list_layout.html +105 -0
- hyperadmin/templates/login.html +79 -0
- hyperadmin/templates/permissions/_banner.html +55 -0
- hyperadmin/templates/permissions/_diff_row.html +6 -0
- hyperadmin/templates/permissions/_group_gone.html +16 -0
- hyperadmin/templates/permissions/_macros.html +22 -0
- hyperadmin/templates/permissions/matrix.html +408 -0
- hyperadmin/templates/permissions/object_popover.html +95 -0
- hyperadmin/templates/update.html +69 -0
- hyperadmin/templates/widgets/autocomplete.html +100 -0
- hyperadmin/templates/widgets/checkbox_input.html +20 -0
- hyperadmin/templates/widgets/choices_options.html +3 -0
- hyperadmin/templates/widgets/date_input.html +13 -0
- hyperadmin/templates/widgets/datetime_input.html +18 -0
- hyperadmin/templates/widgets/encoded_textarea.html +4 -0
- hyperadmin/templates/widgets/file_input.html +15 -0
- hyperadmin/templates/widgets/float_input.html +12 -0
- hyperadmin/templates/widgets/image_input.html +108 -0
- hyperadmin/templates/widgets/multiselect_input.html +13 -0
- hyperadmin/templates/widgets/number_input.html +11 -0
- hyperadmin/templates/widgets/popup_form.html +64 -0
- hyperadmin/templates/widgets/relation_multiselect_input.html +36 -0
- hyperadmin/templates/widgets/relation_select_input.html +38 -0
- hyperadmin/templates/widgets/select_input.html +17 -0
- hyperadmin/templates/widgets/text_input.html +13 -0
- hyperadmin/templates/widgets/textarea.html +4 -0
- hyperadmin/uploads/__init__.py +44 -0
- hyperadmin/uploads/images.py +272 -0
- hyperadmin/uploads/storage.py +265 -0
- hyperadmin/uploads/validation.py +37 -0
- hyperadmin/views/__init__.py +3 -0
- hyperadmin/views/csrf.py +316 -0
- hyperadmin/views/dynamic/__init__.py +35 -0
- hyperadmin/views/dynamic/actions.py +264 -0
- hyperadmin/views/dynamic/base.py +576 -0
- hyperadmin/views/dynamic/concurrency.py +138 -0
- hyperadmin/views/dynamic/core.py +33 -0
- hyperadmin/views/dynamic/create.py +189 -0
- hyperadmin/views/dynamic/detail.py +231 -0
- hyperadmin/views/dynamic/files.py +233 -0
- hyperadmin/views/dynamic/form_data.py +162 -0
- hyperadmin/views/dynamic/helpers.py +151 -0
- hyperadmin/views/dynamic/inline.py +348 -0
- hyperadmin/views/dynamic/list_view.py +612 -0
- hyperadmin/views/dynamic/model_view.py +17 -0
- hyperadmin/views/dynamic/palette_search.py +78 -0
- hyperadmin/views/dynamic/relations.py +544 -0
- hyperadmin/views/dynamic/saved_views.py +159 -0
- hyperadmin/views/dynamic/update.py +313 -0
- hyperadmin/views/error_pages.py +46 -0
- hyperadmin/views/forms.py +1294 -0
- hyperadmin/views/htmx.py +53 -0
- hyperadmin/views/locale.py +113 -0
- hyperadmin/views/nav_pins.py +149 -0
- hyperadmin/views/navigation.py +143 -0
- hyperadmin/views/palette_context.py +91 -0
- hyperadmin/views/palette_search.py +103 -0
- hyperadmin/views/permissions_matrix.py +490 -0
- hyperadmin/views/rate_limit.py +87 -0
- hyperadmin/views/route.py +316 -0
- hyperadmin/views/static.py +138 -0
- hyperadmin/views/template_filters.py +305 -0
- hyperadmin/views/uploads.py +92 -0
- hyperadmin/views/urls.py +180 -0
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"""Structured errors of the built-in authentication flows (OAuth sign-in, MFA).
|
|
2
|
+
|
|
3
|
+
They carry context only (provider, detail, user, redirect) so a view can map each
|
|
4
|
+
failure to a template and a status code. Everything derives from
|
|
5
|
+
:class:`HyperAdminAuthError`. See ``docs/specs/oauth-sso.md``.
|
|
6
|
+
|
|
7
|
+
This module imports nothing from the ORM models: importing it registers no table.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
from typing import Any
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class HyperAdminAuthError(Exception):
|
|
16
|
+
"""Base class of every HyperAdmin authentication error."""
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class OAuthConfigError(HyperAdminAuthError, ValueError):
|
|
20
|
+
"""OAuth is misconfigured: a missing secret, client id, extra or collaborator.
|
|
21
|
+
|
|
22
|
+
Raised when the configuration is built (``OAuthProviderConfig``, ``OAuthBackend``,
|
|
23
|
+
``Admin(oauth_backend=...)``), so a deployment fails at start-up and not on the
|
|
24
|
+
first sign-in. It is a ``ValueError`` too, like the other ``Admin`` wiring errors.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class OAuthProviderError(HyperAdminAuthError):
|
|
29
|
+
"""An identity provider refused a request, answered garbage or was unreachable.
|
|
30
|
+
|
|
31
|
+
Attributes:
|
|
32
|
+
provider: Name of the provider (``"google"``).
|
|
33
|
+
detail: Human-readable description; never contains a token or a secret.
|
|
34
|
+
error_code: The provider's OAuth ``error`` code (``"invalid_grant"``) when it sent one.
|
|
35
|
+
transient: ``True`` for a network failure or a 5xx answer, where a retry may work.
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
def __init__(
|
|
39
|
+
self,
|
|
40
|
+
provider: str,
|
|
41
|
+
detail: str,
|
|
42
|
+
*,
|
|
43
|
+
error_code: str | None = None,
|
|
44
|
+
transient: bool = False,
|
|
45
|
+
) -> None:
|
|
46
|
+
self.provider = provider
|
|
47
|
+
self.detail = detail
|
|
48
|
+
self.error_code = error_code
|
|
49
|
+
self.transient = transient
|
|
50
|
+
super().__init__(f"OAuth provider {provider!r}: {detail}")
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class OAuthStateError(HyperAdminAuthError):
|
|
54
|
+
"""The authorize ``state`` is missing, does not match or is expired (CSRF guard)."""
|
|
55
|
+
|
|
56
|
+
def __init__(self, detail: str = "Invalid or expired OAuth state") -> None:
|
|
57
|
+
self.detail = detail
|
|
58
|
+
super().__init__(detail)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class AccountLinkRequiredError(HyperAdminAuthError):
|
|
62
|
+
"""The identity cannot be used as is: an account conflict needs an explicit link.
|
|
63
|
+
|
|
64
|
+
HyperAdmin never links an identity to an existing account by email alone.
|
|
65
|
+
|
|
66
|
+
Attributes:
|
|
67
|
+
provider: Name of the provider the identity comes from.
|
|
68
|
+
email: The email address the provider reported.
|
|
69
|
+
"""
|
|
70
|
+
|
|
71
|
+
def __init__(self, provider: str, email: str) -> None:
|
|
72
|
+
self.provider = provider
|
|
73
|
+
self.email = email
|
|
74
|
+
super().__init__(
|
|
75
|
+
f"An account already exists for {email!r}; sign in with your password and "
|
|
76
|
+
f"link {provider!r} from settings."
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
class OAuthSignupDisabledError(HyperAdminAuthError):
|
|
81
|
+
"""An unknown identity signed in on a provider that does not create accounts."""
|
|
82
|
+
|
|
83
|
+
def __init__(self, provider: str) -> None:
|
|
84
|
+
self.provider = provider
|
|
85
|
+
super().__init__(f"OAuth provider {provider!r} does not create new accounts.")
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
class AccountDisabledError(HyperAdminAuthError):
|
|
89
|
+
"""The identity belongs to a user whose ``is_active`` is false."""
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
class LastSignInMethodError(HyperAdminAuthError):
|
|
93
|
+
"""Unlinking would leave the user without any way to sign in."""
|
|
94
|
+
|
|
95
|
+
def __init__(self, provider: str) -> None:
|
|
96
|
+
self.provider = provider
|
|
97
|
+
super().__init__("Set a password before unlinking your only sign-in method")
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
class MFARequiredError(HyperAdminAuthError):
|
|
101
|
+
"""The first factor succeeded and the user still owes a second one.
|
|
102
|
+
|
|
103
|
+
Attributes:
|
|
104
|
+
user: The user who passed the first factor.
|
|
105
|
+
redirect: URL of the challenge page (``"/admin/mfa/challenge"``).
|
|
106
|
+
"""
|
|
107
|
+
|
|
108
|
+
def __init__(self, user: Any, redirect: str) -> None:
|
|
109
|
+
self.user = user
|
|
110
|
+
self.redirect = redirect
|
|
111
|
+
super().__init__(f"MFA required; continue at {redirect}")
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
__all__ = [
|
|
115
|
+
"AccountDisabledError",
|
|
116
|
+
"AccountLinkRequiredError",
|
|
117
|
+
"HyperAdminAuthError",
|
|
118
|
+
"LastSignInMethodError",
|
|
119
|
+
"MFARequiredError",
|
|
120
|
+
"OAuthConfigError",
|
|
121
|
+
"OAuthProviderError",
|
|
122
|
+
"OAuthSignupDisabledError",
|
|
123
|
+
"OAuthStateError",
|
|
124
|
+
]
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
"""Authentication middleware for HyperAdmin.
|
|
2
|
+
|
|
3
|
+
Redirects unauthenticated requests to the login page and populates
|
|
4
|
+
``request.state.user`` for authenticated requests. Adds a partial-auth
|
|
5
|
+
gate (C3-A, #487) that bounces users mid-MFA back to the challenge page
|
|
6
|
+
for any admin route other than the MFA endpoints themselves.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import logging
|
|
12
|
+
import time
|
|
13
|
+
from typing import TYPE_CHECKING, Any
|
|
14
|
+
|
|
15
|
+
# FastAPI itself imports ``get_route_path`` from here (see core/lifecycle.py).
|
|
16
|
+
from starlette._utils import get_route_path
|
|
17
|
+
from starlette.requests import Request
|
|
18
|
+
from starlette.responses import RedirectResponse, Response
|
|
19
|
+
|
|
20
|
+
from hyperadmin.auth.bridge import default_display_name
|
|
21
|
+
from hyperadmin.auth.otp import PARTIAL_AUTH_SESSION_KEY
|
|
22
|
+
from hyperadmin.auth.user_cache import CachedUser, UserCache, clone_user
|
|
23
|
+
|
|
24
|
+
if TYPE_CHECKING:
|
|
25
|
+
from collections.abc import Callable
|
|
26
|
+
|
|
27
|
+
from starlette.types import Receive, Scope, Send
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
logger = logging.getLogger(__name__)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _cache_supported(auth_backend: Any, permission_checker: Any) -> bool:
|
|
34
|
+
"""Return True when the user cache can be invalidated for these collaborators.
|
|
35
|
+
|
|
36
|
+
Invalidation watches the built-in auth tables only, so a custom backend or checker
|
|
37
|
+
(tenant roles, token versions, session revocation) would be served stale on every hit.
|
|
38
|
+
"""
|
|
39
|
+
from hyperadmin.auth.permissions import ModelPermissionChecker # noqa: PLC0415
|
|
40
|
+
from hyperadmin.auth.session import SessionAuthBackend # noqa: PLC0415
|
|
41
|
+
|
|
42
|
+
return type(auth_backend) is SessionAuthBackend and (
|
|
43
|
+
permission_checker is None or type(permission_checker) is ModelPermissionChecker
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class AuthenticationMiddleware:
|
|
48
|
+
"""Plain ASGI middleware that enforces authentication on admin routes."""
|
|
49
|
+
|
|
50
|
+
# Paths that don't require authentication (relative to admin prefix)
|
|
51
|
+
PUBLIC_SUFFIXES = ("/login", "/login/")
|
|
52
|
+
# Paths an in-flight MFA user MUST be allowed to reach so they can
|
|
53
|
+
# resolve their partial-auth state. Without this, the gate below
|
|
54
|
+
# would loop the user infinitely on /admin/mfa/challenge.
|
|
55
|
+
MFA_PREFIX = "/mfa/"
|
|
56
|
+
# API-style endpoints that must reject anonymous callers with HTTP 401
|
|
57
|
+
# rather than the HTML-redirect-to-login dance — long-lived clients
|
|
58
|
+
# like EventSource cannot follow a redirect into a login form.
|
|
59
|
+
API_PREFIXES = ("/realtime/",)
|
|
60
|
+
|
|
61
|
+
def __init__( # noqa: PLR0913 - one keyword per option
|
|
62
|
+
self,
|
|
63
|
+
app: Any,
|
|
64
|
+
auth_backend: Any,
|
|
65
|
+
admin_prefix: str = "/admin",
|
|
66
|
+
permission_checker: Any = None,
|
|
67
|
+
*,
|
|
68
|
+
isolated: bool = False,
|
|
69
|
+
admin_path: Callable[[Request], str] | None = None,
|
|
70
|
+
user_cache_ttl: int = 0,
|
|
71
|
+
clock: Callable[[], float] = time.monotonic,
|
|
72
|
+
public_paths: tuple[str, ...] = (),
|
|
73
|
+
) -> None:
|
|
74
|
+
"""Build the middleware.
|
|
75
|
+
|
|
76
|
+
Args:
|
|
77
|
+
app: The downstream ASGI app.
|
|
78
|
+
auth_backend: The ``AuthBackend`` resolving the current user.
|
|
79
|
+
admin_prefix: The admin mount prefix (router mode only: used to tell
|
|
80
|
+
admin requests from host requests and to build redirect URLs).
|
|
81
|
+
permission_checker: Optional ``PermissionChecker`` for the sidebar.
|
|
82
|
+
isolated: ``True`` when installed on the admin's private sub-app
|
|
83
|
+
(SDD C.1): every request it sees is an admin request, and redirect
|
|
84
|
+
URLs are built from the sub-app's ``root_path``, which already
|
|
85
|
+
holds the full external admin path (host ``root_path`` included).
|
|
86
|
+
admin_path: Returns the per-request external admin path (SDD C.1)
|
|
87
|
+
that ``login_url`` and the MFA redirect are built from. ``Admin``
|
|
88
|
+
passes one that honours the host's ``root_path`` in both modes;
|
|
89
|
+
without it, the sub-app ``root_path`` (isolated) or the static
|
|
90
|
+
``admin_prefix`` (router mode) is used.
|
|
91
|
+
user_cache_ttl: Seconds to cache the user and permission set per session
|
|
92
|
+
user id in this worker; ``0`` (default) loads them on every request.
|
|
93
|
+
Honoured only with the built-in ``SessionAuthBackend`` and
|
|
94
|
+
``ModelPermissionChecker`` (or no checker); otherwise it is ignored
|
|
95
|
+
with a warning, since only their tables invalidate the cache. With
|
|
96
|
+
the cache on, a hit exposes ``request.state.user`` as a detached copy
|
|
97
|
+
holding only column data (no relationships), and an inactive user is
|
|
98
|
+
rejected.
|
|
99
|
+
clock: Monotonic clock of the cache (injectable for tests).
|
|
100
|
+
public_paths: Further path prefixes (below the admin prefix) that need no
|
|
101
|
+
authentication, next to ``PUBLIC_SUFFIXES`` (the OAuth sign-in routes).
|
|
102
|
+
"""
|
|
103
|
+
self.app = app
|
|
104
|
+
self._public_paths = (*self.PUBLIC_SUFFIXES, *public_paths)
|
|
105
|
+
self.auth_backend = auth_backend
|
|
106
|
+
self.admin_prefix = admin_prefix.rstrip("/")
|
|
107
|
+
self.permission_checker = permission_checker
|
|
108
|
+
self.isolated = isolated
|
|
109
|
+
self._admin_path_for = admin_path
|
|
110
|
+
self._user_cache: UserCache | None = None
|
|
111
|
+
if user_cache_ttl > 0:
|
|
112
|
+
if _cache_supported(auth_backend, permission_checker):
|
|
113
|
+
self._user_cache = UserCache(user_cache_ttl, clock=clock)
|
|
114
|
+
else:
|
|
115
|
+
logger.warning(
|
|
116
|
+
"user_cache_ttl is ignored: the user cache supports only the built-in "
|
|
117
|
+
"SessionAuthBackend and ModelPermissionChecker."
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
def _relative_admin_path(self, request: Request) -> str | None:
|
|
121
|
+
"""Return the path below the admin prefix, or ``None`` for a host request.
|
|
122
|
+
|
|
123
|
+
Router mode matches the prefix on a segment boundary, so a host's own
|
|
124
|
+
``/administrator`` is not treated as an admin path.
|
|
125
|
+
"""
|
|
126
|
+
path = get_route_path(request.scope)
|
|
127
|
+
if self.isolated:
|
|
128
|
+
return path
|
|
129
|
+
prefix = self.admin_prefix
|
|
130
|
+
if path == prefix or path.startswith(prefix + "/"):
|
|
131
|
+
return path[len(prefix) :]
|
|
132
|
+
return None
|
|
133
|
+
|
|
134
|
+
def _admin_path(self, request: Request) -> str:
|
|
135
|
+
"""Return the external admin path that redirect URLs are built from."""
|
|
136
|
+
if self._admin_path_for is not None:
|
|
137
|
+
return self._admin_path_for(request)
|
|
138
|
+
if self.isolated:
|
|
139
|
+
return request.scope.get("root_path", "")
|
|
140
|
+
return self.admin_prefix
|
|
141
|
+
|
|
142
|
+
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
|
|
143
|
+
if scope["type"] != "http":
|
|
144
|
+
# Websocket scopes skip the gate: realtime/ws.py authenticates itself.
|
|
145
|
+
await self.app(scope, receive, send)
|
|
146
|
+
return
|
|
147
|
+
# Built without ``receive``: ``get_current_user`` must not read the body, or the
|
|
148
|
+
# inner app would see a drained stream. A body read raises instead of consuming it.
|
|
149
|
+
response = await self._authenticate(Request(scope))
|
|
150
|
+
if response is not None:
|
|
151
|
+
await response(scope, receive, send)
|
|
152
|
+
return
|
|
153
|
+
await self.app(scope, receive, send)
|
|
154
|
+
|
|
155
|
+
async def _authenticate(self, request: Request) -> Response | None:
|
|
156
|
+
"""Run the auth gate; return a short-circuit response, or ``None`` to pass through.
|
|
157
|
+
|
|
158
|
+
``request.state`` writes land in ``scope["state"]``, which the inner app's
|
|
159
|
+
``Request.state`` reads.
|
|
160
|
+
"""
|
|
161
|
+
# Only enforce auth on admin routes
|
|
162
|
+
relative = self._relative_admin_path(request)
|
|
163
|
+
if relative is None:
|
|
164
|
+
return None
|
|
165
|
+
|
|
166
|
+
# Allow public paths (login page, static assets)
|
|
167
|
+
if any(relative == suffix or relative.startswith(suffix) for suffix in self._public_paths):
|
|
168
|
+
request.state.user = None
|
|
169
|
+
return None
|
|
170
|
+
|
|
171
|
+
# Allow static assets
|
|
172
|
+
if relative.startswith("/static"):
|
|
173
|
+
return None
|
|
174
|
+
|
|
175
|
+
# MFA endpoints are reachable in BOTH partial-auth and full-auth states.
|
|
176
|
+
# Partial-auth: the user is resolving the challenge.
|
|
177
|
+
# Full-auth: the user is managing /mfa/settings.
|
|
178
|
+
# The endpoints themselves enforce the appropriate guard internally.
|
|
179
|
+
on_mfa_route = relative.startswith(self.MFA_PREFIX)
|
|
180
|
+
|
|
181
|
+
# Check authentication
|
|
182
|
+
generation = self._user_cache.generation if self._user_cache is not None else 0
|
|
183
|
+
user, cached = await self._resolve_user(request)
|
|
184
|
+
if user is None:
|
|
185
|
+
# API endpoints reject with 401 — long-lived clients (EventSource,
|
|
186
|
+
# XHR) can't follow a redirect into the login form.
|
|
187
|
+
if any(relative.startswith(prefix) for prefix in self.API_PREFIXES):
|
|
188
|
+
return Response(status_code=401)
|
|
189
|
+
# Anonymous + partial-auth marker present → redirect to challenge
|
|
190
|
+
# so the user can complete the OTP step. Anonymous + no marker →
|
|
191
|
+
# redirect to login as before. Either way, MFA routes themselves
|
|
192
|
+
# remain reachable so the user has a way out.
|
|
193
|
+
if on_mfa_route:
|
|
194
|
+
return None
|
|
195
|
+
if _has_partial_auth(request):
|
|
196
|
+
challenge_url = f"{self._admin_path(request)}/mfa/challenge"
|
|
197
|
+
return RedirectResponse(url=challenge_url, status_code=302)
|
|
198
|
+
login_url = f"{self._admin_path(request)}/login"
|
|
199
|
+
return RedirectResponse(url=login_url, status_code=302)
|
|
200
|
+
|
|
201
|
+
# Full-auth user — pass through. Defensive: if a partial-auth marker
|
|
202
|
+
# somehow lingers alongside a full session (e.g. cookie tampering),
|
|
203
|
+
# the marker is meaningless and we ignore it; full auth wins.
|
|
204
|
+
request.state.user = user
|
|
205
|
+
# The same ``request.state`` keys the bridge dependency sets (SDD B.3),
|
|
206
|
+
# so realtime and templates read one contract in both auth modes.
|
|
207
|
+
request.state.user_key = user.id
|
|
208
|
+
request.state.user_display = default_display_name(user)
|
|
209
|
+
request.state.user_permissions = await self._permissions_for(
|
|
210
|
+
request, user, cached, generation
|
|
211
|
+
)
|
|
212
|
+
return None
|
|
213
|
+
|
|
214
|
+
async def _resolve_user(self, request: Request) -> tuple[Any, CachedUser | None]:
|
|
215
|
+
"""Return the current user and the cache entry it came from, if any.
|
|
216
|
+
|
|
217
|
+
A cache hit yields a private copy of the cached user, never the shared snapshot.
|
|
218
|
+
"""
|
|
219
|
+
key = self._cache_key(request)
|
|
220
|
+
if key is not None and self._user_cache is not None:
|
|
221
|
+
cached = self._user_cache.get(key)
|
|
222
|
+
if cached is not None:
|
|
223
|
+
return clone_user(cached.user), cached
|
|
224
|
+
user = await self.auth_backend.get_current_user(request)
|
|
225
|
+
if self._user_cache is not None and getattr(user, "is_active", True) is False:
|
|
226
|
+
# The cache would keep serving a deactivated user for a TTL, so it must not
|
|
227
|
+
# start with one. With the cache off the backend's answer passes unchanged.
|
|
228
|
+
return None, None
|
|
229
|
+
return user, None
|
|
230
|
+
|
|
231
|
+
async def _permissions_for(
|
|
232
|
+
self, request: Request, user: Any, cached: CachedUser | None, generation: int
|
|
233
|
+
) -> frozenset[str] | None:
|
|
234
|
+
"""Return the permission set, loading and caching it unless the entry was a hit.
|
|
235
|
+
|
|
236
|
+
``generation`` was read before the user was loaded; the cache drops the fill when a
|
|
237
|
+
write invalidated it in between.
|
|
238
|
+
"""
|
|
239
|
+
if cached is not None:
|
|
240
|
+
return cached.permissions
|
|
241
|
+
permissions = await self._user_permissions(user)
|
|
242
|
+
key = self._cache_key(request)
|
|
243
|
+
if key is not None and self._user_cache is not None:
|
|
244
|
+
snapshot = clone_user(user)
|
|
245
|
+
if snapshot is not None:
|
|
246
|
+
self._user_cache.set(key, CachedUser(snapshot, permissions), generation)
|
|
247
|
+
return permissions
|
|
248
|
+
|
|
249
|
+
def _cache_key(self, request: Request) -> Any:
|
|
250
|
+
"""Return the session user id to key the cache on, or ``None`` to bypass it."""
|
|
251
|
+
if self._user_cache is None:
|
|
252
|
+
return None
|
|
253
|
+
try:
|
|
254
|
+
user_id = request.session.get("user_id")
|
|
255
|
+
except (AssertionError, AttributeError):
|
|
256
|
+
return None
|
|
257
|
+
return user_id if isinstance(user_id, int) else None
|
|
258
|
+
|
|
259
|
+
async def _user_permissions(self, user: Any) -> frozenset[str] | None:
|
|
260
|
+
"""Load the user's permission codenames once per request (the sidebar reads them).
|
|
261
|
+
|
|
262
|
+
``None`` means unrestricted: no permission checker, or a superuser.
|
|
263
|
+
"""
|
|
264
|
+
if self.permission_checker is None or getattr(user, "is_superuser", False):
|
|
265
|
+
return None
|
|
266
|
+
return frozenset(await self.permission_checker.get_user_permissions(user))
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
def _has_partial_auth(request: Request) -> bool:
|
|
270
|
+
"""Return True iff the session carries a well-formed partial-auth marker.
|
|
271
|
+
|
|
272
|
+
Mirrors the validation in ``hyperadmin.auth.views._read_partial_auth`` —
|
|
273
|
+
the marker must be a dict with ``stage == "mfa_pending"`` and an integer
|
|
274
|
+
``user_id``. Anything else (a stale bare-int from earlier drafts, a
|
|
275
|
+
truncated dict from a buggy cookie, etc.) is treated as absent so the
|
|
276
|
+
user is bounced to /login rather than into a broken MFA loop.
|
|
277
|
+
"""
|
|
278
|
+
try:
|
|
279
|
+
raw = request.session.get(PARTIAL_AUTH_SESSION_KEY)
|
|
280
|
+
except (AssertionError, AttributeError):
|
|
281
|
+
# SessionMiddleware not installed → session attribute missing.
|
|
282
|
+
return False
|
|
283
|
+
if not isinstance(raw, dict):
|
|
284
|
+
return False
|
|
285
|
+
if raw.get("stage") != "mfa_pending":
|
|
286
|
+
return False
|
|
287
|
+
return isinstance(raw.get("user_id"), int)
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
from datetime import datetime
|
|
2
|
+
from typing import Any
|
|
3
|
+
|
|
4
|
+
from sqlalchemy import JSON, Column, UniqueConstraint
|
|
5
|
+
from sqlmodel import Field, Relationship, SQLModel
|
|
6
|
+
|
|
7
|
+
from hyperadmin.auth._types import UTCNaiveDateTime, utc_now
|
|
8
|
+
from hyperadmin.auth.backend import is_password_usable
|
|
9
|
+
from hyperadmin.auth.oauth.tokens import token_cipher
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class User(SQLModel, table=True):
|
|
13
|
+
"""User model for authentication."""
|
|
14
|
+
|
|
15
|
+
__tablename__ = "hyperadmin_users" # type: ignore[reportIncompatibleVariableOverride]
|
|
16
|
+
|
|
17
|
+
id: int | None = Field(default=None, primary_key=True)
|
|
18
|
+
username: str = Field(index=True, max_length=50, unique=True)
|
|
19
|
+
email: str = Field(index=True, max_length=100, unique=True)
|
|
20
|
+
password_hash: str = Field(
|
|
21
|
+
max_length=255, schema_extra={"json_schema_extra": {"hyperadmin_sensitive": True}}
|
|
22
|
+
)
|
|
23
|
+
first_name: str = Field(default="", max_length=50)
|
|
24
|
+
last_name: str = Field(default="", max_length=50)
|
|
25
|
+
is_active: bool = Field(default=True)
|
|
26
|
+
is_superuser: bool = Field(default=False)
|
|
27
|
+
# MFA fields (additive, backward compatible — existing rows default to disabled, no
|
|
28
|
+
# migration required). ``mfa_method`` is a free-form string rather than an Enum so
|
|
29
|
+
# future methods (totp/sms/webauthn) can be added without a breaking schema change;
|
|
30
|
+
# validation of allowed values lives in the service layer. For the MVP only "email"
|
|
31
|
+
# is wired end-to-end — see docs/specs/object-permissions-mfa.md (Track B).
|
|
32
|
+
mfa_enabled: bool = Field(default=False)
|
|
33
|
+
mfa_method: str | None = Field(default=None, max_length=32)
|
|
34
|
+
created_at: datetime = Field(default_factory=utc_now, sa_type=UTCNaiveDateTime)
|
|
35
|
+
updated_at: datetime | None = Field(default=None, sa_type=UTCNaiveDateTime)
|
|
36
|
+
|
|
37
|
+
user_groups: list["UserGroup"] = Relationship(
|
|
38
|
+
back_populates="user",
|
|
39
|
+
sa_relationship_kwargs={
|
|
40
|
+
"foreign_keys": "UserGroup.user_id",
|
|
41
|
+
"lazy": "selectin",
|
|
42
|
+
},
|
|
43
|
+
)
|
|
44
|
+
user_permissions: list["UserPermission"] = Relationship(
|
|
45
|
+
back_populates="user",
|
|
46
|
+
sa_relationship_kwargs={
|
|
47
|
+
"foreign_keys": "UserPermission.user_id",
|
|
48
|
+
"lazy": "selectin",
|
|
49
|
+
},
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
@property
|
|
53
|
+
def oauth_only(self) -> bool:
|
|
54
|
+
"""``True`` when no password can sign this user in (created by an OAuth sign-in)."""
|
|
55
|
+
return not is_password_usable(self.password_hash)
|
|
56
|
+
|
|
57
|
+
def __str__(self) -> str:
|
|
58
|
+
if self.first_name or self.last_name:
|
|
59
|
+
return f"{self.first_name} {self.last_name}".strip()
|
|
60
|
+
return self.username
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class Group(SQLModel, table=True):
|
|
64
|
+
"""Group model for organizing users and assigning permissions."""
|
|
65
|
+
|
|
66
|
+
__tablename__ = "hyperadmin_groups" # type: ignore[reportIncompatibleVariableOverride]
|
|
67
|
+
|
|
68
|
+
id: int | None = Field(default=None, primary_key=True)
|
|
69
|
+
name: str = Field(index=True, max_length=80, unique=True)
|
|
70
|
+
description: str | None = Field(default=None, max_length=255)
|
|
71
|
+
is_active: bool = Field(default=True)
|
|
72
|
+
created_at: datetime = Field(default_factory=utc_now, sa_type=UTCNaiveDateTime)
|
|
73
|
+
# Optimistic-concurrency stamp of the permission matrix; see docs/specs/permission-matrix-ui.md
|
|
74
|
+
permissions_version: int = Field(default=0, sa_column_kwargs={"server_default": "0"})
|
|
75
|
+
|
|
76
|
+
user_groups: list["UserGroup"] = Relationship(
|
|
77
|
+
back_populates="group",
|
|
78
|
+
sa_relationship_kwargs={
|
|
79
|
+
"foreign_keys": "UserGroup.group_id",
|
|
80
|
+
"lazy": "selectin",
|
|
81
|
+
},
|
|
82
|
+
)
|
|
83
|
+
group_permissions: list["GroupPermission"] = Relationship(
|
|
84
|
+
back_populates="group",
|
|
85
|
+
sa_relationship_kwargs={
|
|
86
|
+
"foreign_keys": "GroupPermission.group_id",
|
|
87
|
+
"lazy": "selectin",
|
|
88
|
+
},
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
def __str__(self) -> str:
|
|
92
|
+
return self.name
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
class Permission(SQLModel, table=True):
|
|
96
|
+
"""Permission model with unique codename for authorization checks."""
|
|
97
|
+
|
|
98
|
+
__tablename__ = "hyperadmin_permissions" # type: ignore[reportIncompatibleVariableOverride]
|
|
99
|
+
|
|
100
|
+
id: int | None = Field(default=None, primary_key=True)
|
|
101
|
+
codename: str = Field(index=True, max_length=100, unique=True)
|
|
102
|
+
name: str = Field(max_length=200)
|
|
103
|
+
content_type: str | None = Field(default=None, max_length=100)
|
|
104
|
+
created_at: datetime = Field(default_factory=utc_now, sa_type=UTCNaiveDateTime)
|
|
105
|
+
|
|
106
|
+
user_permissions: list["UserPermission"] = Relationship(
|
|
107
|
+
back_populates="permission",
|
|
108
|
+
sa_relationship_kwargs={
|
|
109
|
+
"foreign_keys": "UserPermission.permission_id",
|
|
110
|
+
"lazy": "selectin",
|
|
111
|
+
},
|
|
112
|
+
)
|
|
113
|
+
group_permissions: list["GroupPermission"] = Relationship(
|
|
114
|
+
back_populates="permission",
|
|
115
|
+
sa_relationship_kwargs={
|
|
116
|
+
"foreign_keys": "GroupPermission.permission_id",
|
|
117
|
+
"lazy": "selectin",
|
|
118
|
+
},
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
def __str__(self) -> str:
|
|
122
|
+
return self.name
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
class UserGroup(SQLModel, table=True):
|
|
126
|
+
"""Junction table: User <-> Group."""
|
|
127
|
+
|
|
128
|
+
__tablename__ = "hyperadmin_user_groups" # type: ignore[reportIncompatibleVariableOverride]
|
|
129
|
+
|
|
130
|
+
id: int | None = Field(default=None, primary_key=True)
|
|
131
|
+
user_id: int = Field(foreign_key="hyperadmin_users.id", index=True)
|
|
132
|
+
group_id: int = Field(foreign_key="hyperadmin_groups.id", index=True)
|
|
133
|
+
|
|
134
|
+
user: User | None = Relationship(
|
|
135
|
+
back_populates="user_groups",
|
|
136
|
+
sa_relationship_kwargs={"foreign_keys": "UserGroup.user_id"},
|
|
137
|
+
)
|
|
138
|
+
group: Group | None = Relationship(
|
|
139
|
+
back_populates="user_groups",
|
|
140
|
+
sa_relationship_kwargs={"foreign_keys": "UserGroup.group_id"},
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
class UserPermission(SQLModel, table=True):
|
|
145
|
+
"""Junction table: User <-> Permission (direct grants)."""
|
|
146
|
+
|
|
147
|
+
__tablename__ = "hyperadmin_user_permissions" # type: ignore[reportIncompatibleVariableOverride]
|
|
148
|
+
|
|
149
|
+
id: int | None = Field(default=None, primary_key=True)
|
|
150
|
+
user_id: int = Field(foreign_key="hyperadmin_users.id", index=True)
|
|
151
|
+
permission_id: int = Field(foreign_key="hyperadmin_permissions.id", index=True)
|
|
152
|
+
|
|
153
|
+
user: User | None = Relationship(
|
|
154
|
+
back_populates="user_permissions",
|
|
155
|
+
sa_relationship_kwargs={"foreign_keys": "UserPermission.user_id"},
|
|
156
|
+
)
|
|
157
|
+
permission: Permission | None = Relationship(
|
|
158
|
+
back_populates="user_permissions",
|
|
159
|
+
sa_relationship_kwargs={"foreign_keys": "UserPermission.permission_id"},
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
class GroupPermission(SQLModel, table=True):
|
|
164
|
+
"""Junction table: Group <-> Permission."""
|
|
165
|
+
|
|
166
|
+
__tablename__ = "hyperadmin_group_permissions" # type: ignore[reportIncompatibleVariableOverride]
|
|
167
|
+
|
|
168
|
+
id: int | None = Field(default=None, primary_key=True)
|
|
169
|
+
group_id: int = Field(foreign_key="hyperadmin_groups.id", index=True)
|
|
170
|
+
permission_id: int = Field(foreign_key="hyperadmin_permissions.id", index=True)
|
|
171
|
+
|
|
172
|
+
group: Group | None = Relationship(
|
|
173
|
+
back_populates="group_permissions",
|
|
174
|
+
sa_relationship_kwargs={"foreign_keys": "GroupPermission.group_id"},
|
|
175
|
+
)
|
|
176
|
+
permission: Permission | None = Relationship(
|
|
177
|
+
back_populates="group_permissions",
|
|
178
|
+
sa_relationship_kwargs={"foreign_keys": "GroupPermission.permission_id"},
|
|
179
|
+
)
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
class OAuthToken(SQLModel, table=True):
|
|
183
|
+
"""Tokens of one user at one OAuth2/OIDC identity provider, encrypted at rest.
|
|
184
|
+
|
|
185
|
+
A row is also the link between a ``User`` and an identity: ``(provider, sub)``
|
|
186
|
+
is unique, and so is ``(user_id, provider)``. Only ciphertext is stored; the
|
|
187
|
+
``access_token`` and ``refresh_token`` properties encrypt on write and decrypt
|
|
188
|
+
on read with the key of ``hyperadmin.auth.oauth.tokens``. The table is created
|
|
189
|
+
and used only by hosts that enable OAuth. See ``docs/specs/oauth-sso.md``.
|
|
190
|
+
"""
|
|
191
|
+
|
|
192
|
+
__tablename__ = "hyperadmin_oauth_tokens" # type: ignore[reportIncompatibleVariableOverride]
|
|
193
|
+
__table_args__ = (
|
|
194
|
+
UniqueConstraint("provider", "sub", name="uq_oauth_provider_sub"),
|
|
195
|
+
UniqueConstraint("user_id", "provider", name="uq_oauth_user_provider"),
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
id: int | None = Field(default=None, primary_key=True)
|
|
199
|
+
user_id: int = Field(foreign_key="hyperadmin_users.id", index=True)
|
|
200
|
+
provider: str = Field(max_length=32, index=True)
|
|
201
|
+
#: The provider's stable id of the user (OIDC ``sub``, GitHub numeric ``id``).
|
|
202
|
+
sub: str = Field(max_length=255, index=True)
|
|
203
|
+
access_token_encrypted: bytes = Field(default=b"")
|
|
204
|
+
refresh_token_encrypted: bytes | None = Field(default=None)
|
|
205
|
+
expires_at: datetime | None = Field(default=None, sa_type=UTCNaiveDateTime)
|
|
206
|
+
scopes: list[str] = Field(default_factory=list, sa_column=Column(JSON))
|
|
207
|
+
created_at: datetime = Field(default_factory=utc_now, sa_type=UTCNaiveDateTime)
|
|
208
|
+
updated_at: datetime = Field(default_factory=utc_now, sa_type=UTCNaiveDateTime)
|
|
209
|
+
|
|
210
|
+
user: User | None = Relationship(
|
|
211
|
+
sa_relationship_kwargs={"foreign_keys": "OAuthToken.user_id", "lazy": "selectin"},
|
|
212
|
+
)
|
|
213
|
+
|
|
214
|
+
def __init__(self, **data: Any) -> None:
|
|
215
|
+
"""Accept plaintext ``access_token`` / ``refresh_token`` next to the columns."""
|
|
216
|
+
access_token = data.pop("access_token", None)
|
|
217
|
+
refresh_token = data.pop("refresh_token", None)
|
|
218
|
+
super().__init__(**data)
|
|
219
|
+
if access_token is not None:
|
|
220
|
+
self.access_token = access_token
|
|
221
|
+
if refresh_token is not None:
|
|
222
|
+
self.refresh_token = refresh_token
|
|
223
|
+
|
|
224
|
+
@property
|
|
225
|
+
def access_token(self) -> str:
|
|
226
|
+
"""The decrypted access token."""
|
|
227
|
+
return token_cipher().decrypt(self.access_token_encrypted)
|
|
228
|
+
|
|
229
|
+
@access_token.setter
|
|
230
|
+
def access_token(self, value: str) -> None:
|
|
231
|
+
self.access_token_encrypted = token_cipher().encrypt(value)
|
|
232
|
+
|
|
233
|
+
@property
|
|
234
|
+
def refresh_token(self) -> str | None:
|
|
235
|
+
"""The decrypted refresh token, or ``None`` when the provider issued none."""
|
|
236
|
+
if self.refresh_token_encrypted is None:
|
|
237
|
+
return None
|
|
238
|
+
return token_cipher().decrypt(self.refresh_token_encrypted)
|
|
239
|
+
|
|
240
|
+
@refresh_token.setter
|
|
241
|
+
def refresh_token(self, value: str | None) -> None:
|
|
242
|
+
self.refresh_token_encrypted = None if value is None else token_cipher().encrypt(value)
|
|
243
|
+
|
|
244
|
+
def __str__(self) -> str:
|
|
245
|
+
return f"{self.provider}:{self.sub}"
|