from __future__ import annotations
from dataclasses import dataclass
import urllib.parse
from lxml import objectify # type: ignore
from q2_sdk.tools.utils import to_bool
from .base import BaseAuthRequest, BaseAuthResponse
[docs]
@dataclass
class Request(BaseAuthRequest):
"""
.. code-block:: xml
<HQ request="InitiateLogonUserExternal" messageID="{0}">
<IsPrelogonSession>False</IsPrelogonSession>
<SessionId>shb3cnukdl32c54nodvyxcsj</SessionId>
</HQ>
"""
raw: objectify.Element
is_prelogon_session: bool
session_id: str
@staticmethod
def from_xml(xml: objectify.Element) -> Request:
is_prelogon_session = to_bool(xml.IsPrelogonSession.text)
session_id = xml.SessionId.text
return Request(
xml,
is_prelogon_session,
session_id,
)
[docs]
@dataclass
class Response(BaseAuthResponse):
"""
Object representation of the Initiate Logon User External Response. This just follows the OIDC spec:
https://openid.net/specs/openid-connect-core-1_0.html#authRequest
Then embeds this URL inside of an HQ response under the <RedirectUrl> node
.. code-block:: xml
<Q2Bridge request="InitiateLogonUserExternal" messageId="messageID">
<Status>Success</Status>
<RedirectUrl>url</RedirectUrl>
</Q2Bridge>
"""
[docs]
@classmethod
def get_success(
cls,
client_id: str,
state: str,
authorize_url: str,
redirect_uri: str,
):
resp = cls(cls._get_standard_auth_success_fields())
params = {
"response_type": "code",
"client_id": client_id,
"state": state,
"redirect_uri": redirect_uri,
}
# if audience:
# params["audience"] = audience
params = urllib.parse.urlencode(params)
resp.add_response_field("redirect_url", f"{authorize_url}?{params}")
return resp