Browse files

Added support for configurations

  • Loading branch information...
1 parent 465b270 commit e35732fc085ea8efe3884772d2b1b93d1e0dc1f9 @mitsuhiko committed Jun 21, 2010
Showing with 95 additions and 21 deletions.
  1. +18 −1 docs/index.rst
  2. +13 −10 example/example.py
  3. +62 −8 flaskext/openid.py
  4. +2 −2 setup.py
View
19 docs/index.rst
@@ -35,13 +35,20 @@ To integrate Flask-OpenID into your application you need to create an
instance of the :class:`OpenID` object first::
from flaskext.openid import OpenID
- oid = OpenID('/path/to/store')
+ oid = OpenID(app, '/path/to/store')
By default it will use the filesystem as store for information needed by
OpenID for the authentication process. You can alternatively implement
your own store that uses the database or a no-sql server. For more
information about that, consult the python-openid documentation.
+The path to the store can also be specified with the
+``OPENID_FS_STORE_PATH`` configuration variable.
+
+Alternatively the object can be instanciated without the application in
+which case it can later be registered for an application with the
+:meth:`~OpenID.init_app` method.
+
The current logged in user has to memorized somewhere, we will use the
``'openid'`` key in the `session`. This can be implemented in a
`before_request` function::
@@ -213,6 +220,16 @@ Full Example
To see the full code of that example, you can download the code `from
github <http://github.com/mitsuhiko/flask-openid>`_.
+Changes
+-------
+
+1.0
+```
+
+- the OpenID object is not registered to an application which allows
+ configuration values to be used and is also consistent with other
+ Flask extensions.
+
API References
--------------
View
23 example/example.py
@@ -18,22 +18,19 @@
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
-# configuration
-DATABASE_URI = 'sqlite:////tmp/flask-openid.db'
-OPENID_FS_PATH = '/tmp/flask-openid-store'
-SECRET_KEY = 'development key'
-DEBUG = True
-
# setup flask
app = Flask(__name__)
-app.debug = DEBUG
-app.secret_key = SECRET_KEY
+app.config.update(
+ DATABASE_URI = 'sqlite:////tmp/flask-openid.db',
+ SECRET_KEY = 'development key',
+ DEBUG = True
+)
# setup flask-openid
-oid = OpenID(OPENID_FS_PATH)
+oid = OpenID(app)
# setup sqlalchemy
-engine = create_engine(DATABASE_URI)
+engine = create_engine(app.config['DATABASE_URI'])
db_session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
@@ -64,6 +61,12 @@ def before_request():
g.user = User.query.filter_by(openid=session['openid']).first()
+@app.after_request
+def after_request(response):
+ db_session.remove()
+ return response
+
+
@app.route('/')
def index():
return render_template('index.html')
View
70 flaskext/openid.py
@@ -248,10 +248,28 @@ class OpenID(object):
"""Simple helper class for OpenID auth. Has to be created in advance
like a :class:`~flask.Flask` object.
+ There are two usage modes which work very similar. One is binding
+ the instance to a very specific Flask application::
+
+ app = Flask(__name__)
+ db = OpenID(app)
+
+ The second possibility is to create the object once and configure the
+ application later to support it::
+
+ oid = OpenID()
+
+ def create_app():
+ app = Flask(__name__)
+ oid.init_app(app)
+ return app
+
+ :param app: the application to register this openid controller with.
:param fs_store_path: if given this is the name of a folder where the
OpenID auth process can store temporary
information. If neither is provided a temporary
- folder is assumed.
+ folder is assumed. This is overridden by the
+ ``OPENID_FS_STORE_PATH`` configuration key.
:param store_factory: alternatively a function that creates a
python-openid store object.
:param fallback_endpoint: optionally a string with the name of an URL
@@ -261,20 +279,56 @@ class OpenID(object):
application's index in that case.
"""
- # XXX: GAE support
-
- def __init__(self, fs_store_path=None, store_factory=None,
+ def __init__(self, app=None, fs_store_path=None, store_factory=None,
fallback_endpoint=None):
+ # backwards compatibility support
+ if isinstance(app, basestring):
+ from warnings import warn
+ warn(DeprecationWarning('OpenID constructor expects application '
+ 'as first argument now. If you want to '
+ 'provide a hardcoded fs_store_path you '
+ 'have to use a keyword argument. It is '
+ 'recommended though to use the config '
+ 'key.'), stacklevel=2)
+ fs_store_path = app
+ app = None
+
+ self.app = app
+ if app is not None:
+ self.init_app(app)
+
self.fs_store_path = fs_store_path
if store_factory is None:
- if self.fs_store_path is None:
- self.fs_store_path = os.path.join(tempfile.gettempdir(),
- 'flask-openid')
- store_factory = lambda: FileOpenIDStore(self.fs_store_path)
+ store_factory = self._default_store_factory
self.store_factory = store_factory
self.after_login_func = None
self.fallback_endpoint = fallback_endpoint
+ def init_app(self, app):
+ """This callback can be used to initialize an application for the
+ use with this openid controller.
+
+ .. versionadded:: 1.0
+ """
+ app.config.setdefault('OPENID_FS_STORE_PATH', None)
+
+ def _default_store_factory(self):
+ """Default store factory that creates a filesystem store from
+ the configuration.
+ """
+ app = self.app if self.app is not None else current_app
+
+ if 'OPENID_FS_STORE_PATH' not in app.config:
+ self.init_app(app)
+ from warnings import warn
+ warn(DeprecationWarning('init_app not called for this '
+ 'application. This is deprecated functionality'))
+
+ path = app.config['OPENID_FS_STORE_PATH'] or self.fs_store_path
+ if path is None:
+ path = os.path.join(tempfile.gettempdir(), 'flask-openid')
+ return FileOpenIDStore(path)
+
def signal_error(self, msg):
"""Signals an error. It does this by storing the message in the
session. Use :meth:`errorhandler` to this method.
View
4 setup.py
@@ -16,7 +16,7 @@
setup(
name='Flask-OpenID',
- version='0.9.1',
+ version='1.0',
url='http://github.com/mitsuhiko/flask-openid/',
license='BSD',
author='Armin Ronacher',
@@ -28,7 +28,7 @@
zip_safe=False,
platforms='any',
install_requires=[
- 'Flask',
+ 'Flask>=0.3',
'python-openid>=2.0'
],
classifiers=[

0 comments on commit e35732f

Please sign in to comment.