user.py 28.5 KB
Newer Older
Őry Máté committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# Copyright 2014 Budapest University of Technology and Economics (BME IK)
#
# This file is part of CIRCLE Cloud.
#
# CIRCLE is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# CIRCLE 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 General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with CIRCLE.  If not, see <http://www.gnu.org/licenses/>.
from __future__ import unicode_literals, absolute_import

import json
import logging

22 23
import pyotp

Őry Máté committed
24 25
from django.conf import settings
from django.contrib import messages
26
from django.contrib.auth import login, authenticate
Őry Máté committed
27 28 29
from django.contrib.auth.models import User, Group
from django.contrib.messages.views import SuccessMessageMixin
from django.core import signing
30
from django.core.exceptions import PermissionDenied, SuspiciousOperation
Őry Máté committed
31
from django.core.urlresolvers import reverse, reverse_lazy
32
from django.core.paginator import Paginator, InvalidPage
33
from django.db.models import Q
Őry Máté committed
34 35
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.shortcuts import redirect, get_object_or_404
36
from django.templatetags.static import static
Őry Máté committed
37 38 39
from django.utils.translation import ugettext as _
from django.views.decorators.http import require_POST
from django.views.generic import (
40
    TemplateView, View, UpdateView, CreateView, FormView
Őry Máté committed
41 42 43 44 45
)
from django_sshkey.models import UserKey

from braces.views import LoginRequiredMixin, PermissionRequiredMixin

46 47
from django_tables2 import SingleTableView

Őry Máté committed
48 49 50 51 52
from vm.models import Instance, InstanceTemplate

from ..forms import (
    CircleAuthenticationForm, MyProfileForm, UserCreationForm, UnsubscribeForm,
    UserKeyForm, CirclePasswordChangeForm, ConnectCommandForm,
53
    UserListSearchForm, UserEditForm, TwoFactorForm, TwoFactorConfirmationForm,
Őry Máté committed
54
)
55
from ..models import Profile, GroupProfile, ConnectCommand
56 57 58
from ..tables import (
    UserKeyListTable, ConnectCommandListTable, UserListTable,
)
Őry Máté committed
59

60
from .util import saml_available, DeleteViewBase, LoginView
Őry Máté committed
61 62 63 64 65


logger = logging.getLogger(__name__)


66 67 68 69 70 71 72 73
def set_session_expiry(request, user):
    if user.is_superuser:
        messages.info(request, _("You've logged in with an administrator "
                                 "account, your session will expire when "
                                 "the web browser is closed."))
        request.session.set_expiry(0)


Őry Máté committed
74 75 76 77 78 79 80 81 82 83 84
class NotificationView(LoginRequiredMixin, TemplateView):

    def get_template_names(self):
        if self.request.is_ajax():
            return ['dashboard/_notifications-timeline.html']
        else:
            return ['dashboard/notifications.html']

    def get_context_data(self, *args, **kwargs):
        context = super(NotificationView, self).get_context_data(
            *args, **kwargs)
85 86 87 88 89 90 91 92 93 94 95 96
        paginate_by = 10 if self.request.is_ajax() else 25
        page = self.request.GET.get("page", 1)

        notifications = self.request.user.notification_set.all()
        paginator = Paginator(notifications, paginate_by)
        try:
            current_page = paginator.page(page)
        except InvalidPage:
            current_page = paginator.page(1)

        context['page'] = current_page
        context['paginator'] = paginator
Őry Máté committed
97 98 99 100 101 102 103 104 105 106 107
        return context

    def get(self, *args, **kwargs):
        response = super(NotificationView, self).get(*args, **kwargs)
        un = self.request.user.notification_set.filter(status="new")
        for u in un:
            u.status = "read"
            u.save()
        return response


