2016-10-26 20:04:12 +00:00
|
|
|
from hashlib import sha224
|
2016-01-11 19:04:04 +00:00
|
|
|
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
|
2022-11-27 16:31:32 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2015-04-22 14:53:19 +00:00
|
|
|
|
2016-01-25 20:52:24 +00:00
|
|
|
from oidc_provider.models import Client, Code, Token, RSAKey
|
|
|
|
|
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)
|
2016-04-05 21:31:08 +00:00
|
|
|
|
|
|
|
secret = ''
|
|
|
|
|
2016-01-11 19:04:04 +00:00
|
|
|
if instance and instance.pk:
|
2016-04-05 21:31:08 +00:00
|
|
|
if (self.cleaned_data['client_type'] == 'confidential') and not instance.client_secret:
|
2016-10-26 20:04:12 +00:00
|
|
|
secret = sha224(uuid4().hex.encode()).hexdigest()
|
2016-04-05 21:31:08 +00:00
|
|
|
elif (self.cleaned_data['client_type'] == 'confidential') and instance.client_secret:
|
|
|
|
secret = instance.client_secret
|
2016-01-11 19:04:04 +00:00
|
|
|
else:
|
2016-09-07 15:30:20 +00:00
|
|
|
if (self.cleaned_data['client_type'] == 'confidential'):
|
2016-10-26 20:04:12 +00:00
|
|
|
secret = sha224(uuid4().hex.encode()).hexdigest()
|
2016-04-05 21:31:08 +00:00
|
|
|
|
|
|
|
return secret
|
2016-01-11 19:04:04 +00:00
|
|
|
|
2015-04-17 20:02:10 +00:00
|
|
|
|
2015-12-04 16:55:04 +00:00
|
|
|
@admin.register(Client)
|
|
|
|
class ClientAdmin(admin.ModelAdmin):
|
2016-09-07 15:30:20 +00:00
|
|
|
|
2016-09-09 18:57:25 +00:00
|
|
|
fieldsets = [
|
|
|
|
[_(u''), {
|
2017-08-08 22:41:42 +00:00
|
|
|
'fields': (
|
2018-08-15 20:43:48 +00:00
|
|
|
'name', 'owner', 'client_type', 'response_types', '_redirect_uris', 'jwt_alg',
|
2017-11-09 10:57:22 +00:00
|
|
|
'require_consent', 'reuse_consent'),
|
2016-09-09 18:57:25 +00:00
|
|
|
}],
|
|
|
|
[_(u'Credentials'), {
|
2018-04-08 20:43:24 +00:00
|
|
|
'fields': ('client_id', 'client_secret', '_scope'),
|
2016-09-09 18:57:25 +00:00
|
|
|
}],
|
|
|
|
[_(u'Information'), {
|
|
|
|
'fields': ('contact_email', 'website_url', 'terms_url', 'logo', 'date_created'),
|
|
|
|
}],
|
2016-10-31 19:36:58 +00:00
|
|
|
[_(u'Session Management'), {
|
|
|
|
'fields': ('_post_logout_redirect_uris',),
|
|
|
|
}],
|
2016-09-09 18:57:25 +00:00
|
|
|
]
|
2016-01-11 19:04:04 +00:00
|
|
|
form = ClientForm
|
2018-08-15 20:43:48 +00:00
|
|
|
list_display = ['name', 'client_id', 'response_type_descriptions', 'date_created']
|
2016-01-11 19:04:04 +00:00
|
|
|
readonly_fields = ['date_created']
|
2015-12-04 16:55:04 +00:00
|
|
|
search_fields = ['name']
|
2017-11-09 10:57:22 +00:00
|
|
|
raw_id_fields = ['owner']
|
2015-12-04 16:55:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
@admin.register(Code)
|
|
|
|
class CodeAdmin(admin.ModelAdmin):
|
2016-09-07 15:30:20 +00:00
|
|
|
|
2015-12-04 16:55:04 +00:00
|
|
|
def has_add_permission(self, request):
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
@admin.register(Token)
|
|
|
|
class TokenAdmin(admin.ModelAdmin):
|
2016-09-07 15:30:20 +00:00
|
|
|
|
2015-12-04 16:55:04 +00:00
|
|
|
def has_add_permission(self, request):
|
|
|
|
return False
|
2016-01-25 20:52:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
@admin.register(RSAKey)
|
|
|
|
class RSAKeyAdmin(admin.ModelAdmin):
|
|
|
|
|
|
|
|
readonly_fields = ['kid']
|