Ejemplo n.º 1
0
    def test_02_token(self):
        r = get_token_list()
        self.assertTrue("privacyidea.lib.tokens.totptoken" in r, r)
        self.assertTrue("privacyidea.lib.tokens.hotptoken" in r, r)

        # check modules
        mlist = get_token_module_list()
        mod_name = "privacyidea.lib.tokens.totptoken"
        exec("import %s" % mod_name)
        module = eval(mod_name)
        self.assertTrue(module in mlist, mlist)

#        r = get_resolver_classes()
#        self.assertTrue(UserResolver in r, r)
#        self.assertTrue(PWResolver in r, r)

        # get_token_class_dict
        (classes, types) = get_token_class_dict()
        self.assertTrue('privacyidea.lib.tokens.hotptoken.HotpTokenClass'
                        in classes, classes)
        self.assertTrue(classes.get(
            'privacyidea.lib.tokens.hotptoken.HotpTokenClass') ==
                        HotpTokenClass, classes)
        self.assertTrue('privacyidea.lib.tokens.totptoken.TotpTokenClass'
                        in classes, classes)
        self.assertTrue(classes.get(
            'privacyidea.lib.tokens.totptoken.TotpTokenClass') ==
                        TotpTokenClass, classes)

        self.assertTrue('privacyidea.lib.tokens.hotptoken.HotpTokenClass'
                        in types, types)
        self.assertTrue('privacyidea.lib.tokens.totptoken.TotpTokenClass'
                        in types, types)
        self.assertTrue(types.get('privacyidea.lib.tokens.hotptoken'
                                  '.HotpTokenClass') == "hotp", types)
        self.assertTrue(types.get('privacyidea.lib.tokens.totptoken'
                                  '.TotpTokenClass') == "totp", types)

        types = get_token_types()
        self.assertTrue("totp" in types, types)
        self.assertTrue("hotp" in types, types)
        # Now the resolver types are contained.
        self.assertTrue("pi_token_types" in current_app.config,
                        current_app.config)
        types = get_token_types()
        self.assertTrue("totp" in types, types)
        self.assertTrue("hotp" in types, types)

        # At the beginning the token classes are not cached.
        self.assertFalse("pi_token_classes" in current_app.config,
                         current_app.config)
        r = get_token_classes()
        self.assertTrue(TotpTokenClass in r, r)
        self.assertTrue(HotpTokenClass in r, r)
        # Now the token classes are cached
        self.assertTrue("pi_token_classes" in current_app.config,
                        current_app.config)
        r = get_token_classes()
        self.assertTrue(TotpTokenClass in r, r)
        self.assertTrue(HotpTokenClass in r, r)
Ejemplo n.º 2
0
    def ui_get_enroll_tokentypes(self, client, logged_in_user):
        """
        Return a dictioary of the allowed tokentypes for the logged in user.
        This used for the token enrollment UI.

        It looks like this:

           {"hotp": "HOTP: event based One Time Passwords",
            "totp": "TOTP: time based One Time Passwords",
            "spass": "******",
            "motp": "mOTP: classical mobile One Time Passwords",
            "sshkey": "SSH Public Key: The public SSH key",
            "yubikey": "Yubikey AES mode: One Time Passwords with Yubikey",
            "remote": "Remote Token: Forward authentication request to another server",
            "yubico": "Yubikey Cloud mode: Forward authentication request to YubiCloud",
            "radius": "RADIUS: Forward authentication request to a RADIUS server",
            "email": "EMail: Send a One Time Passwort to the users email address",
            "sms": "SMS: Send a One Time Password to the users mobile phone",
            "certificate": "Certificate: Enroll an x509 Certificate Token."}

        :param client: Client IP address
        :type client: basestring
        :param logged_in_user: The Dict of the logged in user
        :type logged_in_user: dict
        :return: list of token types, the user may enroll
        """
        enroll_types = {}
        role = logged_in_user.get("role")
        admin_realm = logged_in_user.get("realm")
        # check, if we have a policy definition at all.
        pols = self.get_policies(scope=role, active=True)
        tokenclasses = get_token_classes()
        for tokenclass in tokenclasses:
            # Check if the tokenclass is ui enrollable for "user" or "admin"
            if role in tokenclass.get_class_info("ui_enroll"):
                enroll_types[tokenclass.get_class_type()] = tokenclass.get_class_info("description")

        if pols:
            # admin policies or user policies are set, so we need to
            # test, which tokens are allowed to be enrolled for this user
            for tokentype in enroll_types.keys():
                # determine, if there is a enrollment policy for this very type
                typepols = self.get_policies(
                    scope=role,
                    client=client,
                    user=logged_in_user.get("username"),
                    realm=logged_in_user.get("realm"),
                    active=True,
                    action="enroll" + tokentype.upper(),
                    adminrealm=admin_realm,
                )
                if len(typepols) == 0:
                    # If there is no policy allowing the enrollment of this
                    # tokentype, it is deleted.
                    del (enroll_types[tokentype])

        return enroll_types
