This repository was archived by the owner on Jan 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhooks.py
More file actions
66 lines (44 loc) · 1.4 KB
/
webhooks.py
File metadata and controls
66 lines (44 loc) · 1.4 KB
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
# encoding: utf-8
from __future__ import unicode_literals, print_function
from functools import wraps
from os import environ
from flask import Flask, Response, request, jsonify
app = Flask(__name__)
TOKEN = environ.get('KOMPASSI_WEBHOOKS_TOKEN', 'secret')
def check_token_authentication():
payload = request.get_json()
if not isinstance(payload, dict):
return False
return payload.get('token') == TOKEN
def token_authentication_required(controller_function):
@wraps(controller_function)
def wrapped(*args, **kwargs):
if not check_token_authentication():
return jsonify(
code=401,
result='Unauthorized',
reason='Token missing or wrong. Hint: {"token": "secret"}',
), 401
return controller_function(*args, **kwargs)
return wrapped
@app.route('/webhooks/ping', methods=['POST'])
@token_authentication_required
def ping():
"""
This endpoint returns a JSON response and does nothing else. Useful for monitoring.
"""
return jsonify(
code=200,
result='OK',
message='Pong!',
)
@app.route('/webhooks/aliases', methods=['POST'])
@token_authentication_required
def update_aliases():
print("TODO put actual implementation here")
return jsonify(
code=200,
result='OK',
)
if __name__ == '__main__':
app.run('0.0.0.0', 33001)