Skip to content

Commit 9dc2f03

Browse files
committed
feat: check for credentials on new connection
1 parent 58840e1 commit 9dc2f03

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

datashield_opal/impl.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,14 @@ def __init__(self, name: str, loginInfo: OpalClient.LoginInfo, profile: str = "d
142142
self.rsession = None
143143
self.rsession_started = False
144144

145+
def check_user(self) -> bool:
146+
"""Check if the user can authenticate by trying to retrieve the current subject profile."""
147+
try:
148+
self._get("/system/subject-profile/_current").fail_on_error().send()
149+
return True
150+
except Exception:
151+
return False
152+
145153
#
146154
# Content listing
147155
#
@@ -191,7 +199,7 @@ def start_session(self, asynchronous: bool = True) -> RSession:
191199
self.rsession.start(asynchronous=asynchronous)
192200
self.rsession_started = not asynchronous or not self.rsession.is_pending()
193201
return self.rsession
194-
202+
195203
def is_session_started(self) -> bool:
196204
if self.rsession is None:
197205
return False
@@ -435,7 +443,11 @@ class OpalDriver(DSDriver):
435443
def new_connection(cls, args: DSLoginInfo, restore: str = None) -> DSConnection:
436444
namedArgs = Namespace(opal=args.url, user=args.user, password=args.password, token=args.token)
437445
loginInfo = OpalClient.LoginInfo.parse(namedArgs)
438-
return OpalConnection(args.name, loginInfo, args.profile, restore)
446+
conn = OpalConnection(args.name, loginInfo, args.profile, restore)
447+
if not conn.check_user():
448+
creds = f"user {args.user}" if args.user else "token"
449+
raise OpalDSError(ValueError(f"Failed to authenticate on {args.url} with {creds}"))
450+
return conn
439451

440452

441453
class OpalResult(DSResult):

0 commit comments

Comments
 (0)