Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental: support token authentication for API #273

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions backend/ciso_assistant/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def set_ciso_assistant_url(_, __, event_dict):
"serdes",
"rest_framework",
"drf_spectacular",
"rest_framework.authtoken",
]

MIDDLEWARE = [
Expand Down Expand Up @@ -181,6 +182,7 @@ def set_ciso_assistant_url(_, __, event_dict):
],
"DEFAULT_AUTHENTICATION_CLASSES": [
"rest_framework.authentication.SessionAuthentication",
"rest_framework.authentication.TokenAuthentication",
],
"DEFAULT_PERMISSION_CLASSES": [
"rest_framework.permissions.IsAuthenticated",
Expand Down
3 changes: 2 additions & 1 deletion backend/ciso_assistant/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
from django.urls import include, path
from ciso_assistant import settings
from drf_spectacular.views import SpectacularAPIView, SpectacularRedocView, SpectacularSwaggerView

from rest_framework.authtoken import views

# beware of the order of url patterns, this can change de behavior in case of multiple matches and avoid giving identical paths that could cause conflicts
urlpatterns = [
path("api/", include("core.urls")),
path('api-token-auth/', views.obtain_auth_token),
path("serdes/", include("serdes.urls")),
path("i18n/", include("django.conf.urls.i18n")),
path('api/schema/', SpectacularAPIView.as_view(), name='schema'),
Expand Down
2 changes: 2 additions & 0 deletions backend/core/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,5 @@ def ready(self):
# avoid post_migrate handler if we are in the main, as it interferes with restore
if not os.environ.get("RUN_MAIN"):
post_migrate.connect(startup, sender=self)

import core.signals
9 changes: 9 additions & 0 deletions backend/core/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.conf import settings
from django.db.models.signals import post_save
from django.dispatch import receiver
from rest_framework.authtoken.models import Token

@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_auth_token(sender, instance=None, created=False, **kwargs):
if created:
Token.objects.create(user=instance)
Loading