pallets / quart

An async Python micro framework for building web applications.
https://quart.palletsprojects.com
MIT License
2.98k stars 162 forks source link

Quart OIDC Keycloak #125

Closed git999-cmd closed 3 years ago

git999-cmd commented 3 years ago

Hello,

I followed the link: https://github.com/pgjones/quart/issues/103 and found it is closed.

I am trying to get my Quart app working with flask_oidc_ext but it fails. Is it because there is no asyncio support for flask_oidc_ext? I use import quart.flask_patch but it does not help. Any help is greatly appreciated.

test_oidc.py:

import quart.flask_patch  # noqa
from quart import jsonify, Quart

from flask_oidc_ext import OpenIDConnect

app = Quart(__name__)
app.clients = set()

app.config.update({
    'SECRET_KEY': 'SomethingNotEntirelySecret',
    'OIDC_CLIENT_SECRETS': './client_secrets.json',
    'OIDC_DEBUG': True,
    'OIDC_ID_TOKEN_COOKIE_SECURE': False,
    'OIDC_REQUIRE_VERIFIED_EMAIL': False,
    'OIDC_USER_INFO_ENABLED': True,
    'OIDC_SCOPES': ['openid', 'email', 'profile'],
    'OIDC_INTROSPECTION_AUTH_METHOD': 'bearer'
})

oidc = OpenIDConnect(app)

@app.route("/")
@oidc.require_login
async def home():
    print(f"!!!!!! HOME")
    return jsonify(sucess=True)

if __name__ == '__main__':
    app.run(host="localhost", port=8080, debug=True)

When I start the server and navigate to http://localhost:8080, I am redirected to the login page provided by Keycloak. This works as expected. However, after I am authenticated, I see the following error:

[2021-05-04 16:03:25,067] Error in ASGI Framework
Traceback (most recent call last):
  File "../venv/lib/python3.7/site-packages/hypercorn/asyncio/context.py", line 39, in _handle
    await invoke_asgi(app, scope, receive, send)
  File "../venv/lib/python3.7/site-packages/hypercorn/utils.py", line 239, in invoke_asgi
    await app(scope, receive, send)
  File "../venv/lib/python3.7/site-packages/quart/app.py", line 2069, in __call__
    await self.asgi_app(scope, receive, send)
  File "../venv/lib/python3.7/site-packages/quart/app.py", line 2092, in asgi_app
    await asgi_handler(receive, send)
  File "../venv/lib/python3.7/site-packages/quart/asgi.py", line 31, in __call__
    _raise_exceptions(done)
  File "../venv/lib/python3.7/site-packages/quart/asgi.py", line 234, in _raise_exceptions
    raise task.exception()
  File "../lib/python3.7/asyncio/tasks.py", line 223, in __step
    result = coro.send(None)
  File "../venv/lib/python3.7/site-packages/quart/asgi.py", line 79, in handle_request
    await asyncio.wait_for(self._send_response(send, response), timeout=timeout)
  File "../lib/python3.7/asyncio/tasks.py", line 416, in wait_for
    return fut.result()
  File "../lib/python3.7/asyncio/futures.py", line 178, in result
    raise self._exception
  File "../lib/python3.7/asyncio/tasks.py", line 223, in __step
    result = coro.send(None)
  File "../venv/lib/python3.7/site-packages/quart/asgi.py", line 93, in _send_response
    async for data in body:
  File "../venv/lib/python3.7/site-packages/quart/wrappers/response.py", line 124, in _aiter
    for data in iterable:  # type: ignore
TypeError: 'coroutine' object is not iterable
pgjones commented 3 years ago

This may be fixed with 4ef7f52a3407c5b5d7835960b5a2a94d5fc56fff, could you give it a try?

khteh commented 3 years ago

Any update on this?

khteh commented 3 years ago

I tested. It works and therefore, this issue can be closed.