Setup Eve-Flask with oAuth over heroku

355 views Asked by At

I have a python API (Eve-Flask with oAuth 2.0) which run successfully in localhost.

I have deployed this app to heroku and I can't access to the API resources after obtain the token key, however I can access to some endpoint with the same token generated.

Here you have:

> curl -k -X POST -d "client_id=XXXXX&grant_type=password&username=XXX&password=YYYY" https://MYAPP.herokuapp.com/oauth/token

And the api response is:

> {"access_token": "VWQjYBOMTkeqPbiql8dl2T1fbBM1WH", "token_type": "Bearer", "expires_in": 3600, "refresh_token": "Z0d1O3LUVrE1lIaYuQ3hOkZSFO9GuX", "scope": ""}

Up to here everything is correct, in addition to that I can access to some endpoint with the token generated:

> curl -k -H "Authorization: Bearer VWQjYBOMTkeqPbiql8dl2T1fbBM1WH" https://MYAPP.herokuapp.com/myendpoint

>You have access the protected resource!

On the other hand:

curl -k -H "Authorization: Bearer VWQjYBOMTkeqPbiql8dl2T1fbBM1WH" https://MYAPP.herokuapp.com/api_resource


Traceback (most recent call last):
  File "/app/.heroku/python/lib/python2.7/site-packages/eve/flaskapp.py", line 968, in __call__
    return super(Eve, self).__call__(environ, start_response)
  File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/app/.heroku/python/lib/python2.7/site-packages/eve/endpoints.py", line 54, in collections_endpoint
    response = get(resource, lookup)
  File "/app/.heroku/python/lib/python2.7/site-packages/eve/methods/common.py", line 242, in rate_limited
    return f(*args, **kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/eve/auth.py", line 77, in decorated
    if not auth.authorized(roles, resource_name, request.method):
  File "/app/oauth2.py", line 45, in authorized
    return self.check_auth(token, allowed_roles, resource, method)
  File "/app/oauth2.py", line 33, in check_auth
    return token and self.redis.get(token)
  File "/app/.heroku/python/lib/python2.7/site-packages/redis/client.py", line 880, in get
    return self.execute_command('GET', name)
  File "/app/.heroku/python/lib/python2.7/site-packages/redis/client.py", line 578, in execute_command
    connection.send_command(*args)
  File "/app/.heroku/python/lib/python2.7/site-packages/redis/connection.py", line 563, in send_command
    self.send_packed_command(self.pack_command(*args))
  File "/app/.heroku/python/lib/python2.7/site-packages/redis/connection.py", line 538, in send_packed_command
    self.connect()
  File "/app/.heroku/python/lib/python2.7/site-packages/redis/connection.py", line 442, in connect
    raise ConnectionError(self._error_message(e))
ConnectionError: Error 111 connecting to localhost:6379. Connection refused.

pice of run.py

app = Eve(auth=BearerAuth)
ResourceOwnerPasswordCredentials(app)

@app.route('/myendpoint')
@oauth.require_oauth()
def restricted_access():
    return "You have access the protected resource!"

if __name__ == '__main__':
    host = str(os.environ.get('HOST', '0.0.0.0'))
    app.run(host, int(os.environ.get('PORT')),debug=True)

I have set Redis in heroku, however, this piece of error:

ConnectionError: Error 111 connecting to localhost:6379. Connection refused.

Eve trying to connect with my redis localhost, I don't know why, this error will be kill me soon.

Thanks in advance.

nicolas

2

There are 2 answers

0
Nicolas Moraes On BEST ANSWER

Finally I could mitigate redis error and run the api over Heroku editing oauth2.py:

def __init__(self):
        super(BearerAuth, self).__init__()
        self.redis = StrictRedis(host='XXX',port=123,password='YYY')

instead of

self.redis = StrictRedis()

Cheers.

2
Nicola Iarocci On

The stacktrace suggests an issue connecting to redis:

 File "/app/.heroku/python/lib/python2.7/site-packages/redis/connection.py", line 442, in connect
raise ConnectionError(self._error_message(e))

So I would make sure that redis is up and running on your instance. Also make sure that SENTINEL_REDIS_URL is set to the correct port (maybe heroku runs redis on a non-standard port?)