Skip to content
Snippets Groups Projects
Verified Commit 3a9e8fb9 authored by Jonathan Weth's avatar Jonathan Weth :keyboard:
Browse files

Fix models to work correctly with client secrets

parent f00e832d
No related branches found
No related tags found
1 merge request!14Draft: Resolve "Object / Person identification using SDM NFC cards"
...@@ -14,6 +14,7 @@ from django.utils.translation import gettext as _ ...@@ -14,6 +14,7 @@ from django.utils.translation import gettext as _
from celery.result import AsyncResult from celery.result import AsyncResult
from model_utils.models import TimeStampedModel from model_utils.models import TimeStampedModel
from oauth2_provider.generators import generate_client_secret
from aleksis.core.mixins import ExtensibleModel from aleksis.core.mixins import ExtensibleModel
from aleksis.core.models import OAuthApplication, Person from aleksis.core.models import OAuthApplication, Person
...@@ -82,27 +83,41 @@ class CardPrinter(ExtensibleModel): ...@@ -82,27 +83,41 @@ class CardPrinter(ExtensibleModel):
null=True, null=True,
related_name="card_printers", related_name="card_printers",
) )
oauth2_client_secret = models.CharField(
max_length=255,
blank=True,
verbose_name=_("OAuth2 client secret"),
)
# Settings # Settings
cups_printer = models.CharField(max_length=255, verbose_name=_("CUPS printer"), blank=True) cups_printer = models.CharField(
max_length=255,
verbose_name=_("CUPS printer"),
blank=True,
help_text=_("Leave blank to deactivate CUPS printing"),
)
generate_number_on_server = models.BooleanField( generate_number_on_server = models.BooleanField(
default=True, verbose_name=_("Generate card number on server") default=True, verbose_name=_("Generate card number on server")
) )
card_detector = models.CharField(max_length=255, verbose_name=_("Card detector"), blank=True) card_detector = models.CharField(max_length=255, verbose_name=_("Card detector"), blank=True)
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
super().save(*args, **kwargs)
if not self.oauth2_application: if not self.oauth2_application:
client_secret = generate_client_secret()
application = OAuthApplication( application = OAuthApplication(
client_type=OAuthApplication.CLIENT_CONFIDENTIAL, client_type=OAuthApplication.CLIENT_CONFIDENTIAL,
authorization_grant_type=OAuthApplication.GRANT_CLIENT_CREDENTIALS, authorization_grant_type=OAuthApplication.GRANT_CLIENT_CREDENTIALS,
name=f"Card printer: {self.name}", name=f"Card printer: {self.name}",
redirect_uris="urn:ietf:wg:oauth:2.0:oob",
allowed_scopes=[self.scope], allowed_scopes=[self.scope],
client_secret=client_secret,
) )
application.save() application.save()
self.oauth2_application = application self.oauth2_application = application
self.oauth2_client_secret = client_secret
super().save(*args, **kwargs) super().save(*args, **kwargs)
def __str__(self): def __str__(self):
return self.name return self.name
...@@ -127,7 +142,7 @@ class CardPrinter(ExtensibleModel): ...@@ -127,7 +142,7 @@ class CardPrinter(ExtensibleModel):
config = { config = {
"base_url": settings.BASE_URL, "base_url": settings.BASE_URL,
"client_id": self.oauth2_application.client_id, "client_id": self.oauth2_application.client_id,
"client_secret": self.oauth2_application.client_secret, "client_secret": self.oauth2_client_secret,
} }
return config return config
...@@ -294,7 +309,7 @@ class Card(ExtensibleModel): ...@@ -294,7 +309,7 @@ class Card(ExtensibleModel):
} }
def generate_pdf(self) -> Union[bool, AsyncResult]: def generate_pdf(self) -> Union[bool, AsyncResult]:
from .tasks import generate_card_pdf from ..tasks import generate_card_pdf
if self.pdf_file: if self.pdf_file:
return True return True
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment