Skip to content

Commit 0751ad5

Browse files
committed
Handle token refresh for all auth methods in KGClient.refresh()
For static tokens (passed directly or via env var), prompt the user interactively for a new token. For clb_oauth, re-request from the platform. Credentials and device_flow already refresh automatically.
1 parent e981439 commit 0751ad5

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

fairgraph/client.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,19 +117,24 @@ def __init__(
117117
raise ImportError("Please install the ebrains-kg-core package")
118118
if client_id and client_secret:
119119
self._kg_client_builder = kg(host).with_credentials(client_id, client_secret)
120+
self._auth_method = "credentials"
120121
elif token:
121122
self._kg_client_builder = kg(host).with_token(token)
123+
self._auth_method = "token"
122124
elif clb_oauth:
123125
self._kg_client_builder = kg(host).with_token(clb_oauth.get_token()) # running in EBRAINS Jupyter Lab
126+
self._auth_method = "clb_oauth"
124127
else:
125128
try:
126129
self._kg_client_builder = kg(host).with_token(os.environ["KG_AUTH_TOKEN"])
130+
self._auth_method = "env"
127131
except KeyError:
128132
if allow_interactive:
129133
iam_config_url = "https://iam.ebrains.eu/auth/realms/hbp/.well-known/openid-configuration"
130134
self._kg_client_builder = kg(host).with_device_flow(
131135
client_id="kg-core-python", open_id_configuration_url=iam_config_url
132136
)
137+
self._auth_method = "device_flow"
133138
else:
134139
raise AuthenticationError("Need to provide either token or client id/secret.")
135140
self._kg_client = self._kg_client_builder.build()
@@ -144,7 +149,20 @@ def __init__(
144149
self.user_info()
145150

146151
def refresh(self):
147-
"""Rebuild the client with a fresh authentication token."""
152+
"""Rebuild the client with a fresh authentication token.
153+
154+
For credentials and device_flow auth, this obtains a new token automatically.
155+
For clb_oauth (Jupyter Lab), this requests a fresh token from the platform.
156+
For static tokens (passed directly or via env var), this prompts the user
157+
to paste a new token interactively.
158+
"""
159+
if self._auth_method in ("token", "env"):
160+
new_token = input("Authentication token has expired. Please paste a new token: ").strip()
161+
if not new_token:
162+
raise AuthenticationError("No token provided.")
163+
self._kg_client_builder = kg(self.host).with_token(new_token)
164+
elif self._auth_method == "clb_oauth":
165+
self._kg_client_builder = kg(self.host).with_token(clb_oauth.get_token())
148166
self._kg_client = self._kg_client_builder.build()
149167
self.__kg_admin_client = None
150168
self._user_info = None

0 commit comments

Comments
 (0)