Skip to content

Commit 33c7569

Browse files
authored
RocketChat Token Support (#1060)
1 parent 7a985ea commit 33c7569

File tree

2 files changed

+54
-15
lines changed

2 files changed

+54
-15
lines changed

apprise/plugins/NotifyRocketChat.py

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,17 @@ class RocketChatAuthMode:
5959
# providing a webhook
6060
WEBHOOK = "webhook"
6161

62+
# Support token submission
63+
TOKEN = "token"
64+
6265
# Providing a username and password (default)
6366
BASIC = "basic"
6467

6568

6669
# Define our authentication modes
6770
ROCKETCHAT_AUTH_MODES = (
6871
RocketChatAuthMode.WEBHOOK,
72+
RocketChatAuthMode.TOKEN,
6973
RocketChatAuthMode.BASIC,
7074
)
7175

@@ -107,6 +111,8 @@ class NotifyRocketChat(NotifyBase):
107111
templates = (
108112
'{schema}://{user}:{password}@{host}:{port}/{targets}',
109113
'{schema}://{user}:{password}@{host}/{targets}',
114+
'{schema}://{user}:{token}@{host}:{port}/{targets}',
115+
'{schema}://{user}:{token}@{host}/{targets}',
110116
'{schema}://{webhook}@{host}',
111117
'{schema}://{webhook}@{host}:{port}',
112118
'{schema}://{webhook}@{host}/{targets}',
@@ -135,6 +141,11 @@ class NotifyRocketChat(NotifyBase):
135141
'type': 'string',
136142
'private': True,
137143
},
144+
'token': {
145+
'name': _('API Token'),
146+
'map_to': 'password',
147+
'private': True,
148+
},
138149
'webhook': {
139150
'name': _('Webhook'),
140151
'type': 'string',
@@ -230,13 +241,20 @@ def __init__(self, webhook=None, targets=None, mode=None, avatar=None,
230241
if self.webhook is not None:
231242
# Just a username was specified, we treat this as a webhook
232243
self.mode = RocketChatAuthMode.WEBHOOK
244+
elif self.password and len(self.password) > 32:
245+
self.mode = RocketChatAuthMode.TOKEN
233246
else:
234247
self.mode = RocketChatAuthMode.BASIC
235248

236-
if self.mode == RocketChatAuthMode.BASIC \
249+
self.logger.debug(
250+
"Auto-Detected Rocketchat Auth Mode: %s", self.mode)
251+
252+
if self.mode in (RocketChatAuthMode.BASIC, RocketChatAuthMode.TOKEN) \
237253
and not (self.user and self.password):
238254
# Username & Password is required for Rocket Chat to work
239-
msg = 'No Rocket.Chat user/pass combo was specified.'
255+
msg = 'No Rocket.Chat {} was specified.'.format(
256+
'user/pass combo' if self.mode == RocketChatAuthMode.BASIC else
257+
'user/apikey')
240258
self.logger.warning(msg)
241259
raise TypeError(msg)
242260

@@ -245,6 +263,13 @@ def __init__(self, webhook=None, targets=None, mode=None, avatar=None,
245263
self.logger.warning(msg)
246264
raise TypeError(msg)
247265

266+
if self.mode == RocketChatAuthMode.TOKEN:
267+
# Set our headers for further communication
268+
self.headers.update({
269+
'X-User-Id': self.user,
270+
'X-Auth-Token': self.password,
271+
})
272+
248273
# Validate recipients and drop bad ones:
249274
for recipient in parse_list(targets):
250275
result = IS_CHANNEL.match(recipient)
@@ -309,12 +334,13 @@ def url(self, privacy=False, *args, **kwargs):
309334
params.update(self.url_parameters(privacy=privacy, *args, **kwargs))
310335

311336
# Determine Authentication
312-
if self.mode == RocketChatAuthMode.BASIC:
337+
if self.mode in (RocketChatAuthMode.BASIC, RocketChatAuthMode.TOKEN):
313338
auth = '{user}:{password}@'.format(
314339
user=NotifyRocketChat.quote(self.user, safe=''),
315340
password=self.pprint(
316341
self.password, privacy, mode=PrivacyMode.Secret, safe=''),
317342
)
343+
318344
else:
319345
auth = '{user}{webhook}@'.format(
320346
user='{}:'.format(NotifyRocketChat.quote(self.user, safe=''))
@@ -359,8 +385,11 @@ def send(self, body, title='', notify_type=NotifyType.INFO, **kwargs):
359385
# Call the _send_ function applicable to whatever mode we're in
360386
# - calls _send_webhook_notification if the mode variable is set
361387
# - calls _send_basic_notification if the mode variable is not set
362-
return getattr(self, '_send_{}_notification'.format(self.mode))(
363-
body=body, title=title, notify_type=notify_type, **kwargs)
388+
return getattr(self, '_send_{}_notification'.format(
389+
RocketChatAuthMode.WEBHOOK
390+
if self.mode == RocketChatAuthMode.WEBHOOK
391+
else RocketChatAuthMode.BASIC))(
392+
body=body, title=title, notify_type=notify_type, **kwargs)
364393

365394
def _send_webhook_notification(self, body, title='',
366395
notify_type=NotifyType.INFO, **kwargs):
@@ -412,7 +441,7 @@ def _send_basic_notification(self, body, title='',
412441
"""
413442
# Track whether we authenticated okay
414443

415-
if not self.login():
444+
if self.mode == RocketChatAuthMode.BASIC and not self.login():
416445
return False
417446

418447
# prepare JSON Object
@@ -432,9 +461,7 @@ def _send_basic_notification(self, body, title='',
432461
channel = channels.pop(0)
433462
payload['channel'] = channel
434463

435-
if not self._send(
436-
payload, notify_type=notify_type, headers=self.headers,
437-
**kwargs):
464+
if not self._send(payload, notify_type=notify_type, **kwargs):
438465

439466
# toggle flag
440467
has_error = True
@@ -447,15 +474,14 @@ def _send_basic_notification(self, body, title='',
447474
room = rooms.pop(0)
448475
payload['roomId'] = room
449476

450-
if not self._send(
451-
payload, notify_type=notify_type, headers=self.headers,
452-
**kwargs):
477+
if not self._send(payload, notify_type=notify_type, **kwargs):
453478

454479
# toggle flag
455480
has_error = True
456481

457-
# logout
458-
self.logout()
482+
if self.mode == RocketChatAuthMode.BASIC:
483+
# logout
484+
self.logout()
459485

460486
return not has_error
461487

@@ -476,7 +502,7 @@ def _payload(self, body, title='', notify_type=NotifyType.INFO):
476502
return payload
477503

478504
def _send(self, payload, notify_type, path='api/v1/chat.postMessage',
479-
headers={}, **kwargs):
505+
**kwargs):
480506
"""
481507
Perform Notify Rocket.Chat Notification
482508
"""
@@ -487,6 +513,9 @@ def _send(self, payload, notify_type, path='api/v1/chat.postMessage',
487513
api_url, self.verify_certificate))
488514
self.logger.debug('Rocket.Chat Payload: %s' % str(payload))
489515

516+
# Copy our existing headers
517+
headers = self.headers.copy()
518+
490519
# Apply minimum headers
491520
headers.update({
492521
'User-Agent': self.app_id,

test/test_plugin_rocket_chat.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@
107107
},
108108
'privacy_url': 'rockets://user:****@localhost',
109109
}),
110+
# A channel using token based
111+
('rockets://user:token@localhost/#channel?mode=token', {
112+
'instance': NotifyRocketChat,
113+
'privacy_url': 'rockets://user:****@localhost',
114+
}),
115+
# Token is detected based o it's length
116+
('rockets://user:{}@localhost/#channel'.format('t' * 40), {
117+
'instance': NotifyRocketChat,
118+
'privacy_url': 'rockets://user:****@localhost',
119+
}),
110120
# Several channels
111121
('rocket://user:pass@localhost/#channel1/#channel2/?avatar=Yes', {
112122
'instance': NotifyRocketChat,

0 commit comments

Comments
 (0)