Skip to content

Commit

Permalink
Matrix: Allow client API authorization with token parameter (#1236)
Browse files Browse the repository at this point in the history
  • Loading branch information
voc0der authored Nov 22, 2024
1 parent 5441c15 commit f17df45
Showing 1 changed file with 36 additions and 24 deletions.
60 changes: 36 additions & 24 deletions apprise/plugins/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,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}',
Expand Down Expand Up @@ -612,6 +614,9 @@ def _send_server_notification(self, body, title='',
Perform Direct Matrix Server Notification (no webhook)
"""

if self.access_token is None and self.password and not self.user:
self.access_token = self.password

if self.access_token is None:
# We need to register
if not self._login():
Expand Down Expand Up @@ -890,32 +895,33 @@ def _login(self):
# Login not required; silently skip-over
return True

if not (self.user and self.password):
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,
}

else:
payload = {
'type': 'm.login.password',
'user': self.user,
'password': 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: '
'user/pass combo is missing.')
'token or user/pass combo is missing.')
return False

# 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,
}

else:
payload = {
'type': 'm.login.password',
'user': self.user,
'password': self.password,
}

# Build our URL
postokay, response = self._fetch('/login', payload=payload)
if not (postokay and isinstance(response, dict)):
Expand Down Expand Up @@ -1483,9 +1489,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
Expand Down Expand Up @@ -1567,6 +1574,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'] = \
Expand Down

0 comments on commit f17df45

Please sign in to comment.