Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example from documentation doesn't work #714

Open
John2013 opened this issue Dec 13, 2024 · 2 comments
Open

Example from documentation doesn't work #714

John2013 opened this issue Dec 13, 2024 · 2 comments
Labels
documentation Improvements or additions to documentation

Comments

@John2013
Copy link

I tried to run the example from https://authx.yezz.me/get-started/basic-usage/ and got an error:

INFO:     127.0.0.1:18932 - "GET /login?username=test&password=test HTTP/1.1" 200 OK
INFO:     127.0.0.1:18980 - "GET /protected HTTP/1.1" 500 Internal Server Error
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "C:\Users\nawas\PycharmProjects\fastapi_playground\.venv\Lib\site-packages\uvicorn\protocols\http\httptools_impl.py", line 409, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\nawas\PycharmProjects\fastapi_playground\.venv\Lib\site-packages\uvicorn\middleware\proxy_headers.py", line 60, in __call__
    return await self.app(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\nawas\PycharmProjects\fastapi_playground\.venv\Lib\site-packages\fastapi\applications.py", line 1054, in __call__
    await super().__call__(scope, receive, send)
  File "C:\Users\nawas\PycharmProjects\fastapi_playground\.venv\Lib\site-packages\starlette\applications.py", line 113, in __call__
    await self.middleware_stack(scope, receive, send)
  File "C:\Users\nawas\PycharmProjects\fastapi_playground\.venv\Lib\site-packages\starlette\middleware\errors.py", line 187, in __call__
    raise exc
  File "C:\Users\nawas\PycharmProjects\fastapi_playground\.venv\Lib\site-packages\starlette\middleware\errors.py", line 165, in __call__
    await self.app(scope, receive, _send)
  File "C:\Users\nawas\PycharmProjects\fastapi_playground\.venv\Lib\site-packages\starlette\middleware\exceptions.py", line 62, in __call__
    await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
  File "C:\Users\nawas\PycharmProjects\fastapi_playground\.venv\Lib\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
    raise exc
  File "C:\Users\nawas\PycharmProjects\fastapi_playground\.venv\Lib\site-packages\starlette\_exception_handler.py", line 42, in wrapped_app
    await app(scope, receive, sender)
  File "C:\Users\nawas\PycharmProjects\fastapi_playground\.venv\Lib\site-packages\starlette\routing.py", line 715, in __call__
    await self.middleware_stack(scope, receive, send)
  File "C:\Users\nawas\PycharmProjects\fastapi_playground\.venv\Lib\site-packages\starlette\routing.py", line 735, in app
    await route.handle(scope, receive, send)
  File "C:\Users\nawas\PycharmProjects\fastapi_playground\.venv\Lib\site-packages\starlette\routing.py", line 288, in handle
    await self.app(scope, receive, send)
  File "C:\Users\nawas\PycharmProjects\fastapi_playground\.venv\Lib\site-packages\starlette\routing.py", line 76, in app
    await wrap_app_handling_exceptions(app, request)(scope, receive, send)
  File "C:\Users\nawas\PycharmProjects\fastapi_playground\.venv\Lib\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
    raise exc
  File "C:\Users\nawas\PycharmProjects\fastapi_playground\.venv\Lib\site-packages\starlette\_exception_handler.py", line 42, in wrapped_app
    await app(scope, receive, sender)
  File "C:\Users\nawas\PycharmProjects\fastapi_playground\.venv\Lib\site-packages\starlette\routing.py", line 73, in app
    response = await f(request)
               ^^^^^^^^^^^^^^^^
  File "C:\Users\nawas\PycharmProjects\fastapi_playground\.venv\Lib\site-packages\fastapi\routing.py", line 291, in app
    solved_result = await solve_dependencies(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\nawas\PycharmProjects\fastapi_playground\.venv\Lib\site-packages\fastapi\dependencies\utils.py", line 638, in solve_dependencies
    solved = await call(**solved_result.values)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\nawas\PycharmProjects\fastapi_playground\.venv\Lib\site-packages\authx\main.py", line 619, in _auth_required
    return await self._auth_required(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\nawas\PycharmProjects\fastapi_playground\.venv\Lib\site-packages\authx\main.py", line 351, in _auth_required
    request_token = await method(
                    ^^^^^^^^^^^^^
  File "C:\Users\nawas\PycharmProjects\fastapi_playground\.venv\Lib\site-packages\authx\main.py", line 308, in get_access_token_from_request
    return await self._get_token_from_request(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\nawas\PycharmProjects\fastapi_playground\.venv\Lib\site-packages\authx\main.py", line 281, in _get_token_from_request
    return await _get_token_from_request(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\nawas\PycharmProjects\fastapi_playground\.venv\Lib\site-packages\authx\core.py", line 156, in _get_token_from_request
    raise MissingTokenError(*(str(err) for err in errors))
authx.exceptions.MissingTokenError: Missing 'Bearer' in 'Authorization' header.
@gokhanmeteerturk
Copy link

gokhanmeteerturk commented Dec 13, 2024

I believe your second request is just curl -s http://0.0.0.0:8000/protected . You used this because it is under the Protected Routes - curl-without-jsonwebtoken section.

The error is on purpose, as it demonstrates an unauthorized request to a protected endpoint. If you want to see response to an authorized request, you should use the example under the tab "With a valid JsonWebToken".

This behaviour is actually explained under that very section:

In the curl requests above, a 401 HTTP Error is raised when the token is not valid. By default, AuthX triggers a 500 Internal Server Error HTTP Error. For the sake of simplicity, we won't delve into error handling in this section.

Example of a valid request (access token will be different in your case):

curl -s --oauth2-bearer eyJhbGciOiJIUzI1NiIsInR5cCI6...9vJcIWEE http://127.0.0.1:8000/protected

(I just tried it in local just to be sure that it still works. response: {"message":"Hello World"} )

Edit: I wanted to add that maybe you are looking for handling these exceptions automatically, in which case you should use auth.handle_errors(app) and (optionally) try&except as you can see here.

@yezz123 yezz123 added the documentation Improvements or additions to documentation label Dec 25, 2024
@yezz123
Copy link
Owner

yezz123 commented Dec 25, 2024

Hello @John2013 Yes I'm working on a big refactoring for the documentation!

cc @gokhanmeteerturk in case you want to open a Pull request to fix the issue 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants