From 6e1f0822aa9186867389b1cfdb9eec555c44e187 Mon Sep 17 00:00:00 2001 From: juanifioren Date: Mon, 11 Jan 2016 16:04:04 -0300 Subject: [PATCH] Auto-generate client_id and client_secret in the admin. --- oidc_provider/admin.py | 35 ++++++++++++++++ .../migrations/0007_auto_20160111_1844.py | 40 +++++++++++++++++++ oidc_provider/models.py | 1 + 3 files changed, 76 insertions(+) create mode 100644 oidc_provider/migrations/0007_auto_20160111_1844.py diff --git a/oidc_provider/admin.py b/oidc_provider/admin.py index b162918..140ad74 100644 --- a/oidc_provider/admin.py +++ b/oidc_provider/admin.py @@ -1,11 +1,46 @@ +from hashlib import md5 +from random import randint +from uuid import uuid4 + +from django.forms import ModelForm from django.contrib import admin from oidc_provider.models import Client, Code, Token +class ClientForm(ModelForm): + + class Meta: + model = Client + exclude = [] + + def __init__(self, *args, **kwargs): + super(ClientForm, self).__init__(*args, **kwargs) + self.fields['client_id'].required = False + self.fields['client_id'].widget.attrs['disabled'] = 'true' + self.fields['client_secret'].required = False + self.fields['client_secret'].widget.attrs['disabled'] = 'true' + + def clean_client_id(self): + instance = getattr(self, 'instance', None) + if instance and instance.pk: + return instance.client_id + else: + return str(randint(1, 999999)).zfill(6) + + def clean_client_secret(self): + instance = getattr(self, 'instance', None) + if instance and instance.pk: + return instance.client_secret + else: + return md5(str(uuid4())).hexdigest() + @admin.register(Client) class ClientAdmin(admin.ModelAdmin): + form = ClientForm + list_display = ['name', 'client_id', 'response_type', 'date_created'] + readonly_fields = ['date_created'] search_fields = ['name'] diff --git a/oidc_provider/migrations/0007_auto_20160111_1844.py b/oidc_provider/migrations/0007_auto_20160111_1844.py new file mode 100644 index 0000000..a160cc5 --- /dev/null +++ b/oidc_provider/migrations/0007_auto_20160111_1844.py @@ -0,0 +1,40 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9 on 2016-01-11 18:44 +from __future__ import unicode_literals + +import datetime +from django.db import migrations, models +from django.utils.timezone import utc + + +class Migration(migrations.Migration): + + dependencies = [ + ('oidc_provider', '0006_unique_user_client'), + ] + + operations = [ + migrations.AlterModelOptions( + name='client', + options={'verbose_name': 'Client', 'verbose_name_plural': 'Clients'}, + ), + migrations.AlterModelOptions( + name='code', + options={'verbose_name': 'Authorization Code', 'verbose_name_plural': 'Authorization Codes'}, + ), + migrations.AlterModelOptions( + name='token', + options={'verbose_name': 'Token', 'verbose_name_plural': 'Tokens'}, + ), + migrations.AddField( + model_name='client', + name='date_created', + field=models.DateField(auto_now_add=True, default=datetime.datetime(2016, 1, 11, 18, 44, 32, 192477, tzinfo=utc)), + preserve_default=False, + ), + migrations.AlterField( + model_name='client', + name='_redirect_uris', + field=models.TextField(default=b'', help_text='Enter each URI on a new line.', verbose_name='Redirect URI'), + ), + ] diff --git a/oidc_provider/models.py b/oidc_provider/models.py index cb8eb14..c7cb0ed 100644 --- a/oidc_provider/models.py +++ b/oidc_provider/models.py @@ -19,6 +19,7 @@ class Client(models.Model): client_secret = models.CharField(max_length=255, unique=True) response_type = models.CharField(max_length=30, choices=RESPONSE_TYPE_CHOICES) + date_created = models.DateField(auto_now_add=True) _redirect_uris = models.TextField(default='', verbose_name=_(u'Redirect URI'), help_text=_(u'Enter each URI on a new line.'))