From 71f289c1d06c688eb0ceab4610d0f691e7359933 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Christian=20Gr=C3=BCnhage?= Date: Wed, 10 Aug 2022 13:21:29 +0200 Subject: [PATCH] fix(modules/matrix): fix regression in access token handling 52785ab768b30d179b6dc201dada70f059dd4fb7 refactored the login logic, which, switching to nio's token login when being passed a token. The token being passed is an *access* token though, not a *login* token, so that fails and thereby breaks any tasks that use access tokens instead of username/password for authenticating to matrix. It also accidentally negated the logic on when to invalidate a token upon logout, so that has been resolved as well. --- plugins/module_utils/matrix.py | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/plugins/module_utils/matrix.py b/plugins/module_utils/matrix.py index 13351a9..e7f0fb2 100644 --- a/plugins/module_utils/matrix.py +++ b/plugins/module_utils/matrix.py @@ -90,7 +90,7 @@ class AnsibleNioModule: if user_logout is None: # If a user/password login is provided, should we logout when exiting? - self.user_logout = self.module.params.get('token') is not None + self.user_logout = self.module.params.get("token") is None else: self.user_logout = user_logout @@ -105,21 +105,25 @@ class AnsibleNioModule: async def matrix_login(self): # Login with token or supplied user account - if self.module.params.get('token') is None: - self.client = AsyncClient(self.module.params.get('hs_url'), self.module.params.get('user_id')) - login_response = await self.client.login(password=self.module.params.get('password')) + if self.module.params.get("token") is None: + self.client = AsyncClient( + self.module.params.get("hs_url"), self.module.params.get("user_id") + ) + login_response = await self.client.login( + password=self.module.params.get("password") + ) + if isinstance(login_response, LoginResponse): + self.access_token = login_response.access_token + self.device_id = login_response.device_id + else: + result = { + "msg": login_response.message, + "http_status_code": login_response.status_code, + } + self.module.fail_json(**result) else: - self.client = AsyncClient(self.module.params.get('hs_url')) - login_response = await self.client.login(token=self.module.params.get('token')) - if isinstance(login_response, LoginResponse): - self.access_token = login_response.access_token - self.device_id = login_response.device_id - else: - result = { - 'msg': login_response.message, - 'http_status_code': login_response.status_code - } - self.module.fail_json(**result) + self.client = AsyncClient(self.module.params.get("hs_url")) + self.client.access_token = self.module.params.get("token") async def matrix_logout(self): if self.client.logged_in: