Example #1
0
def status(request, template_name="latch_status.html"):
    """
    Gives information about Latch status, if it's configured,
    and data relative to the user making the request.
    """
    configured = True
    try:
        LatchSetup.instance()
    except ImproperlyConfigured:
        configured = False

    acc_id = None
    account_status = None
    try:
        if configured:
            acc_id = UserProfile.accountid(request.user)
            if acc_id:
                latch_instance = LatchSetup.instance()
                status_response = latch_instance.status(acc_id)
                data = status_response.get_data()["operations"]
                account_status = data[LatchSetup.appid()]["status"]

    except HTTPException:
        logger.exception("Couldn't connect with Latch service")
        return render(request, template_name, {"error": True})

    return render(
        request,
        template_name,
        {
            "configured": configured,
            "accountid": acc_id,
            "account_status": account_status,
        },
    )
Example #2
0
    def wrapper(request, *args, **kwargs):
        try:
            LatchSetup.instance()
        except ImproperlyConfigured:
            messages.error(request, _("Latch is not configured"))
            return redirect(status)

        return view(request, *args, **kwargs)
Example #3
0
    def test_api_called_with_correct_settings(self, mock_latch):
        mock_latch.return_value = None

        LatchSetup.instance()

        mock_latch.assert_called_once_with(
            "abcdefghijklmnopqrst",
            "abcdefghijklmnopqrstuvwxyzabcdefghijklmno")
Example #4
0
def _process_pair_post(request, template_name="latch_message.html"):
    form = LatchPairForm(request.POST)
    if form.is_valid():
        form.clean()
        try:
            latch_instance = LatchSetup.instance()
            account_id = latch_instance.pair(form.cleaned_data["latch_pin"])
            if "accountId" in account_id.get_data():
                UserProfile.save_user_accountid(
                    request.user,
                    account_id.get_data()["accountId"])
                messages.success(request, _("Account paired with Latch"))
            else:
                logger.warning(
                    "Pair process of user %s failed with error %s",
                    request.user,
                    account_id.get_error(),
                )
                messages.error(request, _("Account not paired with Latch"))

        except HTTPException as err:
            logger.exception("Couldn't connect with Latch service")

            msg = (_("Error pairing the account: %(error)s") % {
                "error": err
            } if settings.DEBUG else _("Error pairing the account"))

            messages.error(request, msg)

        return redirect(status)
Example #5
0
    def latch_permits_login(user):
        if not LatchSetup.is_configured() or not UserProfile.accountid(user):
            # Always return on if is not configured or the user does not have latch configured
            return True

        try:
            latch_instance = LatchSetup.instance()
            status_response = latch_instance.status(UserProfile.accountid(user))
            data = status_response.get_data()
            return data["operations"][LatchSetup.appid()]["status"] == "on"
        except HTTPException:
            logger.exception("Couldn't connect with Latch service")
            bypass = getattr(settings, "LATCH_BYPASS_WHEN_UNREACHABLE", True)
            if bypass:
                logger.info("Bypassing login attempt")

            return bool(bypass)
Example #6
0
    def setUpTestData(cls):
        cls.latch_setup = LatchSetup.instance()

        cls.paired_user = User.objects.create_user(username="******",
                                                   email="*****@*****.**",
                                                   password="******")

        cls.paired_profile = UserProfile.objects.create(
            user=cls.paired_user,
            latch_accountId=
            "abcdefghijlkmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl",
        )

        cls.unpaired_user = User.objects.create_user(username="******",
                                                     email="*****@*****.**",
                                                     password="******")
Example #7
0
def unpair_before_delete(sender, **kwargs):
    latch = LatchSetup.instance()
    latch.unpair(kwargs.get('instance').latch_accountId)
Example #8
0
 def test_configured_app_returns_correct_app_id(self):
     self.assertEqual(LatchSetup.appid(), "abcdefghijklmnopqrst")
Example #9
0
 def test_exception_raised_when_app_secret_not_configured(self):
     with self.assertRaises(ImproperlyConfigured):
         LatchSetup.instance()