-
-
Notifications
You must be signed in to change notification settings - Fork 459
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
175 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
from django.urls import path | ||
from django.urls import include | ||
|
||
from . import views | ||
|
||
urlpatterns = [ | ||
path("", include("allauth.urls")), | ||
path("ssoproviders/", views.GetAddSSOProvider.as_view()), | ||
path("ssoproviders/<int:pk>/", views.GetUpdateDeleteSSOProvider.as_view()), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import re | ||
from django.shortcuts import get_object_or_404 | ||
|
||
from allauth.socialaccount.models import SocialApp | ||
from rest_framework.serializers import ModelSerializer, ReadOnlyField | ||
from rest_framework.response import Response | ||
from rest_framework.views import APIView | ||
from accounts.permissions import AccountsPerms | ||
from rest_framework.permissions import IsAuthenticated | ||
|
||
class SocialAppSerializer(ModelSerializer): | ||
server_url = ReadOnlyField(source="settings.server_url") | ||
class Meta: | ||
model = SocialApp | ||
fields = [ | ||
"id", | ||
"name", | ||
"provider", | ||
"provider_id", | ||
"client_id", | ||
"secret", | ||
"server_url", | ||
"settings", | ||
] | ||
|
||
|
||
class GetAddSSOProvider(APIView): | ||
permission_classes = [IsAuthenticated, AccountsPerms] | ||
|
||
def get(self, request): | ||
providers = SocialApp.objects.all() | ||
return Response(SocialAppSerializer(providers, many=True).data) | ||
|
||
class InputSerializer(ModelSerializer): | ||
server_url = ReadOnlyField() | ||
class Meta: | ||
model = SocialApp | ||
fields = [ | ||
"name", | ||
"client_id", | ||
"secret", | ||
"server_url", | ||
"provider", | ||
"provider_id", | ||
"settings" | ||
] | ||
|
||
# removed any special characters and replaces spaces with a hyphen | ||
def generate_provider_id(self, string): | ||
id = re.sub(r'[^A-Za-z0-9\s]', '', string) | ||
id = id.replace(' ', '-') | ||
return id | ||
|
||
def post(self, request): | ||
data = request.data | ||
|
||
# need to move server_url into json settings | ||
data["settings"] = {} | ||
data["settings"]["server_url"] = data["server_url"] | ||
|
||
# set provider to 'openid_connect' | ||
data["provider"] = "openid_connect" | ||
|
||
# generate a url friendly provider id from the name | ||
data["provider_id"] = self.generate_provider_id(data["name"]) | ||
|
||
serializer = self.InputSerializer(data=data) | ||
serializer.is_valid(raise_exception=True) | ||
serializer.save() | ||
return Response("ok") | ||
|
||
|
||
class GetUpdateDeleteSSOProvider(APIView): | ||
permission_classes = [IsAuthenticated, AccountsPerms] | ||
|
||
class InputSerialzer(ModelSerializer): | ||
server_url = ReadOnlyField() | ||
class Meta: | ||
model = SocialApp | ||
fields = [ | ||
"client_id", | ||
"secret", | ||
"server_url", | ||
"settings" | ||
] | ||
|
||
def put(self, request, pk): | ||
provider = get_object_or_404(SocialApp, pk=pk) | ||
data = request.data | ||
|
||
# need to move server_url into json settings | ||
data["settings"] = {} | ||
data["settings"]["server_url"] = data["server_url"] | ||
|
||
serializer = self.InputSerialzer(instance=provider, data=request.data, partial=True) | ||
serializer.is_valid(raise_exception=True) | ||
serializer.save() | ||
return Response("ok") | ||
|
||
def delete(self, request, pk): | ||
provider = get_object_or_404(SocialApp, pk=pk) | ||
provider.delete() | ||
return Response("ok") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters