Spaces:
Runtime error
Runtime error
Update app.py to add authentication.
Browse files
app.py
CHANGED
|
@@ -1,15 +1,15 @@
|
|
| 1 |
-
from flask import Flask, request, redirect, url_for, send_file, render_template, flash
|
| 2 |
from flask_cors import CORS
|
| 3 |
from werkzeug.utils import secure_filename
|
| 4 |
from pymongo.mongo_client import MongoClient
|
| 5 |
from pymongo.server_api import ServerApi
|
| 6 |
import urllib.parse
|
|
|
|
| 7 |
import os
|
| 8 |
import io
|
| 9 |
|
| 10 |
app = Flask(__name__)
|
| 11 |
app.secret_key = os.getenv('SECRET_KEY')
|
| 12 |
-
CORS(app,resources={r"/*":{"origins":"*"}})
|
| 13 |
|
| 14 |
username = urllib.parse.quote_plus(os.getenv('MONGO_USERNAME'))
|
| 15 |
password = urllib.parse.quote_plus(os.getenv('MONGO_PASSWORD'))
|
|
@@ -19,14 +19,42 @@ client = MongoClient(uri, server_api=ServerApi('1'))
|
|
| 19 |
db = client['file_storage']
|
| 20 |
files_collection = db['files']
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
@app.route('/')
|
|
|
|
| 26 |
def index():
|
| 27 |
return render_template('index.html')
|
| 28 |
|
| 29 |
@app.route('/upload', methods=['GET', 'POST'])
|
|
|
|
| 30 |
def upload_file():
|
| 31 |
if request.method == 'POST':
|
| 32 |
if 'file' not in request.files:
|
|
@@ -37,7 +65,7 @@ def upload_file():
|
|
| 37 |
if file.filename == '':
|
| 38 |
flash('No selected file')
|
| 39 |
return redirect(request.url)
|
| 40 |
-
if file
|
| 41 |
filename = secure_filename(file.filename)
|
| 42 |
file_data = {
|
| 43 |
'filename': filename,
|
|
@@ -49,6 +77,7 @@ def upload_file():
|
|
| 49 |
return render_template('upload.html')
|
| 50 |
|
| 51 |
@app.route('/uploads/<filename>')
|
|
|
|
| 52 |
def uploaded_file(filename):
|
| 53 |
file_data = files_collection.find_one({'filename': filename})
|
| 54 |
if file_data:
|
|
@@ -61,11 +90,13 @@ def uploaded_file(filename):
|
|
| 61 |
|
| 62 |
|
| 63 |
@app.route('/files')
|
|
|
|
| 64 |
def list_files():
|
| 65 |
files = [file_data['filename'] for file_data in files_collection.find({}, {'_id': 1, 'filename': 1})]
|
| 66 |
return render_template('files.html', files=files)
|
| 67 |
|
| 68 |
@app.route('/delete/<filename>', methods=['POST'])
|
|
|
|
| 69 |
def delete_file(filename):
|
| 70 |
file_data = files_collection.find_one({'filename': filename})
|
| 71 |
if file_data:
|
|
@@ -76,4 +107,4 @@ def delete_file(filename):
|
|
| 76 |
return redirect(url_for('list_files'))
|
| 77 |
|
| 78 |
if __name__ == '__main__':
|
| 79 |
-
app.run(debug=True
|
|
|
|
| 1 |
+
from flask import Flask, request, redirect, url_for, send_file, render_template, flash, Response
|
| 2 |
from flask_cors import CORS
|
| 3 |
from werkzeug.utils import secure_filename
|
| 4 |
from pymongo.mongo_client import MongoClient
|
| 5 |
from pymongo.server_api import ServerApi
|
| 6 |
import urllib.parse
|
| 7 |
+
from functools import wraps
|
| 8 |
import os
|
| 9 |
import io
|
| 10 |
|
| 11 |
app = Flask(__name__)
|
| 12 |
app.secret_key = os.getenv('SECRET_KEY')
|
|
|
|
| 13 |
|
| 14 |
username = urllib.parse.quote_plus(os.getenv('MONGO_USERNAME'))
|
| 15 |
password = urllib.parse.quote_plus(os.getenv('MONGO_PASSWORD'))
|
|
|
|
| 19 |
db = client['file_storage']
|
| 20 |
files_collection = db['files']
|
| 21 |
|
| 22 |
+
try:
|
| 23 |
+
client.admin.command('ping')
|
| 24 |
+
print("Pinged your deployment. You successfully connected to MongoDB!")
|
| 25 |
+
except Exception as e:
|
| 26 |
+
print(e)
|
| 27 |
+
|
| 28 |
+
# Get the password from the environment variable
|
| 29 |
+
APP_PASSWORD = os.getenv('APP_PASSWORD')
|
| 30 |
+
|
| 31 |
+
def check_auth(username, password):
|
| 32 |
+
"""Check if a username/password combination is valid."""
|
| 33 |
+
return username == 'admin' and password == APP_PASSWORD
|
| 34 |
+
|
| 35 |
+
def authenticate():
|
| 36 |
+
"""Send a 401 response that enables basic auth."""
|
| 37 |
+
return Response(
|
| 38 |
+
'Could not verify your access level for that URL.\n'
|
| 39 |
+
'You have to login with proper credentials', 401,
|
| 40 |
+
{'WWW-Authenticate': 'Basic realm="Login Required"'})
|
| 41 |
+
|
| 42 |
+
def requires_auth(f):
|
| 43 |
+
@wraps(f)
|
| 44 |
+
def decorated(*args, **kwargs):
|
| 45 |
+
auth = request.authorization
|
| 46 |
+
if not auth or not check_auth(auth.username, auth.password):
|
| 47 |
+
return authenticate()
|
| 48 |
+
return f(*args, **kwargs)
|
| 49 |
+
return decorated
|
| 50 |
|
| 51 |
@app.route('/')
|
| 52 |
+
@requires_auth
|
| 53 |
def index():
|
| 54 |
return render_template('index.html')
|
| 55 |
|
| 56 |
@app.route('/upload', methods=['GET', 'POST'])
|
| 57 |
+
@requires_auth
|
| 58 |
def upload_file():
|
| 59 |
if request.method == 'POST':
|
| 60 |
if 'file' not in request.files:
|
|
|
|
| 65 |
if file.filename == '':
|
| 66 |
flash('No selected file')
|
| 67 |
return redirect(request.url)
|
| 68 |
+
if file:
|
| 69 |
filename = secure_filename(file.filename)
|
| 70 |
file_data = {
|
| 71 |
'filename': filename,
|
|
|
|
| 77 |
return render_template('upload.html')
|
| 78 |
|
| 79 |
@app.route('/uploads/<filename>')
|
| 80 |
+
@requires_auth
|
| 81 |
def uploaded_file(filename):
|
| 82 |
file_data = files_collection.find_one({'filename': filename})
|
| 83 |
if file_data:
|
|
|
|
| 90 |
|
| 91 |
|
| 92 |
@app.route('/files')
|
| 93 |
+
@requires_auth
|
| 94 |
def list_files():
|
| 95 |
files = [file_data['filename'] for file_data in files_collection.find({}, {'_id': 1, 'filename': 1})]
|
| 96 |
return render_template('files.html', files=files)
|
| 97 |
|
| 98 |
@app.route('/delete/<filename>', methods=['POST'])
|
| 99 |
+
@requires_auth
|
| 100 |
def delete_file(filename):
|
| 101 |
file_data = files_collection.find_one({'filename': filename})
|
| 102 |
if file_data:
|
|
|
|
| 107 |
return redirect(url_for('list_files'))
|
| 108 |
|
| 109 |
if __name__ == '__main__':
|
| 110 |
+
app.run(debug=True)
|