This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
authentik/passbook/providers/app_gw/api.py

46 lines
1.6 KiB
Python
Raw Normal View History

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
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
from passbook.providers.oidc.api import OpenIDProviderSerializer
2019-10-28 16:40:57 +00:00
class ApplicationGatewayProviderSerializer(ModelSerializer):
"""ApplicationGatewayProvider Serializer"""
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()
)
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"]
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