108 109 110 111 112 113 114 115 116 117 118 119 120 121
class CircleLoginView(LoginView):
    form_class = CircleAuthenticationForm

    def get_context_data(self, **kwargs):
        ctx = super(CircleLoginView, self).get_context_data(**kwargs)
        ctx.update({
            'saml2': saml_available,
            'og_image': (settings.DJANGO_URL.rstrip("/") +
                         static("dashboard/img/og.png"))
        })
        return ctx

    def form_valid(self, form):
        user = form.get_user()
122
        if hasattr(user, "profile") and user.profile.two_factor_secret:
123 124 125 126 127 128 129
            self.request.session['two-fa-user'] = user.pk
            self.request.session['two-fa-redirect'] = self.get_success_url()
            self.request.session['login-type'] = "password"
            return redirect(reverse("two-factor-login"))
        else:
            response = super(CircleLoginView, self).form_valid(form)
            set_language_cookie(self.request, response)
130
            set_session_expiry(self.request, user)
131
            return response
Őry Máté committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222


class TokenLogin(View):

    token_max_age = 120  # seconds

    @classmethod
    def get_salt(cls):
        return unicode(cls)

    @classmethod
    def get_token(cls, user, sudoer):
        return signing.dumps((sudoer.pk, user.pk),
                             salt=cls.get_salt(), compress=True)

    @classmethod
    def get_token_url(cls, user, sudoer):
        key = cls.get_token(user, sudoer)
        return reverse("dashboard.views.token-login", args=(key, ))

    def get(self, request, token, *args, **kwargs):
        try:
            data = signing.loads(token, salt=self.get_salt(),
                                 max_age=self.token_max_age)
            logger.debug('TokenLogin token data: %s', unicode(data))
            sudoer, user = data
            logger.debug('Extracted TokenLogin data: sudoer: %s, user: %s',
                         unicode(sudoer), unicode(user))
        except (signing.BadSignature, ValueError, TypeError) as e:
            logger.warning('Tried invalid TokenLogin token. '
                           'Token: %s, user: %s. %s',
                           token, unicode(self.request.user), unicode(e))
            raise SuspiciousOperation()
        sudoer = User.objects.get(pk=sudoer)
        if not sudoer.is_superuser:
            raise PermissionDenied()
        user = User.objects.get(pk=user)
        user.backend = 'django.contrib.auth.backends.ModelBackend'
        logger.warning('%s %d logged in as user %s %d',
                       unicode(sudoer), sudoer.pk, unicode(user), user.pk)
        login(request, user)
        messages.info(request, _("Logged in as user %s.") % unicode(user))
        return redirect("/")


class MyPreferencesView(UpdateView):
    model = Profile

    def get_context_data(self, *args, **kwargs):
        context = super(MyPreferencesView, self).get_context_data(*args,
                                                                  **kwargs)
        context['forms'] = {
            'change_password': CirclePasswordChangeForm(
                user=self.request.user),
            'change_language': MyProfileForm(instance=self.get_object()),
        }
        key_table = UserKeyListTable(
            UserKey.objects.filter(user=self.request.user),
            request=self.request)
        key_table.page = None
        context['userkey_table'] = key_table
        cmd_table = ConnectCommandListTable(
            self.request.user.command_set.all(),
            request=self.request)
        cmd_table.page = None
        context['connectcommand_table'] = cmd_table
        return context

    def get_object(self, queryset=None):
        if self.request.user.is_anonymous():
            raise PermissionDenied()
        try:
            return self.request.user.profile
        except Profile.DoesNotExist:
            raise Http404(_("You don't have a profile."))

    def post(self, request, *args, **kwargs):
        self.ojbect = self.get_object()
        redirect_response = HttpResponseRedirect(
            reverse("dashboard.views.profile-preferences"))
        if "preferred_language" in request.POST:
            form = MyProfileForm(request.POST, instance=self.get_object())
            if form.is_valid():
                lang = form.cleaned_data.get("preferred_language")
                set_language_cookie(self.request, redirect_response, lang)
                form.save()
        else:
            form = CirclePasswordChangeForm(user=request.user,
                                            data=request.POST)
            if form.is_valid():
                form.save()
