2019-07-05 13:21:48 +00:00
|
|
|
"""passbook OIDC IDP Forms"""
|
|
|
|
|
|
|
|
from django import forms
|
2019-12-31 11:51:16 +00:00
|
|
|
from oauth2_provider.generators import generate_client_id, generate_client_secret
|
2019-07-05 13:21:48 +00:00
|
|
|
from oidc_provider.models import Client
|
|
|
|
|
2020-05-16 14:02:42 +00:00
|
|
|
from passbook.providers.oidc.models import OpenIDProvider
|
2019-07-05 13:21:48 +00:00
|
|
|
|
|
|
|
|
2020-05-16 14:02:42 +00:00
|
|
|
class OIDCProviderForm(forms.ModelForm):
|
2019-07-05 13:21:48 +00:00
|
|
|
"""OpenID Client form"""
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
# Correctly load data from 1:1 rel
|
2019-12-31 11:51:16 +00:00
|
|
|
if "instance" in kwargs and kwargs["instance"]:
|
|
|
|
kwargs["instance"] = kwargs["instance"].oidc_client
|
2019-07-05 13:21:48 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
2019-12-31 11:51:16 +00:00
|
|
|
self.fields["client_id"].initial = generate_client_id()
|
|
|
|
self.fields["client_secret"].initial = generate_client_secret()
|
2019-07-05 13:21:48 +00:00
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
2020-02-24 12:15:27 +00:00
|
|
|
self.instance.reuse_consent = False # This is managed by passbook
|
|
|
|
self.instance.require_consent = True # This is managed by passbook
|
2019-07-05 13:21:48 +00:00
|
|
|
response = super().save(*args, **kwargs)
|
|
|
|
# Check if openidprovider class instance exists
|
2020-05-16 14:02:42 +00:00
|
|
|
if not OpenIDProvider.objects.filter(oidc_client=self.instance).exists():
|
|
|
|
OpenIDProvider.objects.create(oidc_client=self.instance)
|
2019-07-05 13:21:48 +00:00
|
|
|
return response
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Client
|
|
|
|
fields = [
|
2019-12-31 11:51:16 +00:00
|
|
|
"name",
|
|
|
|
"client_type",
|
|
|
|
"client_id",
|
|
|
|
"client_secret",
|
|
|
|
"response_types",
|
|
|
|
"jwt_alg",
|
|
|
|
"_redirect_uris",
|
|
|
|
"_scope",
|
2019-07-05 13:21:48 +00:00
|
|
|
]
|
2019-12-31 11:51:16 +00:00
|
|
|
labels = {"client_secret": "Client Secret"}
|