From 224a31179278a92f67dd9c4cb08fc9804e1ec75b Mon Sep 17 00:00:00 2001 From: Eric Lopes Date: Mon, 15 Jun 2020 20:04:03 +0800 Subject: [PATCH] fix "Authorization: Bearer Undefined" on Swagger (#213) * fix "Authorization: Bearer Undefined" on Swagger * updating pytest to recognize access_token * fix pytest KeyError * adding changes requested (add token_type to documentation and tests) * fix documentation --- docs/configuration/authentication/jwt.md | 3 ++- docs/usage/flow.md | 3 ++- fastapi_users/authentication/jwt.py | 2 +- tests/test_authentication_jwt.py | 5 +++-- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/docs/configuration/authentication/jwt.md b/docs/configuration/authentication/jwt.md index 11471026..0606118e 100644 --- a/docs/configuration/authentication/jwt.md +++ b/docs/configuration/authentication/jwt.md @@ -36,7 +36,8 @@ This method will return a JWT token upon successful login: !!! success "`200 OK`" ```json { - "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiOTIyMWZmYzktNjQwZi00MzcyLTg2ZDMtY2U2NDJjYmE1NjAzIiwiYXVkIjoiZmFzdGFwaS11c2VyczphdXRoIiwiZXhwIjoxNTcxNTA0MTkzfQ.M10bjOe45I5Ncu_uXvOmVV8QxnL-nZfcH96U90JaocI" + "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiOTIyMWZmYzktNjQwZi00MzcyLTg2ZDMtY2U2NDJjYmE1NjAzIiwiYXVkIjoiZmFzdGFwaS11c2VyczphdXRoIiwiZXhwIjoxNTcxNTA0MTkzfQ.M10bjOe45I5Ncu_uXvOmVV8QxnL-nZfcH96U90JaocI", + "token_type": "bearer" } ``` diff --git a/docs/usage/flow.md b/docs/usage/flow.md index bce9e3f8..22f1740f 100644 --- a/docs/usage/flow.md +++ b/docs/usage/flow.md @@ -94,7 +94,8 @@ You'll get a JSON response looking like this: ```json { - "token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNGZkMzQ3N2ItZWNjZi00ZWUzLThmN2QtNjhhZDcyMjYxNDc2IiwiYXVkIjoiZmFzdGFwaS11c2VyczphdXRoIiwiZXhwIjoxNTg3ODE4NDI5fQ.anO3JR8-WYCozZ4_2-PQ2Ov9O38RaLP2RAzQIiZhteM" + "access_token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNGZkMzQ3N2ItZWNjZi00ZWUzLThmN2QtNjhhZDcyMjYxNDc2IiwiYXVkIjoiZmFzdGFwaS11c2VyczphdXRoIiwiZXhwIjoxNTg3ODE4NDI5fQ.anO3JR8-WYCozZ4_2-PQ2Ov9O38RaLP2RAzQIiZhteM", + "token_type": "bearer" } ``` diff --git a/fastapi_users/authentication/jwt.py b/fastapi_users/authentication/jwt.py index 185356e6..e238e28e 100644 --- a/fastapi_users/authentication/jwt.py +++ b/fastapi_users/authentication/jwt.py @@ -65,7 +65,7 @@ class JWTAuthentication(BaseAuthentication[str]): async def get_login_response(self, user: BaseUserDB, response: Response) -> Any: token = await self._generate_token(user) - return {"token": token} + return {"access_token": token, "token_type": "bearer"} async def _generate_token(self, user: BaseUserDB) -> str: data = {"user_id": str(user.id), "aud": self.token_audience} diff --git a/tests/test_authentication_jwt.py b/tests/test_authentication_jwt.py index a16383a8..aebe83c9 100644 --- a/tests/test_authentication_jwt.py +++ b/tests/test_authentication_jwt.py @@ -68,9 +68,10 @@ class TestAuthenticate: async def test_get_login_response(jwt_authentication, user): login_response = await jwt_authentication.get_login_response(user, Response()) - assert "token" in login_response + assert "access_token" in login_response + assert login_response["token_type"] == "bearer" - token = login_response["token"] + token = login_response["access_token"] decoded = jwt.decode( token, SECRET, audience="fastapi-users:auth", algorithms=[JWT_ALGORITHM] )