223 224
                messages.success(self.request,
                                 _("Password successfully changed."))
Őry Máté committed
225 226 227 228 229 230 231 232 233

        if form.is_valid():
            return redirect_response
        else:
            return self.get(request, form=form, *args, **kwargs)

    def get(self, request, form=None, *args, **kwargs):
        # if this is not here, it won't work
        self.object = self.get_object()
234
        context = self.get_context_data(form=form, *args, **kwargs)
Őry Máté committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
        if form is not None:
            # a little cheating, users can't post invalid
            # language selection forms (without modifying the HTML)
            context['forms']['change_password'] = form
        return self.render_to_response(context)


class UnsubscribeFormView(SuccessMessageMixin, UpdateView):
    model = Profile
    form_class = UnsubscribeForm
    template_name = "dashboard/unsubscribe.html"
    success_message = _("Successfully modified subscription.")

    def get_success_url(self):
        if self.request.user.is_authenticated():
            return super(UnsubscribeFormView, self).get_success_url()
        else:
            return self.request.path

    @classmethod
    def get_salt(cls):
        return unicode(cls)

    @classmethod
    def get_token(cls, user):
        return signing.dumps(user.pk, salt=cls.get_salt(), compress=True)

    def get_object(self, queryset=None):
        key = self.kwargs['token']
        try:
            pk = signing.loads(key, salt=self.get_salt(), max_age=48 * 3600)
        except signing.SignatureExpired:
            raise
        except (signing.BadSignature, ValueError, TypeError) as e:
            logger.warning('Tried invalid token. Token: %s, user: %s. %s',
                           key, unicode(self.request.user), unicode(e))
            raise Http404
        else:
            return (queryset or self.get_queryset()).get(user_id=pk)

    def dispatch(self, request, *args, **kwargs):
        try:
            return super(UnsubscribeFormView, self).dispatch(
                request, *args, **kwargs)
        except signing.SignatureExpired:
            return redirect('dashboard.views.profile-preferences')


def set_language_cookie(request, response, lang=None):
    if lang is None:
        try:
            lang = request.user.profile.preferred_language
        except:
            return

    cname = getattr(settings, 'LANGUAGE_COOKIE_NAME', 'django_language')
    response.set_cookie(cname, lang, 365 * 86400)


class UserCreationView(LoginRequiredMixin, PermissionRequiredMixin,
                       CreateView):
    form_class = UserCreationForm
    model = User
    template_name = 'dashboard/user-create.html'
    permission_required = "auth.add_user"

301 302 303 304 305 306 307 308 309 310 311 312
    def get_template_names(self):
        return ['dashboard/nojs-wrapper.html']

    def get_context_data(self, *args, **kwargs):
        context = super(UserCreationView, self).get_context_data(*args,
                                                                 **kwargs)
        context.update({
            'template': self.template_name,
            'box_title': _('Create a User'),
        })
        return context

Bach Dániel committed
313
    def get_success_url(self):
314 315 316 317 318 319 320 321 322 323 324 325
        return reverse('dashboard.views.profile', args=[self.object.username])

    def get_form_kwargs(self):
        profiles = GroupProfile.get_objects_with_level(
            'owner', self.request.user)
        choices = Group.objects.filter(groupprofile__in=profiles)
        group_pk = self.request.GET.get('group_pk')
        if group_pk:
            try:
                default = choices.get(pk=group_pk)
            except (ValueError, Group.DoesNotExist):
                raise Http404()
Őry Máté committed
326
        else:
327 328 329 330 331
            default = None

        val = super(UserCreationView, self).get_form_kwargs()
        val.update({'choices': choices, 'default': default})
        return val
Őry Máté committed
332 333


334
class ProfileView(LoginRequiredMixin, SuccessMessageMixin, UpdateView):
Őry Máté committed
335 336 337 338
    template_name = "dashboard/profile.html"
    model = User
    slug_field = "username"
    slug_url_kwarg = "username"
339 340
    form_class = UserEditForm
    success_message = _("Successfully modified user.")
Őry Máté committed
341

342 343 344 345 346 347 348 349 350 351 352 353 354 355
    def get(self, *args, **kwargs):
        user = self.request.user
        target = self.get_object()

        # get the list of groups where the user is operator
        user_g_w_op = GroupProfile.get_objects_with_level("operator", user)
        # get the list of groups the "target" (the profile) is member of
        target_groups = GroupProfile.objects.filter(
            group__in=target.groups.all())
        intersection = set(user_g_w_op).intersection(target_groups)

        # if the intersection of the 2 lists is empty the logged in user
        # has no permission to check the target's profile
        # (except if the user want to see his own profile)
356
        if not intersection and target != user and not user.is_superuser:
357 358 359 360
            raise PermissionDenied

        return super(ProfileView, self).get(*args, **kwargs)

Őry Máté committed
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
    def get_context_data(self, **kwargs):
        context = super(ProfileView, self).get_context_data(**kwargs)
        user = self.get_object()
        context['profile'] = user
        context['avatar_url'] = user.profile.get_avatar_url()
        context['instances_owned'] = Instance.get_objects_with_level(
            "owner", user, disregard_superuser=True).filter(destroyed_at=None)
        context['instances_with_access'] = Instance.get_objects_with_level(
            "user", user, disregard_superuser=True
        ).filter(destroyed_at=None).exclude(pk__in=context['instances_owned'])

        group_profiles = GroupProfile.get_objects_with_level(
            "operator", self.request.user)
        groups = Group.objects.filter(groupprofile__in=group_profiles)
        context['groups'] = user.groups.filter(pk__in=groups)

        # permissions
        # show groups only if the user is superuser, or have access
        # to any of the groups the user belongs to
        context['perm_group_list'] = (
            self.request.user.is_superuser or len(context['groups']) > 0)
        context['perm_email'] = (
            context['perm_group_list'] or self.request.user == user)

        # filter the virtual machine list
        # if the logged in user is not superuser or not the user itself
        # filter the list so only those virtual machines are shown that are
        # originated from templates the logged in user is operator or higher
        if not (self.request.user.is_superuser or self.request.user == user):
            it = InstanceTemplate.get_objects_with_level("operator",
                                                         self.request.user)
            context['instances_owned'] = context['instances_owned'].filter(
                template__in=it)
            context['instances_with_access'] = context[
                'instances_with_access'].filter(template__in=it)
        if self.request.user.is_superuser:
            context['login_token'] = TokenLogin.get_token_url(
                user, self.request.user)
        return context

401 402 403 404 405 406 407 408 409
    def post(self, request, *args, **kwargs):
        if not request.user.has_perm('auth.change_user'):
            raise PermissionDenied()
        return super(ProfileView, self).post(self, request, *args, **kwargs)

    def get_success_url(self):
        return reverse('dashboard.views.profile',
                       kwargs=self.kwargs)

Őry Máté committed
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450

@require_POST
def toggle_use_gravatar(request, **kwargs):
    user = get_object_or_404(User, username=kwargs['username'])
    if not request.user == user:
        raise PermissionDenied()

    profile = user.profile
    profile.use_gravatar = not profile.use_gravatar
    profile.save()

    new_avatar_url = user.profile.get_avatar_url()
    return HttpResponse(
        json.dumps({'new_avatar_url': new_avatar_url}),
        content_type="application/json",
    )


class UserKeyDetail(LoginRequiredMixin, SuccessMessageMixin, UpdateView):
    model = UserKey
    template_name = "dashboard/userkey-edit.html"
    form_class = UserKeyForm
    success_message = _("Successfully modified SSH key.")

    def get(self, request, *args, **kwargs):
        object = self.get_object()
        if object.user != request.user:
            raise PermissionDenied()
        return super(UserKeyDetail, self).get(request, *args, **kwargs)

    def get_success_url(self):
        return reverse_lazy("dashboard.views.userkey-detail",
                            kwargs=self.kwargs)

    def post(self, request, *args, **kwargs):
        object = self.get_object()
        if object.user != request.user:
            raise PermissionDenied()
        return super(UserKeyDetail, self).post(self, request, args, kwargs)


