From a3247db273fd26b787dca71432cf3f9b699a5882 Mon Sep 17 00:00:00 2001 From: Ignacio Fiorentino Date: Tue, 5 Apr 2016 18:31:08 -0300 Subject: [PATCH] Improve handle of client_secret with client_types. --- oidc_provider/admin.py | 15 ++++++++++++-- .../migrations/0012_auto_20160405_2041.py | 20 +++++++++++++++++++ oidc_provider/models.py | 2 +- 3 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 oidc_provider/migrations/0012_auto_20160405_2041.py diff --git a/oidc_provider/admin.py b/oidc_provider/admin.py index 9963b35..9b3bd0a 100644 --- a/oidc_provider/admin.py +++ b/oidc_provider/admin.py @@ -30,10 +30,21 @@ class ClientForm(ModelForm): def clean_client_secret(self): instance = getattr(self, 'instance', None) + + secret = '' + + print self.cleaned_data + if instance and instance.pk: - return instance.client_secret + if (self.cleaned_data['client_type'] == 'confidential') and not instance.client_secret: + secret = md5(uuid4().hex.encode()).hexdigest() + elif (self.cleaned_data['client_type'] == 'confidential') and instance.client_secret: + secret = instance.client_secret else: - return md5(uuid4().hex.encode()).hexdigest() + if (instance.client_type == 'confidential'): + secret = md5(uuid4().hex.encode()).hexdigest() + + return secret @admin.register(Client) diff --git a/oidc_provider/migrations/0012_auto_20160405_2041.py b/oidc_provider/migrations/0012_auto_20160405_2041.py new file mode 100644 index 0000000..c04b613 --- /dev/null +++ b/oidc_provider/migrations/0012_auto_20160405_2041.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9 on 2016-04-05 20:41 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('oidc_provider', '0011_client_client_type'), + ] + + operations = [ + migrations.AlterField( + model_name='client', + name='client_secret', + field=models.CharField(blank=True, default=b'', max_length=255), + ), + ] diff --git a/oidc_provider/models.py b/oidc_provider/models.py index 69dcc39..8d9ad39 100644 --- a/oidc_provider/models.py +++ b/oidc_provider/models.py @@ -24,7 +24,7 @@ class Client(models.Model): name = models.CharField(max_length=100, default='') client_type = models.CharField(max_length=30, choices=CLIENT_TYPE_CHOICES, default='confidential', help_text=_(u'Confidential clients are capable of maintaining the confidentiality of their credentials. Public clients are incapable.')) client_id = models.CharField(max_length=255, unique=True) - client_secret = models.CharField(max_length=255, unique=True) + client_secret = models.CharField(max_length=255, blank=True, default='') response_type = models.CharField(max_length=30, choices=RESPONSE_TYPE_CHOICES) date_created = models.DateField(auto_now_add=True)