Expand source code
class CallbackResponseBuilder:
def __init__(
self,
*,
logger: Logger,
state_utils: OAuthStateUtils,
redirect_uri_page_renderer: RedirectUriPageRenderer,
):
self._logger = logger
self._state_utils = state_utils
self._redirect_uri_page_renderer = redirect_uri_page_renderer
def _build_callback_success_response(
self,
request: Union[BoltRequest, "AsyncBoltRequest"], # type: ignore[name-defined]
installation: Installation,
) -> BoltResponse:
debug_message = f"Handling an OAuth callback success (request: {request.query})"
self._logger.debug(debug_message)
page_content = self._redirect_uri_page_renderer.render_success_page(
app_id=installation.app_id, # type: ignore[arg-type]
team_id=installation.team_id,
is_enterprise_install=installation.is_enterprise_install,
enterprise_url=installation.enterprise_url,
)
return BoltResponse(
status=200,
headers={
"Content-Type": "text/html; charset=utf-8",
"Set-Cookie": self._state_utils.build_set_cookie_for_deletion(),
},
body=page_content,
)
def _build_callback_failure_response(
self,
request: Union[BoltRequest, "AsyncBoltRequest"], # type: ignore[name-defined]
reason: str,
status: int = 500,
error: Optional[Exception] = None,
) -> BoltResponse:
debug_message = "Handling an OAuth callback failure " f"(reason: {reason}, error: {error}, request: {request.query})"
self._logger.debug(debug_message)
# Adding a bit more details to the error code to help installers understand what's happening.
# This modification in the HTML page works only when developers use this built-in failure handler.
detailed_error = build_detailed_error(reason)
return BoltResponse(
status=status,
headers={
"Content-Type": "text/html; charset=utf-8",
"Set-Cookie": self._state_utils.build_set_cookie_for_deletion(),
},
body=self._redirect_uri_page_renderer.render_failure_page(detailed_error),
)