-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
69 lines (49 loc) · 1.63 KB
/
main.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
"""Mosquitto HTTP auth backend."""
import os
import json
import logging
import requests
from flask import Flask, request, Response, abort
app = Flask(__name__.split(".")[0])
app.config["DEBUG"] = True
API_URL = os.getenv("API_URL", "https://www.iot-lab.info/api")
@app.route("/user", methods=["POST"])
def user():
"""Check user credentials."""
app.logger.info(f"user request from {request.environ['REMOTE_ADDR']}")
try:
data = json.loads(request.data.decode())
except:
return Response('{"OK": false}')
username = data["username"]
password = data["password"]
try:
req = requests.get(f"{API_URL}/user", auth=(username, password))
ret = "true" if req.status_code == 200 else "false"
except:
ret = "false"
if ret == "true":
app.logger.info(f"Connection accepted for user '{username}'")
else:
app.logger.info(f"Connection refused for user '{username}'")
return Response(f'{{"OK": {ret}}}')
@app.route("/acls", methods=["POST"])
def acls():
"""Check acls."""
app.logger.info(f"acl request from {request.environ['REMOTE_ADDR']}")
try:
data = json.loads(request.data.decode())
except:
return Response('{"OK": false}')
username = data["username"]
topic = data["topic"]
ret = "true" if topic.startswith(f"iotlab/{username}") else "false"
if ret == "true":
app.logger.info(
f"ACL accepted for user '{username}' on topic '{topic}'"
)
else:
app.logger.info(
f"ACL rejected for user '{username}' on topic '{topic}'"
)
return Response(f'{{"OK": {ret}}}')