diff --git a/couchpotato/__init__.py b/couchpotato/__init__.py index 42529ffc..5ef06609 100644 --- a/couchpotato/__init__.py +++ b/couchpotato/__init__.py @@ -1,6 +1,8 @@ +from couchpotato.core.auth import requires_auth from couchpotato.core.logger import CPLog -from flask import Flask, Module +from flask.app import Flask from flask.helpers import url_for +from flask.module import Module from flask.templating import render_template app = Flask(__name__) @@ -9,5 +11,6 @@ web = Module(__name__, 'web') @web.route('/') +@requires_auth def index(): return render_template('index.html') diff --git a/couchpotato/cli.py b/couchpotato/cli.py index 1644d920..708c5ec5 100644 --- a/couchpotato/cli.py +++ b/couchpotato/cli.py @@ -1,7 +1,6 @@ -from couchpotato import app from couchpotato.api import api from couchpotato.core.logger import CPLog -from couchpotato.core.settings import Settings +from couchpotato.core.settings import settings from couchpotato import web from logging import handlers from optparse import OptionParser @@ -34,7 +33,7 @@ def cmd_couchpotato(base_path): # Register settings - settings = Settings(os.path.join(options.data_dir, 'settings.conf')) + settings.setFile(os.path.join(options.data_dir, 'settings.conf')) debug = options.debug or settings.get('debug', default = False) @@ -69,6 +68,7 @@ def cmd_couchpotato(base_path): # Create app + from couchpotato import app api_key = settings.get('api_key') url_base = '/%s/' % settings.get('url_base') @@ -86,7 +86,7 @@ def cmd_couchpotato(base_path): # Register modules app.register_module(web, url_prefix = url_base) - app.register_module(api, url_prefix = '%sapi/%s' % (url_base, api_key)) + app.register_module(api, url_prefix = '%s/%s' % (url_base + 'api', api_key)) # Go go go! app.run() diff --git a/couchpotato/core/auth.py b/couchpotato/core/auth.py new file mode 100644 index 00000000..87e5820f --- /dev/null +++ b/couchpotato/core/auth.py @@ -0,0 +1,24 @@ +from couchpotato.core.settings import settings +from flask import request, Response +from functools import wraps + +def check_auth(username, password): + return username == settings.get('username') and password == settings.get('password') + +def authenticate(): + return Response( + 'This is not the page you are looking for. *waves hand*', 401, + {'WWW-Authenticate': 'Basic realm="CouchPotato Login"'} + ) + +def requires_auth(f): + + @wraps(f) + def decorated(*args, **kwargs): + auth = request.authorization + if settings.get('username') and (not auth or not check_auth(auth.username, auth.password)): + return authenticate() + + return f(*args, **kwargs) + + return decorated diff --git a/couchpotato/core/settings/__init__.py b/couchpotato/core/settings/__init__.py index 7a52e999..f0a61b41 100644 --- a/couchpotato/core/settings/__init__.py +++ b/couchpotato/core/settings/__init__.py @@ -14,16 +14,18 @@ class Settings(): bool = {'true':True, 'false':False} - def __init__(self, file): - self.file = file - - self.p = ConfigParser.RawConfigParser() - self.p.read(file) + def __init__(self): # Connect signals signal('settings.register').connect(self.registerDefaults) signal('settings.save').connect(self.save) + def setFile(self, file): + self.file = file + + self.p = ConfigParser.RawConfigParser() + self.p.read(file) + def parser(self): return self.p @@ -81,3 +83,5 @@ class Settings(): return True except ValueError: return False + +settings = Settings()