Closed bestickley closed 6 months ago
After seeing the below note from Authjs docs here:
Note Auth.js JWTs are meant to be used by the same app that issued them. If you need JWT authentication for your third-party API, you should rely on your Identity Provider instead.
I've decided to add my identity providers id token to the user session payload and send that to FastAPI to validate instead. Leaving this open in case others run into same issue and want to resolve.
Hi @bestickley,
Thanks for reporting this! Looks like Auth.js changed their default encryption algorithm and key derivation in v5, which my code does not take into account. I'll see if I can fix it!
Hey @TCatshoek, thanks for the response! Default encryption algo could have changed, but I think my issue was that I included Cognito's id token in the payload which isn't compliant with payload cryptographically (don't totally understand it). Removing the id_token from the payload I think will resolve my issue.
I want to be able to change the cookie name and csrf cookie name dynamically based on request, so I ended up implementing my own solution. I used some of the code from this library, so thank you!
I added support for Auth.js v5 in #9
I'll do a release soon, if you have time it would be great if you could give it a try!
Fixed in #9,
Feel free to reopen if you run into any more issues!
Wow. @TCatshoek, that is the fastest turn around I've ever seen in open-source. πππ You should win maintainer of the year award. This works perfectly. THANK YOU!
Hi, first thanks so much for this library! Exactly what I needed.
I'm trying to use this library with next-auth@5 and running into some errors. Here is what I've got:
But when I hit the /chat endpoint, I get the error below. The important part of the error message seems to be:
Key must be 256 bit for alg A256CBC
. I tried finding the length of the key by printing the length of it here but the value is 32 which checks out since 32 * 8 = 256. Any ideas why I'm getting this error?Expand for Error
/Users/stickb/Library/Caches/pypoetry/virtualenvs/non-package-mode-cN-H_gxH-py3.12/lib/python3.12/site-packages/fastapi_nextauth_jwt/fastapi_nextauth_jwt.py:76: RuntimeWarning: NEXTAUTH_URL not set warnings.warn("NEXTAUTH_URL not set", RuntimeWarning) INFO: Started server process [68495] INFO: Waiting for application startup. INFO: Application startup complete. INFO: Uvicorn running on http://0.0.0.0:8080 (Press CTRL+C to quit) len of self.key: 32 Key must be 256 bit for alg A256CBC INFO: 127.0.0.1:50048 - "POST /chat HTTP/1.1" 500 Internal Server Error ERROR: Exception in ASGI application Traceback (most recent call last): File "/Users/stickb/Library/Caches/pypoetry/virtualenvs/non-package-mode-cN-H_gxH-py3.12/lib/python3.12/site-packages/jose/jwe.py", line 178, in decrypt plain_text = _decrypt_and_auth(cek_bytes, enc, cipher_text, iv, aad, auth_tag) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/stickb/Library/Caches/pypoetry/virtualenvs/non-package-mode-cN-H_gxH-py3.12/lib/python3.12/site-packages/jose/jwe.py", line 231, in _decrypt_and_auth encryption_key, mac_key, key_len = _get_encryption_key_mac_key_and_key_length_from_cek(cek_bytes, enc) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/stickb/Library/Caches/pypoetry/virtualenvs/non-package-mode-cN-H_gxH-py3.12/lib/python3.12/site-packages/jose/jwe.py", line 252, in _get_encryption_key_mac_key_and_key_length_from_cek encryption_key = jwk.construct(encryption_key_bytes, encryption_alg) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/stickb/Library/Caches/pypoetry/virtualenvs/non-package-mode-cN-H_gxH-py3.12/lib/python3.12/site-packages/jose/jwk.py", line 79, in construct return key_class(key_data, algorithm) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/stickb/Library/Caches/pypoetry/virtualenvs/non-package-mode-cN-H_gxH-py3.12/lib/python3.12/site-packages/jose/backends/cryptography_backend.py", line 456, in __init__ raise JWKError(f"Key must be 256 bit for alg {algorithm}") jose.exceptions.JWKError: Key must be 256 bit for alg A256CBC During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/Users/stickb/Library/Caches/pypoetry/virtualenvs/non-package-mode-cN-H_gxH-py3.12/lib/python3.12/site-packages/fastapi_nextauth_jwt/fastapi_nextauth_jwt.py", line 119, in __call__ decrypted_token_string = jwe.decrypt(encrypted_token, self.key) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/stickb/Library/Caches/pypoetry/virtualenvs/non-package-mode-cN-H_gxH-py3.12/lib/python3.12/site-packages/jose/jwe.py", line 182, in decrypt raise JWEError(e) jose.exceptions.JWEError: Key must be 256 bit for alg A256CBC During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/Users/stickb/Library/Caches/pypoetry/virtualenvs/non-package-mode-cN-H_gxH-py3.12/lib/python3.12/site-packages/uvicorn/protocols/http/h11_impl.py", line 407, in run_asgi result = await app( # type: ignore[func-returns-value] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/stickb/Library/Caches/pypoetry/virtualenvs/non-package-mode-cN-H_gxH-py3.12/lib/python3.12/site-packages/uvicorn/middleware/proxy_headers.py", line 69, in __call__ return await self.app(scope, receive, send) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/stickb/Library/Caches/pypoetry/virtualenvs/non-package-mode-cN-H_gxH-py3.12/lib/python3.12/site-packages/fastapi/applications.py", line 1054, in __call__ await super().__call__(scope, receive, send) File "/Users/stickb/Library/Caches/pypoetry/virtualenvs/non-package-mode-cN-H_gxH-py3.12/lib/python3.12/site-packages/starlette/applications.py", line 123, in __call__ await self.middleware_stack(scope, receive, send) File "/Users/stickb/Library/Caches/pypoetry/virtualenvs/non-package-mode-cN-H_gxH-py3.12/lib/python3.12/site-packages/starlette/middleware/errors.py", line 186, in __call__ raise exc File "/Users/stickb/Library/Caches/pypoetry/virtualenvs/non-package-mode-cN-H_gxH-py3.12/lib/python3.12/site-packages/starlette/middleware/errors.py", line 164, in __call__ await self.app(scope, receive, _send) File "/Users/stickb/Library/Caches/pypoetry/virtualenvs/non-package-mode-cN-H_gxH-py3.12/lib/python3.12/site-packages/starlette/middleware/exceptions.py", line 65, in __call__ await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send) File "/Users/stickb/Library/Caches/pypoetry/virtualenvs/non-package-mode-cN-H_gxH-py3.12/lib/python3.12/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app raise exc File "/Users/stickb/Library/Caches/pypoetry/virtualenvs/non-package-mode-cN-H_gxH-py3.12/lib/python3.12/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app await app(scope, receive, sender) File "/Users/stickb/Library/Caches/pypoetry/virtualenvs/non-package-mode-cN-H_gxH-py3.12/lib/python3.12/site-packages/starlette/routing.py", line 756, in __call__ await self.middleware_stack(scope, receive, send) File "/Users/stickb/Library/Caches/pypoetry/virtualenvs/non-package-mode-cN-H_gxH-py3.12/lib/python3.12/site-packages/starlette/routing.py", line 776, in app await route.handle(scope, receive, send) File "/Users/stickb/Library/Caches/pypoetry/virtualenvs/non-package-mode-cN-H_gxH-py3.12/lib/python3.12/site-packages/starlette/routing.py", line 297, in handle await self.app(scope, receive, send) File "/Users/stickb/Library/Caches/pypoetry/virtualenvs/non-package-mode-cN-H_gxH-py3.12/lib/python3.12/site-packages/starlette/routing.py", line 77, in app await wrap_app_handling_exceptions(app, request)(scope, receive, send) File "/Users/stickb/Library/Caches/pypoetry/virtualenvs/non-package-mode-cN-H_gxH-py3.12/lib/python3.12/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app raise exc File "/Users/stickb/Library/Caches/pypoetry/virtualenvs/non-package-mode-cN-H_gxH-py3.12/lib/python3.12/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app await app(scope, receive, sender) File "/Users/stickb/Library/Caches/pypoetry/virtualenvs/non-package-mode-cN-H_gxH-py3.12/lib/python3.12/site-packages/starlette/routing.py", line 72, in app response = await func(request) ^^^^^^^^^^^^^^^^^^^ File "/Users/stickb/Library/Caches/pypoetry/virtualenvs/non-package-mode-cN-H_gxH-py3.12/lib/python3.12/site-packages/fastapi/routing.py", line 269, in app solved_result = await solve_dependencies( ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/stickb/Library/Caches/pypoetry/virtualenvs/non-package-mode-cN-H_gxH-py3.12/lib/python3.12/site-packages/fastapi/dependencies/utils.py", line 602, in solve_dependencies solved = await run_in_threadpool(call, **sub_values) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/stickb/Library/Caches/pypoetry/virtualenvs/non-package-mode-cN-H_gxH-py3.12/lib/python3.12/site-packages/starlette/concurrency.py", line 42, in run_in_threadpool return await anyio.to_thread.run_sync(func, *args) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/stickb/Library/Caches/pypoetry/virtualenvs/non-package-mode-cN-H_gxH-py3.12/lib/python3.12/site-packages/anyio/to_thread.py", line 56, in run_sync return await get_async_backend().run_sync_in_worker_thread( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/stickb/Library/Caches/pypoetry/virtualenvs/non-package-mode-cN-H_gxH-py3.12/lib/python3.12/site-packages/anyio/_backends/_asyncio.py", line 2144, in run_sync_in_worker_thread return await future ^^^^^^^^^^^^ File "/Users/stickb/Library/Caches/pypoetry/virtualenvs/non-package-mode-cN-H_gxH-py3.12/lib/python3.12/site-packages/anyio/_backends/_asyncio.py", line 851, in run result = context.run(func, *args) ^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/stickb/Library/Caches/pypoetry/virtualenvs/non-package-mode-cN-H_gxH-py3.12/lib/python3.12/site-packages/fastapi_nextauth_jwt/fastapi_nextauth_jwt.py", line 123, in __call__ raise InvalidTokenError(status_code=401, message="Invalid JWT format") fastapi_nextauth_jwt.exceptions.InvalidTokenErrorAnd this is the request I'm using: