code-specialist / fastapi-keycloak

Keycloak integration for Python FastAPI
https://fastapi-keycloak.code-specialist.com/
Apache License 2.0
193 stars 50 forks source link

FastAPIKeycloak Timeout #44

Closed valeriiduz closed 2 years ago

valeriiduz commented 2 years ago

My team is using this lib in a commercial project to connect Keycloak from Fastapi and we observed this lib doesn't use timeout in connection to Keycloak, to avoid that I wrote this:

class CustomTimeout(TimeoutSauce):
    def __init__(self, *args, **kwargs):
        if kwargs['connect'] is None:
            kwargs['connect'] = 5
        if kwargs['read'] is None:
            kwargs['read'] = 5
        super().__init__(*args, **kwargs)

requests.adapters.TimeoutSauce = CustomTimeout
IDP = FastAPIKeycloak(
    server_url=KEYCLOAK_SERVER_URL,
    client_id=KEYCLOAK_CLIENT_ID,
    client_secret=KEYCLOAK_CLIENT_SECRET,
    admin_client_secret=KEYCLOAK_ADMIN_CLIENT_SECRET,
    realm=KEYCLOAK_REALM,
    callback_uri=KEYCLOAK_CALLBACK_URI,
)

And decided to add timeout to FastAPIKeycloak init. Code checked and all tests passed

Please review my PR and approve if it is okay for you to see timeout in this lib

yannicschroeer commented 2 years ago

Thank you for this contribution!