Skip to content

Commit

Permalink
API Key Creation & Management
Browse files Browse the repository at this point in the history
Added functionality for superusers and users to create and manage API keys, with Knox integration for secure key hashing.
  • Loading branch information
NEZRI Ygal authored and NEZRI Ygal committed Jul 22, 2024
1 parent 56b1975 commit 822c57c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
12 changes: 11 additions & 1 deletion Watcher/Watcher/accounts/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
from django.utils import timezone
from datetime import timedelta
from knox.models import AuthToken

from django.db.models.signals import post_delete
from django.dispatch import receiver

"""
Log Entries Snippet
Expand Down Expand Up @@ -289,6 +290,15 @@ def key_details(self, obj):
admin.site.register(APIKey, APIKeyAdmin)


@receiver(post_delete, sender=APIKey)
def delete_authtoken_when_apikey_deleted(sender, instance, **kwargs):
try:
if instance.auth_token:
instance.auth_token.delete()
except AuthToken.DoesNotExist:
pass


class AuthTokenAdmin(admin.ModelAdmin):
list_display = ('user', 'digest', 'created', 'expiry')
readonly_fields = ('user', 'digest', 'created', 'expiry')
Expand Down
11 changes: 1 addition & 10 deletions Watcher/Watcher/accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
from django_auth_ldap.backend import populate_user
from django.contrib.auth.models import User
from knox.models import AuthToken
from django.db.models.signals import post_delete
from django.dispatch import receiver


class APIKey(models.Model):
"""
Expand All @@ -19,14 +18,6 @@ class Meta:
verbose_name_plural = "API Keys"
app_label = 'auth'

@receiver(post_delete, sender=APIKey)
def delete_authtoken_when_apikey_deleted(sender, instance, **kwargs):
try:
if instance.auth_token:
instance.auth_token.delete()
except AuthToken.DoesNotExist:
pass


def make_inactive(sender, user, **kwargs):
if not User.objects.filter(username=user.username):
Expand Down

0 comments on commit 822c57c

Please sign in to comment.