Example #1
0
def logout(request):
    """
    Logs the user out of their Uniauth account, and
    redirects to the next page, defaulting to the URL
    specified by the UNIAUTH_LOGOUT_REDIRECT_URL setting.

    If no redirect page is set (URL parameter not given
    and UNIAUTH_LOGOUT_REDIRECT_URL is None), renders the
    logout template.

    Also logs the user out of CAS if they logged in
    via CAS, and the UNIAUTH_LOGOUT_CAS_COMPLETELY
    setting is true.
    """
    next_page = request.GET.get('next')
    auth_method = request.session.get('auth-method')

    if not next_page and get_setting('UNIAUTH_LOGOUT_REDIRECT_URL'):
        next_page = get_redirect_url(
            request, get_setting('UNIAUTH_LOGOUT_REDIRECT_URL'))

    # Formally log out user
    auth_logout(request)

    # Determine whether the user logged in through an institution's CAS
    institution = None
    if auth_method and auth_method.startswith("cas-"):
        try:
            institution = Institution.objects.get(slug=auth_method[4:])
        except Institution.DoesNotExist:
            pass

    # If we need to logout an institution's CAS,
    # redirect to that CAS server's logout URL
    if institution and get_setting('UNIAUTH_LOGOUT_CAS_COMPLETELY'):
        redirect_url = urlunparse(
            (get_protocol(request), request.get_host(), next_page
             or reverse('uniauth:logout'), '', '', ''))
        client = CASClient(version=2,
                           service_url=get_service_url(request),
                           server_url=institution.cas_server_url)
        return HttpResponseRedirect(client.get_logout_url(redirect_url))

    # If next page is set, proceed to it
    elif next_page:
        return HttpResponseRedirect(next_page)

    # Otherwise, render the logout view
    else:
        return render(request, 'uniauth/logout.html')
Example #2
0
def _send_verification_email(request, to_email, verify_email):
    """
    Sends an email (to to_email) containing a link to
    verify the target email (verify_email) as a linked
    email address for the user.

    Expects to_email as a string, and verify_email as
    a LinkedEmail model instance.
    """
    subject = "Verify your email address."
    message = render_to_string(
        'uniauth/verification-email.html', {
            'protocol': get_protocol(request),
            'domain': get_current_site(request),
            'pk': encode_pk(verify_email.pk),
            'token': token_generator.make_token(verify_email),
            'query_params': _get_global_context(request)["query_params"],
        })
    email = EmailMessage(subject,
                         message,
                         to=[to_email],
                         from_email=get_setting('UNIAUTH_FROM_EMAIL'))
    email.send()