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

Fix models to work correctly with client secrets

(cherry picked from commit 3a9e8fb9)
parent 9d78e1a1
No related branches found
No related tags found
No related merge requests found
# Generated by Django 4.1.9 on 2023-06-17 18:42 # Generated by Django 4.2.4 on 2023-08-30 14:25
from django.db import migrations, models from django.db import migrations, models
import django.db.models.deletion import django.db.models.deletion
...@@ -8,7 +8,7 @@ class Migration(migrations.Migration): ...@@ -8,7 +8,7 @@ class Migration(migrations.Migration):
dependencies = [ dependencies = [
("sites", "0002_alter_domain_unique"), ("sites", "0002_alter_domain_unique"),
("kort", "0016_card_last_read_counter"), ("kort", "0015_migrate_scopes"),
] ]
operations = [ operations = [
......
...@@ -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
......
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