{ "info": { "author": "Krisztian Toth", "author_email": "tkrisztiana@gmail.com", "bugtrack_url": null, "classifiers": [ "Operating System :: OS Independent", "Programming Language :: Python :: 3" ], "description": "# Keycloak based authentication policy for Pyramid framework\n\n\nAuthentication policy which uses Keycloak's OpenID API via a client\nto authenticate the user. It uses cookies to hold the access token and\nthe refresh token.\n\nThis package is dependant on the package `python-keycloak`.\n\n## Usage\n\n```python\nfrom keycloak import KeycloakOpenID\nfrom pyramid.config import Configurator\nfrom keycloak_auth_policy import KeycloakBasedAuthenticationPolicy\n\ndef main():\n openid_client = KeycloakOpenID(...)\n config = Configurator()\n config.set_authentication_policy(\n KeycloakBasedAuthenticationPolicy(openid_client))\n```\n\nYou have to handle the redirect logic in your forbidden view based on your \nneeds:\n\n```python\nfrom pyramid.httpexceptions import HTTPFound, HTTPForbidden\nfrom pyramid.request import Request\nfrom pyramid.view import forbidden_view_config\n\n@forbidden_view_config()\ndef forbidden_view(request: Request):\n if ...: # user has no privileges\n raise HTTPForbidden(\"You don't have permissions for this action\")\n\n # Keycloak's URL to redirect to where the user can log in\n url = ...\n\n # you can either redirect to the URL or return it if you have a client \n # which consumes your API\n return HTTPFound(url)\n``` \n\nYou also need to have a callback endpoint where Keycloak redirects to after a \nsuccessful login\n\n```python\nfrom keycloak.exceptions import KeycloakError\nfrom pyramid.httpexceptions import HTTPFound\nfrom pyramid.request import Request\nfrom pyramid.view import view_defaults, view_config\n\n\n@view_defaults(renderer='json')\nclass AuthApi:\n\n def __init__(self, request: Request) -> None:\n self.request = request\n self._openid_client = ... # get OpenID client\n\n @view_config(route_name='auth.exchange', request_method='GET',\n permission='public')\n def exchange(self):\n try:\n token_response = self._openid_client.exchange(\n self.request.GET.get(\"code\"),\n self.request.route_url(\"auth.exchange\")) # the redirect URI\n except KeycloakError as e:\n ... # handle exception\n\n access_token = token_response.get(\"access_token\")\n refresh_token = token_response.get(\"refresh_token\")\n\n # set the tokens as cookies to the client and return a response\n # you can either redirect from here or if your application is consumed\n # as an API you can return a successful response\n response = HTTPFound(...)\n response.set_cookie(\"refresh_token_cookie_name\", refresh_token)\n response.set_cookie(\"access_token_cookie_name\", access_token)\n\n return response\n```\n\nYou can also implement a logout endpoint if you feel like to\n\n```python\nfrom pyramid.security import forget\n\n ...\n\n @view_config(route_name='auth.logout', request_method='GET',\n permission='private')\n def logout(self):\n headers = forget(self.request)\n response = self.request.response\n response.headerlist.extend(headers)\n return response\n```\n\nFor more information see the docstrings of each method in the source.\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/watsta/keycloak_pyramid", "keywords": "", "license": "", "maintainer": "", "maintainer_email": "", "name": "pyramid-keycloak", "package_url": "https://pypi.org/project/pyramid-keycloak/", "platform": "", "project_url": "https://pypi.org/project/pyramid-keycloak/", "project_urls": { "Homepage": "https://github.com/watsta/keycloak_pyramid" }, "release_url": "https://pypi.org/project/pyramid-keycloak/0.0.13/", "requires_dist": [ "python-keycloak (<1.0)", "pyramid (<2.0,>1.0)" ], "requires_python": "", "summary": "Keycloak based authentication policy for Pyramid", "version": "0.0.13" }, "last_serial": 4359479, "releases": { "0.0.13": [ { "comment_text": "", "digests": { "md5": "cfa0c00f7cc1dcbdd8133218e9efbe7e", "sha256": "b5ddb84b61c749f3c36f053545a16a39ebca42ec2bcec9a60274215deae12f79" }, "downloads": -1, "filename": "pyramid_keycloak-0.0.13-py3-none-any.whl", "has_sig": false, "md5_digest": "cfa0c00f7cc1dcbdd8133218e9efbe7e", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 5176, "upload_time": "2018-10-10T10:32:15", "url": "https://files.pythonhosted.org/packages/a8/f1/858c9d6950cdad32ba7dc164e6ea5eb3212b17ac6736538117fc452648a0/pyramid_keycloak-0.0.13-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "bc455199df6b1e159add2b4bbd09bebd", "sha256": "8ffabe6541edd1d7b7eed0f82925ef922997afaebf95137f81b30183f11dada7" }, "downloads": -1, "filename": "pyramid_keycloak-0.0.13.tar.gz", "has_sig": false, "md5_digest": "bc455199df6b1e159add2b4bbd09bebd", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3855, "upload_time": "2018-10-10T10:32:17", "url": "https://files.pythonhosted.org/packages/77/db/3535e98d8ca11a69fad24baeabdf8a5cd9d331481c49d602172dcc6453f0/pyramid_keycloak-0.0.13.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "cfa0c00f7cc1dcbdd8133218e9efbe7e", "sha256": "b5ddb84b61c749f3c36f053545a16a39ebca42ec2bcec9a60274215deae12f79" }, "downloads": -1, "filename": "pyramid_keycloak-0.0.13-py3-none-any.whl", "has_sig": false, "md5_digest": "cfa0c00f7cc1dcbdd8133218e9efbe7e", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 5176, "upload_time": "2018-10-10T10:32:15", "url": "https://files.pythonhosted.org/packages/a8/f1/858c9d6950cdad32ba7dc164e6ea5eb3212b17ac6736538117fc452648a0/pyramid_keycloak-0.0.13-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "bc455199df6b1e159add2b4bbd09bebd", "sha256": "8ffabe6541edd1d7b7eed0f82925ef922997afaebf95137f81b30183f11dada7" }, "downloads": -1, "filename": "pyramid_keycloak-0.0.13.tar.gz", "has_sig": false, "md5_digest": "bc455199df6b1e159add2b4bbd09bebd", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3855, "upload_time": "2018-10-10T10:32:17", "url": "https://files.pythonhosted.org/packages/77/db/3535e98d8ca11a69fad24baeabdf8a5cd9d331481c49d602172dcc6453f0/pyramid_keycloak-0.0.13.tar.gz" } ] }