From a340c225e58a9d5cc18aef6da36f3017d591ca5b Mon Sep 17 00:00:00 2001 From: Sushant Pradhan Date: Sun, 29 Apr 2018 12:28:08 -0500 Subject: [PATCH] Issue#12 fix --- README.md | 2 ++ auth.py | 25 +++++++++++++++++++++++++ main.py | 5 ++++- 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 auth.py diff --git a/README.md b/README.md index 19860ed..8583ef4 100644 --- a/README.md +++ b/README.md @@ -107,6 +107,8 @@ You can view a live stream by visiting the ip address of your pi in a browser on Visit `:5000` in your browser to view the stream. +The default password is pi/securehome. To change the credentials, please modify `auth.py` + Note: To view the live stream on a different network than your Raspberry Pi, you can use [ngrok](https://ngrok.com/) to expose a local tunnel. Once downloaded, run ngrok with `./ngrok http 5000` and visit one of the generated links in your browser. Note: The video stream will not start automatically on startup. To start the video stream automatically, you will need to run the program from your `/etc/rc.local` file see this [video](https://youtu.be/51dg2MsYHns?t=7m4s) for more information about how to configure that. diff --git a/auth.py b/auth.py new file mode 100644 index 0000000..bd3c059 --- /dev/null +++ b/auth.py @@ -0,0 +1,25 @@ +from functools import wraps +from flask import request, Response + + +def check_auth(username, password): + """This function is called to check if a username / + password combination is valid. + """ + return username == 'pi' and password == 'securehome' + +def authenticate(): + """Sends a 401 response that enables basic auth""" + return Response( + 'Could not verify your access level for that URL.\n' + 'You have to login with proper credentials', 401, + {'WWW-Authenticate': 'Basic realm="Login Required"'}) + +def requires_auth(f): + @wraps(f) + def decorated(*args, **kwargs): + auth = request.authorization + if not auth or not check_auth(auth.username, auth.password): + return authenticate() + return f(*args, **kwargs) + return decorated diff --git a/main.py b/main.py index fef7650..e89690e 100644 --- a/main.py +++ b/main.py @@ -5,6 +5,7 @@ from camera import VideoCamera import time import threading +from auth import requires_auth email_update_interval = 600 # sends an email only once in this time interval video_camera = VideoCamera(flip=True) # creates a camera object, flip vertically @@ -28,6 +29,7 @@ def check_for_objects(): print "Error sending email: ", sys.exc_info()[0] @app.route('/') +@requires_auth def index(): return render_template('index.html') @@ -38,6 +40,7 @@ def gen(camera): b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n') @app.route('/video_feed') +@requires_auth def video_feed(): return Response(gen(video_camera), mimetype='multipart/x-mixed-replace; boundary=frame') @@ -46,4 +49,4 @@ def video_feed(): t = threading.Thread(target=check_for_objects, args=()) t.daemon = True t.start() - app.run(host='0.0.0.0', debug=False) + app.run(threaded=True, host='0.0.0.0', debug=False)