auth¶
panel_flowdash.auth
¶
Per-page and per-dashboard authorization.
The module defines a small, self-contained authorization model shared by both
code-authored pages (permissions declared on the @register decorator) and
runtime-created dashboards (permissions persisted in the store):
- :class:
Identity— the resolved principal for a session (OAuth user, system user, resolveduserand the set ofgroupsit belongs to). - :class:
Permission— an allow/deny rule set keyed on users and groups. - :class:
AuthConfig— project-level configuration controlling how groups are discovered and which groups may administer any dashboard. - :func:
resolve_identity— builds an :class:Identityfrom the live Panel session plus theAuthConfig. - :func:
is_authorized— evaluates a :class:Permissionagainst an :class:Identity.
The same :func:is_authorized evaluator serves pages, dashboards and (in the
future) components, so the semantics only ever live in one place.
ANONYMOUS_USER = 'anonymous'
module-attribute
¶
DEFAULT_GROUP_CLAIMS = ('groups', 'roles')
module-attribute
¶
logger = logging.getLogger('panel_flowdash')
module-attribute
¶
AuthConfig
dataclass
¶
Project-level authorization configuration.
Loaded from the project's __init__.py by the serve command. All fields
are optional; the defaults reproduce the pre-auth behavior (allow by
default, groups read from the standard claim keys, no admin groups).
admin_groups = field(default_factory=frozenset)
class-attribute
instance-attribute
¶
default_allow = True
class-attribute
instance-attribute
¶
group_claims = DEFAULT_GROUP_CLAIMS
class-attribute
instance-attribute
¶
resolve_groups = None
class-attribute
instance-attribute
¶
user_groups = field(default_factory=dict)
class-attribute
instance-attribute
¶
from_module(module)
classmethod
¶
Build an :class:AuthConfig from names on a project __init__.
Reads group_claims, user_groups, resolve_groups,
admin_groups and default_allow if present, falling back to the
defaults otherwise. Missing module or names yield a default config.
Identity
dataclass
¶
The resolved principal for a session.
groups = field(default_factory=frozenset)
class-attribute
instance-attribute
¶
oauth_user = None
class-attribute
instance-attribute
¶
system_user = None
class-attribute
instance-attribute
¶
user
instance-attribute
¶
user_info = field(default_factory=dict)
class-attribute
instance-attribute
¶
user_names
property
¶
All names this identity may be referenced by in a rule.
in_groups(groups)
¶
Whether the identity belongs to any of groups.
is_user(users)
¶
Whether the identity matches any of users (OAuth or system name).
Permission
dataclass
¶
An allow/deny rule set evaluated against an :class:Identity.
The rule fields match either the resolved user (OAuth login or system
user) or one of the identity's groups. An empty Permission declares
no constraints and defers entirely to the caller's default policy.
allow_edit_users and allow_edit_groups only apply to dashboards:
they grant editing (which includes viewing) on top of the view rules,
without restricting who else may view.
allow_edit_groups = field(default_factory=frozenset)
class-attribute
instance-attribute
¶
allow_edit_users = field(default_factory=frozenset)
class-attribute
instance-attribute
¶
allow_groups = field(default_factory=frozenset)
class-attribute
instance-attribute
¶
allow_users = field(default_factory=frozenset)
class-attribute
instance-attribute
¶
deny_groups = field(default_factory=frozenset)
class-attribute
instance-attribute
¶
deny_users = field(default_factory=frozenset)
class-attribute
instance-attribute
¶
is_empty
property
¶
Whether the permission declares no allow, deny or edit rules.
from_dict(data)
classmethod
¶
Deserialize from a (possibly None or partial) mapping.
from_spec(*, allow_users=None, allow_groups=None, deny_users=None, deny_groups=None, allow_edit_users=None, allow_edit_groups=None)
classmethod
¶
Build a :class:Permission from loosely-typed iterables.
grants_edit(identity)
¶
Whether an edit rule names identity; deny rules are not consulted.
to_dict()
¶
Serialize to sorted lists for JSON persistence.
RegistryEntry
dataclass
¶
A registered component/page with its metadata.
app = None
class-attribute
instance-attribute
¶
app_id
instance-attribute
¶
load_error = field(default=None, repr=False, compare=False)
class-attribute
instance-attribute
¶
metadata
instance-attribute
¶
module_name
instance-attribute
¶
module_path = None
class-attribute
instance-attribute
¶
name
instance-attribute
¶
page_path
instance-attribute
¶
section
instance-attribute
¶
spec = field(default=None, repr=False, compare=False)
class-attribute
instance-attribute
¶
title
property
¶
Human-readable title.
from_app(app, *, app_id=None, section=None, name=None)
classmethod
¶
Build an entry from an already-imported app object.
Unlike :func:build_registry, which discovers modules on disk and defers
importing them, this wraps a live object so load() is a no-op. Used
by the programmatic API where components are passed in directly.
Objects without @register metadata are treated as components, since
a bare Viewer subclass handed to the editor is only ever meant to be
one.
load()
¶
Import the module and return the app object.
Caches the result on self.app. Raises on import failure.
_flag(value, label)
¶
Read a configured boolean, refusing values whose truthiness is misleading.
_groups_from_claims(user_info, group_claims)
¶
Extract group names from the user_info claims dict.
A claim value may be a list of strings or a single comma/space-delimited string; both shapes are normalized to a set of names.
_names(value, label)
¶
Normalize a configured name list, treating a lone string as one name.
Iterating a string would otherwise turn "bob" into {"b", "o"}.
_system_user()
¶
Return the OS account running the server, or None if undetermined.
can_administer(identity, owner, admin_groups)
¶
Whether identity may administer a resource owned by owner.
The owner and members of admin_groups may administer a resource; nobody else may. In an auth-less deployment every session resolves to the same system user, which owns every dashboard it creates, so such a deployment stays fully editable without configuring admin groups.
can_edit(permission, identity, owner, admin_groups)
¶
Whether identity may edit the contents of a dashboard owned by owner.
Everyone who may administer it may edit it. Otherwise the identity must
match an allow_edit_users/allow_edit_groups rule and no deny rule.
Editing covers changing and saving the canvas and the dashboard settings,
not renaming, deleting or sharing it, which stay administration.
entry_authorized(entry, identity, *, default_allow=True, load=True)
¶
Whether identity may access a code-authored page or component.
A @register call whose rules could not be read from the source without
importing it (allow_groups=ADMINS, authorize=...) leaves the entry's
rules pending. With load such an entry is imported here so its decorator
supplies the real rules; pass load=False on paths that must not import
synchronously (listings rendered on the event loop), which treats a
pending entry as denied until something else imports it. A pending entry
whose import already failed is denied without importing it again.
evaluate_permission(permission, identity, *, is_admin=False, default_allow=True)
¶
Evaluate a resource permission against identity with admin bypass for allowlists.
Order of precedence:
- An empty or
Nonepermission defers to default_allow. - A non-empty permission without an identity fails closed (False).
- Explicit deny (
deny_users/deny_groups) denies, even for admins. - Admins (
is_admin=True) pass allowlists unless denied in step 3. - Otherwise delegates to :func:
is_authorizedwith default_allow.
is_authorized(permission, identity, *, default_allow=True, owner=None)
¶
Evaluate permission against identity.
Order of precedence:
- A matching
deny_users/deny_groupsrule denies access (deny always wins, even for the owner). - The owner, if given and matching, is allowed.
- An identity granted editing (
allow_edit_users/allow_edit_groups) is allowed, since editing includes viewing. - Any
allow_users/allow_groupsrule present: allowed iff the identity matches at least one of them. - No allow/deny rules at all: fall back to default_allow.
make_authorize_callback(registry, auth_config, *, denied_route=None, url_prefix='')
¶
Build a Panel config.authorize_callback gating page routes.
The returned callback authorizes direct HTTP access to code-authored page
URLs against the resolved identity. Non-page routes (/components,
/asset/... and unknown paths) always pass here and are gated in-app,
where per-dashboard permissions and richer denied views live.
Rules are read from the registry entry on every request, so metadata refreshed by importing a module takes effect immediately.
auth_config may be a callable returning the config in force, e.g. one the project file layers settings under, read on every request.
url_prefix strips a server route prefix from the request path before
matching against registry page paths.
With denied_route, a refused request is redirected there (under
url_prefix) with the page's path in the page query parameter, so
the app can explain the refusal in its own page rather than Panel's
generic authorization error.
path_permission_lookup(registry)
¶
Map each page's URL path to its declared :class:Permission.
A snapshot of the declared rule fields; use :func:entry_authorized to
evaluate an entry, which also honors authorize and pending rules.
permission_lockout_reason(permission, identity, *, is_admin=True)
¶
Return why saving permission would lock out identity, or None if allowed.
resolve_identity(auth_config=None)
¶
Resolve the current session's :class:Identity.
Prefers the OAuth login (pn.state.user) when a real provider populated
it; otherwise falls back to the system user, then to "anonymous". The
system user is only part of the identity when there is no OAuth login, so a
signed-in user never inherits the server account's dashboards or groups.
Groups are the union of claim-derived groups, the static user_groups
entry for the resolved user and any dynamic resolve_groups callback.