Skip to content

The open alternative to Salesforce, designed for AI.

[Website] Add internal enterprise key reissue endpoint. (#21807)

Abdullah.9eb90e72f930
diff --git a/packages/twenty-website-redone/.env.example b/packages/twenty-website-redone/.env.example
index 61d8adc22ad..b3ac1c405d6 100644
--- a/packages/twenty-website-redone/.env.example
+++ b/packages/twenty-website-redone/.env.example
@@ -25,3 +25,7 @@ ENTERPRISE_JWT_PUBLIC_KEY=
# Optional: enterprise validity token lifetime in days (default 30).
# ENTERPRISE_VALIDITY_TOKEN_DURATION_DAYS=
+
+# Shared secret guarding the internal enterprise key reissue endpoint
+# (POST /api/enterprise/reissue), used to regenerate an enterprise key.
+ENTERPRISE_ADMIN_API_SECRET=
diff --git a/packages/twenty-website-redone/src/app/api/enterprise/activate/route.ts b/packages/twenty-website-redone/src/app/api/enterprise/activate/route.ts
index 889ac35ef8d..ef0a58d0694 100644
--- a/packages/twenty-website-redone/src/app/api/enterprise/activate/route.ts
+++ b/packages/twenty-website-redone/src/app/api/enterprise/activate/route.ts
@@ -1,6 +1,10 @@
import { NextResponse } from 'next/server';
-import { getStripeClient, signEnterpriseKey } from '@/platform/enterprise';
+import {
+ getLicenseeFromStripeCustomer,
+ getStripeClient,
+ signEnterpriseKey,
+} from '@/platform/enterprise';
export const dynamic = 'force-dynamic';
@@ -67,11 +71,7 @@ export async function GET(request: Request) {
);
}
- const customer = session.customer;
- const licensee =
- customer && typeof customer !== 'string' && !customer.deleted
- ? (customer.name ?? customer.email ?? 'Unknown')
- : 'Unknown';
+ const licensee = getLicenseeFromStripeCustomer(session.customer);
const enterpriseKey = signEnterpriseKey(subscription.id, licensee);
diff --git a/packages/twenty-website-redone/src/app/api/enterprise/reissue/route.ts b/packages/twenty-website-redone/src/app/api/enterprise/reissue/route.ts
new file mode 100644
index 00000000000..442baecbeef
--- /dev/null
+++ b/packages/twenty-website-redone/src/app/api/enterprise/reissue/route.ts
@@ -0,0 +1,98 @@
+import { createHmac, randomBytes, timingSafeEqual } from 'node:crypto';
+
+import { NextResponse } from 'next/server';
+
+import {
+ getLicenseeFromStripeCustomer,
+ getStripeClient,
+ signEnterpriseKey,
+} from '@/platform/enterprise';
+
+export const dynamic = 'force-dynamic';
+
+function isSecretValid(providedSecret: string): boolean {
+ const expectedSecret = process.env.ENTERPRISE_ADMIN_API_SECRET;
+
+ if (!expectedSecret || !providedSecret) {
+ return false;
+ }
+
+ const comparisonKey = randomBytes(32);
+ const expectedDigest = createHmac('sha256', comparisonKey)
+ .update(expectedSecret)
+ .digest();
+ const providedDigest = createHmac('sha256', comparisonKey)
+ .update(providedSecret)
+ .digest();
+
+ return timingSafeEqual(expectedDigest, providedDigest);
+}
+
+export async function POST(request: Request) {
+ if (
+ !process.env.STRIPE_SECRET_KEY ||
+ !process.env.ENTERPRISE_JWT_PRIVATE_KEY ||
+ !process.env.ENTERPRISE_ADMIN_API_SECRET
+ ) {
+ console.error(
+ '[enterprise-reissue] 503 — STRIPE_SECRET_KEY, ENTERPRISE_JWT_PRIVATE_KEY and/or ENTERPRISE_ADMIN_API_SECRET are not configured',
+ );
+ return NextResponse.json(
+ { error: 'Enterprise key reissue is not configured.' },
+ { status: 503 },
+ );
+ }
+
+ try {
+ let body: { subscriptionId?: unknown; secret?: unknown } = {};
+
+ try {
+ body = await request.json();
+ } catch {
+ body = {};
+ }
+
+ const secret = typeof body.secret === 'string' ? body.secret : '';
+
+ if (!isSecretValid(secret)) {
+ return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
+ }
+
+ const subscriptionId =
+ typeof body.subscriptionId === 'string' ? body.subscriptionId : '';
+
+ if (!subscriptionId) {
+ return NextResponse.json(
+ { error: 'Missing subscriptionId' },
+ { status: 400 },
+ );
+ }
+
+ const stripe = getStripeClient();
+
+ const subscription = await stripe.subscriptions.retrieve(subscriptionId, {
+ expand: ['customer'],
+ });
+
+ const licensee = getLicenseeFromStripeCustomer(subscription.customer);
+ const enterpriseKey = signEnterpriseKey(subscription.id, licensee);
+
+ const response = NextResponse.json({
+ enterpriseKey,
+ licensee,
+ subscriptionId: subscription.id,
+ subscriptionStatus: subscription.status,
+ });
+
+ response.headers.set('Cache-Control', 'no-store');
+
+ return response;
+ } catch (error: unknown) {
+ console.error('Enterprise key reissue failed', error);
+
+ return NextResponse.json(
+ { error: 'Internal server error' },
+ { status: 500 },
+ );
+ }
+}
diff --git a/packages/twenty-website-redone/src/platform/enterprise/get-licensee-from-stripe-customer.test.ts b/packages/twenty-website-redone/src/platform/enterprise/get-licensee-from-stripe-customer.test.ts
new file mode 100644
index 00000000000..ea3c514d6eb
--- /dev/null
+++ b/packages/twenty-website-redone/src/platform/enterprise/get-licensee-from-stripe-customer.test.ts
@@ -0,0 +1,48 @@
+import { getLicenseeFromStripeCustomer } from './get-licensee-from-stripe-customer';
+
+type CustomerArg = Parameters<typeof getLicenseeFromStripeCustomer>[0];
+
+const customer = (fields: Record<string, unknown>): CustomerArg =>
+ fields as unknown as CustomerArg;
+
+describe('getLicenseeFromStripeCustomer', () => {
+ it('should return the customer name when present', () => {
+ expect(
+ getLicenseeFromStripeCustomer(
+ customer({ deleted: false, email: '[email protected]', name: 'Acme Inc' }),
+ ),
+ ).toBe('Acme Inc');
+ });
+
+ it('should fall back to the email when the name is missing', () => {
+ expect(
+ getLicenseeFromStripeCustomer(
+ customer({ deleted: false, email: '[email protected]', name: null }),
+ ),
+ ).toBe('[email protected]');
+ });
+
+ it('should return Unknown when both name and email are missing', () => {
+ expect(
+ getLicenseeFromStripeCustomer(
+ customer({ deleted: false, email: null, name: null }),
+ ),
+ ).toBe('Unknown');
+ });
+
+ it('should return Unknown for a deleted customer', () => {
+ expect(
+ getLicenseeFromStripeCustomer(
+ customer({ deleted: true, name: 'Acme Inc' }),
+ ),
+ ).toBe('Unknown');
+ });
+
+ it('should return Unknown for an unexpanded customer id string', () => {
+ expect(getLicenseeFromStripeCustomer('cus_123')).toBe('Unknown');
+ });
+
+ it('should return Unknown when there is no customer', () => {
+ expect(getLicenseeFromStripeCustomer(null)).toBe('Unknown');
+ });
+});
diff --git a/packages/twenty-website-redone/src/platform/enterprise/get-licensee-from-stripe-customer.ts b/packages/twenty-website-redone/src/platform/enterprise/get-licensee-from-stripe-customer.ts
new file mode 100644
index 00000000000..c1bd59b48f8
--- /dev/null
+++ b/packages/twenty-website-redone/src/platform/enterprise/get-licensee-from-stripe-customer.ts
@@ -0,0 +1,17 @@
+import type Stripe from 'stripe';
+
+type StripeCustomerField =
+ | string
+ | Stripe.Customer
+ | Stripe.DeletedCustomer
+ | null;
+
+export function getLicenseeFromStripeCustomer(
+ customer: StripeCustomerField,
+): string {
+ if (customer && typeof customer !== 'string' && !customer.deleted) {
+ return customer.name ?? customer.email ?? 'Unknown';
+ }
+
+ return 'Unknown';
+}
diff --git a/packages/twenty-website-redone/src/platform/enterprise/index.ts b/packages/twenty-website-redone/src/platform/enterprise/index.ts
index 8f332679482..6f4a13585d3 100644
--- a/packages/twenty-website-redone/src/platform/enterprise/index.ts
+++ b/packages/twenty-website-redone/src/platform/enterprise/index.ts
@@ -1,4 +1,5 @@
export { getEnterprisePriceId } from './enterprise-price-id';
+export { getLicenseeFromStripeCustomer } from './get-licensee-from-stripe-customer';
export { getStripeClient } from './stripe-client';
export { getSubscriptionCurrentPeriodEnd } from './subscription-current-period-end';
export { signEnterpriseKey } from './sign-enterprise-key';