Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Implement token cache expiration for restricted tokens in Stripe webhook handlers

Kiran K740add5a25f6
diff --git a/apps/web/app/(ee)/api/stripe/webhook/checkout-session-completed.ts b/apps/web/app/(ee)/api/stripe/webhook/checkout-session-completed.ts
index 7d641e34cc..7b500023d5 100644
--- a/apps/web/app/(ee)/api/stripe/webhook/checkout-session-completed.ts
+++ b/apps/web/app/(ee)/api/stripe/webhook/checkout-session-completed.ts
@@ -1,5 +1,6 @@
import { claimDotLinkDomain } from "@/lib/api/domains/claim-dot-link-domain";
import { inviteUser } from "@/lib/api/users";
+import { tokenCache } from "@/lib/auth/token-cache";
import { limiter } from "@/lib/cron/limiter";
import { stripe } from "@/lib/stripe";
import { WorkspaceProps } from "@/lib/types";
@@ -86,6 +87,11 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {
},
},
},
+ restrictedTokens: {
+ select: {
+ hashedKey: true,
+ },
+ },
},
});
@@ -130,6 +136,11 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {
dublink: true,
},
}),
+
+ // expire restricted tokens cache
+ tokenCache.expireMany({
+ hashedKeys: workspace.restrictedTokens.map(({ hashedKey }) => hashedKey),
+ }),
]);
}
diff --git a/apps/web/app/(ee)/api/stripe/webhook/customer-subscription-deleted.ts b/apps/web/app/(ee)/api/stripe/webhook/customer-subscription-deleted.ts
index 73e06b7a8a..f6df038fac 100644
--- a/apps/web/app/(ee)/api/stripe/webhook/customer-subscription-deleted.ts
+++ b/apps/web/app/(ee)/api/stripe/webhook/customer-subscription-deleted.ts
@@ -1,4 +1,5 @@
import { deleteWorkspaceFolders } from "@/lib/api/folders/delete-workspace-folders";
+import { tokenCache } from "@/lib/auth/token-cache";
import { recordLink } from "@/lib/tinybird";
import { redis } from "@/lib/upstash";
import { webhookCache } from "@/lib/webhook/cache";
@@ -52,6 +53,11 @@ export async function customerSubscriptionDeleted(event: Stripe.Event) {
},
},
},
+ restrictedTokens: {
+ select: {
+ hashedKey: true,
+ },
+ },
},
});
@@ -177,6 +183,11 @@ export async function customerSubscriptionDeleted(event: Stripe.Event) {
webhookEnabled: false,
},
}),
+
+ // expire restricted tokens cache
+ tokenCache.expireMany({
+ hashedKeys: workspace.restrictedTokens.map(({ hashedKey }) => hashedKey),
+ }),
]);
// Update the webhooks cache
diff --git a/apps/web/app/(ee)/api/stripe/webhook/customer-subscription-updated.ts b/apps/web/app/(ee)/api/stripe/webhook/customer-subscription-updated.ts
index ceffffaaa0..17750517ed 100644
--- a/apps/web/app/(ee)/api/stripe/webhook/customer-subscription-updated.ts
+++ b/apps/web/app/(ee)/api/stripe/webhook/customer-subscription-updated.ts
@@ -1,4 +1,5 @@
import { deleteWorkspaceFolders } from "@/lib/api/folders/delete-workspace-folders";
+import { tokenCache } from "@/lib/auth/token-cache";
import { webhookCache } from "@/lib/webhook/cache";
import { prisma } from "@dub/prisma";
import { getPlanFromPriceId } from "@dub/utils";
@@ -47,6 +48,11 @@ export async function customerSubscriptionUpdated(event: Stripe.Event) {
},
},
},
+ restrictedTokens: {
+ select: {
+ hashedKey: true,
+ },
+ },
},
});
@@ -97,6 +103,13 @@ export async function customerSubscriptionUpdated(event: Stripe.Event) {
rateLimit: plan.limits.api,
},
}),
+
+ // expire restricted tokens cache
+ tokenCache.expireMany({
+ hashedKeys: workspace.restrictedTokens.map(
+ ({ hashedKey }) => hashedKey,
+ ),
+ }),
]);
// Disable the webhooks if the new plan does not support webhooks
diff --git a/apps/web/lib/auth/token-cache.ts b/apps/web/lib/auth/token-cache.ts
index ed7f0f1411..e654a3f249 100644
--- a/apps/web/lib/auth/token-cache.ts
+++ b/apps/web/lib/auth/token-cache.ts
@@ -46,8 +46,19 @@ class TokenCache {
return await redis.del(this._createKey({ hashedKey }));
}
- // TODO:
- // Delete the cache when the plan is updated, workspace is deleted, etc.
+ async expireMany({ hashedKeys }: { hashedKeys: string[] }) {
+ if (hashedKeys.length === 0) {
+ return;
+ }
+
+ const pipeline = redis.pipeline();
+
+ hashedKeys.forEach((hashedKey) => {
+ pipeline.expire(this._createKey({ hashedKey }), 1);
+ });
+
+ return await pipeline.exec();
+ }
_createKey({ hashedKey }: { hashedKey: string }) {
return `${CACHE_KEY_PREFIX}:${hashedKey}`;