chore: keychecker todo remove type ignores (#2337)

fixes a comment `TODO`
This commit is contained in:
dni ⚡ 2024-03-21 11:36:01 +01:00 committed by GitHub
parent 241b286e21
commit 8dcb53aea0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -3,7 +3,7 @@ from typing import Annotated, Literal, Optional, Type, Union
from fastapi import Cookie, Depends, Query, Request, Security from fastapi import Cookie, Depends, Query, Request, Security
from fastapi.exceptions import HTTPException from fastapi.exceptions import HTTPException
from fastapi.openapi.models import APIKey, APIKeyIn from fastapi.openapi.models import APIKey, APIKeyIn, SecuritySchemeType
from fastapi.security import APIKeyHeader, APIKeyQuery, OAuth2PasswordBearer from fastapi.security import APIKeyHeader, APIKeyQuery, OAuth2PasswordBearer
from fastapi.security.base import SecurityBase from fastapi.security.base import SecurityBase
from jose import ExpiredSignatureError, JWTError, jwt from jose import ExpiredSignatureError, JWTError, jwt
@ -17,14 +17,13 @@ from lnbits.core.crud import (
get_user, get_user,
get_wallet_for_key, get_wallet_for_key,
) )
from lnbits.core.models import User, WalletType, WalletTypeInfo from lnbits.core.models import User, Wallet, WalletType, WalletTypeInfo
from lnbits.db import Filter, Filters, TFilterModel from lnbits.db import Filter, Filters, TFilterModel
from lnbits.settings import AuthMethods, settings from lnbits.settings import AuthMethods, settings
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="api/v1/auth", auto_error=False) oauth2_scheme = OAuth2PasswordBearer(tokenUrl="api/v1/auth", auto_error=False)
# TODO: fix type ignores
class KeyChecker(SecurityBase): class KeyChecker(SecurityBase):
def __init__( def __init__(
self, self,
@ -33,23 +32,25 @@ class KeyChecker(SecurityBase):
api_key: Optional[str] = None, api_key: Optional[str] = None,
): ):
self.scheme_name = scheme_name or self.__class__.__name__ self.scheme_name = scheme_name or self.__class__.__name__
self.auto_error = auto_error self.auto_error: bool = auto_error
self._key_type = WalletType.invoice self._key_type: WalletType = WalletType.invoice
self._api_key = api_key self._api_key = api_key
if api_key: if api_key:
key = APIKey( openapi_model = APIKey(
**{"in": APIKeyIn.query}, # type: ignore **{"in": APIKeyIn.query},
type=SecuritySchemeType.apiKey,
name="X-API-KEY", name="X-API-KEY",
description="Wallet API Key - QUERY", description="Wallet API Key - QUERY",
) )
else: else:
key = APIKey( openapi_model = APIKey(
**{"in": APIKeyIn.header}, # type: ignore **{"in": APIKeyIn.header},
type=SecuritySchemeType.apiKey,
name="X-API-KEY", name="X-API-KEY",
description="Wallet API Key - HEADER", description="Wallet API Key - HEADER",
) )
self.wallet = None self.wallet: Optional[Wallet] = None
self.model: APIKey = key self.model: APIKey = openapi_model
async def __call__(self, request: Request): async def __call__(self, request: Request):
try: try:
@ -67,7 +68,7 @@ class KeyChecker(SecurityBase):
status_code=HTTPStatus.UNAUTHORIZED, status_code=HTTPStatus.UNAUTHORIZED,
detail="Invalid key or wallet.", detail="Invalid key or wallet.",
) )
self.wallet = wallet # type: ignore self.wallet = wallet
except KeyError: except KeyError:
raise HTTPException( raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST, detail="`X-API-KEY` header missing." status_code=HTTPStatus.BAD_REQUEST, detail="`X-API-KEY` header missing."