-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
32 lines (26 loc) · 957 Bytes
/
api.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
from flask import Blueprint, abort, request
from models import Tokens, Lock, db
api_blueprint = Blueprint('api', __name__)
def check_authority(nfc_id, lock_id):
token = db.session.query(Tokens).join(Lock).filter(
Tokens.nfc_id == nfc_id,
Tokens.lock_id == lock_id
).first()
return token is not None
@api_blueprint.route('/api', methods=['GET', 'POST'])
def api():
if request.method == 'POST':
token = request.form['token']
card_id = request.form['card_id']
print(token, card_id)
if token is None or card_id is None: abort(403)
lock = Lock.query.filter_by(token=token).first()
if lock is None: abort(403)
lock_id = lock.id
if check_authority(card_id, lock_id):
return 'Authorized'
else:
abort(403)
elif request.method == 'GET':
return '<title>API</title><h1>For API usage, dummy!</h1>'
abort(403)