capacifier/capacifier.py

33 lines
976 B
Python

import imp
import role
from flask import Flask, request, make_response, g
app = Flask('capacifier')
app.config.from_pyfile('capacifier.cfg')
@app.before_first_request
def load_roles():
# heavily inspired by flask.config
app.roles = {}
rmodule = __import__(app.config['ROLES_MODULE'])
execfile(app.config['ROLE_FILE'], rmodule.__dict__, app.roles)
for name in app.roles.iterkeys():
if name[0] == '_':
app.roles.pop(name)
cmodule, cfun = app.config['CONTEXT_MAKER'].rsplit('.', 1)
app.maker = getattr(__import__(cmodule), cfun)
@app.before_request
def run_maker():
app.maker(app.config)
@app.route('/<cap_name>/<login>')
def check_cap(cap_name, login):
cap = app.roles.get(cap_name, role.Allow())
code, res = 401, 'NO'
if cap(login, g.context):
code, res = 200, 'YES'
return make_response(res, code, { 'Content-Type': 'text/plain' })
if __name__ == '__main__':
app.run('0.0.0.0', 8083, debug=True)