diff options
author | Frederick Muriuki Muriithi | 2023-03-07 05:49:21 +0300 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2023-03-07 05:49:21 +0300 |
commit | 09c2330e9e8279f6c9fd391a736435ceb4705873 (patch) | |
tree | 38f3f2835cf608d89994da00bf060734de0baa65 /gn3/auth/authentication/oauth2/models/oauth2client.py | |
parent | 361a934d1c0d75da01bd19d8ac78bf15aa7e2cad (diff) | |
download | genenetwork3-09c2330e9e8279f6c9fd391a736435ceb4705873.tar.gz |
auth: user_by_id: Return a user or raise an exception
Fetching the user by id should return the user, or raise an exception. We get
rid of the Maybe monad here since it is leading to some weird code flows -
probably the wrong monad to use here.
Diffstat (limited to 'gn3/auth/authentication/oauth2/models/oauth2client.py')
-rw-r--r-- | gn3/auth/authentication/oauth2/models/oauth2client.py | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/gn3/auth/authentication/oauth2/models/oauth2client.py b/gn3/auth/authentication/oauth2/models/oauth2client.py index 70b8f59..14f4d5d 100644 --- a/gn3/auth/authentication/oauth2/models/oauth2client.py +++ b/gn3/auth/authentication/oauth2/models/oauth2client.py @@ -9,6 +9,8 @@ from pymonad.maybe import Just, Maybe, Nothing from gn3.auth import db from gn3.auth.authentication.users import User, user_by_id +from gn3.auth.authorisation.errors import NotFoundError + class OAuth2Client(NamedTuple): """ Client to the OAuth2 Server. @@ -134,8 +136,12 @@ def client(conn: db.DbConnection, client_id: uuid.UUID, cursor.execute( "SELECT * FROM oauth2_clients WHERE client_id=?", (str(client_id),)) result = cursor.fetchone() - the_user = user or user_by_id(conn, result["user_id"]).maybe( - None, lambda usr: usr)# type: ignore + the_user = user + if not bool(the_user): + try: + the_user = user_by_id(conn, result["user_id"]) + except NotFoundError as _nfe: + the_user = None if result: return Just( OAuth2Client(uuid.UUID(result["client_id"]), @@ -145,6 +151,6 @@ def client(conn: db.DbConnection, client_id: uuid.UUID, datetime.datetime.fromtimestamp( result["client_secret_expires_at"]), json.loads(result["client_metadata"]), - the_user)) + the_user))# type: ignore[arg-type] return Nothing |