2016-01-11 19:04:04 +00:00
|
|
|
from hashlib import md5
|
|
|
|
from random import randint
|
|
|
|
from uuid import uuid4
|
|
|
|
|
|
|
|
from django.forms import ModelForm
|
2015-04-17 20:02:10 +00:00
|
|
|
from django.contrib import admin
|
2015-04-22 14:53:19 +00:00
|
|
|
|
2015-08-05 16:21:17 +00:00
|
|
|
from oidc_provider.models import Client, Code, Token
|
2015-04-22 14:53:19 +00:00
|
|
|
|
2016-01-11 19:04:04 +00:00
|
|
|
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()
|
|
|
|
|
2015-04-17 20:02:10 +00:00
|
|
|
|
2015-12-04 16:55:04 +00:00
|
|
|
@admin.register(Client)
|
|
|
|
class ClientAdmin(admin.ModelAdmin):
|
|
|
|
|
2016-01-11 19:04:04 +00:00
|
|
|
form = ClientForm
|
|
|
|
list_display = ['name', 'client_id', 'response_type', 'date_created']
|
|
|
|
readonly_fields = ['date_created']
|
2015-12-04 16:55:04 +00:00
|
|
|
search_fields = ['name']
|
|
|
|
|
|
|
|
|
|
|
|
@admin.register(Code)
|
|
|
|
class CodeAdmin(admin.ModelAdmin):
|
|
|
|
|
|
|
|
def has_add_permission(self, request):
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
@admin.register(Token)
|
|
|
|
class TokenAdmin(admin.ModelAdmin):
|
|
|
|
|
|
|
|
def has_add_permission(self, request):
|
|
|
|
return False
|