Authentication
Global settings
This commit is contained in:
@@ -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')
|
||||
|
||||
+4
-4
@@ -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()
|
||||
|
||||
@@ -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
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user