Name | django-ninja-oauth2 JSON |
Version |
0.1.5
JSON |
| download |
home_page | None |
Summary | Django Ninja OAuth2 package enables support of OAuth2 / OpenID Connect Authorization Code Flow for your swagger documentation. |
upload_time | 2024-10-02 13:48:23 |
maintainer | None |
docs_url | None |
author | Marcel |
requires_python | <4.0,>=3.8 |
license | None |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# Django Ninja OAuth2
**Django Ninja OAuth2** package enables support of OAuth2 / OpenID Connect "Authorization Code Flow" for your swagger
documentation.
### Requirements
- Python >= 3.8
- django >= 3.1
- pydantic >= 2.0
- Django-Ninja >= 1.1.0
## Installation
```
pip install django-ninja-oauth2
```
After installation, change settings.py file. Locally it only worked with None. On a real domain it should work with
"same-origin-allow-popups".
```Python
# in <myapp>/settings.py
SECURE_CROSS_ORIGIN_OPENER_POLICY = None # or "same-origin-allow-popups"
```
## Usage
Initialize NinjaAPIOAuth2 wherever you would initialize the original Django Ninja api.
Set your authorization, token and public key url
By default, if no HTTP Authorization header is provided, required for OAuth2 authentication, it will automatically cancel the request and send the client an error.
If auto_error is set to False, when the HTTP Authorization header is not available, instead of erroring out, the dependency result will be None.
```Python
from ninja_oauth2 import NinjaAPIOAuth2, SwaggerOAuth2
from ninja_oauth2.security.oauth2 import OAuth2AuthorizationCodeBearer
oauth2 = OAuth2AuthorizationCodeBearer(
authorization_url="https://test.com/auth/realms/<realm>/protocol/openid-connect/auth",
token_url="https://test.com/auth/realms/<realm>/protocol/openid-connect/token",
public_key_url="https://test.com/auth/realms/<realm>",
auto_error=True # Default True
)
api = NinjaAPIOAuth2(
docs=SwaggerOAuth2(
auth={"clientId": "<client_id>"}
),
auth=oauth2) # Use auth for all endpoints, optional
@api.get("/add", tags=["Main"], auth=oauth2) # Use auth for specific endpoint
def add(request, a: int, b: int):
return {"result": a + b}
```
If you want to check the encoded jwt token against some condition, you can extend the OAuth2AuthorizationCodeBearer
in the following way:
```Python
from typing import Optional, Any
from django.http import HttpRequest
from ninja_oauth2 import NinjaAPIOAuth2, SwaggerOAuth2
from ninja_oauth2.security.oauth2 import OAuth2AuthorizationCodeBearer
class MyOAuth2(OAuth2AuthorizationCodeBearer):
# token_info returns the encoded jwt token
def authenticate(self, request: HttpRequest, token_info: dict) -> Optional[Any]:
if token_info["resource_access"]["<clien_id>"]["roles"] == "admin":
return token_info
# Otherwise it will return a 401 unauthorized
oauth2 = MyOAuth2(
authorization_url="https://test.com/auth/realms/<realm>/protocol/openid-connect/auth",
token_url="https://test.com/auth/realms/<realm>/protocol/openid-connect/token",
public_key_url="https://test.com/auth/realms/<realm>"
)
api = NinjaAPIOAuth2(
docs=SwaggerOAuth2(
auth={"clientId": "<client_id>"}
),
auth=oauth2)
```
Raw data
{
"_id": null,
"home_page": null,
"name": "django-ninja-oauth2",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.8",
"maintainer_email": null,
"keywords": null,
"author": "Marcel",
"author_email": "siegmann@eomap.de",
"download_url": "https://files.pythonhosted.org/packages/68/38/58c54292d75d767f3e9660746b81e30b5cb734d4052fa97491a409ae4d3a/django_ninja_oauth2-0.1.5.tar.gz",
"platform": null,
"description": "# Django Ninja OAuth2\n\n**Django Ninja OAuth2** package enables support of OAuth2 / OpenID Connect \"Authorization Code Flow\" for your swagger\ndocumentation.\n\n### Requirements\n\n- Python >= 3.8\n- django >= 3.1\n- pydantic >= 2.0\n- Django-Ninja >= 1.1.0\n\n## Installation\n\n```\npip install django-ninja-oauth2\n```\n\nAfter installation, change settings.py file. Locally it only worked with None. On a real domain it should work with\n\"same-origin-allow-popups\".\n\n```Python \n# in <myapp>/settings.py\nSECURE_CROSS_ORIGIN_OPENER_POLICY = None # or \"same-origin-allow-popups\"\n```\n\n## Usage\n\nInitialize NinjaAPIOAuth2 wherever you would initialize the original Django Ninja api.\n\nSet your authorization, token and public key url\n\nBy default, if no HTTP Authorization header is provided, required for OAuth2 authentication, it will automatically cancel the request and send the client an error.\n\nIf auto_error is set to False, when the HTTP Authorization header is not available, instead of erroring out, the dependency result will be None.\n\n```Python\nfrom ninja_oauth2 import NinjaAPIOAuth2, SwaggerOAuth2\nfrom ninja_oauth2.security.oauth2 import OAuth2AuthorizationCodeBearer\n\noauth2 = OAuth2AuthorizationCodeBearer(\n authorization_url=\"https://test.com/auth/realms/<realm>/protocol/openid-connect/auth\",\n token_url=\"https://test.com/auth/realms/<realm>/protocol/openid-connect/token\",\n public_key_url=\"https://test.com/auth/realms/<realm>\",\n auto_error=True # Default True\n)\n\napi = NinjaAPIOAuth2(\n docs=SwaggerOAuth2(\n auth={\"clientId\": \"<client_id>\"}\n ),\n auth=oauth2) # Use auth for all endpoints, optional\n\n@api.get(\"/add\", tags=[\"Main\"], auth=oauth2) # Use auth for specific endpoint\ndef add(request, a: int, b: int):\n return {\"result\": a + b}\n```\n\nIf you want to check the encoded jwt token against some condition, you can extend the OAuth2AuthorizationCodeBearer\nin the following way:\n\n```Python\nfrom typing import Optional, Any\nfrom django.http import HttpRequest\nfrom ninja_oauth2 import NinjaAPIOAuth2, SwaggerOAuth2\nfrom ninja_oauth2.security.oauth2 import OAuth2AuthorizationCodeBearer\n\nclass MyOAuth2(OAuth2AuthorizationCodeBearer):\n # token_info returns the encoded jwt token\n def authenticate(self, request: HttpRequest, token_info: dict) -> Optional[Any]:\n if token_info[\"resource_access\"][\"<clien_id>\"][\"roles\"] == \"admin\":\n return token_info\n # Otherwise it will return a 401 unauthorized\n\n \noauth2 = MyOAuth2(\n authorization_url=\"https://test.com/auth/realms/<realm>/protocol/openid-connect/auth\",\n token_url=\"https://test.com/auth/realms/<realm>/protocol/openid-connect/token\",\n public_key_url=\"https://test.com/auth/realms/<realm>\"\n)\n\napi = NinjaAPIOAuth2(\n docs=SwaggerOAuth2(\n auth={\"clientId\": \"<client_id>\"}\n ),\n auth=oauth2)\n```",
"bugtrack_url": null,
"license": null,
"summary": "Django Ninja OAuth2 package enables support of OAuth2 / OpenID Connect Authorization Code Flow for your swagger documentation.",
"version": "0.1.5",
"project_urls": null,
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "4a1537aa7718b16d80de7ddc2fbab0323dd781ba7f8f6ca08564e4f6cf87884b",
"md5": "09316f614a2442349a714bad37e27492",
"sha256": "9f5ced3f43dcad54171f3d7af33eeb269e078c062f50bb333e0ca6381c3bdcfa"
},
"downloads": -1,
"filename": "django_ninja_oauth2-0.1.5-py3-none-any.whl",
"has_sig": false,
"md5_digest": "09316f614a2442349a714bad37e27492",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.8",
"size": 8285,
"upload_time": "2024-10-02T13:48:21",
"upload_time_iso_8601": "2024-10-02T13:48:21.765904Z",
"url": "https://files.pythonhosted.org/packages/4a/15/37aa7718b16d80de7ddc2fbab0323dd781ba7f8f6ca08564e4f6cf87884b/django_ninja_oauth2-0.1.5-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "683858c54292d75d767f3e9660746b81e30b5cb734d4052fa97491a409ae4d3a",
"md5": "9f2f33099c2e653eed49fb922ec20c18",
"sha256": "c1d3a49b548d9aa1d3eb515944bdc21ecab1e3920febf8ef0fcffb362f838b58"
},
"downloads": -1,
"filename": "django_ninja_oauth2-0.1.5.tar.gz",
"has_sig": false,
"md5_digest": "9f2f33099c2e653eed49fb922ec20c18",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.8",
"size": 9927,
"upload_time": "2024-10-02T13:48:23",
"upload_time_iso_8601": "2024-10-02T13:48:23.607203Z",
"url": "https://files.pythonhosted.org/packages/68/38/58c54292d75d767f3e9660746b81e30b5cb734d4052fa97491a409ae4d3a/django_ninja_oauth2-0.1.5.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-02 13:48:23",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "django-ninja-oauth2"
}