Skip to content

Commit

Permalink
update bornhack_allauth_provider to work with allauth >= 0.62.0
Browse files Browse the repository at this point in the history
  • Loading branch information
tykling committed May 26, 2024
1 parent 494f81f commit 654d91f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ classifiers = [
dependencies = [
"dealer==2.1.0",
"Django==5.0.6",
"django-allauth==0.63.1",
"django-allauth[socialaccount]==0.63.2",
"django-bootstrap5==24.2",
"django-cors-headers==4.3.1",
"django-filter==24.2",
Expand Down
36 changes: 35 additions & 1 deletion src/bornhack_allauth_provider/adapters.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
"""The BornHackSocialAccountAdapter takes care of populating fields in the BMA User model from the BornHack profile."""

from allauth.account.utils import user_field
from allauth.account.utils import user_username
from allauth.socialaccount.adapter import DefaultSocialAccountAdapter
from allauth.socialaccount.models import SocialApp
from allauth.socialaccount.models import SocialLogin
from allauth.socialaccount.providers.oauth2.client import OAuth2Client
from allauth.utils import build_absolute_uri
from django.conf import settings
from django.contrib.auth.models import Group
from django.forms import Form
from django.http import HttpRequest
from django.urls import reverse
from users.models import User

from .client import BornHackOAuth2Client


class BornHackSocialAccountAdapter(DefaultSocialAccountAdapter):
"""The allauth SocialAccountAdapter for BornHack populates the BMA User with data from the BornHack profile."""

provider_id = "bornhack"
client_class = BornHackOAuth2Client
access_token_method = "POST" # noqa: S105
access_token_url = f"{settings.OAUTH_SERVER_BASEURL}/o/token/"
authorize_url = f"{settings.OAUTH_SERVER_BASEURL}/o/authorize/"
scope_delimiter = ","
basic_auth = False
headers: dict[str, str] | None = None

def is_open_for_signup(self, request: HttpRequest, sociallogin: SocialLogin) -> bool:
"""Always open for business."""
return True
Expand All @@ -38,3 +52,23 @@ def save_user(self, request: HttpRequest, sociallogin: SocialLogin, form: Form |
curators, created = Group.objects.get_or_create(name=settings.BMA_CURATOR_GROUP_NAME)
curators.user_set.add(user)
return user # type: ignore[no-any-return]

def get_client(self, request: HttpRequest, app: SocialApp) -> OAuth2Client:
"""Generate callback url, initialise and return client."""
callback_url = self.get_callback_url(request, app)
return self.client_class(
self.request,
app.client_id,
app.secret,
self.access_token_method,
self.access_token_url,
callback_url,
scope_delimiter=self.scope_delimiter,
headers=self.headers,
basic_auth=self.basic_auth,
)

def get_callback_url(self, request: HttpRequest, app: SocialApp) -> str:
"""Callback url."""
callback_url = reverse(self.provider_id + "_callback")
return build_absolute_uri(request, callback_url, "https" if request.is_secure() else "http") # type: ignore[no-any-return]
6 changes: 6 additions & 0 deletions src/bornhack_allauth_provider/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""Client class used in BornHackSocialAccountAdapter."""
from allauth.socialaccount.providers.oauth2.client import OAuth2Client


class BornHackOAuth2Client(OAuth2Client):
"""Client class used in BornHackSocialAccountAdapter."""
6 changes: 4 additions & 2 deletions src/bornhack_allauth_provider/provider.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""This module contains the BornHackProvider class and BornHackAccount classes."""
from allauth.socialaccount import providers
from allauth.socialaccount.providers.base import ProviderAccount
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider

from .adapters import BornHackSocialAccountAdapter


class BornHackAccount(ProviderAccount):
"""The BornHackAccount class only defines the to_str() method to find the username."""
Expand All @@ -18,6 +19,7 @@ class BornHackProvider(OAuth2Provider):
id = "bornhack"
name = "BornHack"
account_class = BornHackAccount
oauth2_adapter_class = BornHackSocialAccountAdapter

def extract_uid(self, data: dict[str, dict[str, str]]) -> str:
"""Get user_id from the user object."""
Expand All @@ -37,4 +39,4 @@ def get_default_scope(self) -> list[str]:
return ["profile:read"]


providers.registry.register(BornHackProvider)
provider_classes = [BornHackProvider]
5 changes: 3 additions & 2 deletions src/bornhack_allauth_provider/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
from django.conf import settings
from django.http import HttpRequest

from .provider import BornHackProvider
from .client import BornHackOAuth2Client


class BornHackViewAdapter(OAuth2Adapter):
"""View adapter class for the oauth2_login and oauth2_callback views."""

provider_id = BornHackProvider.id
provider_id = "bornhack"
client_class = BornHackOAuth2Client

# Accessed by Django
access_token_url = f"{settings.OAUTH_SERVER_BASEURL}/o/token/"
Expand Down

0 comments on commit 654d91f

Please sign in to comment.