Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't Merge! Proof of Concept: Using the Boost Site as an OAuth Provider #897

Draft
wants to merge 6 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"mailing_list",
"news",
"core",
"providers",
]

AUTH_USER_MODEL = "users.User"
Expand Down Expand Up @@ -313,6 +314,7 @@
ACCOUNT_AUTHENTICATION_METHOD = "email"
SOCIALACCOUNT_QUERY_EMAIL = True
ACCOUNT_UNIQUE_EMAIL = True
OAUTH_SERVER_BASEURL = "https://www.stage.boost.cppalliance.org"

# Allow us to override some of allauth's forms
ACCOUNT_FORMS = {
Expand Down
1 change: 1 addition & 0 deletions config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
path("homepage-beta/", HomepageBetaView.as_view(), name="home-beta"),
path("admin/", admin.site.urls),
path("oauth2/", include("oauth2_provider.urls", namespace="oauth2_provider")),
path("accounts/provider/", include("providers.urls")),
path("feed/downloads.rss", RSSVersionFeed(), name="downloads_feed_rss"),
path("feed/downloads.atom", AtomVersionFeed(), name="downloads_feed_atom"),
path("feed/news.rss", RSSNewsFeed(), name="news_feed_rss"),
Expand Down
Empty file added providers/__init__.py
Empty file.
1 change: 1 addition & 0 deletions providers/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Register your models here.
6 changes: 6 additions & 0 deletions providers/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class ProvidersConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "providers"
Empty file.
1 change: 1 addition & 0 deletions providers/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Create your models here.
28 changes: 28 additions & 0 deletions providers/provider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from allauth.socialaccount import providers
from allauth.socialaccount.providers.base import ProviderAccount
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider


class BoostAccount(ProviderAccount):
pass


class BoostProvider(OAuth2Provider):
id = "provider"
name = "Boost OAuth2 Provider"
account_class = BoostAccount

def extract_uid(self, data):
return str(data["id"])

def extract_common_fields(self, data):
return {
dict(
email="[email protected]",
first_name="Tester",
last_name="Testerson",
)
}


providers.registry.register(BoostProvider)
1 change: 1 addition & 0 deletions providers/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Create your tests here.
4 changes: 4 additions & 0 deletions providers/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from allauth.socialaccount.providers.oauth2.urls import default_urlpatterns
from .provider import BoostProvider

urlpatterns = default_urlpatterns(BoostProvider)
38 changes: 38 additions & 0 deletions providers/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import requests
from allauth.socialaccount.providers.oauth2.views import (
OAuth2Adapter,
OAuth2LoginView,
OAuth2CallbackView,
)
from .provider import BoostProvider
from django.conf import settings


class CustomAdapter(OAuth2Adapter):
provider_id = BoostProvider.id

access_token_url = f"{settings.OAUTH_SERVER_BASEURL}/oauth2/token/"
profile_url = f"{settings.OAUTH_SERVER_BASEURL}/api/v1/users/me/"

authorize_url = f"{settings.OAUTH_SERVER_BASEURL}/oauth2/authorize/"

def dispatch(self, request, *args, **kwargs):
print("In the dispatch")
return super().dispatch(request, *args, **kwargs)

def complete_login(self, request, app, token, **kwargs):
print("in the complete login method")
headers = {
"Authorization": f"Bearer {token.token}",
"Accept": "application/json",
}
kwargs["response"]["email"]
resp = requests.get(self.profile_url, headers=headers)
resp.raise_for_status()
print(resp.status_code)
extra_data = resp.json()
return self.get_provider().sociallogin_from_response(request, extra_data)


oauth2_login = OAuth2LoginView.adapter_view(CustomAdapter)
oauth2_callback = OAuth2CallbackView.adapter_view(CustomAdapter)
10 changes: 10 additions & 0 deletions templates/socialaccount/snippets/provider_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,14 @@
Use {{provider.name}}
</a>
{% endif %}
{% if provider.name == "Boost OAuth2 Provider" %}
<a title="{{provider.name}}"
class="relative w-2/3 mx-auto block px-8 py-3 text-base font-medium rounded-md border border-orange !text-white hover:!text-white bg-orange hover:bg-orange/80 dark:bg-slate dark:hover:bg-charcoal hover:drop-shadow-md md:py-4 md:text-lg md:px-10 {{provider.id}}"
href="{% provider_login_url provider.id process=process scope=scope auth_params=auth_params %}"
>
<span x-cloak class="absolute right-1 top-1 text-xs bg-white text-slate rounded p-1" x-show='loginMethod === "\"Boost\""'>Last Log in</span>
<i class="fab fa-google"></i>
Use {{provider.name}}
</a>
{% endif %}
{% endfor %}
Loading