p2-inc / phasetwo-python

Phase Two API Python SDK
https://phasetwo.io
Apache License 2.0
3 stars 4 forks source link

Auto refresh token #6

Open kbumsik opened 6 months ago

kbumsik commented 6 months ago

Hi,

Thank you for making a proper org feature in Keycloak!

We have been using keycloak-org for weeks with this Python client.

I noticed that python-keycloak package automatically refresh token when the token is expired, but this package does not.

Because of this, every time I make a request I have to refresh token (when expired) and create phasetwo.Configuration and phasetwo.ApiClient

keycloak_admin = KeycloakAdmin(server_url="https://my-keycloak-host/auth/",
                               username='admin',
                               password='password',
                               realm_name=realm,
                               client_id='admin-cli',
                               verify=True)

# Before making a request
if (keycloak_admin.connection.expires_at is None) or (datetime.now() >= keycloak_admin.connection.expires_at):
            keycloak_admin.connection.refresh_token()

configuration = phasetwo.Configuration(
    host = "https://app.phasetwo.io/auth/realms"
    access_token = keycloak_admin.token()
)

client = phasetwo.ApiClient(configuration)

Devs won't notice this until they come across this issue.

xgp commented 6 months ago

Good catch. I wasn't sure if calling the token() method automatically got a fresh token. Would you mind doing the PR?

kbumsik commented 6 months ago

Maybe I will think about proposing a PR after my current dev sprint at work ends 😝