def token_login(form_data: HTTPBasicCredentials = Depends(HTTPBasic()), api_config=Depends(get_api_config)): if verify_auth(api_config, form_data.username, form_data.password): token_data = {'identity': {'u': form_data.username}} access_token = create_token( token_data, api_config.get('jwt_secret_key', 'super-secret')) refresh_token = create_token(token_data, api_config.get('jwt_secret_key', 'super-secret'), token_type="refresh") return { "access_token": access_token, "refresh_token": refresh_token, } else: raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Incorrect username or password", )
from fastapi.params import Depends from fastapi.security.http import HTTPBasic, HTTPBasicCredentials import services.dopagent_scraper as scraper security = HTTPBasic() def get_auth_token( credentials: HTTPBasicCredentials = Depends(security), ) -> scraper.AuthToken: return scraper.get_auth_token(credentials.username, credentials.password)
from fastapi.security.http import HTTPBasic, HTTPBearer __all__ = () jwt_bearer_scheme = HTTPBearer( auto_error=False, bearerFormat="JWT-formatted OAuth2 Access Token", scheme_name="JWT-formatted OAuth2 Access Token", ) http_basic_scheme = HTTPBasic(auto_error=False)
from freqtrade.rpc.api_server.api_schemas import AccessAndRefreshToken, AccessToken from freqtrade.rpc.api_server.deps import get_api_config ALGORITHM = "HS256" router_login = APIRouter() def verify_auth(api_config, username: str, password: str): """Verify username/password""" return (secrets.compare_digest(username, api_config.get('username')) and secrets.compare_digest(password, api_config.get('password'))) httpbasic = HTTPBasic(auto_error=False) oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token", auto_error=False) def get_user_from_token(token, secret_key: str, token_type: str = "access"): credentials_exception = HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Could not validate credentials", headers={"WWW-Authenticate": "Bearer"}, ) try: payload = jwt.decode(token, secret_key, algorithms=[ALGORITHM]) username: str = payload.get("identity", {}).get('u') if username is None: raise credentials_exception if payload.get("type") != token_type:
def get_security(sec=Security(HTTPBasic())): return sec