Skip to content

AttributeError: 'TokenMeForm' object has no attribute 'rsplit' #337

@DragosSofia

Description

@DragosSofia

When tuning a fastapi program i code and decode a jwt token with the functions bellow and i get the next error:
Any idee why?

SECRET_KEY = "c6258bf0076e5f28f08142a60b95fc78f9cdc9827ce192c6dcb0d934ef816b83"
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRES_MINUTES = 30

def create_access_token(data: dict, expires_delta: timedelta or None = None):
to_enconde = data.copy()
if expires_delta:
expire = datetime.utcnow() + expires_delta
else:
expire = datetime.utcnow() + timedelta(minutes=15)

to_enconde.update({"exp": expire})
encode_jwt = jwt.encode(to_enconde, SECRET_KEY, algorithm=ALGORITHM)
return encode_jwt

async def get_current_user(token: str = Depends(oauth2_scheme)):
credential_exception = HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Could not validate credentials", headers={"WWW-Authenticate": "Bearer"})
print(token)
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=ALGORITHM)
username: str = payload.get("sub")
if username is None:
raise credential_exception

    token_data = TokenData(username=username)
except JWTError:
    raise credential_exception

user = get_user(db, username=token_data.username)
if user is None:
    raise credential_exception

return user

auth_logic-web-1 | INFO: Will watch for changes in these directories: ['/app']
auth_logic-web-1 | INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
auth_logic-web-1 | INFO: Started reloader process [7] using WatchFiles
auth_logic-web-1 | INFO: Started server process [9]
auth_logic-web-1 | INFO: Waiting for application startup.
auth_logic-web-1 | INFO: Application startup complete.
auth_logic-web-1 | (trapped) error reading bcrypt version
auth_logic-web-1 | Traceback (most recent call last):
auth_logic-web-1 | File "/usr/local/lib/python3.11/site-packages/passlib/handlers/bcrypt.py", line 620, in _load_backend_mixin
auth_logic-web-1 | version = _bcrypt.about.version
auth_logic-web-1 | ^^^^^^^^^^^^^^^^^
auth_logic-web-1 | AttributeError: module 'bcrypt' has no attribute 'about'
auth_logic-web-1 | {'tim': {'username': 'tim', 'email': 'tim@gmail.com', 'hashed_password': '', 'disabled': False}, 'dragos': {'username': 'dragos', 'email': 'asd', 'hashed_password': '$2b$12$COdo2Shaq4ZPz1K1r7VaDuuuF4TCzxK.1GODYl.vY76BFCIdAvlnW', 'disabled': False}}
auth_logic-web-1 | INFO: 172.19.0.1:44658 - "POST /register HTTP/1.1" 200 OK
auth_logic-web-1 | INFO: 172.19.0.1:44658 - "GET /login HTTP/1.1" 200 OK
auth_logic-web-1 | INFO: 172.19.0.1:44658 - "POST /token HTTP/1.1" 200 OK
auth_logic-web-1 | token='eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJkc…zMjJ9.1B2efhpvAGvAm8lbKlPcomWto_8GvNp51fFpfLqWyZg'
auth_logic-web-1 | INFO: 172.19.0.1:44664 - "POST /users/me/ HTTP/1.1" 500 Internal Server Error
auth_logic-web-1 | ERROR: Exception in ASGI application
auth_logic-web-1 | Traceback (most recent call last):
auth_logic-web-1 | File "/usr/local/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 411, in run_asgi
auth_logic-web-1 | result = await app( # type: ignore[func-returns-value]
auth_logic-web-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
auth_logic-web-1 | File "/usr/local/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 69, in call
auth_logic-web-1 | return await self.app(scope, receive, send)
auth_logic-web-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
auth_logic-web-1 | File "/usr/local/lib/python3.11/site-packages/fastapi/applications.py", line 1054, in call
auth_logic-web-1 | await super().call(scope, receive, send)
auth_logic-web-1 | File "/usr/local/lib/python3.11/site-packages/starlette/applications.py", line 123, in call
auth_logic-web-1 | await self.middleware_stack(scope, receive, send)
auth_logic-web-1 | File "/usr/local/lib/python3.11/site-packages/starlette/middleware/errors.py", line 186, in call
auth_logic-web-1 | raise exc
auth_logic-web-1 | File "/usr/local/lib/python3.11/site-packages/starlette/middleware/errors.py", line 164, in call
auth_logic-web-1 | await self.app(scope, receive, _send)
auth_logic-web-1 | File "/usr/local/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 65, in call
auth_logic-web-1 | await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
auth_logic-web-1 | File "/usr/local/lib/python3.11/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app
auth_logic-web-1 | raise exc
auth_logic-web-1 | File "/usr/local/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
auth_logic-web-1 | await app(scope, receive, sender)
auth_logic-web-1 | File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 756, in call
auth_logic-web-1 | await self.middleware_stack(scope, receive, send)
auth_logic-web-1 | File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 776, in app
auth_logic-web-1 | await route.handle(scope, receive, send)
auth_logic-web-1 | File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 297, in handle
auth_logic-web-1 | await self.app(scope, receive, send)
auth_logic-web-1 | File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 77, in app
auth_logic-web-1 | await wrap_app_handling_exceptions(app, request)(scope, receive, send)
auth_logic-web-1 | File "/usr/local/lib/python3.11/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app
auth_logic-web-1 | raise exc
auth_logic-web-1 | File "/usr/local/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
auth_logic-web-1 | await app(scope, receive, sender)
auth_logic-web-1 | File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 72, in app
auth_logic-web-1 | response = await func(request)
auth_logic-web-1 | ^^^^^^^^^^^^^^^^^^^
auth_logic-web-1 | File "/usr/local/lib/python3.11/site-packages/fastapi/routing.py", line 278, in app
auth_logic-web-1 | raw_response = await run_endpoint_function(
auth_logic-web-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
auth_logic-web-1 | File "/usr/local/lib/python3.11/site-packages/fastapi/routing.py", line 191, in run_endpoint_function
auth_logic-web-1 | return await dependant.call(**values)
auth_logic-web-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
auth_logic-web-1 | File "/app/main.py", line 142, in read_users_me
auth_logic-web-1 | current_user = await get_current_user(token)
auth_logic-web-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
auth_logic-web-1 | File "/app/main.py", line 94, in get_current_user
auth_logic-web-1 | payload = jwt.decode(token, SECRET_KEY, algorithms=ALGORITHM)
auth_logic-web-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
auth_logic-web-1 | File "/usr/local/lib/python3.11/site-packages/jose/jwt.py", line 142, in decode
auth_logic-web-1 | payload = jws.verify(token, key, algorithms, verify=verify_signature)
auth_logic-web-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
auth_logic-web-1 | File "/usr/local/lib/python3.11/site-packages/jose/jws.py", line 70, in verify
auth_logic-web-1 | header, payload, signing_input, signature = _load(token)
auth_logic-web-1 | ^^^^^^^^^^^^
auth_logic-web-1 | File "/usr/local/lib/python3.11/site-packages/jose/jws.py", line 176, in _load
auth_logic-web-1 | signing_input, crypto_segment = jwt.rsplit(b".", 1)
auth_logic-web-1 | ^^^^^^^^^^
auth_logic-web-1 | File "/usr/local/lib/python3.11/site-packages/pydantic/main.py", line 792, in getattr
auth_logic-web-1 | raise AttributeError(f'{type(self).name!r} object has no attribute {item!r}')
auth_logic-web-1 | AttributeError: 'TokenMeForm' object has no attribute 'rsplit'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions