| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright (C) 2012-2023 by the Free Software Foundation, Inc. |
| 3 |
# |
| 4 |
# This file is part of Django-Mailman. |
| 5 |
# |
| 6 |
# HyperKitty is free software: you can redistribute it and/or modify it under |
| 7 |
# the terms of the GNU General Public License as published by the Free |
| 8 |
# Software Foundation, either version 3 of the License, or (at your option) |
| 9 |
# any later version. |
| 10 |
# |
| 11 |
# HyperKitty is distributed in the hope that it will be useful, but WITHOUT |
| 12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 |
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 14 |
# more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License along with |
| 17 |
# Django-Mailman. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
# |
| 19 |
# Author: Aurelien Bompard <[email protected]> |
| 20 |
# |
| 21 |
|
| 22 |
from django.http import HttpResponseRedirect |
| 23 |
from django.shortcuts import render |
| 24 |
from django.urls import reverse |
| 25 |
from django.utils.decorators import method_decorator |
| 26 |
from django.views.decorators.csrf import csrf_exempt |
| 27 |
from django.views.generic import View |
| 28 |
|
| 29 |
from allauth.socialaccount import providers |
| 30 |
from allauth.socialaccount.app_settings import QUERY_EMAIL |
| 31 |
from allauth.socialaccount.helpers import ( |
| 32 |
complete_social_login, render_authentication_error) |
| 33 |
from allauth.socialaccount.models import SocialLogin |
| 34 |
from allauth.socialaccount.providers.base import AuthError |
| 35 |
from allauth.socialaccount.providers.openid.forms import LoginForm |
| 36 |
from allauth.socialaccount.providers.openid.utils import ( |
| 37 |
AXAttributes, SRegFields) |
| 38 |
from allauth.socialaccount.providers.openid.views import _openid_consumer |
| 39 |
from openid.consumer import consumer |
| 40 |
from openid.consumer.discover import DiscoveryFailure |
| 41 |
from openid.extensions.ax import AttrInfo, FetchRequest |
| 42 |
from openid.extensions.sreg import SRegRequest |
| 43 |
|
| 44 |
from .provider import FedoraProvider |
| 45 |
|
| 46 |
|
| 47 |
class LoginView(View): |
| 48 |
|
| 49 |
form_class = LoginForm |
| 50 |
template_name = 'openid/login.html' |
| 51 |
provider = FedoraProvider |
| 52 |
callback_view = 'fedora_callback' |
| 53 |
|
| 54 |
def get(self, request, *args, **kwargs): |
| 55 |
if 'openid' in request.GET or self.provider.endpoint: |
| 56 |
return self.post(request, *args, **kwargs) |
| 57 |
form = LoginForm(initial={'next': request.GET.get('next'), |
| 58 |
'process': request.GET.get('process')}) |
| 59 |
return render(request, self.template_name, {'form': form}) |
| 60 |
|
| 61 |
def post(self, request, *args, **kwargs): |
| 62 |
data = dict(list(request.GET.items()) + list(request.POST.items())) |
| 63 |
if self.provider.endpoint: |
| 64 |
data['openid'] = self.provider.endpoint |
| 65 |
form = LoginForm(data) |
| 66 |
if form.is_valid(): |
| 67 |
client = _openid_consumer( |
| 68 |
request, self.provider, self.provider.endpoint) |
| 69 |
try: |
| 70 |
auth_request = client.begin(form.cleaned_data['openid']) |
| 71 |
if QUERY_EMAIL: |
| 72 |
sreg = SRegRequest() |
| 73 |
for name in SRegFields: |
| 74 |
sreg.requestField(field_name=name, |
| 75 |
required=True) |
| 76 |
auth_request.addExtension(sreg) |
| 77 |
ax = FetchRequest() |
| 78 |
for name in AXAttributes: |
| 79 |
ax.add(AttrInfo(name, |
| 80 |
required=True)) |
| 81 |
auth_request.addExtension(ax) |
| 82 |
callback_url = reverse(self.callback_view) |
| 83 |
SocialLogin.stash_state(request) |
| 84 |
redirect_url = auth_request.redirectURL( |
| 85 |
request.build_absolute_uri('/'), |
| 86 |
request.build_absolute_uri(callback_url)) |
| 87 |
return HttpResponseRedirect(redirect_url) |
| 88 |
# UnicodeDecodeError: |
| 89 |
# see https://github.com/necaris/python3-openid/issues/1 |
| 90 |
except (UnicodeDecodeError, DiscoveryFailure) as e: |
| 91 |
if request.method == 'POST': |
| 92 |
form._errors["openid"] = form.error_class([e]) |
| 93 |
else: |
| 94 |
return render_authentication_error( |
| 95 |
request, self.provider.id, exception=e) |
| 96 |
return render(request, self.template_name, {'form': form}) |
| 97 |
|
| 98 |
|
| 99 |
class CallbackView(View): |
| 100 |
|
| 101 |
provider = FedoraProvider |
| 102 |
|
| 103 |
@method_decorator(csrf_exempt) |
| 104 |
def dispatch(self, request, *args, **kwargs): |
| 105 |
client = _openid_consumer(request) |
| 106 |
response = client.complete( |
| 107 |
dict(list(request.GET.items()) + list(request.POST.items())), |
| 108 |
request.build_absolute_uri(request.path)) |
| 109 |
if response.status == consumer.SUCCESS: |
| 110 |
login = providers.registry \ |
| 111 |
.by_id(self.provider.id) \ |
| 112 |
.sociallogin_from_response(request, response) |
| 113 |
login.state = SocialLogin.unstash_state(request) |
| 114 |
ret = complete_social_login(request, login) |
| 115 |
else: |
| 116 |
if response.status == consumer.CANCEL: |
| 117 |
error = AuthError.CANCELLED |
| 118 |
else: |
| 119 |
error = AuthError.UNKNOWN |
| 120 |
ret = render_authentication_error( |
| 121 |
request, |
| 122 |
self.provider.id, |
| 123 |
error=error) |
| 124 |
return ret |