This commit is contained in:
Iliyan Angelov
2025-09-14 23:24:25 +03:00
commit c67067a2a4
71311 changed files with 6800714 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
from allauth.socialaccount.providers.base import ProviderAccount
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider
class NextCloudAccount(ProviderAccount):
pass
class NextCloudProvider(OAuth2Provider):
id = "nextcloud"
name = "NextCloud"
account_class = NextCloudAccount
def extract_uid(self, data):
return str(data["id"])
def extract_common_fields(self, data):
return dict(
username=data["displayname"],
email=data["email"],
)
def get_default_scope(self):
scope = ["read"]
return scope
provider_classes = [NextCloudProvider]

View File

@@ -0,0 +1,66 @@
from django.test.utils import override_settings
from allauth.socialaccount.tests import OAuth2TestsMixin
from allauth.tests import MockedResponse, TestCase
from .provider import NextCloudProvider
@override_settings(
SOCIALACCOUNT_PROVIDERS={"nextcloud": {"SERVER": "https://nextcloud.example.org"}}
)
class NextCloudTests(OAuth2TestsMixin, TestCase):
provider_id = NextCloudProvider.id
def get_login_response_json(self, with_refresh_token=True):
return (
super(NextCloudTests, self)
.get_login_response_json(with_refresh_token=with_refresh_token)
.replace("uid", "user_id")
)
def get_mocked_response(self):
return MockedResponse(
200,
"""<?xml version="1.0"?>
<ocs>
<meta>
<status>ok</status>
<statuscode>100</statuscode>
<message>OK</message>
<totalitems></totalitems>
<itemsperpage></itemsperpage>
</meta>
<data>
<enabled>1</enabled>
<id>batman</id>
<storageLocation>/var/www/html/data/batman</storageLocation>
<lastLogin>1553946472000</lastLogin>
<backend>Database</backend>
<subadmin/>
<quota>
<free>1455417655296</free>
<used>467191265</used>
<total>1455884846561</total>
<relative>0.03</relative>
<quota>-3</quota>
</quota>
<email>batman@wayne.com</email>
<displayname>batman</displayname>
<phone>7351857301</phone>
<address>BatCave, Gotham City</address>
<website>https://batman.org</website>
<twitter>@the_batman</twitter>
<groups>
<element>admin</element>
</groups>
<language>fr</language>
<locale>fr_FR</locale>
<backendCapabilities>
<setDisplayName>1</setDisplayName>
<setPassword>1</setPassword>
</backendCapabilities>
</data>
</ocs>
""",
)

View File

@@ -0,0 +1,6 @@
from allauth.socialaccount.providers.oauth2.urls import default_urlpatterns
from .provider import NextCloudProvider
urlpatterns = default_urlpatterns(NextCloudProvider)

View File

@@ -0,0 +1,35 @@
import requests
import xml.etree.ElementTree as ET
from allauth.socialaccount import app_settings
from allauth.socialaccount.providers.oauth2.views import (
OAuth2Adapter,
OAuth2CallbackView,
OAuth2LoginView,
)
from .provider import NextCloudProvider
class NextCloudAdapter(OAuth2Adapter):
provider_id = NextCloudProvider.id
settings = app_settings.PROVIDERS.get(provider_id, {})
server = settings.get("SERVER", "https://nextcloud.example.org")
access_token_url = "{0}/apps/oauth2/api/v1/token".format(server)
authorize_url = "{0}/apps/oauth2/authorize".format(server)
profile_url = "{0}/ocs/v1.php/cloud/users/".format(server)
def complete_login(self, request, app, token, **kwargs):
extra_data = self.get_user_info(token, kwargs["response"]["user_id"])
return self.get_provider().sociallogin_from_response(request, extra_data)
def get_user_info(self, token, user_id):
headers = {"Authorization": "Bearer {0}".format(token)}
resp = requests.get(self.profile_url + user_id, headers=headers)
resp.raise_for_status()
data = ET.fromstring(resp.content.decode())[1]
return {d.tag: d.text.strip() for d in data if d.text is not None}
oauth2_login = OAuth2LoginView.adapter_view(NextCloudAdapter)
oauth2_callback = OAuth2CallbackView.adapter_view(NextCloudAdapter)