2021-09-09 06:29:31 -07:00
|
|
|
from flask import *
|
|
|
|
import pyrebase
|
|
|
|
from datetime import datetime
|
|
|
|
import pytz
|
|
|
|
import os
|
2021-09-10 07:30:39 -07:00
|
|
|
from dotenv import load_dotenv
|
2021-09-23 07:21:59 -07:00
|
|
|
import requests
|
2021-09-26 06:37:34 -07:00
|
|
|
from manage import manage
|
|
|
|
from upload import upload
|
2021-09-10 07:30:39 -07:00
|
|
|
load_dotenv()
|
2021-09-09 06:29:31 -07:00
|
|
|
app = Flask(__name__)
|
2021-09-26 06:37:34 -07:00
|
|
|
app.register_blueprint(manage)
|
|
|
|
app.register_blueprint(upload)
|
2021-09-09 06:29:31 -07:00
|
|
|
|
|
|
|
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY')
|
|
|
|
config = {
|
|
|
|
"apiKey": os.environ.get('apiKey'),
|
|
|
|
"authDomain": os.environ.get('authDomain'),
|
|
|
|
"databaseURL": os.environ.get('databaseURL'),
|
|
|
|
"storageBucket": os.environ.get('storageBucket'),
|
|
|
|
"serviceAccount": os.environ.get('serviceAccount'),
|
|
|
|
"messagingSenderId": os.environ.get('messagingSenderId'),
|
|
|
|
"appId": os.environ.get('appId'),
|
|
|
|
"measurementId": os.environ.get('measurementId'),
|
|
|
|
}
|
|
|
|
firebase = pyrebase.initialize_app(config)
|
|
|
|
auth = firebase.auth()
|
|
|
|
tz = pytz.timezone('Asia/Taipei')
|
|
|
|
|
|
|
|
|
|
|
|
def check_login_status():
|
|
|
|
return ('is_logged_in' not in session or
|
|
|
|
session['is_logged_in'] == False or
|
|
|
|
(datetime.now(tz) - session['loginTime']).total_seconds() > 3600)
|
|
|
|
|
|
|
|
|
2021-09-23 07:21:59 -07:00
|
|
|
def verify_recaptcha(response):
|
|
|
|
data = {
|
|
|
|
'secret': os.environ.get('RECAPTCHA_SECRET'),
|
|
|
|
'response': response,
|
|
|
|
'remoteip': request.remote_addr
|
|
|
|
}
|
|
|
|
r = requests.post(
|
|
|
|
'https://www.google.com/recaptcha/api/siteverify', data=data)
|
|
|
|
return r.json()['success']
|
|
|
|
|
|
|
|
|
2021-09-24 22:31:36 -07:00
|
|
|
@ app.route('/', methods=['GET', 'POST'])
|
2021-09-10 07:30:39 -07:00
|
|
|
def index():
|
|
|
|
if request.method == 'GET':
|
|
|
|
if check_login_status():
|
2021-09-24 22:31:36 -07:00
|
|
|
return render_template('login.html')
|
2021-09-10 07:30:39 -07:00
|
|
|
return redirect('/manage')
|
|
|
|
elif request.method == 'POST':
|
2021-10-01 08:08:10 -07:00
|
|
|
email = request.form['username'] + "@group-attendance.fhjh.tp.edu.tw"
|
2021-09-10 07:30:39 -07:00
|
|
|
if check_login_status():
|
|
|
|
try:
|
2021-09-23 07:21:59 -07:00
|
|
|
if (verify_recaptcha(request.form['g-recaptcha-response'])):
|
|
|
|
user = auth.sign_in_with_email_and_password(
|
2021-10-01 08:08:10 -07:00
|
|
|
email, request.form['password'])
|
|
|
|
print("Login SUCC:", email, flush=True)
|
2021-09-23 07:21:59 -07:00
|
|
|
session['is_logged_in'] = True
|
|
|
|
session['email'] = user['email']
|
|
|
|
session['uid'] = user['localId']
|
|
|
|
session['token'] = user['idToken']
|
|
|
|
session['refreshToken'] = user['refreshToken']
|
|
|
|
session['loginTime'] = datetime.now(tz)
|
|
|
|
return redirect('/manage')
|
|
|
|
else:
|
2021-10-01 08:08:10 -07:00
|
|
|
print("ReC Error:", email, flush=True)
|
2021-09-24 22:31:36 -07:00
|
|
|
flash(
|
|
|
|
'reCAPTCHA 錯誤,請稍後再試一次<br>reCAPTCHA Failed. Please try again later.')
|
|
|
|
return redirect('/')
|
2021-09-10 07:30:39 -07:00
|
|
|
except Exception as e:
|
2021-10-01 08:08:10 -07:00
|
|
|
print("Error:", email, str(e), flush=True)
|
|
|
|
flash(
|
2021-09-24 22:31:36 -07:00
|
|
|
'帳號或密碼錯誤,請重新輸入<br>Incorrect username or password')
|
2021-10-01 08:08:10 -07:00
|
|
|
return redirect('/')
|
2021-09-10 07:30:39 -07:00
|
|
|
else:
|
|
|
|
return redirect('/manage')
|
|
|
|
|
|
|
|
|
2021-09-12 02:39:09 -07:00
|
|
|
@ app.route('/logout', methods=['GET'])
|
2021-09-09 06:29:31 -07:00
|
|
|
def logout():
|
|
|
|
session.clear()
|
|
|
|
return redirect('/')
|
|
|
|
|
|
|
|
|
2021-09-10 07:30:39 -07:00
|
|
|
if __name__ == '__main__':
|
|
|
|
app.run(debug=True)
|