sqlalchemy_authorize.oso package¶
Submodules¶
sqlalchemy_authorize.oso.oso_permissions_mixin module¶
- class sqlalchemy_authorize.oso.oso_permissions_mixin.OsoPermissionsMixin(*args, protected=True, check_create=False, **kwargs)[source]¶
Bases:
BasePermissionsMixinAuthorize your fields using Oso.
E.g. (using the
Usermodel defined in conftest.py and the polar policy provided insqlalchemy_authorize.oso.rbac.polar):>>> admin = User(id="1", username="root", is_admin=True) >>> john_doe = User(username="john_doe", check_create=True) Traceback (most recent call last): oso.exceptions.ForbiddenError: ... >>> with user_set(app, admin): # A context to set `flask.g.user` ... john_doe = User(username="john_doe", check_create=True) ... john_doe.id = "2" >>> john_doe.username, john_doe.id ('john_doe', '2') >>> with user_set(app, john_doe): ... john_doe.username = "doe_john" ... john_doe.id = "3" Traceback (most recent call last): oso.exceptions.ForbiddenError: ... >>> john_doe.username, john_doe.id ('doe_john', '2')
- authorize_field(action, key)[source]¶
This is where you actually implement the check. For an example, see
OsoPermissionsMixin.Usually, you can rely on this being called indirectly (when setting/getting/deleting attributes).
This is meant as a placeholder method, not a working example, that authorizes only public actions. In practice, you’ll want to implement your role-based / relation-based / attribute-based access control here (or use a solution like
oso).- Parameters
action – One of CRUD or a custom action.
key – The attribute/field to authorize.
- Returns
Noneif the action is allowed.- Raises
:exec:`PermissionError` (or some custom error like :exec:`oso.ForbiddenError`) if not allowed.
- error(action: str)[source]¶
Returns an appropriate exception for the action.
- Returns
:exec:`ForbiddenError` for create/update/delete, or a
:exec:`NotFoundError` for reads.
- static get_anonymous_user()[source]¶
Returns a mock anonymous user.
You’ll probably want to overload this with a method that creates an anonymous instance of your User model. (You need to call
oso.register_classes).But if all you’re checking in your polar policies is your
user.id, then this may suffice.