Django Ninja OAuth2 package enables support of OAuth2 / OpenID Connect Authorization Code Flow for your swagger documentation.
Project description
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".
# 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.
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}
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django_ninja_oauth2-0.1.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8c5684379c5d9daac483f24ee9a8b25b9659878f490903a3b33cd5b7a885e757 |
|
MD5 | b6e702e476243735e48d7cdd51a73184 |
|
BLAKE2b-256 | 470bac159ad462e679f5654f32c49f08f2b40b660b73ad55492c069845f9162c |
Hashes for django_ninja_oauth2-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c175018b14933930504c525acda7d9ade2fac613233230e14a0e8d4601aca103 |
|
MD5 | b3e781a1e6d9745ef121cd29642600a6 |
|
BLAKE2b-256 | c57c8cc72836a27ed88fc755067c9bf274bf8d3005c95c3e27f86c06c8633684 |