Skip to content

Commit

Permalink
Added support for CAS ESN Accounts login #27
Browse files Browse the repository at this point in the history
  • Loading branch information
thejoeejoee committed Feb 8, 2022
1 parent 73693d3 commit ab320f1
Show file tree
Hide file tree
Showing 15 changed files with 231 additions and 19 deletions.
5 changes: 3 additions & 2 deletions fiesta/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
FROM python:3.9.7-alpine as builder

RUN apk add --no-cache --virtual build-deps \
build-base gcc python3-dev musl-dev gettext-dev libffi-dev g++ postgresql-dev mariadb-dev \
musl-dev rust cargo patchelf
build-base gcc python3-dev musl-dev gettext-dev libffi-dev g++ \
postgresql-dev mariadb-dev libxml2-dev libxslt-dev \
musl-dev rust cargo patchelf git

COPY pyproject.toml poetry.lock ./

Expand Down
26 changes: 26 additions & 0 deletions fiesta/apps/accounts/templates/accounts/profile.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{% extends "fiesta/base.html" %}

{% block main %}
<ul class="list-disc">
{% for sa in request.user.socialaccount_set.all %}
<li>
{% if sa.provider == 'esnaccounts' %}
<ul class="list-disc">
{% for role in sa.extra_data.roles %}
<li>role: {{ role }}</li>
{% endfor %}

<li>joined: {{ sa.date_joined }}
<li>email: {{ sa.extra_data.mail }}
<li>uid: {{ sa.extra_data.uid }}
<li><img src="{{ sa.extra_data.picture }}" width="60px">
<li>data: {{ sa.extra_data }}
</ul>
{% else %}
{{ sa.provider }}
{% endif %}
</li>
{% endfor %}
</ul>

{% endblock %}
4 changes: 3 additions & 1 deletion fiesta/apps/accounts/urls.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from django.urls import path

# Define your urls here
from django.views.generic import TemplateView

from apps.accounts.views.profile import ProfileView

urlpatterns = [
path(
"auth/login",
TemplateView.as_view(template_name="accounts/auth/login.html"),
name="login",
),
path("profile", ProfileView.as_view()),
path("", TemplateView.as_view(template_name="accounts/index.html")),
]
Empty file.
9 changes: 9 additions & 0 deletions fiesta/apps/accounts/views/profile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.views.generic import TemplateView
from django.utils.translation import gettext_lazy as _

from apps.utils.breadcrumbs import with_breadcrumb


@with_breadcrumb(_('My Profile'))
class ProfileView(TemplateView):
template_name = 'accounts/profile.html'
1 change: 1 addition & 0 deletions fiesta/apps/esnaccounts/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "0.5.0"
6 changes: 6 additions & 0 deletions fiesta/apps/esnaccounts/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class ESNAccountsConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "apps.esnaccounts"
17 changes: 17 additions & 0 deletions fiesta/apps/esnaccounts/provider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from allauth.socialaccount.providers.base import ProviderAccount
from allauth_cas.providers import CASProvider


class ESNAccountsAccount(ProviderAccount):
pass


class ESNAccountsProvider(CASProvider):
id = "esnaccounts"
name = "ESN Accounts"
account_class = ESNAccountsAccount

# TODO: fix extract_common_fields to load data from extra_data


provider_classes = [ESNAccountsProvider]
5 changes: 5 additions & 0 deletions fiesta/apps/esnaccounts/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from allauth_cas.urls import default_urlpatterns

from .provider import ESNAccountsProvider

urlpatterns = default_urlpatterns(ESNAccountsProvider)
16 changes: 16 additions & 0 deletions fiesta/apps/esnaccounts/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from allauth_cas.views import CASAdapter, CASCallbackView, CASLoginView, CASLogoutView

from .provider import ESNAccountsProvider


class ESNAccountsAdapter(CASAdapter):
provider_id = ESNAccountsProvider.id
url = "https://accounts.esn.org/cas/"
version = 3


login = CASLoginView.adapter_view(ESNAccountsAdapter)

callback = CASCallbackView.adapter_view(ESNAccountsAdapter)

logout = CASLogoutView.adapter_view(ESNAccountsAdapter)
6 changes: 5 additions & 1 deletion fiesta/apps/utils/templatetags/breadcrumbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django import template
from django.template.loader import render_to_string
from django.views import View
from django.utils.translation import gettext_lazy as _

from apps.plugins.middleware.plugin import HttpRequest

Expand Down Expand Up @@ -34,7 +35,10 @@ def breadcrumbs(context: dict):
None,
[
# TODO: slash is not always the home page?
BreadcrumbTitle(req.membership.section, "/"),
BreadcrumbTitle(
req.membership.section if req.membership else _('Home'),
"/"
),
BreadcrumbTitle(apps.title, f"/{apps.url_prefix}")
if (plugin := req.plugin) and (apps := plugin.app_config)
else None,
Expand Down
16 changes: 11 additions & 5 deletions fiesta/fiesta/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,22 @@
"webpack_loader",
"django_htmx",
# Fiesta apps
"apps.plugins.apps.PluginsConfig",
"apps.accounts.apps.AccountsConfig",
"apps.universities.apps.UniversitiesConfig",
"apps.sections.apps.SectionsConfig",
"apps.utils.apps.UtilsConfig",
"apps.esnaccounts", # cannot have full config Path, since allauth/socialaccount/providers/__init__.py:38 sucks
"apps.esncards.apps.ESNcardsConfig",
"apps.fiestaforms.apps.FiestaformsConfig",
"apps.plugins.apps.PluginsConfig",
"apps.sections.apps.SectionsConfig",
"apps.universities.apps.UniversitiesConfig",
"apps.utils.apps.UtilsConfig",
# Debugs
"django_extensions",
# django-allauth
"allauth",
"allauth.account",
"allauth.socialaccount",
"allauth.socialaccount.providers.facebook",
"allauth_cas",
]

MIDDLEWARE = [
Expand Down Expand Up @@ -164,6 +166,8 @@

SITE_ID = 1

SECURE_PROXY_SSL_HEADER = "HTTP_X_FORWARDED_SSL", "on"

SOCIALACCOUNT_PROVIDERS = {
"facebook": {
"METHOD": "oauth2",
Expand All @@ -184,7 +188,8 @@
"LOCALE_FUNC": lambda request: "en",
"VERIFIED_EMAIL": False,
"VERSION": "v12.0",
}
},
"esnaccounts": {},
}

ACCOUNT_AUTHENTICATED_LOGIN_REDIRECTS = False
Expand All @@ -197,6 +202,7 @@
ACCOUNT_DEFAULT_HTTP_PROTOCOL = "https"

LOGIN_URL = "/accounts/auth/login"
LOGIN_REDIRECT_URL = "/accounts/profile"

# Internationalization
# https://docs.djangoproject.com/en/4.0/topics/i18n/
Expand Down
File renamed without changes.
Loading

0 comments on commit ab320f1

Please sign in to comment.