From a6d4e727e56bbd106227950bf082fe63447e8118 Mon Sep 17 00:00:00 2001 From: Chris Caron Date: Sat, 14 Sep 2024 17:11:47 -0400 Subject: [PATCH] m.login.token support for Matrix --- apprise/plugins/matrix.py | 64 ++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/apprise/plugins/matrix.py b/apprise/plugins/matrix.py index bb9c6dbb2..c626111ff 100644 --- a/apprise/plugins/matrix.py +++ b/apprise/plugins/matrix.py @@ -189,9 +189,11 @@ class NotifyMatrix(NotifyBase): '{schema}://{token}', '{schema}://{user}@{token}', - # Disabled webhook + # Matrix Server '{schema}://{user}:{password}@{host}/{targets}', '{schema}://{user}:{password}@{host}:{port}/{targets}', + '{schema}://{token}@{host}/{targets}', + '{schema}://{token}@{host}:{port}/{targets}', # Webhook mode '{schema}://{user}:{token}@{host}/{targets}', @@ -864,32 +866,40 @@ def _login(self): # Login not required; silently skip-over return True - if not (self.user and self.password): - # It's not possible to register since we need these 2 values to - # make the action possible. - self.logger.warning( - 'Failed to login to Matrix server: ' - 'user/pass combo is missing.') - return False + if (self.user and self.password): + # Prepare our Authentication Payload + if self.version == MatrixVersion.V3: + payload = { + 'type': 'm.login.password', + 'identifier': { + 'type': 'm.id.user', + 'user': self.user, + }, + 'password': self.password, + } - # Prepare our Authentication Payload - if self.version == MatrixVersion.V3: - payload = { - 'type': 'm.login.password', - 'identifier': { - 'type': 'm.id.user', + else: + payload = { + 'type': 'm.login.password', 'user': self.user, - }, - 'password': self.password, - } + 'password': self.password, + } - else: + elif self.password: + # token payload = { - 'type': 'm.login.password', - 'user': self.user, - 'password': self.password, + 'type': 'm.login.token', + 'token': self.password, } + else: + # It's not possible to register since we need these 2 values to + # make the action possible. + self.logger.warning( + 'Failed to login to Matrix server: ' + 'token or user/pass combo is missing.') + return False + # Build our URL postokay, response = self._fetch('/login', payload=payload) if not (postokay and isinstance(response, dict)): @@ -1442,9 +1452,10 @@ def url(self, privacy=False, *args, **kwargs): safe=''), ) - elif self.user: - auth = '{user}@'.format( - user=NotifyMatrix.quote(self.user, safe=''), + elif self.user or self.password: + auth = '{value}@'.format( + value=NotifyMatrix.quote( + self.user if self.user else self.password, safe=''), ) default_port = 443 if self.secure else 80 @@ -1522,6 +1533,11 @@ def parse_url(url): if 'token' in results['qsd'] and len(results['qsd']['token']): results['password'] = NotifyMatrix.unquote(results['qsd']['token']) + elif not results['password'] and results['user']: + # swap + results['password'] = results['user'] + results['user'] = None + # Support the use of the version= or v= keyword if 'version' in results['qsd'] and len(results['qsd']['version']): results['version'] = \