Exemple #1
0
    def get(self, request):
        response = super(QuickerExpirationAuthenticateView, self).get(request)
        request.session[ALDRYN_USER_SESSION_KEY] = True
        request.session.set_expiry(settings.CLOUD_USER_SESSION_EXPIRATION)
        request.session.save()

        # request.is_ajax() does not work for xhr redirects :-(
        next_url = self.get_next()
        next_url = furl(next_url)
        is_ajax = bool(next_url.args.pop(IS_AJAX_URLPARAM, False))
        if is_ajax and user_is_authenticated(request.user):
            # Return JSON response so JS can detect that the login was
            # successful.
            response = HttpResponse(
                json.dumps({
                    'is_authenticated': True,
                    'next': next_url.url,
                }),
                content_type="application/json",
            )
            # If the token were not valid, we'd never get here. So it is safe
            # to set very open CORS headers.
            # 'null' because that is what most browsers send as "Origin" after a
            # xhr redirect from an different domain.
            response['Access-Control-Allow-Origin'] = 'null'
            response['Access-Control-Allow-Credentials'] = 'true'
            return response
        response['Location'] = next_url.url
        return response
Exemple #2
0
def login(request, **kwargs):
    kwargs['authentication_form'] = AuthenticationForm
    extra_context = kwargs.get('extra_context', {})
    extra_context.update(get_shared_context())
    kwargs['extra_context'] = extra_context
    if request.method == 'POST':
        return django.contrib.auth.views.LoginView.as_view(**kwargs)(request,
                                                                     **kwargs)
    next_url = get_redirect_url(
        request,
        fallback=resolve_url(settings.LOGIN_REDIRECT_URL),
    )
    if user_is_authenticated(request.user):
        # already authenticated. no sense in logging in.
        return HttpResponseRedirect(next_url)
    if (settings.ALDRYN_SSO_ENABLE_AUTO_SSO_LOGIN
            and settings.ALDRYN_SSO_ENABLE_SSO_LOGIN
            and not (settings.ALDRYN_SSO_ENABLE_LOCALDEV
                     or settings.ALDRYN_SSO_ENABLE_LOGIN_FORM)):
        # The aldryn SSO button would be the only thing on the page. So we just
        # initiate the login without further ado.
        sso_url = '{}?{}'.format(
            reverse('simple-sso-login'),
            urlencode(dict(next=next_url)),
        )
        return HttpResponseRedirect(sso_url)
    return django.contrib.auth.views.LoginView.as_view(**kwargs)(request,
                                                                 **kwargs)
Exemple #3
0
 def get(self, request):
     request_token = request.GET.get('token', None)
     if not request_token:
         return self.missing_token_argument()
     try:
         self.token = Token.objects.select_related('consumer').get(
             request_token=request_token)
     except Token.DoesNotExist:
         return self.token_not_found()
     if not self.check_token_timeout():
         return self.token_timeout()
     self.token.refresh()
     if user_is_authenticated(request.user):
         return self.handle_authenticated_user()
     else:
         return self.handle_unauthenticated_user()
Exemple #4
0
def login_as_user(request, next_page=None):
    if not next_page:
        fallback = resolve_url(settings.LOGIN_REDIRECT_URL)
        next_page = get_redirect_url(request, fallback=fallback)

    form = LoginAsForm(request.POST or None)

    if user_is_authenticated(request.user):
        response = HttpResponseRedirect(next_page)
    elif form.is_valid():
        django.contrib.auth.login(request, form.cleaned_data['user'])
        response = HttpResponseRedirect(next_page)
    else:
        context = {
            'aldryn_sso_localdev_login_as_form': form,
            django.contrib.auth.REDIRECT_FIELD_NAME: next_page
        }
        context.update(get_shared_context())
        response = render(request, 'aldryn_sso/login_screen.html', context)
    return response
Exemple #5
0
 def get_success_url(self):
     if user_is_authenticated(self.request.user):
         fallback = resolve_url(settings.LOGIN_REDIRECT_URL)
     else:
         fallback = reverse('aldryn_sso_localdev_login')
     return get_redirect_url(self.request, fallback=fallback)