-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement sso client for eventyay-talk to eventyay-ticket
- Loading branch information
Showing
14 changed files
with
446 additions
and
2 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
27 changes: 27 additions & 0 deletions
27
src/pretalx/common/management/commands/create_social_apps.py
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,27 @@ | ||
from django.core.management.base import BaseCommand | ||
from allauth.socialaccount.models import SocialApp | ||
from django.conf import settings | ||
from django.contrib.sites.models import Site | ||
|
||
class Command(BaseCommand): | ||
help = 'Create SocialApp entries for Eventyay-ticket Provider' | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument('--eventyay-ticket-client-id', type=str, help='Eventyay-Ticket Provider Client ID') | ||
parser.add_argument('--eventyay-ticket-secret', type=str, help='Eventyay-Ticket Provider Secret') | ||
|
||
def handle(self, *args, **options): | ||
site = Site.objects.get(pk=settings.SITE_ID) | ||
eventyay_ticket_client_id = options.get('eventyay-ticket-client-id') or input('Enter Eventyay-Ticket Provider Client ID: ') | ||
eventyay_ticket_secret = options.get('eventyay-ticket-secret') or input('Enter Eventyay-Ticket Provider Secret: ') | ||
|
||
if not SocialApp.objects.filter(provider='eventyay').exists(): | ||
custom_app = SocialApp.objects.create( | ||
provider='eventyay', | ||
name='Eventyay Ticket Provider', | ||
client_id=eventyay_ticket_client_id, | ||
secret=eventyay_ticket_secret, | ||
key='' | ||
) | ||
custom_app.sites.add(site) | ||
self.stdout.write(self.style.SUCCESS('Successfully created Eventyay-ticket Provider SocialApp')) |
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,9 @@ | ||
from django.apps import AppConfig | ||
|
||
class SSOProviderConfig(AppConfig): | ||
name = 'pretalx.sso_provider' | ||
|
||
def ready(self): | ||
from allauth.socialaccount import providers | ||
from .providers import EventyayProvider | ||
providers.registry.register(EventyayProvider) |
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.forms import SignupForm | ||
|
||
class CustomSignUpForm(SignupForm): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
# TODO add custom fields here |
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,83 @@ | ||
import requests | ||
|
||
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider | ||
from allauth.socialaccount.providers.base import AuthAction, ProviderAccount | ||
from allauth.socialaccount.app_settings import QUERY_EMAIL | ||
from allauth.account.models import EmailAddress | ||
from allauth.core.exceptions import ImmediateHttpResponse | ||
from allauth.socialaccount.helpers import render_authentication_error | ||
from django.conf import settings | ||
from django.urls import reverse | ||
|
||
from .views import EventyayTicketOAuth2Adapter | ||
|
||
|
||
class Scope(object): | ||
OPEN_ID = "openid" | ||
EMAIL = "email" | ||
PROFILE = "profile" | ||
|
||
|
||
class EventYayTicketAccount(ProviderAccount): | ||
|
||
def get_profile_url(self): | ||
return self.account.extra_data.get("link") | ||
|
||
def get_avatar_url(self): | ||
return self.account.extra_data.get("picture") | ||
|
||
def to_str(self): | ||
dflt = super(GoogleAccount, self).to_str() | ||
return self.account.extra_data.get("name", dflt) | ||
|
||
|
||
class EventyayProvider(OAuth2Provider): | ||
id = 'eventyay' | ||
name = 'Eventyay' | ||
account_class = EventYayTicketAccount | ||
oauth2_adapter_class = EventyayTicketOAuth2Adapter | ||
|
||
def get_openid_config(self): | ||
try: | ||
response = requests.get(settings.EVENTYAY_TICKET_SSO_WELL_KNOW_URL | ||
.format(org=self.request.session.get('org'))) | ||
response.raise_for_status() | ||
except: | ||
raise ImmediateHttpResponse( | ||
render_authentication_error(self.request, | ||
'Error happened when trying get configurations from Eventyay-ticket')) | ||
return response.json() | ||
|
||
def get_default_scope(self): | ||
scope = [Scope.PROFILE] | ||
scope.append(Scope.EMAIL) | ||
scope.append(Scope.OPEN_ID) | ||
return scope | ||
|
||
def extract_uid(self, data): | ||
if "sub" in data: | ||
return data["sub"] | ||
return data["id"] | ||
|
||
def extract_common_fields(self, data): | ||
return dict(email=data.get('email'), | ||
username=data.get('name')) | ||
|
||
def extract_email_addresses(self, data): | ||
ret = [] | ||
email = data.get("email") | ||
if email: | ||
verified = bool(data.get("email_verified") or data.get("verified_email")) | ||
ret.append(EmailAddress(email=email, verified=verified, primary=True)) | ||
return ret | ||
|
||
def get_login_url(self, request, **kwargs): | ||
current_event = request.event | ||
request.session['org'] = current_event.organiser.slug | ||
url = reverse(self.id + "_login") | ||
if kwargs: | ||
url = url + "?" + urlencode(kwargs) | ||
return url | ||
|
||
|
||
provider_classes = [EventyayProvider] |
18 changes: 18 additions & 0 deletions
18
src/pretalx/sso_provider/templates/socialaccount/authentication_error.html
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,18 @@ | ||
{% extends "./base.html" %} | ||
{% load bootstrap4 %} | ||
{% load compress %} | ||
{% load i18n %} | ||
{% load static %} | ||
{% load allauth %} | ||
|
||
{% block head_title %} | ||
{% trans "Third-Party Login Failure" %} | ||
{% endblock head_title %} | ||
{% block content %} | ||
{% element h1 %} | ||
{% trans "Eventyay-ticket Login Failure" %} | ||
{% endelement %} | ||
{% element p %} | ||
{% trans "An error occurred while attempting to login via your Eventyay-ticket account." %} | ||
{% endelement %} | ||
{% endblock content %} |
Oops, something went wrong.