Update Telegram instructions. Fix redirect after login when adding Telegram integration.

This commit is contained in:
Pēteris Caune 2020-02-27 15:52:00 +02:00
parent 0c9c453ea0
commit 29e016d0fc
No known key found for this signature in database
GPG Key ID: E28D7679E9A9EDE2
8 changed files with 36 additions and 8 deletions

View File

@ -15,6 +15,7 @@ All notable changes to this project will be documented in this file.
- API security: check channel ownership when setting check's channels - API security: check channel ownership when setting check's channels
- API: update check's "alert_after" field when changing schedule - API: update check's "alert_after" field when changing schedule
- API: validate channel identifiers before creating/updating a check (#335) - API: validate channel identifiers before creating/updating a check (#335)
- Fix redirect after login when adding Telegram integration
## v1.13.0 - 2020-02-13 ## v1.13.0 - 2020-02-13

View File

@ -1,4 +1,5 @@
from datetime import timedelta as td from datetime import timedelta as td
from urllib.parse import urlparse
import uuid import uuid
from django.conf import settings from django.conf import settings
@ -42,12 +43,17 @@ NEXT_WHITELIST = (
"hc-p-channels", "hc-p-channels",
"hc-add-slack", "hc-add-slack",
"hc-add-pushover", "hc-add-pushover",
"hc-add-telegram",
) )
def _is_whitelisted(path): def _is_whitelisted(redirect_url):
if not redirect_url:
return False
parsed = urlparse(redirect_url)
try: try:
match = resolve(path) match = resolve(parsed.path)
except Resolver404: except Resolver404:
return False return False

View File

@ -34,6 +34,13 @@ class AddTelegramTestCase(BaseTestCase):
self.assertEqual(c.telegram_name, "My Group") self.assertEqual(c.telegram_name, "My Group")
self.assertEqual(c.project, self.project) self.assertEqual(c.project, self.project)
def test_it_handles_bad_signature(self):
self.client.login(username="alice@example.org", password="password")
r = self.client.get(self.url + "?bad-signature")
self.assertContains(r, "Incorrect Link")
self.assertFalse(Channel.objects.exists())
@patch("hc.api.transports.requests.request") @patch("hc.api.transports.requests.request")
def test_it_sends_invite(self, mock_get): def test_it_sends_invite(self, mock_get):
data = { data = {

View File

@ -30,6 +30,7 @@ channel_urls = [
), ),
path("add_discord/", views.add_discord_complete, name="hc-add-discord-complete"), path("add_discord/", views.add_discord_complete, name="hc-add-discord-complete"),
path("add_pushover/", views.add_pushover_help), path("add_pushover/", views.add_pushover_help),
path("telegram/", views.add_telegram_help),
path("telegram/bot/", views.telegram_bot, name="hc-telegram-webhook"), path("telegram/bot/", views.telegram_bot, name="hc-telegram-webhook"),
path("add_pdc/", views.add_pdc_help), path("add_pdc/", views.add_pdc_help),
path("add_slack/", views.add_slack_help), path("add_slack/", views.add_slack_help),

View File

@ -1398,12 +1398,24 @@ def telegram_bot(request):
return HttpResponse() return HttpResponse()
def add_telegram_help(request):
ctx = {
"page": "channels",
"bot_name": settings.TELEGRAM_BOT_NAME,
}
return render(request, "integrations/add_telegram.html", ctx)
@login_required @login_required
def add_telegram(request): def add_telegram(request):
chat_id, chat_type, chat_name = None, None, None chat_id, chat_type, chat_name = None, None, None
qs = request.META["QUERY_STRING"] qs = request.META["QUERY_STRING"]
if qs: if qs:
chat_id, chat_type, chat_name = signing.loads(qs, max_age=600) try:
chat_id, chat_type, chat_name = signing.loads(qs, max_age=600)
except signing.BadSignature:
return render(request, "bad_link.html")
if request.method == "POST": if request.method == "POST":
project = _get_project_for_user(request, request.POST.get("project")) project = _get_project_for_user(request, request.POST.get("project"))

Binary file not shown.

Before

Width:  |  Height:  |  Size: 26 KiB

After

Width:  |  Height:  |  Size: 35 KiB

View File

@ -1,7 +1,7 @@
{% extends "base.html" %} {% extends "base.html" %}
{% load humanize static hc_extras %} {% load humanize static hc_extras %}
{% block title %}Add Slack - {% site_name %}{% endblock %} {% block title %}Slack Integration for {% site_name %}{% endblock %}
{% block content %} {% block content %}
<div class="row"> <div class="row">

View File

@ -1,7 +1,7 @@
{% extends "base.html" %} {% extends "base.html" %}
{% load compress humanize static hc_extras %} {% load compress humanize static hc_extras %}
{% block title %}Notification Channels - {% site_name %}{% endblock %} {% block title %}Telegram Integration for {% site_name %}{% endblock %}
{% block content %} {% block content %}
@ -23,8 +23,7 @@
</p> </p>
</div> </div>
<h2>Integration Settings</h2> <h2>Integration Settings</h2>
<form id="add-telegram" method="post" class="form-horizontal"> <form id="add-telegram" method="post" class="form-horizontal">
{% csrf_token %} {% csrf_token %}
<div class="form-group"> <div class="form-group">
@ -98,7 +97,9 @@
<p>Click or tap on the confirmation link, and <p>Click or tap on the confirmation link, and
{% site_name %} will open in a browser window asking you to {% site_name %} will open in a browser window asking you to
confirm the new integration.</p> confirm the new integration.</p>
<p>Confirm the integration, and it's done!</p> <p>Select the project you want the Telegram integration added to,
click on "Connect Telegram", and it's done!</p>
</div> </div>
<div class="col-sm-6"> <div class="col-sm-6">
<img <img