Ninja JWT रूट प्रमाणीकरण
Ninja JWT, आपके API endpoints तक पहुँचने वाले उपयोगकर्ताओं को authenticate करने के तरीके के रूप में Django Ninja के HttpBearer का उपयोग करता है।
Authenticated उपयोगकर्ता को request.user या request.auth में पाया जा सकता है।
Route Authentication - Class Based
from ninja_extra import api_controller, route
from ninja_jwt.authentication import JWTAuth
@api_controller
class MyController:
@route.get('/some-endpoint', auth=JWTAuth())
def some_endpoint(self):
...
Route Authentication - Function Based
from ninja import router
from ninja_jwt.authentication import JWTAuth
router = router('')
@router.get('/some-endpoint', auth=JWTAuth())
def some_endpoint(request):
...
Custom Authentication Implementation
यदि आप JWTAuth का कोई अलग implementation उपयोग करना चाहते हैं, तो आपको JWTBaseAuthentication को subclass करने की आवश्यकता है।
यदि आप bearer के अलावा कुछ और उपयोग करना चाहते हैं, तो Django Ninja - Authentication के बारे में अधिक पढ़ें।
उदाहरण:
from ninja.security import APIKeyHeader
from ninja_jwt.authentication import JWTBaseAuthentication
from ninja import router
class ApiKey(APIKeyHeader, JWTBaseAuthentication):
param_name = "X-API-Key"
def authenticate(self, request, key):
return self.jwt_authenticate(request, token=key)
header_key = ApiKey()
router = router('')
@router.get("/headerkey", auth=header_key)
def apikey(request):
return f"Token = {request.auth}"
Asynchronous Route Authentication
यदि आप asynchronous route authentication में रुचि रखते हैं, तो एक AsyncJWTAuth class उपलब्ध है
from ninja_extra import api_controller, route
from ninja_jwt.authentication import AsyncJWTAuth
@api_controller
class MyController:
@route.get('/some-endpoint', auth=AsyncJWTAuth())
async def some_endpoint(self):
...
some_endpoint asynchronous होना चाहिए। AsyncJWTAuth से marked कोई भी endpoint function asynchronous होना चाहिए।
Warning
Asynchronous functionality केवल Django versions > 3.0 के लिए उपलब्ध है।
Success
अगले अध्याय Settings पर जारी रखें।
पेज व्यूज़:
