Example #1
0
def verify_password(username_or_token, password):
    """Verify the user name and password.

    Instead of a user name an authentication token
    or an API token can be provided.
    This function is called by the
    @auth.login_required decorator.

    Args:
        username_or_token (str): The username or an authentication token
        password (str): The optional user password, not required in case of token

    Returns:
        bool: True if authorized or False if not

    """
    # first try to authenticate by token
    user = ActiniaUser.verify_auth_token(username_or_token)

    if not user:
        user = ActiniaUser.verify_api_key(username_or_token)

    if not user:
        # try to authenticate with username/password
        user = ActiniaUser(user_id=username_or_token)
        if not user.exists() or not user.verify_password(password):
            return False
    # Store the user globally
    g.user = user
    return True
    def test_create_delete_user(self):

        # Make sure the user database is empty
        user = ActiniaUser(self.user_id)
        if user.exists():
            print("Delete existing user")
            user.delete()

        # Create a new user
        user = ActiniaUser.create_user(self.user_id,
                                       self.user_group,
                                       self.password,
                                       user_role="admin",
                                       accessible_datasets={"nc_spm_08": ["PERMANENT", "user1"]},
                                       accessible_modules=["g.region", "g.mapset", "r.slope.aspect"],
                                       cell_limit=1000,
                                       process_num_limit=3,
                                       process_time_limit=30)

        print(user)

        self.assertEqual(user.get_role(), "admin")
        self.assertEqual(user.get_id(), self.user_id)
        self.assertEqual(user.get_group(), self.user_group)
        self.assertEqual(user.get_cell_limit(), 1000)
        self.assertEqual(user.get_process_num_limit(), 3)
        self.assertEqual(user.get_process_time_limit(), 30)

        datasets = user.get_accessible_datasets()
        modules = user.get_accessible_modules()

        self.assertTrue("nc_spm_08" in datasets)
        self.assertTrue("PERMANENT" in datasets["nc_spm_08"])
        self.assertTrue("g.region" in modules)

        token = user.generate_auth_token()
        user_2 = ActiniaUser.verify_auth_token(token)

        print(user_2)

        self.assertTrue(user_2.exists())
        self.assertEqual(user_2.get_role(), "admin")
        self.assertEqual(user_2.get_id(), self.user_id)
        self.assertEqual(user.get_group(), self.user_group)
        self.assertEqual(user_2.get_cell_limit(), 1000)
        self.assertEqual(user_2.get_process_num_limit(), 3)
        self.assertEqual(user_2.get_process_time_limit(), 30)

        datasets = user_2.get_accessible_datasets()
        modules = user_2.get_accessible_modules()

        self.assertTrue("nc_spm_08" in datasets)
        self.assertTrue("PERMANENT" in datasets["nc_spm_08"])
        self.assertTrue("g.region" in modules)

        api_key = user.generate_api_key()
        user_3 = ActiniaUser.verify_api_key(api_key)

        print(user_3)

        self.assertTrue(user_3.exists())
        self.assertEqual(user_3.get_role(), "admin")
        self.assertEqual(user_3.get_id(), self.user_id)
        self.assertEqual(user.get_group(), self.user_group)
        self.assertEqual(user_3.get_cell_limit(), 1000)
        self.assertEqual(user_3.get_process_num_limit(), 3)
        self.assertEqual(user_3.get_process_time_limit(), 30)

        datasets = user_3.get_accessible_datasets()
        modules = user_3.get_accessible_modules()

        self.assertTrue("nc_spm_08" in datasets)
        self.assertTrue("PERMANENT" in datasets["nc_spm_08"])
        self.assertTrue("g.region" in modules)

        self.assertTrue(user.delete())
        self.assertFalse(user_2.delete())
        self.assertFalse(user_3.delete())