2019-07-05 13:21:48 +00:00
|
|
|
"""passbook OIDC IDP Forms"""
|
|
|
|
|
|
|
|
from django import forms
|
|
|
|
from oauth2_provider.generators import (generate_client_id,
|
|
|
|
generate_client_secret)
|
|
|
|
from oidc_provider.models import Client
|
|
|
|
|
2019-10-07 14:33:48 +00:00
|
|
|
from passbook.providers.oidc.models import OpenIDProvider
|
2019-07-05 13:21:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
class OIDCProviderForm(forms.ModelForm):
|
|
|
|
"""OpenID Client form"""
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
# Correctly load data from 1:1 rel
|
2019-07-05 13:59:52 +00:00
|
|
|
if 'instance' in kwargs and kwargs['instance']:
|
2019-07-05 13:21:48 +00:00
|
|
|
kwargs['instance'] = kwargs['instance'].oidc_client
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.fields['client_id'].initial = generate_client_id()
|
|
|
|
self.fields['client_secret'].initial = generate_client_secret()
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
response = super().save(*args, **kwargs)
|
|
|
|
# Check if openidprovider class instance exists
|
|
|
|
if not OpenIDProvider.objects.filter(oidc_client=self.instance).exists():
|
|
|
|
OpenIDProvider.objects.create(oidc_client=self.instance)
|
|
|
|
return response
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Client
|
|
|
|
fields = [
|
|
|
|
'name', 'client_type', 'client_id', 'client_secret', 'response_types',
|
2019-12-14 13:28:36 +00:00
|
|
|
'jwt_alg', '_redirect_uris', '_scope'
|
2019-07-05 13:21:48 +00:00
|
|
|
]
|
|
|
|
labels = {
|
|
|
|
'client_secret': "Client Secret"
|
|
|
|
}
|