Repository: Significant-Gravitas/Auto-GPT
File: autogpt_platform/autogpt_libs/autogpt_libs/supabase_integration_credentials_store/store.py (Python)

Code before:
            filtered_credentials = [
                c for c in await self.get_all_creds(user_id) if c.id != credentials_id
            ]
            await self._set_user_integration_creds(user_id, filtered_credentials)

    async def store_state_token(
        self, user_id: str, provider: str, scopes: list[str]
    ) -> str:
        token = secrets.token_urlsafe(32)
        expires_at = datetime.now(timezone.utc) + timedelta(minutes=10)

        state = OAuthState(
            token=token,
            provider=provider,
            expires_at=int(expires_at.timestamp()),
            scopes=scopes,
        )

        with self.locked_user_metadata(user_id):
            user_metadata = await self._get_user_metadata(user_id)
            oauth_states = user_metadata.integration_oauth_states
            oauth_states.append(state.model_dump())
            user_metadata.integration_oauth_states = oauth_states

            if not self.prisma.is_connected():
                await self.prisma.connect()
            await self.prisma.user.update(
                where={"id": user_id},
                data={"metadata": Json(user_metadata.model_dump())},
            )

        return token

    async def get_any_valid_scopes_from_state_token(
        self, user_id: str, token: str, provider: str
    ) -> list[str]:
        """
        Get the valid scopes from the OAuth state token. This will return any valid scopes
        from any OAuth state token for the given provider. If no valid scopes are found,
        an empty list is returned. DO NOT RELY ON THIS TOKEN TO AUTHENTICATE A USER, AS IT
        IS TO CHECK IF THE USER HAS GIVEN PERMISSIONS TO THE APPLICATION BEFORE EXCHANGING
        THE CODE FOR TOKENS.
        """
        user_metadata = await self._get_user_metadata(user_id)
        oauth_states = user_metadata.integration_oauth_states

        now = datetime.now(timezone.utc)
        valid_state = next(
            (
                state
                for state in oauth_states

Review comment (suggestion):
We need to find another way, we can't add a wild prisma connection especially in the node executor, supabase connection count limit is very limited.

You can pass `DatabaseManager` (see database.py, execution manager already have this client object where you can easily pass) object and add the method there instead, and let the prisma query executed from there.

Code after:

        state = OAuthState(
            token=token,
            provider=provider,
            expires_at=int(expires_at.timestamp()),
            scopes=scopes,
        )

        with self.locked_user_metadata(user_id):
            user_metadata = self._get_user_metadata(user_id)
            oauth_states = user_metadata.integration_oauth_states
            oauth_states.append(state.model_dump())
            user_metadata.integration_oauth_states = oauth_states

            self.db_manager.update_user_metadata(
                user_id=user_id, metadata=user_metadata
            )

        return token

    def get_any_valid_scopes_from_state_token(
        self, user_id: str, token: str, provider: str
    ) -> list[str]:
        """
        Get the valid scopes from the OAuth state token. This will return any valid scopes
        from any OAuth state token for the given provider. If no valid scopes are found,
        an empty list is returned. DO NOT RELY ON THIS TOKEN TO AUTHENTICATE A USER, AS IT
        IS TO CHECK IF THE USER HAS GIVEN PERMISSIONS TO THE APPLICATION BEFORE EXCHANGING
        THE CODE FOR TOKENS.
        """
        user_metadata = self._get_user_metadata(user_id)
        oauth_states = user_metadata.integration_oauth_states

        now = datetime.now(timezone.utc)
        valid_state = next(
            (
                state
                for state in oauth_states
                if state["token"] == token
                and state["provider"] == provider
                and state["expires_at"] > now.timestamp()
            ),
            None,
        )

        if valid_state:
            return valid_state.get("scopes", [])

        return []

    def verify_state_token(self, user_id: str, token: str, provider: str) -> bool: