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
11 changed files
with
157 additions
and
0 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,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] |
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,63 @@ | ||
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> | ||
""") |
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,6 @@ | ||
from allauth.socialaccount.providers.oauth2.urls import default_urlpatterns | ||
|
||
from .provider import NextCloudProvider | ||
|
||
|
||
urlpatterns = default_urlpatterns(NextCloudProvider) |
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,36 @@ | ||
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(self.server)} | ||
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) |
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