451
class UserKeyDelete(DeleteViewBase):
Őry Máté committed
452
    model = UserKey
453
    success_message = _("SSH key successfully deleted.")
Őry Máté committed
454 455 456 457

    def get_success_url(self):
        return reverse("dashboard.views.profile-preferences")

458 459
    def check_auth(self):
        if self.get_object().user != self.request.user:
Őry Máté committed
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
            raise PermissionDenied()


class UserKeyCreate(LoginRequiredMixin, SuccessMessageMixin, CreateView):
    model = UserKey
    form_class = UserKeyForm
    template_name = "dashboard/userkey-create.html"
    success_message = _("Successfully created a new SSH key.")

    def get_success_url(self):
        return reverse_lazy("dashboard.views.profile-preferences")

    def get_form_kwargs(self):
        kwargs = super(UserKeyCreate, self).get_form_kwargs()
        kwargs['user'] = self.request.user
        return kwargs


class ConnectCommandDetail(LoginRequiredMixin, SuccessMessageMixin,
                           UpdateView):
    model = ConnectCommand
    template_name = "dashboard/connect-command-edit.html"
    form_class = ConnectCommandForm
    success_message = _("Successfully modified command template.")

    def get(self, request, *args, **kwargs):
        object = self.get_object()
        if object.user != request.user:
            raise PermissionDenied()
        return super(ConnectCommandDetail, self).get(request, *args, **kwargs)

    def get_success_url(self):
        return reverse_lazy("dashboard.views.connect-command-detail",
                            kwargs=self.kwargs)

    def post(self, request, *args, **kwargs):
        object = self.get_object()
        if object.user != request.user:
            raise PermissionDenied()
        return super(ConnectCommandDetail, self).post(request, args, kwargs)

    def get_form_kwargs(self):
        kwargs = super(ConnectCommandDetail, self).get_form_kwargs()
        kwargs['user'] = self.request.user
        return kwargs


507
class ConnectCommandDelete(DeleteViewBase):
Őry Máté committed
508
    model = ConnectCommand
509
    success_message = _("Command template successfully deleted.")
Őry Máté committed
510 511 512 513

    def get_success_url(self):
        return reverse("dashboard.views.profile-preferences")

514 515
    def check_auth(self):
        if self.get_object().user != self.request.user:
Őry Máté committed
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
            raise PermissionDenied()


class ConnectCommandCreate(LoginRequiredMixin, SuccessMessageMixin,
                           CreateView):
    model = ConnectCommand
    form_class = ConnectCommandForm
    template_name = "dashboard/connect-command-create.html"
    success_message = _("Successfully created a new command template.")

    def get_success_url(self):
        return reverse_lazy("dashboard.views.profile-preferences")

    def get_form_kwargs(self):
        kwargs = super(ConnectCommandCreate, self).get_form_kwargs()
        kwargs['user'] = self.request.user
        return kwargs
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553


class UserList(LoginRequiredMixin, PermissionRequiredMixin, SingleTableView):
    template_name = "dashboard/user-list.html"
    permission_required = "auth.change_user"
    model = User
    table_class = UserListTable
    table_pagination = True

    def get_context_data(self, *args, **kwargs):
        context = super(UserList, self).get_context_data(*args, **kwargs)
        context['search_form'] = self.search_form
        return context

    def get(self, *args, **kwargs):
        self.search_form = UserListSearchForm(self.request.GET)
        self.search_form.full_clean()

        if self.request.is_ajax():
            users = [
                {'url': reverse("dashboard.views.profile", args=[i.username]),
554 555 556
                 'name': i.get_full_name() or i.username,
                 'org_id': i.profile.org_id,
                 }
557 558 559 560 561 562 563 564 565
                for i in self.get_queryset()]
            return HttpResponse(
                json.dumps(users), content_type="application/json")
        else:
            return super(UserList, self).get(*args, **kwargs)

    def get_queryset(self):
        logger.debug('UserList.get_queryset() called. User: %s',
                     unicode(self.request.user))
