Skip to content
This repository has been archived by the owner on Jan 5, 2021. It is now read-only.

Commit

Permalink
Merge pull request #140 from rayhidayat/basicauth
Browse files Browse the repository at this point in the history
Adding basic authentication to web server as a configurable option
  • Loading branch information
iancmcc committed Sep 14, 2016
2 parents ade137b + d004bf3 commit c7f9e0b
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 6 deletions.
4 changes: 4 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ A configuration file in YAML format will be created at ``~/.wemo/config.yml``::
# Web app bind address
#
# listen: 0.0.0.0:5000

# Require basic authentication (username:password) for the web app
#
# auth: admin:password
6 changes: 4 additions & 2 deletions docs/server.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ The necessary dependencies to run the server are not installed unless the
Configuration
-------------
The IP and port to which the server will bind are specified by the ``listen``
parameter in the configuration file. Default: ``0.0.0.0:5000``.
parameter in the configuration file. Default: ``0.0.0.0:5000``.
Optionally, basic authentication can be enabled for the web server by
setting the ``auth`` parameter in the configuration file.

Web App
--------
Expand Down Expand Up @@ -52,4 +54,4 @@ is as valid as::
POST /api/device/NAME Toggle switch state, specified by optional
``state`` argument (default: "toggle"). Valid
values are "on", "off" or "toggle".
===================== =========================================
===================== =========================================
2 changes: 1 addition & 1 deletion ouimeaux/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def server(args):
except ImportError:
print("ouimeaux server dependencies are not installed. Please run, e.g., 'pip install ouimeaux[server]'")
sys.exit(1)
initialize(bind=getattr(args, 'bind', None))
initialize(bind=getattr(args, 'bind', None), auth=(WemoConfiguration().auth or None))
level = logging.INFO
if getattr(args, 'debug', False):
level = logging.DEBUG
Expand Down
8 changes: 8 additions & 0 deletions ouimeaux/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ def __init__(self, filename=None):
# Web app bind address
#
# listen: 0.0.0.0:5000
# Require basic authentication (username:password) for the web app
#
# auth: admin:password
""")
with open(filename, 'r') as cfg:
self._parsed = yaml.load(cfg)
Expand All @@ -56,3 +60,7 @@ def bind(self):
@property
def listen(self):
return self._parsed.get('listen', None)

@property
def auth(self):
return self._parsed.get('auth', None)
12 changes: 11 additions & 1 deletion ouimeaux/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from flask import render_template, send_from_directory, url_for
from flask import send_file, make_response, abort
from flask.ext.restful import reqparse, abort, Api, Resource
from flask.ext.basicauth import BasicAuth


from ouimeaux.signals import statechange
Expand All @@ -25,12 +26,21 @@
ENV = None


def initialize(bind=None):
def initialize(bind=None, auth=None):
global ENV
if ENV is None:
ENV = Environment(bind=bind)
ENV.start()
gevent.spawn(ENV.discover, 10)
if auth is not None:
elems = auth.split(':', 1)
username = elems[0]
password = elems[1]
print("Protected server with basic auth username/password: ", username, password)
app.config['BASIC_AUTH_USERNAME'] = username
app.config['BASIC_AUTH_PASSWORD'] = password
app.config['BASIC_AUTH_FORCE'] = True
basic_auth = BasicAuth(app)


def serialize(device):
Expand Down
8 changes: 6 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

setup(
name='ouimeaux',
version='0.8',
version='0.8.1',
description='Open source control for Belkin WeMo devices',
long_description=readme + '\n\n' + history,
author='Ian McCracken',
Expand Down Expand Up @@ -57,7 +57,11 @@
]
},
extras_require = {
'server': ["flask-restful", "gevent-socketio"],
'server': [
"flask-restful",
"flask-basicauth",
"gevent-socketio",
],
},
test_suite='tests',
)

0 comments on commit c7f9e0b

Please sign in to comment.