mirror of
https://github.com/lnbits/lnbits-legend.git
synced 2025-02-23 22:47:05 +01:00
chore: keychecker todo remove type ignores (#2337)
fixes a comment `TODO`
This commit is contained in:
parent
241b286e21
commit
8dcb53aea0
1 changed files with 13 additions and 12 deletions
|
@ -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."
|
||||||
|
|
Loading…
Add table
Reference in a new issue