async def post(self, request, data): """Handle token creation.""" hass = request.app["hass"] refresh_token_id = request[KEY_HASS_REFRESH_TOKEN_ID] async with self._lock: if self._async_is_done(): return self.json_message("Integration step already done", HTTPStatus.FORBIDDEN) await self._async_mark_done(hass) # Validate client ID and redirect uri if not await indieauth.verify_redirect_uri(request.app["hass"], data["client_id"], data["redirect_uri"]): return self.json_message("invalid client id or redirect uri", HTTPStatus.BAD_REQUEST) refresh_token = await hass.auth.async_get_refresh_token( refresh_token_id) if refresh_token is None or refresh_token.credential is None: return self.json_message("Credentials for user not available", HTTPStatus.FORBIDDEN) # Return authorization code so we can redirect user and log them in # pylint: disable=import-outside-toplevel from homeassistant.components.auth import create_auth_code auth_code = create_auth_code(hass, data["client_id"], refresh_token.credential) return self.json({"auth_code": auth_code})
async def post(self, request, data): """Handle user creation, area creation.""" hass = request.app["hass"] async with self._lock: if self._async_is_done(): return self.json_message("User step already done", HTTPStatus.FORBIDDEN) provider = _async_get_hass_provider(hass) await provider.async_initialize() user = await hass.auth.async_create_user( data["name"], group_ids=[GROUP_ID_ADMIN]) await hass.async_add_executor_job(provider.data.add_auth, data["username"], data["password"]) credentials = await provider.async_get_or_create_credentials( {"username": data["username"]}) await provider.data.async_save() await hass.auth.async_link_user(user, credentials) if "person" in hass.config.components: await person.async_create_person(hass, data["name"], user_id=user.id) # Create default areas using the users supplied language. translations = await async_get_translations( hass, data["language"], "area", {DOMAIN}) area_registry = ar.async_get(hass) for area in DEFAULT_AREAS: name = translations[f"component.onboarding.area.{area}"] # Guard because area might have been created by an automatically # set up integration. if not area_registry.async_get_area_by_name(name): area_registry.async_create(name) await self._async_mark_done(hass) # Return authorization code for fetching tokens and connect # during onboarding. # pylint: disable=import-outside-toplevel from homeassistant.components.auth import create_auth_code auth_code = create_auth_code(hass, data["client_id"], credentials) return self.json({"auth_code": auth_code})