Ejemplo n.º 3
0
    def test_02_token(self):
        r = get_token_list()
        self.assertTrue("privacyidea.lib.tokens.totptoken" in r, r)
        self.assertTrue("privacyidea.lib.tokens.hotptoken" in r, r)

        # check modules
        mlist = get_token_module_list()
        mod_name = "privacyidea.lib.tokens.totptoken"
        module = importlib.import_module(mod_name)
        self.assertTrue(module in mlist, mlist)

        #        r = get_resolver_classes()
        #        self.assertTrue(UserResolver in r, r)
        #        self.assertTrue(PWResolver in r, r)

        # get_token_class_dict
        (classes, types) = get_token_class_dict()
        self.assertTrue(
            'privacyidea.lib.tokens.hotptoken.HotpTokenClass' in classes,
            classes)
        self.assertTrue(
            classes.get('privacyidea.lib.tokens.hotptoken.HotpTokenClass') ==
            HotpTokenClass, classes)
        self.assertTrue(
            'privacyidea.lib.tokens.totptoken.TotpTokenClass' in classes,
            classes)
        self.assertTrue(
            classes.get('privacyidea.lib.tokens.totptoken.TotpTokenClass') ==
            TotpTokenClass, classes)

        self.assertTrue(
            'privacyidea.lib.tokens.hotptoken.HotpTokenClass' in types, types)
        self.assertTrue(
            'privacyidea.lib.tokens.totptoken.TotpTokenClass' in types, types)
        self.assertTrue(
            types.get('privacyidea.lib.tokens.hotptoken'
                      '.HotpTokenClass') == "hotp", types)
        self.assertTrue(
            types.get('privacyidea.lib.tokens.totptoken'
                      '.TotpTokenClass') == "totp", types)

        types = get_token_types()
        self.assertTrue("totp" in types, types)
        self.assertTrue("hotp" in types, types)

        # token classes are cached with calling 'get_token_types()'
        self.assertTrue("pi_token_classes" in this.config, this.config)
        self.assertTrue("pi_token_types" in this.config, this.config)
        r = get_token_classes()
        self.assertTrue(TotpTokenClass in r, r)
        self.assertTrue(HotpTokenClass in r, r)
Ejemplo n.º 4
0
    def test_02_token(self):
        r = get_token_list()
        self.assertTrue("privacyidea.lib.tokens.totptoken" in r, r)
        self.assertTrue("privacyidea.lib.tokens.hotptoken" in r, r)

        # check modules
        mlist = get_token_module_list()
        mod_name = "privacyidea.lib.tokens.totptoken"
        module = importlib.import_module(mod_name)
        self.assertTrue(module in mlist, mlist)

#        r = get_resolver_classes()
#        self.assertTrue(UserResolver in r, r)
#        self.assertTrue(PWResolver in r, r)

        # get_token_class_dict
        (classes, types) = get_token_class_dict()
        self.assertTrue('privacyidea.lib.tokens.hotptoken.HotpTokenClass'
                        in classes, classes)
        self.assertTrue(classes.get(
            'privacyidea.lib.tokens.hotptoken.HotpTokenClass') ==
                        HotpTokenClass, classes)
        self.assertTrue('privacyidea.lib.tokens.totptoken.TotpTokenClass'
                        in classes, classes)
        self.assertTrue(classes.get(
            'privacyidea.lib.tokens.totptoken.TotpTokenClass') ==
                        TotpTokenClass, classes)

        self.assertTrue('privacyidea.lib.tokens.hotptoken.HotpTokenClass'
                        in types, types)
        self.assertTrue('privacyidea.lib.tokens.totptoken.TotpTokenClass'
                        in types, types)
        self.assertTrue(types.get('privacyidea.lib.tokens.hotptoken'
                                  '.HotpTokenClass') == "hotp", types)
        self.assertTrue(types.get('privacyidea.lib.tokens.totptoken'
                                  '.TotpTokenClass') == "totp", types)

        types = get_token_types()
        self.assertTrue("totp" in types, types)
        self.assertTrue("hotp" in types, types)

        # token classes are cached with calling 'get_token_types()'
        self.assertTrue("pi_token_classes" in this.config, this.config)
        self.assertTrue("pi_token_types" in this.config, this.config)
        r = get_token_classes()
        self.assertTrue(TotpTokenClass in r, r)
        self.assertTrue(HotpTokenClass in r, r)
