{"id":13813687,"url":"https://github.com/ByteInternet/drf-oidc-auth","last_synced_at":"2025-05-15T00:34:04.542Z","repository":{"id":44997456,"uuid":"43142638","full_name":"ByteInternet/drf-oidc-auth","owner":"ByteInternet","description":"OpenID Connect authentication for Django REST Framework","archived":false,"fork":false,"pushed_at":"2024-02-29T14:45:24.000Z","size":103,"stargazers_count":112,"open_issues_count":16,"forks_count":46,"subscribers_count":15,"default_branch":"master","last_synced_at":"2024-11-05T11:58:08.670Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ByteInternet.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-09-25T13:12:35.000Z","updated_at":"2024-10-06T02:33:14.000Z","dependencies_parsed_at":"2024-06-18T17:07:34.647Z","dependency_job_id":"88432e25-82a9-4b0c-9ee1-380c10194ab5","html_url":"https://github.com/ByteInternet/drf-oidc-auth","commit_stats":{"total_commits":98,"total_committers":19,"mean_commits":5.157894736842105,"dds":0.7959183673469388,"last_synced_commit":"a9ab291337edac3e7e55b4a52609a48185665cd9"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ByteInternet%2Fdrf-oidc-auth","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ByteInternet%2Fdrf-oidc-auth/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ByteInternet%2Fdrf-oidc-auth/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ByteInternet%2Fdrf-oidc-auth/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ByteInternet","download_url":"https://codeload.github.com/ByteInternet/drf-oidc-auth/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225319303,"owners_count":17455743,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-04T04:01:25.837Z","updated_at":"2024-11-19T08:30:54.531Z","avatar_url":"https://github.com/ByteInternet.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# OpenID Connect authentication for Django Rest Framework\n\nThis package contains an authentication mechanism for authenticating \nusers of a REST API using tokens obtained from OpenID Connect.\n\nCurrently, it only supports JWT and Bearer tokens. JWT tokens will be \nvalidated against the public keys of an OpenID connect authorization \nservice. Bearer tokens are used to retrieve the OpenID UserInfo for a\nuser to identify him.\n\n# Installation\n\nInstall using pip:\n\n```sh\npip install drf-oidc-auth\n```\n\nConfigure authentication for Django REST Framework in settings.py:\n\n```py\nREST_FRAMEWORK = {\n    'DEFAULT_PERMISSION_CLASSES': (\n        'rest_framework.permissions.IsAuthenticated',\n    ),\n    'DEFAULT_AUTHENTICATION_CLASSES': (\n        # ...\n        'oidc_auth.authentication.JSONWebTokenAuthentication',\n        'oidc_auth.authentication.BearerTokenAuthentication',\n    ),\n}\n```\n\nAnd configure the module itself in settings.py:\n```py\nOIDC_AUTH = {\n    # Specify OpenID Connect endpoint. Configuration will be\n    # automatically done based on the discovery document found\n    # at \u003cendpoint\u003e/.well-known/openid-configuration\n    'OIDC_ENDPOINT': 'https://accounts.google.com',\n\n    # The Claims Options can now be defined by a static string.\n    # ref: https://docs.authlib.org/en/latest/jose/jwt.html#jwt-payload-claims-validation\n    # The old OIDC_AUDIENCES option is removed in favor of this new option.\n    # `aud` is only required, when you set it as an essential claim.\n    'OIDC_CLAIMS_OPTIONS': {\n        'aud': {\n            'values': ['myapp'],\n            'essential': True,\n        }\n    },\n    \n    # (Optional) Function that resolves id_token into user.\n    # This function receives a request and an id_token dict and expects to\n    # return a User object. The default implementation tries to find the user\n    # based on username (natural key) taken from the 'sub'-claim of the\n    # id_token.\n    'OIDC_RESOLVE_USER_FUNCTION': 'oidc_auth.authentication.get_user_by_id',\n    \n    # (Optional) Number of seconds in the past valid tokens can be \n    # issued (default 600)\n    'OIDC_LEEWAY': 600,\n    \n    # (Optional) Time before signing keys will be refreshed (default 24 hrs)\n    'OIDC_JWKS_EXPIRATION_TIME': 24*60*60,\n\n    # (Optional) Time before bearer token validity is verified again (default 10 minutes)\n    'OIDC_BEARER_TOKEN_EXPIRATION_TIME': 10*60,\n    \n    # (Optional) Token prefix in JWT authorization header (default 'JWT')\n    'JWT_AUTH_HEADER_PREFIX': 'JWT',\n    \n    # (Optional) Token prefix in Bearer authorization header (default 'Bearer')\n    'BEARER_AUTH_HEADER_PREFIX': 'Bearer',\n\n    # (Optional) Which Django cache to use\n    'OIDC_CACHE_NAME': 'default',\n\n    # (Optional) A cache key prefix when storing and retrieving cached values\n    'OIDC_CACHE_PREFIX': 'oidc_auth.',\n}\n```\n\n# Running tests\n\n```sh\npip install tox\ntox\n```\n\n## Mocking authentication\n\nThere's a `AuthenticationTestCaseMixin` provided in the `oidc_auth.test` module, which you \ncan use for testing authentication like so:\n```python\nfrom oidc_auth.test import AuthenticationTestCaseMixin\nfrom django.test import TestCase\n\nclass MyTestCase(AuthenticationTestCaseMixin, TestCase):\n    def test_example_cache_of_valid_bearer_token(self):\n        self.responder.set_response(\n            'http://example.com/userinfo', {'sub': self.user.username})\n        auth = 'Bearer egergerg'\n        resp = self.client.get('/test/', HTTP_AUTHORIZATION=auth)\n        self.assertEqual(resp.status_code, 200)\n\n        # Token expires, but validity is cached\n        self.responder.set_response('http://example.com/userinfo', \"\", 401)\n        resp = self.client.get('/test/', HTTP_AUTHORIZATION=auth)\n        self.assertEqual(resp.status_code, 200)\n\n    def test_example_using_invalid_bearer_token(self):\n        self.responder.set_response('http://example.com/userinfo', \"\", 401)\n        auth = 'Bearer hjikasdf'\n        resp = self.client.get('/test/', HTTP_AUTHORIZATION=auth)\n        self.assertEqual(resp.status_code, 401)\n```\n\n# References\n\n* Requires [Django REST Framework](http://www.django-rest-framework.org/)\n* And of course [Django](https://www.djangoproject.com/)\n* Inspired on [REST framework JWT Auth](https://github.com/GetBlimp/django-rest-framework-jwt)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FByteInternet%2Fdrf-oidc-auth","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FByteInternet%2Fdrf-oidc-auth","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FByteInternet%2Fdrf-oidc-auth/lists"}