authentic2-auth-fedict/src/authentic2_auth_fedict/views.py

75 lines
3.0 KiB
Python

# authentic2_auth_fedict - Fedict authentication for Authentic
# Copyright (C) 2016 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import random
from django.core import signing
from django.urls import reverse
from django.db import transaction
from django.http import HttpResponseRedirect
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import View
from authentic2 import utils as a2_utils
import mellon.utils
import mellon.views
class RegistrationView(View):
def get(self, request, *args, **kwargs):
data = utils.get_mapped_attributes_flat(request)
data['no_password'] = True
data['confirm_data'] = not app_settings.auto_register
redirect_to = request.REQUEST.get(REDIRECT_FIELD_NAME, '')
if not is_safe_url(url=redirect_to, host=request.get_host()):
redirect_to = resolve_url(settings.LOGIN_REDIRECT_URL)
if not 'email' in data:
data[REDIRECT_FIELD_NAME] = redirect_to
return HttpResponseRedirect("{}?token={}".format(reverse('registration_register'),
signing.dumps(data)))
data['valid_email'] = False
activation_url = \
a2_utils.build_activation_url(request,
next_url=redirect_to,
**data)
return HttpResponseRedirect(activation_url)
registration = RegistrationView.as_view()
class LoginView(mellon.views.LoginView):
def authenticate(self, request, login, attributes):
idp = mellon.utils.get_idp(attributes['issuer'])
adapter = mellon.utils.get_adapters(idp)[0]
user = adapter.lookup_user(idp, attributes)
if not user.email:
adapter.provision_attribute(user, idp, attributes)
user.is_active = False
user.save()
data = {}
data['email'] = 'adresse@email.invalid#%s' % random.randint(0, 10000000)
data['confirm_data'] = True
data['valid_email'] = False
data['skip_email_check'] = True
data['user_id'] = user.id
return HttpResponseRedirect(a2_utils.build_activation_url(request, **data))
user.is_active = True
user.save()
return super(LoginView, self).authenticate(request, login, attributes)
login = transaction.non_atomic_requests(csrf_exempt(LoginView.as_view()))