add api key auth to swagger

This commit is contained in:
sadnub
2022-03-17 10:05:10 -04:00
parent 30fb855200
commit c7bb94d82a
3 changed files with 17 additions and 1 deletions

View File

@@ -2,4 +2,7 @@ from __future__ import absolute_import, unicode_literals
from .celery import app as celery_app
# drf auto-registers this as an authentication method when imported
from .schema import APIAuthenticationScheme
__all__ = ("celery_app",)

View File

@@ -0,0 +1,12 @@
from drf_spectacular.extensions import OpenApiAuthenticationExtension
# custom api key auth for swagger-ui
class APIAuthenticationScheme(OpenApiAuthenticationExtension):
target_class = "tacticalrmm.auth.APIAuthentication" # full import path OR class ref
name = 'API Key Auth' # name used in the schem
def get_security_definition(self, auto_schema):
return {
'type': 'apiKey',
'in': 'header',
'name': 'X-API-KEY',
}

View File

@@ -58,7 +58,7 @@ except ImportError:
pass
REST_FRAMEWORK = {
"DATETIME_FORMAT": "%b-%d-%Y - %H:%M",
# "DATETIME_FORMAT": "%b-%d-%Y - %H:%M",
"DEFAULT_PERMISSION_CLASSES": ("rest_framework.permissions.IsAuthenticated",),
"DEFAULT_AUTHENTICATION_CLASSES": (
"knox.auth.TokenAuthentication",
@@ -71,6 +71,7 @@ SPECTACULAR_SETTINGS = {
"TITLE": "Tactical RMM API",
"DESCRIPTION": "Simple and Fast remote monitoring and management tool",
"VERSION": TRMM_VERSION,
'AUTHENTICATION_WHITELIST': ["tacticalrmm.auth.APIAuthentication"],
}
if not "AZPIPELINE" in os.environ: