1
- from flask import Flask , render_template , request , redirect , session , url_for , flash , jsonify
1
+ import cv2
2
+ import crypto
3
+ from flask import Flask , Response , render_template , request , redirect , session , url_for , flash , jsonify
2
4
from flask_mail import Mail , Message
3
5
from werkzeug .utils import secure_filename
4
- from facedetector import faceencodingvalues
5
- from flask_recaptcha import ReCaptcha
6
+ from facedetector import faceencodingvalues , predata , detect
7
+ import tokens
6
8
import db
7
9
import os
8
10
13
15
app .config ['UPLOAD_FOLDER' ] = UPLOAD_FOLDER
14
16
app .secret_key = os .getenv ('SECRET_KEY' )
15
17
16
- recaptcha = ReCaptcha (app = app )
17
18
18
- app .config .update (dict (
19
- RECAPTCHA_ENABLED = True ,
20
- RECAPTCHA_SITE_KEY = os .getenv ('RECAPTCHA_SITE_KEY' ),
21
- RECAPTCHA_SECRET_KEY = os .getenv ('RECAPTCHA_SECRET_KEY' ),
22
- RECAPTCHA_SIZE = 'invisible' ,
23
- RECAPTCHA_THEME = "dark" ,
24
- ))
25
-
26
- # RECAPTCHA_DATA_ATTRS = {'bind': 'recaptcha-submit', 'callback': 'onSubmitCallback', 'size': 'invisible'}
19
+ dbconnect = db .connect ()
27
20
28
- recaptcha = ReCaptcha ()
29
- recaptcha .init_app (app )
30
21
31
-
32
- dbconnect = db . connect ()
22
+ global recorded , cap
23
+ recorded = "NO"
33
24
34
25
# =============================================================================================================
35
26
36
- def mailing (tomail ,username ,token ,no ):
37
- if no == 1 :
38
- x ,y = "conformation Email" ,"confirm_email"
39
- elif no == 2 :
40
- x ,y = "reset password" ,"reset_password"
27
+ def mailing (tomail ,username ,token ):
41
28
try :
42
29
app .config ['MAIL_SERVER' ]= 'smtp.gmail.com'
43
30
app .config ['MAIL_PORT' ] = 465
@@ -48,14 +35,14 @@ def mailing(tomail,username,token,no):
48
35
mail = Mail (app )
49
36
msg = Message ('Hello' , sender = os .getenv ('EMAIL' ), recipients = [tomail ])
50
37
msg .body = "<h1>Hello Flask message sent from Flask-Mail</h1>"
51
- msg .subject = x
52
- link = "https ://adist.herokuapp.com/{} /{}" .format (y , token )
53
- msg .html = "<div><h1>change password </h1><h1><a href='" + link + "'}>click me </a></h1></div>"
38
+ msg .subject = "Login to your account"
39
+ link = "http ://127.0.0.1:8000/logincheck /{}" .format (token )
40
+ msg .html = "<div><h1>Login to your account </h1><h1><a href='" + link + "'}>click here </a></h1></div>"
54
41
msg .html = '''<div
55
42
style="text-align:center;max-width:600px;background:rgba( 255, 255, 255, 0.25 );box-shadow: 0 8px 32px 0 rgba( 31, 38, 135, 0.37 );backdrop-filter: blur( 4px );border-radius: 10px;border: 1px solid rgba( 255, 255, 255, 0.18 );">
56
- <h1>Adist </h1>
57
- <h2>Verification mail </h2>
58
- <h2>hi {} click the link below to conform your mail </h2>
43
+ <h1>Authenticator </h1>
44
+ <h2>Login to your account </h2>
45
+ <h2>hi {} click the link below to Login to your account </h2>
59
46
<h3><a href='{}' >Click Here</a></h3>
60
47
<p>Copy paste in browser if the above link is not working: {}</p>
61
48
</div>''' .format (username ,link ,link )
@@ -70,7 +57,10 @@ def mailing(tomail,username,token,no):
70
57
@app .route ("/home" )
71
58
def home ():
72
59
if "user" in session :
73
- return render_template ("index.html" ,user = session ["user" ])
60
+ if "verify" in session :
61
+ return render_template ("index.html" , user = session ["user" ])
62
+ else :
63
+ return render_template ("verify.html" , user = session ["user" ])
74
64
elif "admin" in session :
75
65
return redirect (url_for ("admin" ))
76
66
else :
@@ -83,18 +73,69 @@ def login():
83
73
return render_template ("login.html" ,RECAPTCHA_SITE_KEY = os .getenv ('RECAPTCHA_SITE_KEY' ))
84
74
if request .method == "POST" :
85
75
email = request .form .to_dict ()["email" ]
86
- q = "select * from tempusers where email = '{}'" .format (email )
76
+ q = "select username,email from tempusers where email = '{}'" .format (email )
87
77
result = db .select (q )
88
78
if len (result ) == 0 :
89
79
flash ("Invalid email" )
90
80
return redirect (url_for ("login" ))
91
81
elif len (result ) == 1 :
92
82
result = result [0 ]
93
- session ["user" ] = email
94
- return redirect (url_for ("home" ))
83
+ username ,email = result [0 ],result [1 ]
84
+
85
+ token = tokens .generate_confirmation_token (email )
86
+ q = "update tempusers set token = '{}' where email = '{}'" .format (token ,email )
87
+ if db .insert (q ):
88
+ if mailing (email ,username ,token ):
89
+ flash ("Check your email to for login link" )
90
+ return redirect (url_for ("login" ))
91
+ else :
92
+ flash ("Something went wrong during mailing" )
93
+ return redirect (url_for ("login" ))
94
+ else :
95
+ flash ("Something went wrong with our database" )
96
+ return redirect (url_for ("login" ))
97
+
98
+ return redirect (url_for ("login" ))
99
+ else :
100
+ flash ("Something went wrong" )
101
+ return redirect (url_for ("login" ))
102
+
103
+ # ============================================================================================================
104
+
105
+ @app .route ("/logincheck/<token>" )
106
+ def logincheck (token ):
107
+ email = tokens .confirm_token (token )
108
+ if email == "The token has expired" :
109
+ flash ("The token has expired" )
110
+ return redirect (url_for ("login" ))
111
+ elif email == "the token is invalid" :
112
+ flash ("the token is invalid" )
113
+ return redirect (url_for ("login" ))
114
+ elif email :
115
+ q = "select username,email,token from tempusers where email = '{}'" .format (email )
116
+ result = db .select (q )
117
+ if len (result ) == 1 :
118
+ result = result [0 ]
119
+ username ,email = result [0 ],result [1 ]
120
+ if token == result [2 ]:
121
+ q = "update tempusers set token = 'no' where email = '{}'" .format (email )
122
+ if db .insert (q ):
123
+ session ["user" ] = email
124
+ return redirect (url_for ("home" ))
125
+
126
+ else :
127
+ flash ("Something went wrong with our database" )
128
+ return redirect (url_for ("login" ))
129
+ else :
130
+ flash ("use the link that was last sent to your email" )
131
+ return redirect (url_for ("login" ))
95
132
else :
96
133
flash ("Something went wrong" )
97
134
return redirect (url_for ("login" ))
135
+ else :
136
+ flash ("Something went wrong" )
137
+ return redirect (url_for ("login" ))
138
+
98
139
99
140
# ============================================================================================================
100
141
@@ -135,7 +176,7 @@ def admin():
135
176
if len (facelocs )== 0 :
136
177
flash ("No face detected" )
137
178
return redirect (url_for ("admin" ))
138
- q = "insert into tempusers(username,email,token,encodings) values('{}','{}','{}','{}')" .format (username ,email ,"no" ,str (faceencodings .tolist ()))
179
+ q = "insert into tempusers(username,email,token,encodings) values('{}','{}','{}','{}')" .format (username ,email ,"no" ,crypto . encryption ( str (faceencodings .tolist () )))
139
180
db .insert (q )
140
181
flash ("User added" )
141
182
return redirect (url_for ("admin" ))
@@ -153,6 +194,35 @@ def admin():
153
194
return redirect (url_for ("admin" ))
154
195
155
196
197
+ # ============================================================================================================
198
+
199
+ cap = cv2 .VideoCapture (0 )
200
+ # =============================for attendence recording============================================================================================
201
+ def gen_frames (email ):
202
+ global recorded ,cap
203
+ cap = cv2 .VideoCapture (0 )
204
+ while True :
205
+ sucess ,img = cap .read ()
206
+ (frame ,ans ) = detect (img ,email )
207
+ recorded = ans
208
+ if recorded == "YES" :
209
+ break
210
+
211
+ yield (b'--frame\r \n '
212
+ b'Content-Type: image/jpeg\r \n \r \n ' + frame + b'\r \n ' )
213
+ print ("yield condition exit" )
214
+ cap .release ()
215
+
216
+
217
+
218
+ @app .route ("/video_feed" )
219
+ def video_feed ():
220
+ predata (session ["user" ])
221
+ return Response (gen_frames (session ["user" ]),
222
+ mimetype = 'multipart/x-mixed-replace; boundary=frame' )
223
+
224
+
225
+
156
226
# ============================================================================================================
157
227
158
228
@app .route ("/logout" )
@@ -162,7 +232,8 @@ def logout():
162
232
163
233
@app .errorhandler (404 )
164
234
def page_not_found (e ):
165
- return redirect ("/" )
235
+ flash ("Page not found" )
236
+ return redirect ("/" )
166
237
167
238
168
239
if __name__ == '__main__' :
0 commit comments