Skip to content

Commit

Permalink
protocol: add request_authorization_object API
Browse files Browse the repository at this point in the history
This lets you get access to the underlying response object, in case you
want to get extra fields out of it.
  • Loading branch information
allisonkarlitskaya committed Jul 10, 2024
1 parent 798101c commit 8b07ac8
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/cockpit/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def eof_received(self) -> bool:
# Helpful functionality for "server"-side protocol implementations
class CockpitProtocolServer(CockpitProtocol):
init_host: 'str | None' = None
authorizations: 'dict[str, asyncio.Future[str]] | None' = None
authorizations: 'dict[str, asyncio.Future[JsonObject]] | None' = None
next_auth_id = 0

def do_send_init(self) -> None:
Expand Down Expand Up @@ -232,9 +232,9 @@ def do_ready(self) -> None:
self.do_send_init()

# authorize request/response API
async def request_authorization(
async def request_authorization_object(
self, challenge: str, timeout: 'int | None' = None, **kwargs: JsonValue
) -> str:
) -> JsonObject:
if self.authorizations is None:
self.authorizations = {}
cookie = str(self.next_auth_id)
Expand All @@ -247,12 +247,16 @@ async def request_authorization(
finally:
self.authorizations.pop(cookie)

async def request_authorization(
self, challenge: str, timeout: 'int | None' = None, **kwargs: JsonValue
) -> str:
return get_str(await self.request_authorization_object(challenge, timeout, **kwargs), 'response')

def do_authorize(self, message: JsonObject) -> None:
cookie = get_str(message, 'cookie')
response = get_str(message, 'response')

if self.authorizations is None or cookie not in self.authorizations:
logger.warning('no matching authorize request')
return

self.authorizations[cookie].set_result(response)
self.authorizations[cookie].set_result(message)

0 comments on commit 8b07ac8

Please sign in to comment.