I did some experimenting with authentication using AuthKit.
First, to make it work with the versions of python libraries MapFish is using, you need to patch a few things:
- the SVN revision 156 of AuthKit, patched with AuthKitMapFish.patch
- the version 1.9.2 of Routes, patched with RoutesMapFish.patch
- add a few things to MapFish with the patch MapFishAuthentication.patch
Then, you have to add that to your .ini file:
# AuthKit configuration ===============================================
# choose if you want Form or OpenId athentication
authkit.setup.method = form, cookie
#authkit.setup.method = openid, cookie
# choose between the cached version or the normal version
#authkit.openid.authenticate.user.type = authkit.users.sqlalchemy_driver:UsersFromDatabase
#authkit.form.authenticate.user.type = authkit.users.sqlalchemy_driver:UsersFromDatabase
authkit.openid.authenticate.user.type = mapfish.lib.user_auth:CachedUsersFromDatabase
authkit.form.authenticate.user.type = mapfish.lib.user_auth:CachedUsersFromDatabase
# General configuration
# TODO: we should generate a random secret here
authkit.cookie.secret = xxxxxxxxx
authkit.cookie.signoutpath = /logout
authkit.cookie.name = myproject-auth
authkit.cookie.includeip = True
# OpenId configuration
authkit.openid.store.type = file
authkit.openid.store.config = %(here)s/data
authkit.openid.urltouser = authkit.authenticate.open_id:passurl_urltouser
authkit.openid.path.signedin = /about
authkit.openid.authenticate.user.encrypt = authkit.users:md5
# TODO: we should generate a random secret here
authkit.openid.authenticate.user.encrypt.secret = xxxxxxx
authkit.openid.authenticate.user.data = myproject.model
authkit.openid.template.obj = myproject.lib.template:make_openid_template
# Form configuration
authkit.form.authenticate.user.encrypt = authkit.users:md5
# TODO: we should generate a random secret here
authkit.form.authenticate.user.encrypt.secret = xxxxxxxxxx
authkit.form.authenticate.user.data = myproject.model
authkit.form.template.obj = myproject.lib.template:make_template
The template for the authentication must be provided by two function in myproject.lib.template:
import pylons
from pylons.templating import Buffet
from pylons import config
import myproject.lib.helpers as h
class MyBuffet(Buffet):
def _update_names(self, ns):
return ns
def_eng = config['buffet.template_engines'][0]
buffet = MyBuffet(
def_eng['engine'],
template_root=def_eng['template_root'],
**def_eng['template_options']
)
for e in config['buffet.template_engines'][1:]:
buffet.prepare(
e['engine'],
template_root=e['template_root'],
alias=e['alias'],
**e['template_options']
)
class State:
pass
c = State()
c.user = 'None'
def make_template():
return buffet.render(
template_name="/signin.mako",
namespace=dict(h=h, c=State())
).replace("%", "%%").replace("FORM_ACTION", "%s")
def make_openid_template():
return buffet.render(
template_name="/openid.mako",
namespace=dict(h=h, c=State())
).replace("%", "%%")
Then, from a Paster shell, you can update your database to add some rights:
from unhcr import model
model.meta.create_all()
users=request.environ['authkit.users']
users.user_create('demo', 'demo')
users.user_create('viewuser', 'viewuser')
users.user_create('edituser', 'edituser')
users.role_create('view')
users.role_create('edit')
users.user_add_role('viewuser', 'view')
users.user_add_role('edituser', 'view')
users.user_add_role('edituser', 'edit')
model.meta.Session.commit()
Now, you can use the mapfish.controller.AuthProxy to protect WMS servers and use AuthKit's permissions to protect controllers' actions like that:
from authkit.authorize.pylons_adaptors import authorize
from authkit.permissions import ValidAuthKitUser
[...]
@authorize(ValidAuthKitUser())
def index(self):
return render('/index.mako')