grim/django-allauth-jetbrains-hub

Initial revision, seems to be working fine in my test environment
draft default tip
2022-01-15, Gary Kramlich
e2efa39a9a48
Parents
Children
Initial revision, seems to be working fine in my test environment
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/.hgignore Sat Jan 15 19:18:52 2022 -0600
@@ -0,0 +1,7 @@
+syntax: regexp
+^t\/
+
+syntax: glob
+*.egg-info
+__pycache__
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/LICENSE Sat Jan 15 19:18:52 2022 -0600
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2022 Gary Kramlich
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/README.md Sat Jan 15 19:18:52 2022 -0600
@@ -0,0 +1,65 @@
+# django-allauth-jetbrains-hub
+
+Jetbrains Hub OAuth2 provider for django-allauth
+
+## Installation
+
+### Requirements
+
+* django-allauth (https://github.com/pennersr/django-allauth)
+
+### Python Package
+
+```
+pip install django-allauth-jetbrains-hub
+```
+
+### settings.py
+
+The server value in the path request must be set to point to your Jetbrains
+hub instance and include the full path to it. For example, if your instance
+is at https://example.com/ but you get redirected to https://example.com/hub/
+you should set server to https://example.com/hub/.
+
+```
+INSTALLED_APPS = (
+ ...
+ # The following apps are required:
+ 'django.contrib.auth',
+ 'django.contrib.sites',
+
+ 'allauth',
+ 'allauth.account',
+ 'allauth.socialaccount',
+ ...
+ # Add allauth_microsoft to installed_apps
+ 'allauth_jetbrains_hub'
+ ...
+)
+
+SOCIALACCOUNT_PROVIDERS = {
+ 'jetbrains-hub': {
+ 'SERVER': 'https://example.com/hub/',
+ }
+}
+
+```
+## Post-Installation
+
+### Create service in your hub instance
+
+* Create a service in your hub instance as documented [here](https://www.jetbrains.com/help/hub/add-service.html).
+* Open the service in HUB.
+* Set the Redirect URI to https://example.com/accounts/jetbrains-hub/login/callback/.
+* Copy your Application ID and click change secret to get a new secret.
+* Enter the Application ID and Secret into the Django Admin interface.
+
+## Contributors
+
+* Gary Kramlich <grim@reaperworld.com>
+
+Forked from [github.com/schaenzer/django-allauth-microsoft](https://github.com/schaenzer/django-allauth-microsoft).
+
+## License
+[MIT License](LICENSE)
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/allauth_jetbrains_hub/provider.py Sat Jan 15 19:18:52 2022 -0600
@@ -0,0 +1,42 @@
+from allauth.socialaccount.providers.base import ProviderAccount
+from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider
+
+
+class JetbrainsHubAccount(ProviderAccount):
+ def to_str(self):
+ dflt = super(JetbrainsHubAccount, self).to_str()
+
+ return self.account.extra_data.get('name', dflt)
+
+
+class JetbrainsHubProvider(OAuth2Provider):
+ id = 'jetbrains-hub'
+ name = 'Jetbrains Hub'
+ account_class = JetbrainsHubAccount
+
+ def get_scope(self, request):
+ scope = set(super(JetbrainsHubProvider, self).get_scope(request))
+ scope.add('openid')
+ return list(scope)
+
+ def get_default_scope(self):
+ return ['openid']
+
+ def extract_uid(self, data):
+ return str(data['sub'])
+
+ def extract_common_fields(self, data):
+ first_name = data.get('name')
+ last_name = ''
+ if ' ' in first_name:
+ first_name, last_name = first_name.split(' ', 1)
+
+ return dict(
+ email=data.get('email'),
+ first_name=first_name,
+ last_name=last_name,
+ username=data.get('email'),
+ )
+
+
+provider_classes = [JetbrainsHubProvider]
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/allauth_jetbrains_hub/urls.py Sat Jan 15 19:18:52 2022 -0600
@@ -0,0 +1,4 @@
+from allauth.socialaccount.providers.oauth2.urls import default_urlpatterns
+from .provider import JetbrainsHubProvider
+
+urlpatterns = default_urlpatterns(JetbrainsHubProvider)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/allauth_jetbrains_hub/views.py Sat Jan 15 19:18:52 2022 -0600
@@ -0,0 +1,38 @@
+import requests
+
+from allauth.socialaccount import app_settings
+from allauth.socialaccount.providers.oauth2.views import (
+ OAuth2Adapter,
+ OAuth2CallbackView,
+ OAuth2LoginView,
+)
+from .provider import JetbrainsHubProvider
+
+
+class JetbrainsHubAuth2Adapter(OAuth2Adapter):
+ provider_id = JetbrainsHubProvider.id
+
+ settings = app_settings.PROVIDERS.get(provider_id, {})
+ server = settings.get("SERVER")
+
+ # remove a trailing slash from the server.
+ if server[-1] == '/':
+ server = server[:-1]
+
+ authorize_url = f'{server}/api/rest/oauth2/auth'
+ access_token_url = f'{server}/api/rest/oauth2/token'
+ profile_url = f'{server}/api/rest/oauth2/userinfo'
+
+ def complete_login(self, request, app, token, **kwargs):
+ headers = {'Authorization': 'Bearer {0}'.format(token.token)}
+ resp = requests.get(self.profile_url, headers=headers)
+ extra_data = resp.json()
+
+ return self.get_provider().sociallogin_from_response(
+ request,
+ extra_data,
+ )
+
+
+oauth2_login = OAuth2LoginView.adapter_view(JetbrainsHubAuth2Adapter)
+oauth2_callback = OAuth2CallbackView.adapter_view(JetbrainsHubAuth2Adapter)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/setup.py Sat Jan 15 19:18:52 2022 -0600
@@ -0,0 +1,17 @@
+from setuptools import setup, find_packages
+
+setup(
+ name = "django-allauth-jetbrains-hub",
+ version = "0.0.1",
+ author = "Gary Kramlich",
+ author_email = "grim@reaperworld.com",
+ description = "Jetbrains Hub OAuth2 provider for django-allauth",
+ url = "https://keep.imfreedom.org/grim/django-allauth-jetbrains-hub",
+ packages=find_packages(),
+ install_requires=['django-allauth>=0.34.0'],
+ classifiers = [
+ 'Programming Language :: Python',
+ 'Operating System :: OS Independent',
+ 'Framework :: Django',
+ ],
+)