-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathblueprint.py
73 lines (60 loc) · 2.39 KB
/
blueprint.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import requests as rq
import tweepy
from CTFd.utils.decorators import admins_only
from flask import Blueprint, render_template, request
from .db_utils import DBUtils
notifier_bp = Blueprint("notifier", __name__, template_folder="templates")
def load_bp(plugin_route):
@notifier_bp.route(plugin_route, methods=["GET"])
@admins_only
def get_config():
config = DBUtils.get_config()
return render_template("ctfd_notifier/config.html", config=config)
@notifier_bp.route(plugin_route, methods=["POST"])
@admins_only
def update_config():
config = request.form.to_dict()
del config["nonce"]
errors = test_config(config)
if len(errors) > 0:
return render_template(
"ctfd_notifier/config.html", config=DBUtils.get_config(), errors=errors
)
else:
DBUtils.save_config(config.items())
return render_template(
"ctfd_notifier/config.html", config=DBUtils.get_config()
)
return notifier_bp
def test_config(config):
errors = list()
if "discord_notifier" in config:
if config["discord_notifier"]:
webhookurl = config["discord_webhook_url"]
if not webhookurl.startswith(
"https://discordapp.com/api/webhooks/"
) and not webhookurl.startswith("https://discord.com/api/webhooks"):
errors.append("Invalid Webhook URL!")
else:
try:
r = rq.get(webhookurl)
if not r.status_code == 200:
errors.append("Could not verify that the Webhook is working!")
except rq.exceptions.RequestException as e:
errors.append("Invalid Webhook URL!")
if "twitter_notifier" in config:
if config["twitter_notifier"]:
try:
AUTH = tweepy.OAuthHandler(
config.get("twitter_consumer_key"),
config.get("twitter_consumer_secret"),
)
AUTH.set_access_token(
config.get("twitter_access_token"),
config.get("twitter_access_token_secret"),
)
API = tweepy.API(AUTH)
API.home_timeline()
except tweepy.TweepError:
errors.append("Invalid authentication Data!")
return errors