async def async_step_oauth(self, user_input=None):
     request = current_request.get()
     url_host = yarl.URL(request.url).host
     """Handle flow external step."""
     ais_dom = ais_cloud.AisCloudWS(self.hass)
     json_ws_resp = ais_dom.key("google_calendar_web_client_id")
     client_id = json_ws_resp["key"]
     gate_id = ais_global.get_sercure_android_id_dom()
     auth_url = (
         "https://accounts.google.com/o/oauth2/auth/oauthchooseaccount?client_id="
         + client_id + "&redirect_uri=https://" + ais_global.AIS_HOST +
         "/ords/dom/auth/google_calendar_callback" +
         "&response_type=code&scope=https://www.googleapis.com/auth/calendar"
         + "&access_type=offline" + "&state=" + gate_id + "ais0dom" +
         url_host + "ais0domgoogle-calendar-" + self.flow_id)
     return self.async_external_step(step_id="obtain_token", url=auth_url)
    async def async_step_oauth(self, user_input=None):

        if user_input is not None:
            self.hass.http.register_view(AuthorizationCallbackView)
            request = current_request.get()
            url_host = yarl.URL(request.url).host

            """Handle flow external step."""
            ais_dom = ais_cloud.AisCloudWS(self.hass)
            json_ws_resp = ais_dom.key("supla_mqtt_prod_client_id")
            self.client_id = json_ws_resp["key"]
            gate_id = ais_global.get_sercure_android_id_dom()
            redirect_uri = REDIRECT_URL.replace("AIS_HOST", ais_global.AIS_HOST)
            auth_url = (
                f"{OAUTH_URL}?client_id={self.client_id}&redirect_uri={redirect_uri}&scope={AUTH_SCOPE}&response_type"
                f"=code&state={gate_id}ais0dom{url_host}ais0domsupla-mqtt-{self.flow_id}"
            )
            return self.async_external_step(step_id="obtain_token", url=auth_url)
        return self.async_show_form(step_id="oauth")
Exemple #3
0
def get_url(
    hass: HomeAssistant,
    *,
    require_current_request: bool = False,
    require_ssl: bool = False,
    require_standard_port: bool = False,
    allow_internal: bool = True,
    allow_external: bool = True,
    allow_cloud: bool = True,
    allow_ip: bool = True,
    prefer_external: bool = False,
    prefer_cloud: bool = False,
) -> str:
    """Get a URL to this instance."""
    if require_current_request and current_request.get() is None:
        raise NoURLAvailableError

    order = [TYPE_URL_INTERNAL, TYPE_URL_EXTERNAL]
    if prefer_external:
        order.reverse()

    # Try finding an URL in the order specified
    for url_type in order:

        if allow_internal and url_type == TYPE_URL_INTERNAL:
            try:
                return _get_internal_url(
                    hass,
                    allow_ip=allow_ip,
                    require_current_request=require_current_request,
                    require_ssl=require_ssl,
                    require_standard_port=require_standard_port,
                )
            except NoURLAvailableError:
                pass

        if allow_external and url_type == TYPE_URL_EXTERNAL:
            try:
                return _get_external_url(
                    hass,
                    allow_cloud=allow_cloud,
                    allow_ip=allow_ip,
                    prefer_cloud=prefer_cloud,
                    require_current_request=require_current_request,
                    require_ssl=require_ssl,
                    require_standard_port=require_standard_port,
                )
            except NoURLAvailableError:
                pass

    # For current request, we accept loopback interfaces (e.g., 127.0.0.1),
    # the Supervisor hostname and localhost transparently
    request_host = _get_request_host()
    if (
        require_current_request
        and request_host is not None
        and hass.config.api is not None
    ):
        scheme = "https" if hass.config.api.use_ssl else "http"
        current_url = yarl.URL.build(
            scheme=scheme, host=request_host, port=hass.config.api.port
        )

        known_hostnames = ["localhost"]
        if hass.components.hassio.is_hassio():
            host_info = hass.components.hassio.get_host_info()
            known_hostnames.extend(
                [host_info["hostname"], f"{host_info['hostname']}.local"]
            )

        if (
            (
                (
                    allow_ip
                    and is_ip_address(request_host)
                    and is_loopback(ip_address(request_host))
                )
                or request_host in known_hostnames
            )
            and (not require_ssl or current_url.scheme == "https")
            and (not require_standard_port or current_url.is_default_port())
        ):
            return normalize_url(str(current_url))

    # We have to be honest now, we have no viable option available
    raise NoURLAvailableError
Exemple #4
0
def _get_request_host() -> Optional[str]:
    """Get the host address of the current request."""
    request = current_request.get()
    if request is None:
        raise NoURLAvailableError
    return yarl.URL(request.url).host