-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
151 lines (138 loc) · 4.99 KB
/
Copy pathapp.py
File metadata and controls
151 lines (138 loc) · 4.99 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from flask import Flask, render_template, request, url_for, jsonify
import kv_mail
from itsdangerous import URLSafeTimedSerializer, SignatureExpired
from dotenv import load_dotenv
import os
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
from firebase_config import CONFIG
load_dotenv()
app = Flask(__name__)
TOKEN = URLSafeTimedSerializer(os.getenv("SECRET_KEY"))
cred = credentials.Certificate(CONFIG)
firebase_admin.initialize_app(cred)
db = firestore.client()
@app.route('/')
def home():
return render_template('index.html')
@app.route('/rfid/<rfid>')
def rfid(rfid):
try:
doc_ref = db.collection(u'users').document(rfid)
doc = doc_ref.get()
if doc.exists:
user_email = doc.to_dict()[u'email']
token = TOKEN.dumps(rfid, salt='rfid')
doc_ref = db.collection(u'users').document(rfid)
doc = doc_ref.get()
blocked = doc.to_dict()[u'blocked']
if blocked:
body = 'Alert some one is using your blocked card'
kv_mail.mail(
os.getenv('EMAIL'),
os.getenv('PASSWORD'),
user_email,
"Alert ",
body,
)
res = {'result': -1, 'message': 'Card has been blocked'}
return jsonify(res)
else:
body = (
'click here to login: '
+ url_for('verified_rfid', token=token, _external=True)
+ '\n \n \nTo block this card click here: '
+ url_for('block', token=token, _external=True)
)
kv_mail.mail(
os.getenv('EMAIL'),
os.getenv('PASSWORD'),
user_email,
"Verify your password",
body,
)
res = {'result': 1, 'message': 'Email sent to : ' + user_email}
return jsonify(res)
else:
res = {'result': 0, 'message': 'User not found'}
return jsonify(res)
except Exception as e:
# print(e)
return "Error"
@app.route('/verified_rfid/<token>')
def verified_rfid(token):
try:
rfid = TOKEN.loads(token, salt='rfid', max_age=100)
except SignatureExpired:
res = {'result': 0}
return jsonify(res)
return render_template('unlock.html', rfid=rfid)
@app.route('/block/<token>')
def block(token):
try:
rfid = TOKEN.loads(token, salt='rfid', max_age=500)
doc_ref = db.collection(u'users').document(rfid)
doc = doc_ref.get()
username = doc.to_dict()[u'username']
doc_ref.update({u'blocked': True})
res = {'result': -1, 'message': 'User ' + username + ' has been blocked'}
return jsonify(res)
except SignatureExpired:
res = {'result': 0, 'message': 'block link expired'}
return jsonify(res)
@app.route('/verify', methods=('GET', 'POST'))
def verify():
if request.method == 'POST':
pswd = request.form["pswd"]
rfid = request.form["rfid"]
doc_ref = db.collection(u'users').document(rfid)
doc = doc_ref.get()
blocked = doc.to_dict()[u'blocked']
if blocked is True:
res = {'result': -1, 'message': 'User is blocked'}
return jsonify(res)
if doc.exists:
password = doc.to_dict()[u'password']
if pswd == password:
res = {'result': 1, 'message': 'User verified'}
doc_ref.update({u'status': True})
return jsonify(res)
else:
res = {'result': 0, 'message': 'Wrong password'}
return jsonify(res)
elif request.method == 'GET':
pswd = request.args.get('pswd')
rfid = request.args.get('rfid')
doc_ref = db.collection(u'users').document(rfid)
doc = doc_ref.get()
blocked = doc.to_dict()[u'blocked']
if blocked is True:
res = {'result': -1, 'message': 'User is blocked'}
return jsonify(res)
if doc.exists:
password = doc.to_dict()[u'password']
if pswd == password:
res = {'result': 1, 'message': 'User verified'}
return jsonify(res)
else:
res = {'result': 0, 'message': 'Wrong password'}
return jsonify(res)
@app.route('/open/<rfid>')
def open(rfid):
doc_ref = db.collection(u'users').document(rfid)
doc = doc_ref.get()
if doc.exists:
status = doc.to_dict()[u'status']
if status is True:
doc_ref.update({u'status': False})
res = {'result': 1, 'message': 'User verified'}
return jsonify(res)
else:
res = {'result': 0, 'message': 'Please dont make direct calls to this url'}
return jsonify(res)
else:
res = {'result': 0, 'message': 'User not found attempt to login directly'}
return jsonify(res)
if __name__ == '__main__':
app.run("0.0.0.0", debug=False)