Access Policies
Access control and authorization policies for DiracX routers.
Access policies define who can access specific API endpoints based on user properties, roles, and other security context. Use these decorators and utilities to enforce authorization rules on your routes.
access_policies
AccessPolicy.
We define a set of Policy classes (WMS, DFC, etc). They have a default implementation in diracx. If an extension wants to change it, it can be overwritten in the entry point diracx.access_policies
Each route should either: * have the open_access decorator to make explicit that it does not implement policy * have a callable and call it that will perform the access policy
Adding a new policy:
1. Create a class that inherits from BaseAccessPolicy and implement the policy and enrich_tokens methods
2. create an entry in diracx.access_policy entrypoints
3. Create a dependency such as CheckMyPolicyCallable = Annotated[Callable, Depends(MyAccessPolicy.check)]
Attributes
Classes
BaseAccessPolicy
Base class to be used by all the other Access Policy.
Each child class should implement the policy staticmethod.
Source code in diracx-routers/src/diracx/routers/access_policies.py
Functions
check()
classmethod
all_used_access_policies()
classmethod
Returns the list of classes that are actually called.
This should be overridden by the dependency_override.
Source code in diracx-routers/src/diracx/routers/access_policies.py
available_implementations(access_policy_name)
classmethod
Return the available implementations of the AccessPolicy in reverse priority order.
Source code in diracx-routers/src/diracx/routers/access_policies.py
policy(policy_name, user_info)
abstractmethod
async
staticmethod
This is the method to be implemented in child classes. It should always take an AuthorizedUserInfo parameter, which is passed by check_permissions. The rest is whatever the policy actually needs. There are rules to write it: * This method must be static and async * All parameters must be kw only arguments * All parameters must have a default value (Liskov Substitution principle) It is expected that a policy denying the access raises HTTPException(status.HTTP_403_FORBIDDEN).
Source code in diracx-routers/src/diracx/routers/access_policies.py
enrich_tokens(access_payload, refresh_payload)
staticmethod
This method is called when issuing a token, and can add whatever content it wants inside the access or refresh payload.
:param access_payload: access token payload :param refresh_payload: refresh token payload :returns: extra content for both payload
Source code in diracx-routers/src/diracx/routers/access_policies.py
Functions
check_permissions(policy, policy_name, user_info, dev_settings)
This wrapper just calls the actual implementation, but also makes sure that the policy has been called. If not, diracx will abruptly crash. It is violent, but necessary to make sure that it gets noticed :-).
This method is never called directly, but used in the dependency_override at startup
Source code in diracx-routers/src/diracx/routers/access_policies.py
open_access(f)
Decorator to put around the route that are part of a DiracxRouter that are expected not to do any access policy check. The presence of a token will still be checked if the router has require_auth to True. This is useful to allow the CI to detect routes which may have forgotten to have an access check.