async def create_credential_from_oauth2_code(
        self,
        db: AsyncSession,
        source_short_name: str,
        code: str,
        credential_name: Optional[str] = None,
        credential_description: Optional[str] = None,
        client_id: Optional[str] = None,
        client_secret: Optional[str] = None,
        current_user: schemas.User = None,
    ) -> schemas.IntegrationCredentialInDB:
        """Exchange OAuth2 code for token and create integration credentials.

        This method:
        1. Exchanges the authorization code for a token
        2. Validates the token against the auth config class
        3. Creates and stores the integration credential
        4. Returns the stored credential

        Args:
            db: The database session
            source_short_name: The short name of the source
            code: The authorization code to exchange
            credential_name: Optional custom name for the credential
            credential_description: Optional description for the credential
            client_id: Optional client ID to override the default
            client_secret: Optional client secret to override the default
            current_user: The current user

        Returns:
            The created integration credential

        Raises:
            HTTPException: If code exchange fails or validation fails
        """
        try:
            # Get the source information first
            source = await crud.source.get_by_short_name(db, short_name=source_short_name)
            if not source:
                raise HTTPException(
                    status_code=404, detail=f"Source not found: {source_short_name}"
                )

            # Check if auth type is OAuth2
            if not source.auth_type or not source.auth_type.startswith("oauth2"):
                raise HTTPException(
                    status_code=400,
                    detail=f"Source {source_short_name} does not support OAuth2 authentication",
                )

            # Exchange the authorization code for a token
            token_response = await self._exchange_authorization_code_for_token(
                source_short_name=source_short_name,
                code=code,
                client_id=client_id,
                client_secret=client_secret,
            )

            # Convert token response to auth fields
            auth_fields = token_response.model_dump()

            # Add client_id and client_secret to auth_fields if they were provided
            if client_id:
                auth_fields["client_id"] = client_id
            if client_secret:
                auth_fields["client_secret"] = client_secret

            # Validate the auth fields against the auth config class (critical step!)
            validated_auth_fields = await self._validate_auth_fields(
                db=db, source_short_name=source_short_name, auth_fields=auth_fields
            )

            # Create the integration credential
            async with UnitOfWork(db) as uow:
                # Encrypt the validated auth fields
                encrypted_credentials = credentials.encrypt(validated_auth_fields)

                # Default name and description if not provided
                name = credential_name or f"{source.name} OAuth2 Credential"
                description = credential_description or f"OAuth2 credentials for {source.name}"

                integration_cred_in = schemas.IntegrationCredentialCreateEncrypted(
                    name=name,
                    description=description,
                    integration_short_name=source_short_name,
                    integration_type=IntegrationType.SOURCE,
                    auth_type=source.auth_type,
                    encrypted_credentials=encrypted_credentials,
                    auth_config_class=source.auth_config_class,
                )

                integration_credential = await crud.integration_credential.create(
                    uow.session, obj_in=integration_cred_in, current_user=current_user, uow=uow
                )

                await uow.commit()
                await uow.session.refresh(integration_credential)

                # Get the schema model from the database object and return
                return schemas.IntegrationCredentialInDB.model_validate(
                    integration_credential, from_attributes=True
                )

        except Exception as e:
            source_connection_logger.error(f"Failed to create credential from OAuth2 code: {e}")
            if isinstance(e, HTTPException):
                raise
            raise HTTPException(
                status_code=400, detail=f"Failed to create credential from OAuth2 code: {str(e)}"
            ) from e
Exchange OAuth2 code for token and create integration credentials.

        This method:
        1. Exchanges the authorization code for a token
        2. Validates the token against the auth config class
        3. Creates and stores the integration credential
        4. Returns the stored credential

        Args:
            db: The database session
            source_short_name: The short name of the source
            code: The authorization code to exchange
            credential_name: Optional custom name for the credential
            credential_description: Optional description for the credential
            client_id: Optional client ID to override the default
            client_secret: Optional client secret to override the default
            current_user: The current user

        Returns:
            The created integration credential

        Raises:
            HTTPException: If code exchange fails or validation fails
        
backend/airweave/core/source_connection_service.py