Latest Version: 0.9.6.2
  Dashboard > Pylons Cookbook > ... > Guides to Pylons for people in a hurry > AuthKit 0.4 UsersFromDatabase with SQLAlchemy 0.4 and SQLAlchemyManager
  Pylons Cookbook Log In | Sign Up   View a printable version of the current page.  
  AuthKit 0.4 UsersFromDatabase with SQLAlchemy 0.4 and SQLAlchemyManager
Added by Jeffrey Laughlin, last edited by Jochen Ritzel on Mar 02, 2008  (view change) show comment
Labels: 
(None)

Introduction

Getting UsersFromDatabase authentication working with AuthKit 0.4 and SQLAlchemy 0.4 is hard to figure out, as I learned. Hopefully this will make it easier.

AuthKit 0.4 uses an experimental SQLAlchemy middleware package called SQLAlchemyManager. I don't see any reason why you can't let AuthKit use SQLAlchemyManager, and go on using normal Pylons SQLAlchemy stuff everywhere else in your app. But I haven't really verified that it works perfectly like that.

References

Project base

I started out with the authtest project from http://wiki.pylonshq.com/display/pysbook/Authentication+and+Authorization as a base for this. You should go through that tutorial and build that project before continuing here.

Install prerequisites

Install AuthKit 0.4.1 r143 from SVN

Check out AuthKit from subversion, get r143, as r144 broke form based authentication. Build the egg and install it.

svn co -r 143 http://authkit.org/svn/AuthKit/trunk AuthKit
cd AuthKit/
python setup.py bdist_egg
easy_install dist/AuthKit-0.4.1dev_r143-py2.5.egg

Install SQLAlchemyManager

easy_install sqlalchemymanager

Edit files

development.ini

Put something like this in [app:main] in development.ini

sqlalchemy.url = sqlite:///%(here)s/authtest.db
authkit.setup.method = form, cookie
authkit.cookie.secret = secret string
authkit.cookie.signoutpath = /auth/signout
authkit.form.authenticate.user.type = authkit.users.sqlalchemy_04_driver:UsersFromDatabase
authkit.form.authenticate.user.data = authtest.model

I'm not sure about the wisdom of using sqlalchemy.url instead of sqlalchemy.default.url, someone please chime in.

model/__init__.py

edit model/__init__.py to look like this

1
2
def setup_model(model, metadata, **p):
    pass

If you want to use SQLAlchemyManager for other stuff in your project you can set up the models in there.

websetup.py

Edit your websetup.py file like this

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"""Setup the authtest application"""
import logging

from paste.deploy import appconfig
from pylons import config

from authtest.config.environment import load_environment

from authtest.model import setup_model
from sqlalchemymanager import SQLAlchemyManager
import authkit.users.sqlalchemy_04_driver

log = logging.getLogger(__name__)

def setup_config(command, filename, section, vars):
    """Place any commands to setup authtest here"""
    conf = appconfig('config:' + filename)
    load_environment(conf.global_conf, conf.local_conf)

    manager = SQLAlchemyManager(None, conf.local_conf, 
        [setup_model, authkit.users.sqlalchemy_04_driver.setup_model])
    manager.create_all()

    connection = manager.engine.connect()
    session = manager.session_maker(bind=connection)
    try:
        environ = {}
        environ['sqlalchemy.session'] = session
        environ['sqlalchemy.model'] = manager.model
        users = authkit.users.sqlalchemy_04_driver.UsersFromDatabase(environ)
        users.group_create("pylons")
        users.user_create("james", password="password1", group="pylons")
        session.flush()
        session.commit()
    finally:
        session.close()
        connection.close()

I'm unhappy about explicitly setting up the environ variable. Somebody please find a better way!

You should now be able to run

paster setup-app development.ini

middleware.py

Edit middleware.py to look like this

1
2
3
4
5
6
7
8
...
from authkit.users import sqlalchemy_04_driver
import authkit.authenticate

from authtest.model import setup_model

from sqlalchemymanager import SQLAlchemyManager
...

and later this

1
2
3
4
5
...
    # AuthKit
    app = authkit.authenticate.middleware(app, app_conf)
    app = SQLAlchemyManager(app, app_conf, [setup_model, sqlalchemy_04_driver.setup_model])
...

auth.py

Now edit auth.py to look like this

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import logging

from authtest.lib.base import *

from authkit.authorize.pylons_adaptors import authorize, authorized
from authkit.permissions import RemoteUser, ValidAuthKitUser


log = logging.getLogger(__name__)

class AuthController(BaseController):
    def index(self):
        return 'hello world'

    @authorize(ValidAuthKitUser())
    def private(self):
        return 'You are authenticated'

    def signout(self):
        return 'Signed out.'