566
        qs = User.objects.all().order_by("-pk")
567 568 569

        q = self.search_form.cleaned_data.get('s')
        if q:
570 571
            filters = (Q(username__icontains=q) | Q(email__icontains=q) |
                       Q(profile__org_id__icontains=q))
572 573 574 575 576 577
            for w in q.split()[:3]:
                filters |= (
                    Q(first_name__icontains=w) | Q(last_name__icontains=w))
            qs = qs.filter(filters)

        return qs.select_related("profile")
578 579 580 581 582 583 584 585


class EnableTwoFactorView(LoginRequiredMixin, UpdateView):
    model = Profile
    form_class = TwoFactorForm
    template_name = "dashboard/enable-two-factor.html"
    success_url = reverse_lazy("dashboard.views.profile-preferences")

586 587 588 589 590 591
    def dispatch(self, *args, **kwargs):
        if self.get_object().two_factor_secret:
            messages.info(self.request, _("Two-factor authentication is al"
                                          "ready enabled for your account."))
            return redirect(reverse("dashboard.index"))

592
        return super(EnableTwoFactorView, self).dispatch(*args, **kwargs)
593

594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
    def get_object(self, queryset=None):
        if self.request.user.is_anonymous():
            raise PermissionDenied

        return self.request.user.profile

    def get_context_data(self, **kwargs):
        ctx = super(EnableTwoFactorView, self).get_context_data(**kwargs)
        random_base32 = pyotp.random_base32()
        ctx['uri'] = pyotp.TOTP(random_base32).provisioning_uri(
            self.request.user.username, issuer_name=settings.TWO_FACTOR_ISSUER)
        ctx['secret'] = random_base32
        return ctx


609 610
class DisableTwoFactorView(LoginRequiredMixin, FormView):
    form_class = TwoFactorConfirmationForm
611 612 613
    template_name = "dashboard/disable-two-factor.html"
    success_url = reverse_lazy("dashboard.views.profile-preferences")

614
    def get_profile(self, queryset=None):
615 616 617 618
        if self.request.user.is_anonymous():
            raise PermissionDenied

        return self.request.user.profile
619

620 621 622 623 624 625 626 627 628 629 630
    def get_form_kwargs(self):
        kwargs = super(DisableTwoFactorView, self).get_form_kwargs()
        kwargs['user'] = self.request.user
        return kwargs

    def form_valid(self, form):
        profile = self.get_profile()
        profile.two_factor_secret = ""
        profile.save()
        return super(DisableTwoFactorView, self).form_valid(form)

631 632

class TwoFactorLoginView(FormView):
633
    form_class = TwoFactorConfirmationForm
634 635 636 637 638 639 640 641
    template_name = "registration/two-factor-login.html"

    def dispatch(self, *args, **kwargs):
        if not self.request.session.get('two-fa-user'):
            return redirect("/")

        return super(TwoFactorLoginView, self).dispatch(*args, **kwargs)

642 643 644
    def get_user(self):
        return User.objects.get(pk=self.request.session['two-fa-user'])

645 646
    def get_form_kwargs(self):
        kwargs = super(TwoFactorLoginView, self).get_form_kwargs()
647
        kwargs['user'] = self.get_user()
648 649
        return kwargs

650 651 652 653 654
    def get_context_data(self, **kwargs):
        ctx = super(TwoFactorLoginView, self).get_context_data(**kwargs)
        ctx['user'] = self.get_user()
        return ctx

655
    def form_valid(self, form):
656
        user = self.get_user()
657 658 659 660 661 662 663 664 665

        if self.request.session['login-type'] == "saml2":
            user.backend = 'common.backends.Saml2Backend'
        else:
            user.backend = 'django.contrib.auth.backends.ModelBackend'

        login(self.request, user)
        response = redirect(self.request.session['two-fa-redirect'])
        set_language_cookie(self.request, response)
