Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(data-warehouse): keep stripe oauth alive after a lost refresh response (#111997)

Daniel RC1b0db9b12ae9
diff --git a/posthog/models/integration/oauth.py b/posthog/models/integration/oauth.py
index e72e8a488b8c..99b9e95c8c42 100644
--- a/posthog/models/integration/oauth.py
+++ b/posthog/models/integration/oauth.py
@@ -1417,10 +1417,14 @@ class OauthIntegration:
)
elif kind == "stripe":
# Stripe Apps OAuth: secret as HTTP Basic username, no client_id/client_secret in body.
+ # Stripe rolls the refresh token on every exchange. If the response is lost after
+ # Stripe committed the roll, the stored token is dead and only a reconnect recovers.
+ # A key derived from the token makes the next attempt replay Stripe's saved response.
return requests.post(
oauth_config.token_url,
auth=HTTPBasicAuth(client_secret, ""),
data={"refresh_token": refresh_token, "grant_type": "refresh_token"},
+ headers={"Idempotency-Key": hashlib.sha256(refresh_token.encode()).hexdigest()},
timeout=10,
)
else:
diff --git a/posthog/models/test/integration/test_oauth.py b/posthog/models/test/integration/test_oauth.py
index d81d336dadbf..5d2dbc6f03a2 100644
--- a/posthog/models/test/integration/test_oauth.py
+++ b/posthog/models/test/integration/test_oauth.py
@@ -1209,6 +1209,28 @@ class TestOauthIntegrationModel(BaseTest):
mock_reload.assert_called_once_with(self.team.id, [integration.id])
+ @patch("posthog.models.integration.oauth.reload_integrations_on_workers")
+ @patch("posthog.models.integration.oauth.requests.post")
+ def test_stripe_refresh_retry_after_lost_response_replays_the_same_request(self, mock_post, mock_reload):
+ rotated = MagicMock(status_code=200)
+ rotated.json.return_value = {"access_token": "REFRESHED", "refresh_token": "ROTATED"}
+ rotated_again = MagicMock(status_code=200)
+ rotated_again.json.return_value = {"access_token": "REFRESHED_AGAIN", "refresh_token": "ROTATED_AGAIN"}
+ mock_post.side_effect = [requests.exceptions.ReadTimeout(), rotated, rotated_again]
+
+ integration = self.create_integration(kind="stripe")
+
+ with self.settings(STRIPE_APP_CLIENT_ID="ca_test_clientid", STRIPE_APP_SECRET_KEY="sk_test_secret"):
+ OauthIntegration(integration).refresh_access_token()
+ OauthIntegration(Integration.objects.get(id=integration.id)).refresh_access_token()
+ OauthIntegration(Integration.objects.get(id=integration.id)).refresh_access_token()
+
+ lost, retried, next_rotation = (call.kwargs["headers"]["Idempotency-Key"] for call in mock_post.call_args_list)
+ assert lost == retried
+ assert next_rotation != retried
+ assert "REFRESH" not in lost
+ assert Integration.objects.get(id=integration.id).sensitive_config["refresh_token"] == "ROTATED_AGAIN"
+
@patch("posthog.models.integration.oauth.requests.post")
def test_stripe_oauth_does_not_persist_is_sandbox(self, mock_post):
mock_post.return_value.status_code = 200
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/source.py
index da2d29c1162c..62e5400f5e26 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/stripe/source.py
@@ -272,7 +272,7 @@ If automatic creation failed with a permissions error, the fix depends on how yo
return {
"401 Client Error: Unauthorized for url: https://api.stripe.com": "Your Stripe credentials do not have permissions to access endpoint. Please check your configuration and permissions in Stripe, then try again.",
"403 Client Error: Forbidden for url: https://api.stripe.com": "Your Stripe credentials do not have permissions to access endpoint. Please check your configuration and permissions in Stripe, then try again.",
- "Expired API Key provided": "Your Stripe API key has expired. Please create a new key and reconnect.",
+ "Expired API Key provided": "Your Stripe credentials have expired. If you connected with OAuth, reconnect your Stripe account. If you use an API key, create a new key and update the source.",
"Invalid API Key provided": None,
# Stripe rejects the request when the restricted key has an IP allowlist that doesn't
# include PostHog's egress IPs. This is a customer-side key configuration that retrying
diff --git a/products/warehouse_sources/backend/tests/api/test_external_data_source.py b/products/warehouse_sources/backend/tests/api/test_external_data_source.py
index fb0d47c45182..64885fc9a509 100644
--- a/products/warehouse_sources/backend/tests/api/test_external_data_source.py
+++ b/products/warehouse_sources/backend/tests/api/test_external_data_source.py
@@ -12864,7 +12864,10 @@ class TestExternalDataSourceSetup(APIBaseTest):
},
)
assert response.status_code == status.HTTP_400_BAD_REQUEST, response.json()
- assert response.json()["message"] == "Your Stripe API key has expired. Please create a new key and reconnect."
+ assert (
+ response.json()["message"]
+ == "Your Stripe credentials have expired. If you connected with OAuth, reconnect your Stripe account. If you use an API key, create a new key and update the source."
+ )
mock_capture_exception.assert_not_called()
assert not ExternalDataSource.objects.filter(team=self.team).exists()