Skip to content

Commit 754fbc6

Browse files
authored
fix(iam): tolerate partial Session response when caller lacks sessionsAcl:LIST (#2617)
1 parent fd97419 commit 754fbc6

4 files changed

Lines changed: 80 additions & 12 deletions

File tree

cognite/client/_api/iam/sessions.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@
77
from cognite.client._constants import DEFAULT_LIMIT_READ
88
from cognite.client.config import ClientConfig
99
from cognite.client.credentials import OAuthClientCredentials
10-
from cognite.client.data_classes import ClientCredentials, CreatedSession, Session, SessionList
10+
from cognite.client.data_classes import (
11+
ClientCredentials,
12+
CreatedSession,
13+
RevokedSession,
14+
RevokedSessionList,
15+
Session,
16+
SessionList,
17+
)
1118
from cognite.client.data_classes.iam import SessionStatus, SessionType
1219
from cognite.client.utils._identifier import IdentifierSequence
1320

@@ -77,12 +84,12 @@ async def create(
7784
return CreatedSession.load(response.json()["items"][0])
7885

7986
@overload
80-
async def revoke(self, id: int) -> Session: ...
87+
async def revoke(self, id: int) -> RevokedSession: ...
8188

8289
@overload
83-
async def revoke(self, id: Sequence[int]) -> SessionList: ...
90+
async def revoke(self, id: Sequence[int]) -> RevokedSessionList: ...
8491

85-
async def revoke(self, id: int | Sequence[int]) -> Session | SessionList:
92+
async def revoke(self, id: int | Sequence[int]) -> RevokedSession | RevokedSessionList:
8693
"""`Revoke access to a session <https://api-docs.cognite.com/20230101/tag/Sessions/operation/revokeSessions>`_.
8794
8895
Revocation of a session may in some cases take up to 1 hour to take effect.
@@ -91,7 +98,8 @@ async def revoke(self, id: int | Sequence[int]) -> Session | SessionList:
9198
id (int | Sequence[int]): Id or list of session ids
9299
93100
Returns:
94-
Session | SessionList: List of revoked sessions. If the user does not have the sessionsAcl:LIST capability, then only the session IDs will be present in the response.
101+
RevokedSession | RevokedSessionList: Revoked session(s). If the caller lacks sessionsAcl:LIST, only the
102+
session ID will be present; all other fields will be None.
95103
"""
96104

97105
ident_sequence = IdentifierSequence.load(ids=id, external_ids=None)
@@ -106,7 +114,7 @@ async def revoke(self, id: int | Sequence[int]) -> Session | SessionList:
106114
),
107115
)
108116

109-
revoked_sessions = SessionList._load(revoked_sessions_res)
117+
revoked_sessions = RevokedSessionList._load(revoked_sessions_res)
110118
return revoked_sessions[0] if ident_sequence.is_singleton() else revoked_sessions
111119

112120
@overload

