# This file is auto-generated by oagen. Do not edit.

from __future__ import annotations

from dataclasses import dataclass
from typing import Any, Literal, cast

from workos._types import _raise_deserialize_error

from .profile import Profile
from .sso_token_response_oauth_token import SSOTokenResponseOAuthToken


@dataclass(slots=True)
class SSOTokenResponse:
    """SSO Token Response model."""

    token_type: Literal["Bearer"]
    """The type of token issued."""
    access_token: str
    """An access token that can be exchanged for a user profile. Access tokens are short-lived — see the `expires_in` field for the exact lifetime."""
    expires_in: int
    """The lifetime of the access token in seconds."""
    profile: Profile
    """The user profile returned by the identity provider."""
    oauth_tokens: SSOTokenResponseOAuthToken | None = None
    """OAuth tokens issued by the identity provider, if available."""

    @classmethod
    def from_dict(cls, data: dict[str, Any]) -> SSOTokenResponse:
        """Deserialize from a dictionary."""
        try:
            return cls(
                token_type=data.get("token_type", "Bearer"),
                access_token=data["access_token"],
                expires_in=data["expires_in"],
                profile=Profile.from_dict(cast(dict[str, Any], data["profile"])),
                oauth_tokens=SSOTokenResponseOAuthToken.from_dict(
                    cast(dict[str, Any], _v_oauth_tokens)
                )
                if (_v_oauth_tokens := data.get("oauth_tokens")) is not None
                else None,
            )
        except (KeyError, ValueError) as e:
            _raise_deserialize_error("SSOTokenResponse", e)

    def to_dict(self) -> dict[str, Any]:
        """Serialize to a dictionary."""
        result: dict[str, Any] = {}
        result["token_type"] = self.token_type
        result["access_token"] = self.access_token
        result["expires_in"] = self.expires_in
        result["profile"] = self.profile.to_dict()
        if self.oauth_tokens is not None:
            result["oauth_tokens"] = self.oauth_tokens.to_dict()
        return result