Ejemplo n.º 5
0
    def test_02_token(self):
        r = get_token_list()
        self.assertTrue("privacyidea.lib.tokens.totptoken" in r, r)
        self.assertTrue("privacyidea.lib.tokens.hotptoken" in r, r)

        # check modules
        mlist = get_token_module_list()
        mod_name = "privacyidea.lib.tokens.totptoken"
        module = importlib.import_module(mod_name)
        self.assertTrue(module in mlist, mlist)

        #        r = get_resolver_classes()
        #        self.assertTrue(UserResolver in r, r)
        #        self.assertTrue(PWResolver in r, r)

        # get_token_class_dict
        (classes, types) = get_token_class_dict()
        self.assertTrue(
            'privacyidea.lib.tokens.hotptoken.HotpTokenClass' in classes,
            classes)
        self.assertTrue(
            classes.get('privacyidea.lib.tokens.hotptoken.HotpTokenClass') ==
            HotpTokenClass, classes)
        self.assertTrue(
            'privacyidea.lib.tokens.totptoken.TotpTokenClass' in classes,
            classes)
        self.assertTrue(
            classes.get('privacyidea.lib.tokens.totptoken.TotpTokenClass') ==
            TotpTokenClass, classes)

        self.assertTrue(
            'privacyidea.lib.tokens.hotptoken.HotpTokenClass' in types, types)
        self.assertTrue(
            'privacyidea.lib.tokens.totptoken.TotpTokenClass' in types, types)
        self.assertTrue(
            types.get('privacyidea.lib.tokens.hotptoken'
                      '.HotpTokenClass') == "hotp", types)
        self.assertTrue(
            types.get('privacyidea.lib.tokens.totptoken'
                      '.TotpTokenClass') == "totp", types)

        types = get_token_types()
        self.assertTrue("totp" in types, types)
        self.assertTrue("hotp" in types, types)

        # token classes are cached with calling 'get_token_types()'
        self.assertTrue("pi_token_classes" in this.config, this.config)
        self.assertTrue("pi_token_types" in this.config, this.config)
        r = get_token_classes()
        self.assertTrue(TotpTokenClass in r, r)
        self.assertTrue(HotpTokenClass in r, r)

        # Test custom token types
        with self.app_context:
            self.app.config['PI_TOKEN_MODULES'] = 'tests.testdata.fancytoken'
            r = get_token_list()
            self.assertIn("tests.testdata.fancytoken", r, r)
            mlist = get_token_module_list()
            mod = importlib.import_module('tests.testdata.fancytoken')
            self.assertTrue(mod in mlist, mlist)
            (classes, types) = get_token_class_dict()
            self.assertIn('tests.testdata.fancytoken.FancyTokenClass', classes,
                          classes)
            self.assertIn('tests.testdata.fancytoken.FancyTokenClass', types,
                          types)
            self.assertEqual(
                types['tests.testdata.fancytoken.FancyTokenClass'], 'fancy',
                types)
            self.app.config.pop('PI_TOKEN_MODULES')
Ejemplo n.º 6
0
    def test_02_token(self):
        r = get_token_list()
        self.assertTrue("privacyidea.lib.tokens.totptoken" in r, r)
        self.assertTrue("privacyidea.lib.tokens.hotptoken" in r, r)

        # check modules
        mlist = get_token_module_list()
        mod_name = "privacyidea.lib.tokens.totptoken"
        module = importlib.import_module(mod_name)
        self.assertTrue(module in mlist, mlist)

        #        r = get_resolver_classes()
        #        self.assertTrue(UserResolver in r, r)
        #        self.assertTrue(PWResolver in r, r)

        # get_token_class_dict
        (classes, types) = get_token_class_dict()
        self.assertTrue(
            'privacyidea.lib.tokens.hotptoken.HotpTokenClass' in classes,
            classes)
        self.assertTrue(
            classes.get('privacyidea.lib.tokens.hotptoken.HotpTokenClass') ==
            HotpTokenClass, classes)
        self.assertTrue(
            'privacyidea.lib.tokens.totptoken.TotpTokenClass' in classes,
            classes)
        self.assertTrue(
            classes.get('privacyidea.lib.tokens.totptoken.TotpTokenClass') ==
            TotpTokenClass, classes)

        self.assertTrue(
            'privacyidea.lib.tokens.hotptoken.HotpTokenClass' in types, types)
        self.assertTrue(
            'privacyidea.lib.tokens.totptoken.TotpTokenClass' in types, types)
        self.assertTrue(
            types.get('privacyidea.lib.tokens.hotptoken'
                      '.HotpTokenClass') == "hotp", types)
        self.assertTrue(
            types.get('privacyidea.lib.tokens.totptoken'
                      '.TotpTokenClass') == "totp", types)

        types = get_token_types()
        self.assertTrue("totp" in types, types)
        self.assertTrue("hotp" in types, types)
        # Now the resolver types are contained.
        self.assertTrue("pi_token_types" in current_app.config,
                        current_app.config)
        types = get_token_types()
        self.assertTrue("totp" in types, types)
        self.assertTrue("hotp" in types, types)

        # At the beginning the token classes are not cached.
        self.assertFalse("pi_token_classes" in current_app.config,
                         current_app.config)
        r = get_token_classes()
        self.assertTrue(TotpTokenClass in r, r)
        self.assertTrue(HotpTokenClass in r, r)
        # Now the token classes are cached
        self.assertTrue("pi_token_classes" in current_app.config,
                        current_app.config)
        r = get_token_classes()
        self.assertTrue(TotpTokenClass in r, r)
        self.assertTrue(HotpTokenClass in r, r)