From 24e79c834085826a11cefe1b14a0d38e5efbcf2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Thu, 3 Mar 2022 14:47:55 +0100 Subject: [PATCH] new: get_new_authkey for a user --- pymisp/api.py | 13 +++++++++++++ tests/testlive_comprehensive.py | 3 +++ 2 files changed, 16 insertions(+) diff --git a/pymisp/api.py b/pymisp/api.py index 8311464..88d6d7d 100644 --- a/pymisp/api.py +++ b/pymisp/api.py @@ -2192,6 +2192,19 @@ class PyMISP: usersettings.append(us) return u, role, usersettings + def get_new_authkey(self, user: Union[MISPUser, int, str, UUID] = 'me') -> str: + '''Get a new authorization key for a specific user, defaults to user doing the call. + + :param user: The owner of the key + ''' + data = {"user_id": get_uuid_or_id_from_abstract_misp(user)} + r = self._prepare_request('POST', '/auth_keys/add', data=data) + authkey = self._check_json_response(r) + if 'AuthKey' in authkey and 'authkey_raw' in authkey['AuthKey']: + return authkey['AuthKey']['authkey_raw'] + else: + raise PyMISPUnexpectedResponse(f'Unable to get authkey: {authkey}') + def add_user(self, user: MISPUser, pythonify: bool = False) -> Union[Dict, MISPUser]: """Add a new user diff --git a/tests/testlive_comprehensive.py b/tests/testlive_comprehensive.py index b6dc153..ca19d56 100644 --- a/tests/testlive_comprehensive.py +++ b/tests/testlive_comprehensive.py @@ -1753,6 +1753,9 @@ class TestComprehensive(unittest.TestCase): user.email = 'foo@bar.de' user = self.admin_misp_connector.update_user(user, pythonify=True) self.assertEqual(user.email, 'foo@bar.de') + # get API key + key = self.user_misp_connector.get_new_authkey() + self.assertTrue(isinstance(key, str)) def test_organisation(self): # Get list