# Path: sitegate/signals.py

# 
# Path: sitegate/utils.py
# USER = get_user_model()


import logging
import requests
from typing import Optional, List, NamedTuple
from django.contrib.auth import login
from django.db import IntegrityError
from django.db.transaction import atomic
from django.http import HttpResponseRedirect, HttpResponse, HttpRequest
from django.middleware.csrf import get_token
from django.shortcuts import redirect, render
from django.urls import reverse
from django.utils import timezone
from django.utils.functional import cached_property
from etc.toolbox import get_site_url
from ...signals import sig_user_signup_success, sig_user_signup_fail
from ...utils import USER
    from django.contrib.auth.models import User  # noqa
    from ...models import RemoteRecord
        :param user_data:
        :param remote_record:
        :param user: current authorized user

        """
        flow_name = f'remote-{remote_record.remote_id}'

        with atomic():

            if not user:
                # user is not authorized spawn a new user instance
                try:
                    user = self.construct_user(user_data)

                except Exception as _:
                    LOG.exception(f'{self._auth_fail_prefix} unable to construct a user.')
                    sig_user_signup_fail.send(self, signup_result=None, flow=flow_name, request=request)
                    user = None

            if user:
                remote_record.remote_id = user_data.remote_id
                remote_record.user = user
                remote_record.time_accepted = timezone.now()
                remote_record.save()

        if not user:
            return self.redirect()

        login(request, user, backend='django.contrib.auth.backends.ModelBackend')


# Path: sitegate/signals.py

# 
# Path: sitegate/utils.py
# USER = get_user_model()


# Path: sitegate/signin_flows/remotes/base.py
import logging
import requests
from typing import Optional, List, NamedTuple
from django.contrib.auth import login
from django.db import IntegrityError
from django.db.transaction import atomic
from django.http import HttpResponseRedirect, HttpResponse, HttpRequest
from django.middleware.csrf import get_token
from django.shortcuts import redirect, render
from django.urls import reverse
from django.utils import timezone
from django.utils.functional import cached_property
from etc.toolbox import get_site_url
from ...signals import sig_user_signup_success, sig_user_signup_fail
from ...utils import USER
    from django.contrib.auth.models import User  # noqa
    from ...models import RemoteRecord

        :param user_data:
        :param remote_record:
        :param user: current authorized user

        """
        flow_name = f'remote-{remote_record.remote_id}'

        with atomic():

            if not user:
                # user is not authorized spawn a new user instance
                try:
                    user = self.construct_user(user_data)

                except Exception as _:
                    LOG.exception(f'{self._auth_fail_prefix} unable to construct a user.')
                    sig_user_signup_fail.send(self, signup_result=None, flow=flow_name, request=request)
                    user = None

            if user:
                remote_record.remote_id = user_data.remote_id
                remote_record.user = user
                remote_record.time_accepted = timezone.now()
                remote_record.save()

        if not user:
            return self.redirect()

        login(request, user, backend='django.contrib.auth.backends.ModelBackend')


        sig_user_signup_success.send(