{ "info": { "author": "Aur\u00e9lien", "author_email": "aurelien.amilin@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 4 - Beta", "Programming Language :: Python" ], "description": "# clustaar.authorize\n\n[![Build Status](https://travis-ci.org/Clustaar/clustaar.authorize.svg?branch=master)](https://travis-ci.org/Clustaar/clustaar.authorize)\n[![Code Climate](https://codeclimate.com/github/Clustaar/clustaar.authorize/badges/gpa.svg)](https://codeclimate.com/github/Clustaar/clustaar.authorize)\n\n## Description\n\nA micro authorization system.\n\nDefinition of the access rules is up to you as it's too much specific of a project.\n\nIt can be used with **Falcon**, just use the `@authorize` decorator and be sure to provide an `ability` property on the request context.\n\n## Examples\n### Usage\n#### Creating authorizations\n\n```python\nfrom clustaar.authorize import Action, Ability, Authorizations\nfrom clustaar.authorize.rules import ALLOW, DENY, AccessRule\nfrom clustaar.authorize.conditions import Condition\n\ncreate_action = Action(name=\"create_project\")\nview_action = Action(name=\"view_project\")\n\n\nclass KwargEquals(Condition):\n \"\"\"This conditions validate that a kwarg value equals a expected one.\"\"\"\n def __init__(self, name, expected):\n self._name = name\n self._expected = expected\n\n def __call__(self, context):\n return context.get(self._name) == self._expected\n\n\nclass AdminAuthorizations(Authorizations):\n def __init__(self):\n # Admins can do whatever they want\n super().__init__(default_rule=ALLOW)\n\nclass UserAuthorizations(Authorizations):\n def __init__(self):\n rules = {\n create_action: DENY,\n view_action: AccessRule(condition=KwargEquals(\"id\", \"1\"))\n }\n super().__init__(rules=rules,\n default_rule=DENY)\n\nuser_ability = Ability(UserAuthorizations())\nadmin_ability = Ability(AdminAuthorizations())\n```\n\n#### Using authorizations\n\n```python\nadmin_ability.can(view_action, id=\"1\") # => True\nadmin_ability.can(create_action) # => True\nadmin_ability.authorize(view_action, id=1) # => No exception raised\nadmin_ability.authorize(create_action) # => No exception raised\n\nuser_ability.can(view_action, id=\"1\") # => True\nuser_ability.can(view_action, id=\"2\") # => False\nuser_ability.can(create_action) # => False\nuser_ability.authorize(view_action, id=\"1\") # => No exception raised\nuser_ability.authorize(create_action) # => Raises an Exception : Access denied for create_project ({})\n```\n\n#### Falcon\n\n```python\nimport falcon\nfrom clustaar.authorize.falcon import authorize\n\nclass AbilityInjectionMiddleware(object):\n \"\"\"\n Set the `ability` property from the request context.\n It choses the right ability depending on the user roles (if admin ability\n will be an AdminAbility, etc.)\n \"\"\"\n def process_request(self, request, *args):\n # another middleware has injected current user in context\n user = request.context.user\n if user.has_role(\"admin\"):\n authorizations = AdminAuthorizations()\n else:\n authorizations = UserAuthorizations(user)\n request.context.ability = Ability(authorizations)\n\n\nclass ProjectsHandler(object):\n @authorize(create_action)\n def on_post(self, request, response):\n\t pass\n\nclass ProjectHandler(object):\n @authorize(view_action)\n def on_get(self, request, response, id):\n\t pass\n\napp = falcon.API(middlewares=(AbilityInjectionMiddleware(),))\napp.add_route(\"/projects\", ProjectsHandler())\napp.add_route(\"/projects/{id}\", ProjectHandler())\n```\n\n", "description_content_type": "", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/Clustaar/clustaar.authorize", "keywords": "", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "clustaar.authorize", "package_url": "https://pypi.org/project/clustaar.authorize/", "platform": "", "project_url": "https://pypi.org/project/clustaar.authorize/", "project_urls": { "Homepage": "https://github.com/Clustaar/clustaar.authorize" }, "release_url": "https://pypi.org/project/clustaar.authorize/0.2.0/", "requires_dist": null, "requires_python": "", "summary": "A micro authorization system", "version": "0.2.0" }, "last_serial": 4247928, "releases": { "0.1": [ { "comment_text": "", "digests": { "md5": "85f5376e958210381f07f15d4a66323d", "sha256": "f197d28866f7f26b4d49004db6dda9c99091cbd98eb4cee4671e6abdbbd58d91" }, "downloads": -1, "filename": "clustaar.authorize-0.1.tar.gz", "has_sig": false, "md5_digest": "85f5376e958210381f07f15d4a66323d", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11888, "upload_time": "2017-03-15T21:13:56", "url": "https://files.pythonhosted.org/packages/88/88/183b03859edd799b95dfbf73e03c79687ce98758cc56273cfba4b190a169/clustaar.authorize-0.1.tar.gz" } ], "0.1.1": [ { "comment_text": "", "digests": { "md5": "b2fb0d58deac39287ec8856a52dbba36", "sha256": "5b538056cd25e1782dbdbf9eaeb35cf9987faaf10d82ae9ccd96077346531a11" }, "downloads": -1, "filename": "clustaar.authorize-0.1.1.tar.gz", "has_sig": false, "md5_digest": "b2fb0d58deac39287ec8856a52dbba36", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 12675, "upload_time": "2017-03-22T10:39:01", "url": "https://files.pythonhosted.org/packages/75/55/b90d608e8306569b720cf3a742b298f5841337cba832b9b63dbaecb67bce/clustaar.authorize-0.1.1.tar.gz" } ], "0.2.0": [ { "comment_text": "", "digests": { "md5": "f37c966264d9594e6e4e316ed1fe1083", "sha256": "b0d924ec06825462a709ce41b82eebf0bbc7fc438143d46682e500116f761a44" }, "downloads": -1, "filename": "clustaar.authorize-0.2.0.tar.gz", "has_sig": false, "md5_digest": "f37c966264d9594e6e4e316ed1fe1083", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 15161, "upload_time": "2018-06-06T10:34:23", "url": "https://files.pythonhosted.org/packages/90/80/fb4b192f3e4d8500c83a4867841f2a25153e0e451b0b51f230235f7719ab/clustaar.authorize-0.2.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "f37c966264d9594e6e4e316ed1fe1083", "sha256": "b0d924ec06825462a709ce41b82eebf0bbc7fc438143d46682e500116f761a44" }, "downloads": -1, "filename": "clustaar.authorize-0.2.0.tar.gz", "has_sig": false, "md5_digest": "f37c966264d9594e6e4e316ed1fe1083", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 15161, "upload_time": "2018-06-06T10:34:23", "url": "https://files.pythonhosted.org/packages/90/80/fb4b192f3e4d8500c83a4867841f2a25153e0e451b0b51f230235f7719ab/clustaar.authorize-0.2.0.tar.gz" } ] }