666 667
        set_session_expiry(self.request, user)

668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
        return response


if hasattr(settings, 'SAML_ORG_ID_ATTRIBUTE'):
    from django.http import HttpResponseBadRequest, HttpResponseForbidden
    from django.views.decorators.csrf import csrf_exempt

    from djangosaml2.cache import IdentityCache, OutstandingQueriesCache
    from djangosaml2.conf import get_config
    from djangosaml2.signals import post_authenticated
    from djangosaml2.utils import get_custom_setting
    from saml2.client import Saml2Client
    from saml2.ident import code
    from saml2 import BINDING_HTTP_POST

    @require_POST
    @csrf_exempt
    def circle_assertion_consumer_service(request,
                                          config_loader_path=None,
                                          attribute_mapping=None,
                                          create_unknown_user=None):
        """SAML Authorization Response endpoint
        The IdP will send its response to this view, which
        will process it with pysaml2 help and log the user
        in using the custom Authorization backend or redirect to 2fa
        djangosaml2.backends.Saml2Backend that should be
        enabled in the settings.py
        """
        attribute_mapping = attribute_mapping or get_custom_setting(
            'SAML_ATTRIBUTE_MAPPING', {'uid': ('username', )})
        create_unknown_user = create_unknown_user or get_custom_setting(
            'SAML_CREATE_UNKNOWN_USER', True)
        logger.debug('Assertion Consumer Service started')

        conf = get_config(config_loader_path, request)
        if 'SAMLResponse' not in request.POST:
            return HttpResponseBadRequest(
                'Couldn\'t find "SAMLResponse" in POST data.')
        xmlstr = request.POST['SAMLResponse']
        client = Saml2Client(
            conf, identity_cache=IdentityCache(request.session))

        oq_cache = OutstandingQueriesCache(request.session)
        outstanding_queries = oq_cache.outstanding_queries()

        # process the authentication response
        response = client.parse_authn_request_response(
            xmlstr, BINDING_HTTP_POST, outstanding_queries)
        if response is None:
            logger.error('SAML response is None')
            return HttpResponseBadRequest(
                "SAML response has errors. Please check the logs")

        session_id = response.session_id()
        oq_cache.delete(session_id)

        # authenticate the remote user
        session_info = response.session_info()

        if callable(attribute_mapping):
            attribute_mapping = attribute_mapping()
        if callable(create_unknown_user):
            create_unknown_user = create_unknown_user()

        logger.debug('Trying to authenticate the user')
        user = authenticate(session_info=session_info,
                            attribute_mapping=attribute_mapping,
                            create_unknown_user=create_unknown_user)
        if user is None:
            logger.error('The user is None')
            return HttpResponseForbidden("Permission denied")

        # redirect the user to the view where he came from
        relay_state = request.POST.get('RelayState', '/')
        if not relay_state:
            logger.warning('The RelayState parameter exists but is empty')
            relay_state = settings.LOGIN_REDIRECT_URL
        logger.debug('Redirecting to the RelayState: ' + relay_state)

747
        if hasattr(user, "profile") and user.profile.two_factor_secret:
748 749 750 751 752 753
            request.session['two-fa-user'] = user.pk
            request.session['two-fa-redirect'] = relay_state
            request.session['login-type'] = "saml2"
            return redirect(reverse("two-factor-login"))
        else:
            login(request, user)
754
            set_session_expiry(request, user)
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769

            def _set_subject_id(session, subject_id):
                session['_saml2_subject_id'] = code(subject_id)

            _set_subject_id(request.session, session_info['name_id'])

            logger.debug('Sending the post_authenticated signal')
            post_authenticated.send_robust(sender=user,
                                           session_info=session_info)

            # redirect the user to the view where he came from
            return redirect(relay_state)

    from djangosaml2 import views as saml2_views
    saml2_views.assertion_consumer_service = circle_assertion_consumer_service