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 97f5336 commit bd912d4
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions Watcher/Watcher/accounts/models.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
from django.db import models
from django_auth_ldap.backend import populate_user
from django.contrib.auth.models import User
from knox.models import AuthToken


class APIKey(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
key = models.CharField(max_length=100, unique=True)
created_at = models.DateTimeField(auto_now_add=True)
expiration = models.IntegerField(default=30)
expiry_at = models.DateTimeField(null=True, blank=True)
key_details = models.TextField(null=True, blank=True) # Ajout de ce champ
"""
Manages creation, modification, and deletion of user API keys.
"""
auth_token = models.OneToOneField(AuthToken, on_delete=models.CASCADE, null=True, blank=True)

def __str__(self):
return f"API Key for {self.user.username}"
return f"API Key for {self.auth_token.user.username}"

class Meta:
verbose_name = "API Key"
Expand All @@ -23,5 +23,4 @@ def make_inactive(sender, user, **kwargs):
if not User.objects.filter(username=user.username):
user.is_active = False


populate_user.connect(make_inactive)

0 comments on commit bd912d4

Please sign in to comment.