I have a POST route /publish with a dependency get_current_user.
get_current_user has as fastapi.security.APIKeyCookie as dependency.
If authenticated correctly everything is fine but if I do a curl without the cookie I get 404 not found and get_current_user is not evoked.
Why I don't get 401 if the cookie is missing?
I also tried setting auto_error=False in APIKeyCookie but even then get_current_user was not evoked.
Is this the expected behavior?
EDIT: Added example
in auth.py
cookie_scheme = fastapi.security.APIKeyCookie(name="access_token_cookie", auto_error=False)
JWT_ALGORITHM = "HS256"
class UserAuthorization(pydantic.BaseModel):
username: str
groups: list[str]
async def get_current_user(
token: t.Annotated[str | None, fastapi.Depends(cookie_scheme)],
) -> UserAuthorization:
logger.info("inside get_current_user()") # this is never executed
in main.py
@app.post(
"/publish",
status_code=fastapi.status.HTTP_201_CREATED,
dependencies=[fastapi.Depends(auth.get_current_user)],
)
...
How I test it:
$ curl -X POST myserver/publish
<!doctype html>
<html lang=en>
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.</p>
Testing with invalid cookie value:
$ curl -X POST myserver/publish -b "access_token_cookie=someWrongValue"
<!doctype html>
<html lang=en>
<title>Redirecting...</title>
<h1>Redirecting...</h1>
<p>You should be redirected automatically to the target URL: <a href="myserver/publish">myserver/publish</a>. If not, click the link.