2019-10-28 16:40:57 +00:00
|
|
|
"""ApplicationGatewayProvider API Views"""
|
2019-12-31 11:51:16 +00:00
|
|
|
from oauth2_provider.generators import generate_client_id, generate_client_secret
|
2019-11-11 17:13:46 +00:00
|
|
|
from oidc_provider.models import Client
|
2019-10-28 16:40:57 +00:00
|
|
|
from rest_framework.serializers import ModelSerializer
|
|
|
|
from rest_framework.viewsets import ModelViewSet
|
|
|
|
|
|
|
|
from passbook.providers.app_gw.models import ApplicationGatewayProvider
|
2019-11-11 17:13:46 +00:00
|
|
|
from passbook.providers.oidc.api import OpenIDProviderSerializer
|
2019-10-28 16:40:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ApplicationGatewayProviderSerializer(ModelSerializer):
|
|
|
|
"""ApplicationGatewayProvider Serializer"""
|
|
|
|
|
2019-11-11 17:13:46 +00:00
|
|
|
client = OpenIDProviderSerializer()
|
|
|
|
|
|
|
|
def create(self, validated_data):
|
|
|
|
instance = super().create(validated_data)
|
|
|
|
instance.client = Client.objects.create(
|
2019-12-31 11:51:16 +00:00
|
|
|
client_id=generate_client_id(), client_secret=generate_client_secret()
|
|
|
|
)
|
2019-11-11 17:13:46 +00:00
|
|
|
instance.save()
|
|
|
|
return instance
|
|
|
|
|
|
|
|
def update(self, instance, validated_data):
|
|
|
|
self.instance.client.name = self.instance.name
|
|
|
|
self.instance.client.redirect_uris = [
|
|
|
|
f"http://{self.instance.host}/oauth2/callback",
|
|
|
|
f"https://{self.instance.host}/oauth2/callback",
|
|
|
|
]
|
2019-12-31 11:51:16 +00:00
|
|
|
self.instance.client.scope = ["openid", "email"]
|
2019-11-11 17:13:46 +00:00
|
|
|
self.instance.client.save()
|
|
|
|
return super().update(instance, validated_data)
|
|
|
|
|
2019-10-28 16:40:57 +00:00
|
|
|
class Meta:
|
|
|
|
|
|
|
|
model = ApplicationGatewayProvider
|
2020-02-28 10:48:55 +00:00
|
|
|
fields = ["pk", "name", "internal_host", "external_host", "client"]
|
2019-12-31 11:51:16 +00:00
|
|
|
read_only_fields = ["client"]
|
|
|
|
|
2019-10-28 16:40:57 +00:00
|
|
|
|
|
|
|
class ApplicationGatewayProviderViewSet(ModelViewSet):
|
|
|
|
"""ApplicationGatewayProvider Viewset"""
|
|
|
|
|
|
|
|
queryset = ApplicationGatewayProvider.objects.all()
|
|
|
|
serializer_class = ApplicationGatewayProviderSerializer
|