Exemple #1
0
    def test_run_test_vector_without_fallback_user(self,
                         mock_getUserFromParam
                         ):
        """
        test a set of options dict values without fallback user and fallback realm
        """

        test_sets = [
                    # 1. test simple login name in options
                    {'options_dict': {'user': '******'},
                      'user_from_param' : User('amigo', 'defRealm'),
                      'result': {'login': '******', 'realm': 'defRealm'}},

                     # 2. test login and realn in options
                     {'options_dict': {'user': '******', 'realm': 'mexico'},
                      'user_from_param' : User('amigo', 'mexico'),
                      'result': {'login': '******', 'realm': 'mexico'}},

                     # 3. test user object in options
                     {'options_dict': {'user': User('amigo', 'mexico')},
                      'user_from_param' : User('amigo', 'mexico'),
                      'result': {'login': '******', 'realm': 'mexico'}},

                     # 4. test no login and no realn in options
                     {'options_dict': {},
                      'user_from_param' : User('', ''),
                      'result': {'login': '', 'realm': 'norealm'}},

                     # 5. test no login and realn in options
                     {'options_dict': { 'user': '', 'realm': 'norway'},
                      'user_from_param' : User('', 'norway'),
                      'result': {'login': '', 'realm': 'norealm'}},

        ]  # eof test sets


        for run in test_sets:

            options_dict = run['options_dict']
            mock_getUserFromParam.return_value = run['user_from_param']

            result = run['result']

            login, realm = get_user_from_options(
                                options_dict=options_dict,
                                fallback_user=None,
                                fallback_realm='norealm')

            assert (login == result['login'] and realm == result['realm'],
                        "failed on run %r:%r:%r" % (login, realm, run))

        return
Exemple #2
0
    def submitChallenge(self, options=None):
        """
        submit the sms message - former method name was checkPin

        :param options: the request options context

        :return: tuple of success and message
        """

        res = 0

        fallback_realm = (self.getRealms() or [None])[0]
        login, realm = get_user_from_options(
            options,
            fallback_user=get_token_owner(self),
            fallback_realm=fallback_realm,
        )

        message = options.get("challenge", "<otp>")
        result = _("sending sms failed")

        # it is configurable, if sms should be triggered by a valid pin
        send_by_PIN = getFromConfig("sms.sendByPin") or True

        # if the token is not active or there is no pin triggered sending, we
        # leave
        if not (self.isActive() is True and send_by_PIN is True):
            return res, result

        counter = self.getOtpCount()
        log.debug("[submitChallenge] counter=%r", counter)

        # At this point we MUST NOT bail out in case of an
        # Gateway error, since checkPIN is successful, as the bail
        # out would cancel the checking of the other tokens
        try:
            sms_ret = False
            new_message = None

            sms_ret, new_message = get_auth_smstext(user=login, realm=realm)
            if sms_ret:
                message = new_message

            # ---------------------------------------------------------- --

            # if there is a data or message part in the request, it might
            # overrule the given smstext

            if "data" in options or "message" in options:

                # if there is an enforce policy
                # we do not allow the owerwrite

                enforce = enforce_smstext(user=login, realm=realm)

                if not enforce:
                    message = options.get(
                        "data", options.get("message", "<otp>")
                    )

            # ---------------------------------------------------------- --

            # fallback if no message is defined

            if not message:
                message = "<otp>"

            # ---------------------------------------------------------- --

            # submit the sms message

            transactionid = options.get("transactionid", None)
            res, result = self.sendSMS(
                message=message, transactionid=transactionid
            )

            self.info["info"] = "SMS sent: %r" % res
            log.debug("SMS sent: %s", result)

            return res, result

        except Exception as e:
            # The PIN was correct, but the SMS could not be sent.
            self.info["info"] = str(e)
            info = "The SMS could not be sent: %r" % e
            log.warning("[submitChallenge] %s", info)
            return False, info

        finally:
            # we increment the otp in any case, independend if sending
            # of the sms was sucsessful
            self.incOtpCounter(counter, reset=False)
    def test_run_test_vector(self, mock_getUserFromParam):
        """
        test a set of options dict values with fallback user and fallback realm
        """

        test_sets = [
            # 1. test simple login name in options
            {
                "options_dict": {
                    "user": "******"
                },
                "user_from_param": User("amigo", "defRealm"),
                "result": {
                    "login": "******",
                    "realm": "defRealm"
                },
            },
            # 2. test login and realn in options
            {
                "options_dict": {
                    "user": "******",
                    "realm": "mexico"
                },
                "user_from_param": User("amigo", "mexico"),
                "result": {
                    "login": "******",
                    "realm": "mexico"
                },
            },
            # 3. test user object in options
            {
                "options_dict": {
                    "user": User("amigo", "mexico")
                },
                "user_from_param": User("amigo", "mexico"),
                "result": {
                    "login": "******",
                    "realm": "mexico"
                },
            },
            # 4. test no login and no realn in options
            {
                "options_dict": {},
                "user_from_param": User("", ""),
                "result": {
                    "login": "******",
                    "realm": "norealm"
                },
            },
            # 5. test no login and realn in options
            {
                "options_dict": {
                    "user": "",
                    "realm": "norway"
                },
                "user_from_param": User("", "norway"),
                "result": {
                    "login": "******",
                    "realm": "norealm"
                },
            },
        ]  # eof test sets

        for run in test_sets:

            options_dict = run["options_dict"]
            mock_getUserFromParam.return_value = run["user_from_param"]

            result = run["result"]

            login, realm = get_user_from_options(
                options_dict=options_dict,
                fallback_user=User("token_owner", "norealm"),
                fallback_realm="norealm",
            )

            assert (
                login == result["login"] and realm == result["realm"],
                "failed on run %r:%r:%r" % (login, realm, run),
            )

        return
    def test_run_test_vector(self, mock_getUserFromParam):
        """
        test a set of options dict values with fallback user and fallback realm
        """

        test_sets = [
            # 1. test simple login name in options
            {
                'options_dict': {
                    'user': '******'
                },
                'user_from_param': User('amigo', 'defRealm'),
                'result': {
                    'login': '******',
                    'realm': 'defRealm'
                }
            },

            # 2. test login and realn in options
            {
                'options_dict': {
                    'user': '******',
                    'realm': 'mexico'
                },
                'user_from_param': User('amigo', 'mexico'),
                'result': {
                    'login': '******',
                    'realm': 'mexico'
                }
            },

            # 3. test user object in options
            {
                'options_dict': {
                    'user': User('amigo', 'mexico')
                },
                'user_from_param': User('amigo', 'mexico'),
                'result': {
                    'login': '******',
                    'realm': 'mexico'
                }
            },

            # 4. test no login and no realn in options
            {
                'options_dict': {},
                'user_from_param': User('', ''),
                'result': {
                    'login': '******',
                    'realm': 'norealm'
                }
            },

            # 5. test no login and realn in options
            {
                'options_dict': {
                    'user': '',
                    'realm': 'norway'
                },
                'user_from_param': User('', 'norway'),
                'result': {
                    'login': '******',
                    'realm': 'norealm'
                }
            },
        ]  # eof test sets

        for run in test_sets:

            options_dict = run['options_dict']
            mock_getUserFromParam.return_value = run['user_from_param']

            result = run['result']

            login, realm = get_user_from_options(options_dict=options_dict,
                                                 fallback_user=User(
                                                     'token_owner', 'norealm'),
                                                 fallback_realm='norealm')

            assert (login == result['login'] and realm == result['realm'],
                    "failed on run %r:%r:%r" % (login, realm, run))

        return