(request, idp_slug)
return render(request, error_template, {
        'login_error': getattr(request, 'sso_login_error', None),
    })
ACS stands for "Assertion Consumer Service". The Identity Provider will send
    its response to this view after authenticating a user. This is often
    referred to as the "Entity ID" in the IdP's Service Provider configuration.

    In this view we verify the received SAML 2.0 response and then log in the user
    to CommCare HQ.
ACS stands for "Assertion Consumer Service". The Identity Provider will send
    its response to this view after authenticating a user. This is often
    referred to as the "Entity ID" in the IdP's Service Provider configuration.
ACS stands for Assertion Consumer Service . The Identity Provider will send its response to this view after authenticating a user . This is often referred to as the Entity ID in the IdP s Service Provider configuration .
def sso_saml_acs(request, idp_slug):
    """
    ACS stands for "Assertion Consumer Service". The Identity Provider will send
    its response to this view after authenticating a user. This is often
    referred to as the "Entity ID" in the IdP's Service Provider configuration.

    In this view we verify the received SAML 2.0 response and then log in the user
    to CommCare HQ.
    """
    request_id = request.session.get('AuthNRequestID')
    error_template = 'sso/acs_errors.html'

    try:
        request.saml2_auth.process_response(request_id=request_id)
        errors = request.saml2_auth.get_errors()
    except OneLogin_Saml2_Error as e:
        if e.code == OneLogin_Saml2_Error.SAML_RESPONSE_NOT_FOUND:
            return redirect("sso_saml_login", idp_slug=idp_slug)
        errors = [e]

    if errors:
        return render(request, error_template, {
            'saml_error_reason': request.saml2_auth.get_last_error_reason() or errors[0],
            'idp_type': "Azure AD",  # we will update this later,
            'docs_link': get_documentation_url(request.idp),
        })

    if not request.saml2_auth.is_authenticated():
        return render(request, 'sso/sso_request_denied.html', {})

    if 'AuthNRequestID' in request.session:
        del request.session['AuthNRequestID']

    store_saml_data_in_session(request)

    user = auth.authenticate(
        request=request,
        username=request.session['samlNameId'],
        idp_slug=idp_slug,
        is_handshake_successful=True,
    )

    # we add the messages to the django messages framework here since
    # that middleware was not available for SsoBackend
    if hasattr(request, 'sso_new_user_messages'):
        for success_message in request.sso_new_user_messages['success']:
            messages.success(request, success_message)
        for error_message in request.sso_new_user_messages['error']:
            messages.error(request, error_message)

    if user:
        auth.login(request, user)

        # activate new project if needed
        async_signup = AsyncSignupRequest.get_by_username(user.username)
        if async_signup and async_signup.project_name:
            try:
                request_new_domain(
                    request,
                    async_signup.project_name,
                    is_new_user=True,
                    is_new_sso_user=True
                )
            except NameUnavailableException:
                # this should never happen, but in the off chance it does
                # we don't want to throw a 500 on this view
                messages.error(
                    request,
                    _("We were unable to create your requested project "
                      "because the name was already taken."
                      "Please contact support.")
                )

        AsyncSignupRequest.clear_data_for_username(user.username)

        relay_state = request.saml2_request_data['post_data'].get('RelayState')
        if relay_state not in [
            OneLogin_Saml2_Utils.get_self_url(request.saml2_request_data),
            get_saml_login_url(request.idp),
        ]:
            # redirect to next=<relay_state>
            return HttpResponseRedirect(
                request.saml2_auth.redirect_to(relay_state)
            )

        return redirect("homepage")

    return render(request, error_template, {
        'login_error': getattr(request, 'sso_login_error', None),
    })
def sso_saml_acs ( request , idp_slug ) : request_id = request . session . get ( 'AuthNRequestID' ) error_template = 'sso/acs_errors.html' try : request . saml2_auth . process_response ( request_id = request_id ) errors = request . saml2_auth . get_errors ( ) except OneLogin_Saml2_Error as e : if e . code == OneLogin_Saml2_Error . SAML_RESPONSE_NOT_FOUND : return redirect ( "sso_saml_login" , idp_slug = idp_slug ) errors = [ e ] if errors : return render ( request , error_template , { 'saml_error_reason' : request . saml2_auth . get_last_error_reason ( ) or errors [ 0 ] , 'idp_type' : "Azure AD" , # we will update this later, 'docs_link' : get_documentation_url ( request . idp ) , } ) if not request . saml2_auth . is_authenticated ( ) : return render ( request , 'sso/sso_request_denied.html' , { } ) if 'AuthNRequestID' in request . session : del request . session [ 'AuthNRequestID' ] store_saml_data_in_session ( request ) user = auth . authenticate ( request = request , username = request . session [ 'samlNameId' ] , idp_slug = idp_slug , is_handshake_successful = True , ) # we add the messages to the django messages framework here since # that middleware was not available for SsoBackend if hasattr ( request , 'sso_new_user_messages' ) : for success_message in request . sso_new_user_messages [ 'success' ] : messages . success ( request , success_message ) for error_message in request . sso_new_user_messages [ 'error' ] : messages . error ( request , error_message ) if user : auth . login ( request , user ) # activate new project if needed async_signup = AsyncSignupRequest . get_by_username ( user . username ) if async_signup and async_signup . project_name : try : request_new_domain ( request , async_signup . project_name , is_new_user = True , is_new_sso_user = True ) except NameUnavailableException : # this should never happen, but in the off chance it does # we don't want to throw a 500 on this view messages . error ( request , _ ( "We were unable to create your requested project " "because the name was already taken." "Please contact support." ) ) AsyncSignupRequest . clear_data_for_username ( user . username ) relay_state = request . saml2_request_data [ 'post_data' ] . get ( 'RelayState' ) if relay_state not in [ OneLogin_Saml2_Utils . get_self_url ( request . saml2_request_data ) , get_saml_login_url ( request . idp ) , ] : # redirect to next=<relay_state> return HttpResponseRedirect ( request . saml2_auth . redirect_to ( relay_state ) ) return redirect ( "homepage" ) return render ( request , error_template , { 'login_error' : getattr ( request , 'sso_login_error' , None ) , } )