Repository: MushroomMaula/fastapi_login

Issue:
TypeError: catching classes that do not inherit from BaseException is not allowed
It happens when the token is not correct, for example, a random string token.

```python
INFO:     127.0.0.1:51577 - "POST /auth/refresh HTTP/1.1" 500 Internal Server Error
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/Users/user/my-api/venv/lib/python3.9/site-packages/uvicorn/protocols/http/h11_impl.py", line 369, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "/Users/user/my-api/venv/lib/python3.9/site-packages/uvicorn/middleware/proxy_headers.py", line 59, in __call__
    return await self.app(scope, receive, send)
  File "/Users/user/my-api/venv/lib/python3.9/site-packages/fastapi/applications.py", line 199, in __call__
    await super().__call__(scope, receive, send)
  File "/Users/user/my-api/venv/lib/python3.9/site-packages/starlette/applications.py", line 112, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/Users/user/my-api/venv/lib/python3.9/site-packages/starlette/middleware/errors.py", line 181, in __call__
    raise exc from None
  File "/Users/user/my-api/venv/lib/python3.9/site-packages/starlette/middleware/errors.py", line 159, in __call__
    await self.app(scope, receive, _send)
  File "/Users/user/my-api/venv/lib/python3.9/site-packages/starlette/exceptions.py", line 82, in __call__
    raise exc from None
  File "/Users/user/my-api/venv/lib/python3.9/site-packages/starlette/exceptions.py", line 71, in __call__
    await self.app(scope, receive, sender)
  File "/Users/user/my-api/venv/lib/python3.9/site-packages/starlette/routing.py", line 580, in __call__
    await route.handle(scope, receive, send)
  File "/Users/user/my-api/venv/lib/python3.9/site-packages/starlette/routing.py", line 241, in handle
    await self.app(scope, receive, send)
  File "/Users/user/my-api/venv/lib/python3.9/site-packages/starlette/routing.py", line 52, in app
    response = await func(request)
  File "/Users/user/my-api/venv/lib/python3.9/site-packages/fastapi/routing.py", line 206, in app
    solved_result = await solve_dependencies(
  File "/Users/user/my-api/venv/lib/python3.9/site-packages/fastapi/dependencies/utils.py", line 548, in solve_dependencies
    solved = await call(**sub_values)
  File "/Users/user/my-api/venv/lib/python3.9/site-packages/fastapi_login/fastapi_login.py", line 346, in __call__
    if not self.has_scopes(token, security_scopes):
  File "/Users/user/my-api/venv/lib/python3.9/site-packages/fastapi_login/fastapi_login.py", line 308, in has_scopes
    except self.not_authenticated_exception as e:
TypeError: catching classes that do not inherit from BaseException is not allowed

```

Discussion:

Patch:
diff --git a/fastapi_login/fastapi_login.py b/fastapi_login/fastapi_login.py
index ed75a80..ea07735 100644
--- a/fastapi_login/fastapi_login.py
+++ b/fastapi_login/fastapi_login.py
@@ -308,7 +308,7 @@ class LoginManager(OAuth2PasswordBearer):
         """
         try:
             payload = self._get_payload(token)
-        except self.not_authenticated_exception:
+        except type(self.not_authenticated_exception):
             # We got an error while decoding the token
             return False
 
@@ -369,5 +369,5 @@ class LoginManager(OAuth2PasswordBearer):
                 # it's not a good idea to return the Exception
                 # so we set the user to None
                 request.state.user = None
-            
+
             return await call_next(request)


Test patch:
diff --git a/tests/conftest.py b/tests/conftest.py
index 64fc115..3b2e503 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -85,5 +85,4 @@ class CustomAuthException(Exception):
 
 @pytest.fixture
 def custom_exception():
-
     return CustomAuthException
diff --git a/tests/test_basic_functionality.py b/tests/test_basic_functionality.py
index 0317b43..993d40d 100644
--- a/tests/test_basic_functionality.py
+++ b/tests/test_basic_functionality.py
@@ -68,8 +68,8 @@ def test_token_from_cookie_raises(clean_manager):
         clean_manager._token_from_cookie(request)
 
     assert exc_info.value == InvalidCredentialsException
-    
-    
+
+
 def test_token_from_cookie_returns_none_auto_error_off(clean_manager):
     clean_manager.auto_error = False
     request = Mock(cookies={clean_manager.cookie_name: ""})
@@ -116,5 +116,6 @@ def test_has_scopes_missing_scopes(clean_manager, default_data):
     required_scopes = scopes + ["write"]
     assert clean_manager.has_scopes(token, SecurityScopes(scopes=required_scopes)) is False
 
-
-
+def test_has_scopes_invalid_token(clean_manager):
+    token = "invalid-token"
+    assert not clean_manager.has_scopes(token, SecurityScopes(scopes=["test"]))