Test

Now serve the app with paster serve --reload development.ini and point your browser at http://localhost:5000/auth/private. You should see the AuthKit sign in form. Sign in with james and password1 and you should see the words You are authenticated. Thus everything is working. If it doesn't work, then find the problem, fix it, and update this page accordingly

Hi,

Thanks for this automagic tutorial.

Log in works, but signout not really.

Module authkit.authenticate.cookie:441 in logout_user_cookie
environ[self.session_middleware]['authkit.cookie.user'] = None
<type 'exceptions.KeyError'>: 'beaker.session'

Posted by Romek Szwarc at Feb 18, 2008 09:37 | Permalink

Ah I think maybe I put in the wrong revision of AuthKit to check out from SVN. This looks like a known bug that was fixed in r143. Try using r143 and let me know if it works.

cd AuthKit
svn up -r 143

procede as above

Posted by Jeffrey Laughlin at Feb 20, 2008 18:57 | Permalink

Hi Jeffrey,

It works!

I did:
svn up -r 143
cd AuthKit/
python setup.py bdist_egg
easy_install dist/AuthKit-0.4.1dev_r143-py2.5.egg

Now I have new egg:
python2.5/site-packages/AuthKit-0.4.1dev_r143-py2.5.egg

Thank you once again.

Posted by Romek Szwarc at Feb 21, 2008 14:32 | Permalink

Hello,

Same here as above.
And I want to get Session in controller.
Thanks for your effort.

Posted by Jesse Yuh at Feb 18, 2008 15:31 | Permalink

Hi Laughlin,
I tried svn up -r 143
But svn said;
duing update from external 'authkit\template\authenticate\ez_setup'
svn : unkown host : 'svn.eby-sarna.com'
This is traslated from korean.
What's wrong?

-JY

Posted by Jesse Yuh at Feb 21, 2008 09:11 | Permalink

Hi Jeffrey,

It works me too!

I can't svn up as discribe above.
I checked out 143 to new folder.

After signout cookie is not removed. Right?
Anyway it is working.

Thanks

-JY

Posted by Jesse Yuh at Feb 22, 2008 00:29 | Permalink

I updated the page to display r143 rather than r139

Posted by Jeffrey Laughlin at Feb 25, 2008 23:36 | Permalink

Hello,

When I run

$ easy_install sqlalchmymanager

with python 2.5, I got the error following.

Searching for SQLAlchemyManager
Reading http://pypi.python.org/simple/SQLAlchemyManager/
Best match: SQLAlchemyManager 0.1.0
Downloading http://pypi.python.org/packages/source/S/SQLAlchemyManager/SQLAlchemyManager-0.1.0.tar.gz#md5=6d7eb0f6fd7b5c55dc69964c85658528
Processing SQLAlchemyManager-0.1.0.tar.gz
Running SQLAlchemyManager-0.1.0/setup.py -q bdist_egg --dist-dir /tmp/easy_install-T2a0i4/SQLAlchemyManager-0.1.0/egg-dist-tmp-J0ZjtP
error: /tmp/easy_install-T2a0i4/SQLAlchemyManager-0.1.0/docs/index.txt: No such file or directory

Some not important files are missing, and the error occurs.
I can modify sources, but are there any good solution ?

Posted by Junya HAYASHI at Feb 27, 2008 06:37 | Permalink

I couldn't get svn co -r 143 http://authkit.org/svn/AuthKit/trunk AuthKit.

When i try to access http://authkit.org through browser, i am getting 403 Forbidden error message

Forbidden

You don't have permission to access / on this server.

Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request.

Please help me out

Posted by krishgy at May 17, 2008 06:05 | Permalink

Hi Krishgy,

It looks like the authkit.org server is having issues since the entire site is generating 403 errors for every page that I tried earlier. You may need to wait a day or so for the administrator to fix the issue.

At least it's secure

Posted by Eric Holmberg at May 18, 2008 00:05 | Permalink

Hi,

I needed one help in using SqlAlchemyManager.

I have integrated authkit, sqlalchemymanager and pylons as described in tutorial.
I also setup the middleware for sqlalchemymanager.

Now I have controllers for users to edit their profile in my web application. I have setup profiles table and done with the class mapping using sqlalchemy in model/init_.py.

I added those table definition and mapping in model/_init_.py.
def setup_model(model, metadata, **p):
...my application specific definition goes here....

The problem is how to access sqlalchemy manager and sqlalchemy from my profile controller?

Regards,

Krishgy

Posted by krishgy at May 19, 2008 13:32 | Permalink
Site running on a free Atlassian Confluence Open Source Project License granted to Pylons. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.3.3 Build:#645 Feb 13, 2007) - Bug/feature request - Contact Administrators
Top