Auto-generate client_id and client_secret in the admin.

This commit is contained in:
juanifioren 2016-01-11 16:04:04 -03:00
parent d30fdeb5f7
commit 6e1f0822aa
3 changed files with 76 additions and 0 deletions

View file

@ -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 django.contrib import admin
from oidc_provider.models import Client, Code, Token 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) @admin.register(Client)
class ClientAdmin(admin.ModelAdmin): class ClientAdmin(admin.ModelAdmin):
form = ClientForm
list_display = ['name', 'client_id', 'response_type', 'date_created']
readonly_fields = ['date_created']
search_fields = ['name'] search_fields = ['name']

View file

@ -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'),
),
]

View file

@ -19,6 +19,7 @@ class Client(models.Model):
client_secret = models.CharField(max_length=255, unique=True) client_secret = models.CharField(max_length=255, unique=True)
response_type = models.CharField(max_length=30, response_type = models.CharField(max_length=30,
choices=RESPONSE_TYPE_CHOICES) 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.')) _redirect_uris = models.TextField(default='', verbose_name=_(u'Redirect URI'), help_text=_(u'Enter each URI on a new line.'))