from __future__ import annotations
import base64
from dataclasses import dataclass
from q2_sdk.tools.utils import to_bool
from lxml import objectify # type: ignore
from .base import AuthorizationStatus, BaseAuthRequest, BaseAuthResponse
[docs]
@dataclass
class Request(BaseAuthRequest):
"""
Shape that comes in from HQ from a password change request. On the login page,
click the "Forgot Your Password?" link at the bottom.
.. code-block:: xml
<HQ request="LostPassword" messageID="{0}">
<UserId>2</UserId>
<CSRLogin>MyLoginName</CSRLogin>
<UserPrimaryCIF>MDcyMTIwMTA=</UserPrimaryCIF>
<CustomerPrimaryCIF>MDcyMTIwMTA=</CustomerPrimaryCIF>
<IsPrelogonSession>False</IsPrelogonSession>
<SessionId>shb3cnukdl32c54nodvyxcsj</SessionId>
</HQ>
"""
raw: objectify.Element
user_id: int
user_primary_cif: str
customer_primary_cif: str
is_prelogon_session: bool
session_id: str
@staticmethod
def from_xml(xml: objectify.Element) -> Request:
user_id = int(xml.UserId.text)
user_primary_cif = base64.b64decode(xml.UserPrimaryCIF.text).decode()
customer_primary_cif = base64.b64decode(xml.CustomerPrimaryCIF.text).decode()
is_prelogon_session = to_bool(xml.IsPrelogonSession.text)
session_id = xml.SessionId.text
return Request(
xml,
user_id,
user_primary_cif,
customer_primary_cif,
is_prelogon_session,
session_id,
)
[docs]
@dataclass
class Response(BaseAuthResponse):
"""
.. code-block:: xml
<Q2Bridge request="LostPassword" messageID="messageID">
<Status>"Success"/"Error"</Status>
<StatusDescription>{0}</StatusDescription>
<EndUserMessage>{0}</EndUserMessage>
<HQErrorReturnCode>{0}</HQErrorReturnCode>
<LostPasswordFailed>{0}</LostPasswordFailed>
</Q2Bridge>
"""
[docs]
@classmethod
def get_success(cls):
resp = cls(cls._get_standard_auth_success_fields())
resp.add_response_field(
"authorization_status", AuthorizationStatus.PasswordIsGood
)
return resp
[docs]
@classmethod
def get_failure(cls, failure_reason: str):
"""Same as basic get_failure but can provide a reason for failure"""
resp = cls(cls._get_standard_auth_failure_fields())
resp.add_response_field(
"authorization_status", AuthorizationStatus.PasswordIsBad
)
resp.add_response_field("lost_password_failed", failure_reason)
return resp