Browse Source

Authentication

Global settings
pull/1/merge
Ruud 14 years ago
parent
commit
21f880e030
  1. 5
      couchpotato/__init__.py
  2. 8
      couchpotato/cli.py
  3. 24
      couchpotato/core/auth.py
  4. 14
      couchpotato/core/settings/__init__.py

5
couchpotato/__init__.py

@ -1,6 +1,8 @@
from couchpotato.core.auth import requires_auth
from couchpotato.core.logger import CPLog from couchpotato.core.logger import CPLog
from flask import Flask, Module from flask.app import Flask
from flask.helpers import url_for from flask.helpers import url_for
from flask.module import Module
from flask.templating import render_template from flask.templating import render_template
app = Flask(__name__) app = Flask(__name__)
@ -9,5 +11,6 @@ web = Module(__name__, 'web')
@web.route('/') @web.route('/')
@requires_auth
def index(): def index():
return render_template('index.html') return render_template('index.html')

8
couchpotato/cli.py

@ -1,7 +1,6 @@
from couchpotato import app
from couchpotato.api import api from couchpotato.api import api
from couchpotato.core.logger import CPLog from couchpotato.core.logger import CPLog
from couchpotato.core.settings import Settings from couchpotato.core.settings import settings
from couchpotato import web from couchpotato import web
from logging import handlers from logging import handlers
from optparse import OptionParser from optparse import OptionParser
@ -34,7 +33,7 @@ def cmd_couchpotato(base_path):
# Register settings # 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) debug = options.debug or settings.get('debug', default = False)
@ -69,6 +68,7 @@ def cmd_couchpotato(base_path):
# Create app # Create app
from couchpotato import app
api_key = settings.get('api_key') api_key = settings.get('api_key')
url_base = '/%s/' % settings.get('url_base') url_base = '/%s/' % settings.get('url_base')
@ -86,7 +86,7 @@ def cmd_couchpotato(base_path):
# Register modules # Register modules
app.register_module(web, url_prefix = url_base) 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! # Go go go!
app.run() app.run()

24
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

14
couchpotato/core/settings/__init__.py

@ -14,16 +14,18 @@ class Settings():
bool = {'true':True, 'false':False} bool = {'true':True, 'false':False}
def __init__(self, file): def __init__(self):
self.file = file
self.p = ConfigParser.RawConfigParser()
self.p.read(file)
# Connect signals # Connect signals
signal('settings.register').connect(self.registerDefaults) signal('settings.register').connect(self.registerDefaults)
signal('settings.save').connect(self.save) signal('settings.save').connect(self.save)
def setFile(self, file):
self.file = file
self.p = ConfigParser.RawConfigParser()
self.p.read(file)
def parser(self): def parser(self):
return self.p return self.p
@ -81,3 +83,5 @@ class Settings():
return True return True
except ValueError: except ValueError:
return False return False
settings = Settings()

Loading…
Cancel
Save