cognite/client/_sync_api/iam/sessions.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
===============================================================================
3-
6ed1f4c4630f2ef966184b94bd0c2e50
3+
66ef20c6bf5b2e991efb781b993720f0
44
This file is auto-generated from the Async API modules, - do not edit manually!
55
===============================================================================
66
"""
@@ -13,7 +13,14 @@
1313
from cognite.client import AsyncCogniteClient
1414
from cognite.client._constants import DEFAULT_LIMIT_READ
1515
from cognite.client._sync_api_client import SyncAPIClient
16-
from cognite.client.data_classes import ClientCredentials, CreatedSession, Session, SessionList
16+
from cognite.client.data_classes import (
17+
ClientCredentials,
18+
CreatedSession,
19+
RevokedSession,
20+
RevokedSessionList,
21+
Session,
22+
SessionList,
23+
)
1724
from cognite.client.data_classes.iam import SessionStatus, SessionType
1825
from cognite.client.utils._async_helpers import run_sync
1926

@@ -58,12 +65,12 @@ def create(
5865
)
5966

6067
@overload
61-
def revoke(self, id: int) -> Session: ...
68+
def revoke(self, id: int) -> RevokedSession: ...
6269

6370
@overload
64-
def revoke(self, id: Sequence[int]) -> SessionList: ...
71+
def revoke(self, id: Sequence[int]) -> RevokedSessionList: ...
6572

66-
def revoke(self, id: int | Sequence[int]) -> Session | SessionList:
73+
def revoke(self, id: int | Sequence[int]) -> RevokedSession | RevokedSessionList:
6774
"""
6875
`Revoke access to a session <https://api-docs.cognite.com/20230101/tag/Sessions/operation/revokeSessions>`_.
6976
@@ -73,7 +80,8 @@ def revoke(self, id: int | Sequence[int]) -> Session | SessionList:
7380
id (int | Sequence[int]): Id or list of session ids
7481
7582
Returns:
76-
Session | SessionList: List of revoked sessions. If the user does not have the sessionsAcl:LIST capability, then only the session IDs will be present in the response.
83+
RevokedSession | RevokedSessionList: Revoked session(s). If the caller lacks sessionsAcl:LIST, only the
84+
session ID will be present; all other fields will be None.
7785
"""
7886
return run_sync(self.__async_client.iam.sessions.revoke(id=id))
7987

cognite/client/data_classes/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@
143143
GroupList,
144144
GroupWrite,
145145
GroupWriteList,
146+
RevokedSession,
147+
RevokedSessionList,
146148
SecurityCategory,
147149
SecurityCategoryList,
148150
SecurityCategoryWrite,
@@ -459,6 +461,8 @@
459461
"RelationshipWrite",
460462
"RelationshipWriteList",
461463
"RevisionCameraProperties",
464+
"RevokedSession",
465+
"RevokedSessionList",
462466
"Row",
463467
"RowList",
464468
"RowWrite",

cognite/client/data_classes/iam.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,54 @@ class SessionList(CogniteResourceList[Session], IdTransformerMixin):
523523
_RESOURCE = Session
524524

525525

526+
class RevokedSession(CogniteResource):
527+
"""A session that has been revoked.
528+
529+
When the caller lacks sessionsAcl:LIST, the revoke API returns only the session ID.
530+
All other fields are present only when the caller has sessionsAcl:LIST.
531+
532+
Args:
533+
id (int): ID of the revoked session.
534+
type (SessionType | None): Credentials kind used to create the session.
535+
status (SessionStatus | None): Current status of the session.
536+
creation_time (int | None): Session creation time, in milliseconds since 1970.
537+
expiration_time (int | None): Session expiry time, in milliseconds since 1970. This value is updated on
538+
refreshing a token.
539+
client_id (str | None): Client ID in identity provider.
540+
"""
541+
542+
def __init__(
543+
self,
544+
id: int,
545+
type: SessionType | None = None,
546+
status: SessionStatus | None = None,
547+
creation_time: int | None = None,
548+
expiration_time: int | None = None,
549+
client_id: str | None = None,
550+
) -> None:
551+
self.id = id
552+
self.type = type
553+
self.status = status
554+
self.creation_time = creation_time
555+
self.expiration_time = expiration_time
556+
self.client_id = client_id
557+
558+
@classmethod
559+
def _load(cls, resource: dict[str, Any]) -> Self:
560+
return cls(
561+
id=resource["id"],
562+
type=resource.get("type"),
563+
status=resource.get("status"),
564+
creation_time=resource.get("creationTime"),
565+
expiration_time=resource.get("expirationTime"),
566+
client_id=resource.get("clientId"),
567+
)
568+
569+
570+
class RevokedSessionList(CogniteResourceList[RevokedSession], IdTransformerMixin):
571+
_RESOURCE = RevokedSession
572+
573+
526574
class ClientCredentials(CogniteResource):
527575
"""Client credentials for session creation
528576

0 commit comments

Comments
 (0)