joelverhagen / flask-rauth

Adds OAuth 1.0/a, 2.0, and Ofly consumer support for Flask.
Other
54 stars 22 forks source link

Getting key and secret from app.config doesn't work because self.app isn't set yet #1

Closed lvh closed 11 years ago

lvh commented 11 years ago

When using the current PyPI release of flask-rauth, I get the following exception:

14:07:15 web.1  | Traceback (most recent call last):
14:07:15 web.1  |   File "app.py", line 1, in <module>
14:07:15 web.1  |     from qwake import run
14:07:15 web.1  |   File "/Users/lvh/Code/qwake/qwake/__init__.py", line 57, in <module>
14:07:15 web.1  |     access_token_url='https://connect.stripe.com/oauth/token',
14:07:15 web.1  |   File "/Users/lvh/Code/qwake/venv/lib/python2.7/site-packages/flask_rauth.py", line 266, in __init__
14:07:15 web.1  |     OAuth2Service.__init__(self, consumer_key=consumer_key, consumer_secret=consumer_secret, **kwargs)
14:07:15 web.1  |   File "/Users/lvh/Code/qwake/venv/lib/python2.7/site-packages/rauth/service.py", line 318, in __init__
14:07:15 web.1  |     if None in (self.consumer_key, self.consumer_secret):
14:07:15 web.1  |   File "/Users/lvh/Code/qwake/venv/lib/python2.7/site-packages/flask_rauth.py", line 210, in consumer_key
14:07:15 web.1  |     elif self.app is not None and self._consumer_key_config() in self.app.config:
14:07:15 web.1  | AttributeError: 'RauthOAuth2' object has no attribute 'app'
14:07:15 web.1  | exited with code 1

The required keys are in app.config, but this is never read, because self.app is set in RauthServiceMixin.__init__:

https://github.com/joelverhagen/flask-rauth/blob/master/flask_rauth.py#L155

Which is called after OAuth2Service.__init__:

https://github.com/joelverhagen/flask-rauth/blob/master/flask_rauth.py#L270

So I get the exception raised here:

https://github.com/joelverhagen/flask-rauth/blob/master/flask_rauth.py#L213

....

I worked around this issue by specifying these values when creating the object instead of putting them in the config, but the documentation suggests that isn't recommended. Anyway, I think the current_app using line isn't actually ever used, since the only case where it'd be used is when self.app isn't set yet...

joelverhagen commented 11 years ago

It looks like the base library we are using (rauth) has made some changes. I'll look into getting Flask-Rauth up to speed with these changes. In the mean time, use commit 3eedd3a5 verision 0.4.16 of rauth. You can install this version using pip:

# uninstall the newer version
pip uninstall rauth

# install the last-known-good version
pip install rauth==0.4.16
joelverhagen commented 11 years ago

The mixin's constructor must be called first, so that self.app is established... since the mixin turns self.consumer_key and self.consumer_secret into properties (which touch self.app).