@donotdev/functions 0.0.19 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +3 -3
- package/lib/firebase/auth/getCustomClaims.d.ts +25 -0
- package/lib/firebase/auth/getUserAuthStatus.d.ts +29 -0
- package/lib/firebase/auth/index.d.ts +12 -0
- package/lib/firebase/auth/removeCustomClaims.d.ts +28 -0
- package/lib/firebase/auth/setCustomClaims.d.ts +34 -0
- package/lib/firebase/baseFunction.d.ts +26 -0
- package/lib/firebase/billing/cancelSubscription.d.ts +10 -0
- package/lib/firebase/billing/changePlan.d.ts +13 -0
- package/lib/firebase/billing/createCheckoutSession.d.ts +21 -0
- package/lib/firebase/billing/createCustomerPortal.d.ts +19 -0
- package/lib/firebase/billing/healthCheck.d.ts +5 -0
- package/lib/firebase/billing/index.d.ts +18 -0
- package/lib/firebase/billing/refreshSubscriptionStatus.d.ts +19 -0
- package/lib/firebase/billing/webhookHandler.d.ts +6 -0
- package/lib/firebase/config/constants.d.ts +89 -0
- package/lib/firebase/config/index.d.ts +9 -0
- package/lib/firebase/config/secrets.d.ts +15 -0
- package/lib/firebase/crud/aggregate.d.ts +98 -0
- package/lib/firebase/crud/create.d.ts +16 -0
- package/lib/firebase/crud/delete.d.ts +20 -0
- package/lib/firebase/crud/get.d.ts +24 -0
- package/lib/firebase/crud/index.d.ts +14 -0
- package/lib/firebase/crud/list.d.ts +25 -0
- package/lib/firebase/crud/update.d.ts +25 -0
- package/lib/firebase/helpers/githubAccessHelper.d.ts +30 -0
- package/lib/firebase/helpers/githubTeamAccessHelper.d.ts +36 -0
- package/lib/firebase/helpers/index.d.ts +10 -0
- package/lib/firebase/index.d.ts +17 -0
- package/lib/firebase/index.js +25 -9110
- package/lib/firebase/index.js.map +4 -4
- package/lib/firebase/oauth/disconnect.d.ts +12 -0
- package/lib/firebase/oauth/exchangeToken.d.ts +9 -0
- package/lib/firebase/oauth/getConnections.d.ts +11 -0
- package/lib/firebase/oauth/githubAccess.d.ts +83 -0
- package/lib/firebase/oauth/index.d.ts +13 -0
- package/lib/firebase/oauth/refreshToken.d.ts +9 -0
- package/lib/firebase/registerCrudFunctions.d.ts +32 -0
- package/lib/firebase/scheduled/checkExpiredSubscriptions.d.ts +14 -0
- package/lib/firebase/scheduled/index.d.ts +9 -0
- package/lib/shared/billing/createCheckout.d.ts +50 -0
- package/lib/shared/billing/helpers/index.d.ts +10 -0
- package/lib/shared/billing/helpers/subscriptionManagement.d.ts +34 -0
- package/lib/shared/billing/helpers/updateUserSubscription.d.ts +45 -0
- package/lib/shared/billing/idempotency.d.ts +22 -0
- package/lib/shared/billing/index.d.ts +11 -0
- package/lib/shared/billing/webhookHandler.d.ts +37 -0
- package/lib/shared/errorHandling.d.ts +14 -0
- package/lib/shared/firebase.d.ts +73 -0
- package/lib/shared/github.d.ts +128 -0
- package/lib/shared/index.d.ts +16 -0
- package/lib/shared/index.js +25 -6927
- package/lib/shared/index.js.map +4 -4
- package/lib/shared/logger.d.ts +133 -0
- package/lib/shared/metadata.d.ts +36 -0
- package/lib/shared/oauth/exchangeToken.d.ts +24 -0
- package/lib/shared/oauth/grantAccess.d.ts +30 -0
- package/lib/shared/oauth/index.d.ts +10 -0
- package/lib/shared/schema.d.ts +56 -0
- package/lib/shared/utils/detectFirestore.d.ts +31 -0
- package/lib/shared/utils/external/date.d.ts +36 -0
- package/lib/shared/utils/external/index.d.ts +11 -0
- package/lib/shared/utils/external/references.d.ts +12 -0
- package/lib/shared/utils/external/subscription.d.ts +41 -0
- package/lib/shared/utils/firebaseHelpers.d.ts +23 -0
- package/lib/shared/utils/functionWrapper.d.ts +36 -0
- package/lib/shared/utils/index.d.ts +14 -0
- package/lib/shared/utils/internal/auth.d.ts +57 -0
- package/lib/shared/utils/internal/firebase.d.ts +5 -0
- package/lib/shared/utils/internal/idempotency.d.ts +21 -0
- package/lib/shared/utils/internal/index.d.ts +14 -0
- package/lib/shared/utils/internal/monitoring.d.ts +80 -0
- package/lib/shared/utils/internal/rateLimiter.d.ts +82 -0
- package/lib/shared/utils/internal/validation.d.ts +43 -0
- package/lib/shared/utils/schemaValidation.d.ts +60 -0
- package/lib/shared/utils/validation.d.ts +98 -0
- package/lib/shared/utils/vercelHelpers.d.ts +18 -0
- package/lib/shared/utils.d.ts +234 -0
- package/lib/shared/validation.d.ts +16 -0
- package/lib/supabase/auth/deleteAccount.d.ts +16 -0
- package/lib/supabase/auth/getCustomClaims.d.ts +16 -0
- package/lib/supabase/auth/getUserAuthStatus.d.ts +18 -0
- package/lib/supabase/auth/removeCustomClaims.d.ts +16 -0
- package/lib/supabase/auth/setCustomClaims.d.ts +18 -0
- package/lib/supabase/baseFunction.d.ts +43 -0
- package/lib/supabase/billing/cancelSubscription.d.ts +9 -0
- package/lib/supabase/billing/changePlan.d.ts +11 -0
- package/lib/supabase/billing/createCheckoutSession.d.ts +11 -0
- package/lib/supabase/billing/createCustomerPortal.d.ts +9 -0
- package/lib/supabase/billing/refreshSubscriptionStatus.d.ts +9 -0
- package/lib/supabase/crud/aggregate.d.ts +27 -0
- package/lib/supabase/crud/create.d.ts +23 -0
- package/lib/supabase/crud/delete.d.ts +30 -0
- package/lib/supabase/crud/get.d.ts +23 -0
- package/lib/supabase/crud/index.d.ts +20 -0
- package/lib/supabase/crud/list.d.ts +33 -0
- package/lib/supabase/crud/update.d.ts +24 -0
- package/lib/supabase/helpers/authProvider.d.ts +22 -0
- package/lib/supabase/index.d.ts +30 -0
- package/lib/supabase/index.js +23 -0
- package/lib/supabase/index.js.map +7 -0
- package/lib/supabase/oauth/disconnect.d.ts +13 -0
- package/lib/supabase/oauth/exchangeToken.d.ts +13 -0
- package/lib/supabase/oauth/getConnections.d.ts +13 -0
- package/lib/supabase/oauth/index.d.ts +12 -0
- package/lib/supabase/oauth/refreshToken.d.ts +13 -0
- package/lib/supabase/registerCrudFunctions.d.ts +24 -0
- package/lib/supabase/utils/idempotency.d.ts +36 -0
- package/lib/supabase/utils/monitoring.d.ts +59 -0
- package/lib/supabase/utils/rateLimiter.d.ts +56 -0
- package/lib/vercel/api/auth/get-custom-claims.d.ts +2 -0
- package/lib/vercel/api/auth/get-user-auth-status.d.ts +2 -0
- package/lib/vercel/api/auth/index.d.ts +12 -0
- package/lib/vercel/api/auth/remove-custom-claims.d.ts +2 -0
- package/lib/vercel/api/auth/set-custom-claims.d.ts +10 -0
- package/lib/vercel/api/billing/cancel.d.ts +10 -0
- package/lib/vercel/api/billing/change-plan.d.ts +11 -0
- package/lib/vercel/api/billing/create-checkout-session.d.ts +13 -0
- package/lib/vercel/api/billing/customer-portal.d.ts +3 -0
- package/lib/vercel/api/billing/index.d.ts +14 -0
- package/lib/vercel/api/billing/refresh-subscription-status.d.ts +31 -0
- package/lib/vercel/api/billing/webhook-handler.d.ts +22 -0
- package/lib/vercel/api/config/constants.d.ts +103 -0
- package/lib/vercel/api/config/firebase-admin.d.ts +19 -0
- package/lib/vercel/api/config/index.d.ts +10 -0
- package/lib/vercel/api/crud/create.d.ts +2 -0
- package/lib/vercel/api/crud/delete.d.ts +2 -0
- package/lib/vercel/api/crud/get.d.ts +2 -0
- package/lib/vercel/api/crud/index.d.ts +13 -0
- package/lib/vercel/api/crud/list.d.ts +2 -0
- package/lib/vercel/api/crud/update.d.ts +2 -0
- package/lib/vercel/api/index.d.ts +13 -0
- package/lib/vercel/api/index.js +22 -4790
- package/lib/vercel/api/index.js.map +4 -4
- package/lib/vercel/api/oauth/check-github-access.d.ts +12 -0
- package/lib/vercel/api/oauth/exchange-token.d.ts +8 -0
- package/lib/vercel/api/oauth/grant-github-access.d.ts +12 -0
- package/lib/vercel/api/oauth/index.d.ts +12 -0
- package/lib/vercel/api/oauth/revoke-github-access.d.ts +11 -0
- package/lib/vercel/api/utils/cors.d.ts +49 -0
- package/lib/vercel/baseFunction.d.ts +25 -0
- package/package.json +4 -4
- package/src/firebase/auth/getCustomClaims.ts +3 -3
- package/src/firebase/auth/getUserAuthStatus.ts +3 -3
- package/src/firebase/auth/index.ts +1 -1
- package/src/firebase/auth/removeCustomClaims.ts +3 -3
- package/src/firebase/auth/setCustomClaims.ts +130 -84
- package/src/firebase/baseFunction.ts +2 -2
- package/src/firebase/billing/cancelSubscription.ts +3 -3
- package/src/firebase/billing/changePlan.ts +3 -3
- package/src/firebase/billing/createCheckoutSession.ts +1 -1
- package/src/firebase/billing/createCustomerPortal.ts +1 -1
- package/src/firebase/billing/healthCheck.ts +8 -24
- package/src/firebase/billing/index.d.ts +11 -11
- package/src/firebase/billing/index.ts +1 -1
- package/src/firebase/billing/refreshSubscriptionStatus.ts +3 -3
- package/src/firebase/billing/webhookHandler.ts +1 -2
- package/src/firebase/config/constants.ts +4 -4
- package/src/firebase/config/index.ts +1 -1
- package/src/firebase/config/secrets.ts +1 -1
- package/src/firebase/crud/aggregate.ts +2 -2
- package/src/firebase/crud/create.ts +19 -4
- package/src/firebase/crud/delete.ts +3 -3
- package/src/firebase/crud/get.ts +1 -1
- package/src/firebase/crud/index.ts +1 -1
- package/src/firebase/crud/list.ts +1 -1
- package/src/firebase/crud/update.ts +2 -2
- package/src/firebase/helpers/githubAccessHelper.ts +4 -4
- package/src/firebase/helpers/githubTeamAccessHelper.ts +3 -3
- package/src/firebase/helpers/index.ts +1 -1
- package/src/firebase/index.d.ts +9 -9
- package/src/firebase/index.ts +1 -1
- package/src/firebase/oauth/disconnect.ts +2 -2
- package/src/firebase/oauth/exchangeToken.ts +7 -7
- package/src/firebase/oauth/getConnections.ts +2 -2
- package/src/firebase/oauth/githubAccess.ts +10 -10
- package/src/firebase/oauth/index.ts +1 -1
- package/src/firebase/oauth/refreshToken.ts +2 -2
- package/src/firebase/registerCrudFunctions.ts +1 -1
- package/src/firebase/scheduled/checkExpiredSubscriptions.ts +1 -1
- package/src/firebase/scheduled/index.ts +1 -1
- package/src/shared/__tests__/auth.test.ts +46 -0
- package/src/shared/__tests__/errorHandling.test.ts +66 -1
- package/src/shared/__tests__/firebase.test.ts +293 -0
- package/src/shared/__tests__/internalValidation.test.ts +190 -0
- package/src/shared/__tests__/metadata.test.ts +66 -0
- package/src/shared/__tests__/schema.test.ts +219 -0
- package/src/shared/__tests__/schemaValidation.test.ts +72 -0
- package/src/shared/__tests__/subscription.test.ts +45 -0
- package/src/shared/__tests__/utils.test.ts +281 -0
- package/src/shared/__tests__/validation-env.test.ts +73 -0
- package/src/shared/billing/__tests__/createCheckout.test.ts +1 -1
- package/src/shared/billing/__tests__/webhookHandler.test.ts +1 -1
- package/src/shared/billing/createCheckout.ts +10 -5
- package/src/shared/billing/helpers/index.ts +1 -1
- package/src/shared/billing/helpers/subscriptionManagement.ts +4 -4
- package/src/shared/billing/helpers/updateUserSubscription.ts +4 -4
- package/src/shared/billing/idempotency.ts +4 -4
- package/src/shared/billing/index.ts +1 -1
- package/src/shared/billing/webhookHandler.ts +7 -7
- package/src/shared/errorHandling.ts +2 -2
- package/src/shared/firebase.ts +7 -7
- package/src/shared/github.ts +11 -11
- package/src/shared/index.d.ts +9 -9
- package/src/shared/index.ts +1 -1
- package/src/shared/logger.ts +8 -8
- package/src/shared/metadata.ts +3 -3
- package/src/shared/oauth/__tests__/exchangeToken.test.ts +1 -1
- package/src/shared/oauth/exchangeToken.ts +2 -2
- package/src/shared/oauth/grantAccess.ts +2 -2
- package/src/shared/oauth/index.ts +1 -1
- package/src/shared/schema.ts +4 -4
- package/src/shared/utils/__tests__/functionWrapper.test.ts +1 -1
- package/src/shared/utils/detectFirestore.ts +3 -3
- package/src/shared/utils/external/date.ts +5 -5
- package/src/shared/utils/external/index.ts +1 -1
- package/src/shared/utils/external/references.ts +2 -2
- package/src/shared/utils/external/subscription.ts +5 -5
- package/src/shared/utils/firebaseHelpers.ts +3 -3
- package/src/shared/utils/functionWrapper.ts +4 -4
- package/src/shared/utils/index.ts +1 -1
- package/src/shared/utils/internal/auth.ts +7 -7
- package/src/shared/utils/internal/firebase.ts +1 -1
- package/src/shared/utils/internal/idempotency.ts +3 -3
- package/src/shared/utils/internal/index.ts +1 -1
- package/src/shared/utils/internal/monitoring.ts +8 -8
- package/src/shared/utils/internal/rateLimiter.ts +6 -6
- package/src/shared/utils/internal/validation.ts +6 -6
- package/src/shared/utils/schemaValidation.ts +6 -6
- package/src/shared/utils/validation.ts +10 -10
- package/src/shared/utils/vercelHelpers.ts +3 -3
- package/src/shared/utils.ts +19 -19
- package/src/shared/validation.ts +2 -2
- package/src/supabase/auth/deleteAccount.ts +2 -2
- package/src/supabase/auth/getCustomClaims.ts +2 -2
- package/src/supabase/auth/getUserAuthStatus.ts +2 -2
- package/src/supabase/auth/removeCustomClaims.ts +2 -2
- package/src/supabase/auth/setCustomClaims.ts +2 -2
- package/src/supabase/baseFunction.ts +3 -3
- package/src/supabase/billing/cancelSubscription.ts +2 -2
- package/src/supabase/billing/changePlan.ts +2 -2
- package/src/supabase/billing/createCheckoutSession.ts +2 -2
- package/src/supabase/billing/createCustomerPortal.ts +2 -2
- package/src/supabase/billing/refreshSubscriptionStatus.ts +2 -2
- package/src/supabase/crud/aggregate.ts +11 -8
- package/src/supabase/crud/create.ts +2 -2
- package/src/supabase/crud/delete.ts +1 -1
- package/src/supabase/crud/get.ts +2 -2
- package/src/supabase/crud/index.ts +1 -1
- package/src/supabase/crud/list.ts +2 -2
- package/src/supabase/crud/update.ts +2 -2
- package/src/supabase/helpers/authProvider.ts +2 -2
- package/src/supabase/index.ts +10 -2
- package/src/supabase/oauth/disconnect.ts +104 -0
- package/src/supabase/oauth/exchangeToken.ts +152 -0
- package/src/supabase/oauth/getConnections.ts +85 -0
- package/src/supabase/oauth/index.ts +15 -0
- package/src/supabase/oauth/refreshToken.ts +131 -0
- package/src/supabase/registerCrudFunctions.ts +1 -1
- package/src/supabase/utils/idempotency.ts +1 -1
- package/src/supabase/utils/monitoring.ts +1 -1
- package/src/supabase/utils/rateLimiter.ts +1 -1
- package/src/vercel/api/auth/get-custom-claims.ts +1 -1
- package/src/vercel/api/auth/get-user-auth-status.ts +1 -1
- package/src/vercel/api/auth/index.ts +1 -1
- package/src/vercel/api/auth/remove-custom-claims.ts +1 -1
- package/src/vercel/api/auth/set-custom-claims.ts +69 -24
- package/src/vercel/api/billing/cancel.ts +3 -3
- package/src/vercel/api/billing/change-plan.ts +3 -3
- package/src/vercel/api/billing/create-checkout-session.ts +3 -3
- package/src/vercel/api/billing/customer-portal.ts +1 -1
- package/src/vercel/api/billing/index.ts +1 -1
- package/src/vercel/api/billing/refresh-subscription-status.ts +1 -1
- package/src/vercel/api/billing/webhook-handler.ts +3 -3
- package/src/vercel/api/config/constants.ts +9 -9
- package/src/vercel/api/config/firebase-admin.ts +3 -3
- package/src/vercel/api/config/index.ts +1 -1
- package/src/vercel/api/crud/create.ts +1 -1
- package/src/vercel/api/crud/delete.ts +1 -1
- package/src/vercel/api/crud/get.ts +1 -1
- package/src/vercel/api/crud/index.ts +1 -1
- package/src/vercel/api/crud/list.ts +1 -1
- package/src/vercel/api/crud/update.ts +1 -1
- package/src/vercel/api/index.d.ts +1 -1
- package/src/vercel/api/index.ts +1 -1
- package/src/vercel/api/oauth/check-github-access.ts +3 -3
- package/src/vercel/api/oauth/exchange-token.ts +1 -1
- package/src/vercel/api/oauth/grant-github-access.ts +3 -3
- package/src/vercel/api/oauth/index.ts +1 -1
- package/src/vercel/api/oauth/revoke-github-access.ts +1 -1
- package/src/vercel/api/utils/cors.ts +4 -4
- package/src/vercel/baseFunction.ts +2 -2
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
// packages/functions/src/supabase/oauth/refreshToken.ts
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @fileoverview OAuth token refresh Supabase Edge Function
|
|
5
|
+
* @description Supabase Edge Function for refreshing OAuth tokens
|
|
6
|
+
*
|
|
7
|
+
* @version 0.1.0
|
|
8
|
+
* @since 0.1.0
|
|
9
|
+
* @author AMBROISE PARK Consulting
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import * as v from 'valibot';
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
OAUTH_PARTNERS,
|
|
16
|
+
type OAuthRefreshRequest,
|
|
17
|
+
} from '@donotdev/core/server';
|
|
18
|
+
|
|
19
|
+
import { createSupabaseHandler } from '../baseFunction.js';
|
|
20
|
+
import type { SupabaseHandlerContext } from '../baseFunction.js';
|
|
21
|
+
|
|
22
|
+
/** Cross-runtime env var access (Deno Edge Functions + Node tests) */
|
|
23
|
+
function getEnv(key: string): string | undefined {
|
|
24
|
+
return typeof Deno !== 'undefined'
|
|
25
|
+
? Deno.env.get(key)
|
|
26
|
+
: process.env[key];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
declare const Deno:
|
|
30
|
+
| { env: { get(key: string): string | undefined } }
|
|
31
|
+
| undefined;
|
|
32
|
+
|
|
33
|
+
const refreshTokenSchema = v.object({
|
|
34
|
+
provider: v.string() as v.BaseSchema<
|
|
35
|
+
unknown,
|
|
36
|
+
OAuthRefreshRequest['provider'],
|
|
37
|
+
v.BaseIssue<unknown>
|
|
38
|
+
>,
|
|
39
|
+
refreshToken: v.pipe(
|
|
40
|
+
v.string(),
|
|
41
|
+
v.minLength(1, 'Refresh token is required')
|
|
42
|
+
),
|
|
43
|
+
redirectUri: v.pipe(v.string(), v.url('Valid redirect URI is required')),
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Create an OAuth token refresh Edge Function handler
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* import { createRefreshToken } from '@donotdev/functions/supabase';
|
|
52
|
+
* Deno.serve(createRefreshToken());
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* @version 0.1.0
|
|
56
|
+
* @since 0.1.0
|
|
57
|
+
*/
|
|
58
|
+
export function createRefreshToken() {
|
|
59
|
+
return createSupabaseHandler(
|
|
60
|
+
'refresh-token',
|
|
61
|
+
refreshTokenSchema,
|
|
62
|
+
async (data: OAuthRefreshRequest, ctx: SupabaseHandlerContext) => {
|
|
63
|
+
const { provider, refreshToken: refreshTokenValue, redirectUri } = data;
|
|
64
|
+
|
|
65
|
+
// Get provider endpoints
|
|
66
|
+
const endpoints = OAUTH_PARTNERS[provider];
|
|
67
|
+
if (!endpoints?.endpoints?.tokenUrl) {
|
|
68
|
+
throw new Error(`Provider ${provider} does not support token refresh`);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Get client credentials
|
|
72
|
+
const clientId =
|
|
73
|
+
getEnv(`OAUTH_${provider.toUpperCase()}_CLIENT_ID`);
|
|
74
|
+
const clientSecret =
|
|
75
|
+
getEnv(`OAUTH_${provider.toUpperCase()}_CLIENT_SECRET`);
|
|
76
|
+
|
|
77
|
+
if (!clientId || !clientSecret) {
|
|
78
|
+
throw new Error(`Provider ${provider} is not configured`);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Refresh the token
|
|
82
|
+
const response = await fetch(endpoints.endpoints.tokenUrl, {
|
|
83
|
+
method: 'POST',
|
|
84
|
+
headers: {
|
|
85
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
86
|
+
Accept: 'application/json',
|
|
87
|
+
},
|
|
88
|
+
body: new URLSearchParams({
|
|
89
|
+
grant_type: 'refresh_token',
|
|
90
|
+
client_id: clientId,
|
|
91
|
+
client_secret: clientSecret,
|
|
92
|
+
refresh_token: refreshTokenValue,
|
|
93
|
+
redirect_uri: redirectUri,
|
|
94
|
+
}).toString(),
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
if (!response.ok) {
|
|
98
|
+
const errorText = await response.text();
|
|
99
|
+
console.error(`Token refresh error for ${provider}:`, errorText);
|
|
100
|
+
throw new Error(`Failed to refresh token: ${response.status}`);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const tokenData = (await response.json()) as any;
|
|
104
|
+
|
|
105
|
+
const credentials = {
|
|
106
|
+
accessToken: tokenData.access_token,
|
|
107
|
+
refreshToken: tokenData.refresh_token || refreshTokenValue,
|
|
108
|
+
expiresAt: tokenData.expires_in
|
|
109
|
+
? Math.floor(Date.now() / 1000) + tokenData.expires_in
|
|
110
|
+
: null,
|
|
111
|
+
scope: tokenData.scope ? tokenData.scope.split(' ') : [],
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
// Update connection in Supabase
|
|
115
|
+
const { error: updateError } = await ctx.supabaseAdmin
|
|
116
|
+
.from('oauth_connections')
|
|
117
|
+
.update({
|
|
118
|
+
credentials,
|
|
119
|
+
updated_at: new Date().toISOString(),
|
|
120
|
+
})
|
|
121
|
+
.eq('user_id', ctx.uid)
|
|
122
|
+
.eq('provider', provider);
|
|
123
|
+
|
|
124
|
+
if (updateError) {
|
|
125
|
+
console.error('Error updating OAuth connection:', updateError);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return { success: true, credentials };
|
|
129
|
+
}
|
|
130
|
+
);
|
|
131
|
+
}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @fileoverview Auto-register CRUD handlers from entities for Supabase Edge Functions
|
|
5
5
|
* @description Utility to automatically generate CRUD Edge Function handlers for all entities
|
|
6
6
|
*
|
|
7
|
-
* @version 0.0
|
|
7
|
+
* @version 0.1.0
|
|
8
8
|
* @since 0.5.0
|
|
9
9
|
* @author AMBROISE PARK Consulting
|
|
10
10
|
*/
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @fileoverview Monitoring and metrics utilities for Supabase Edge Functions
|
|
5
5
|
* @description Provides metrics collection and analytics queries using Postgres table
|
|
6
6
|
*
|
|
7
|
-
* @version 0.0
|
|
7
|
+
* @version 0.1.0
|
|
8
8
|
* @since 0.5.0
|
|
9
9
|
* @author AMBROISE PARK Consulting
|
|
10
10
|
*/
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
* On RPC error or unexpected failure, this function FAILS CLOSED (blocks the request).
|
|
19
19
|
* A broken rate limiter must not silently allow unlimited access to protected resources.
|
|
20
20
|
*
|
|
21
|
-
* @version 0.0
|
|
21
|
+
* @version 0.1.0
|
|
22
22
|
* @since 0.5.0
|
|
23
23
|
* @author AMBROISE PARK Consulting
|
|
24
24
|
*/
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @fileoverview Set custom claims API handler
|
|
5
5
|
* @description Vercel API route for setting custom user claims
|
|
6
6
|
*
|
|
7
|
-
* @version 0.0
|
|
7
|
+
* @version 0.1.0
|
|
8
8
|
* @since 0.0.1
|
|
9
9
|
* @author AMBROISE PARK Consulting
|
|
10
10
|
*/
|
|
@@ -16,35 +16,80 @@ import { verifyAuthToken } from '../../../shared/utils/internal/auth.js';
|
|
|
16
16
|
|
|
17
17
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
/**
|
|
20
|
+
* Claim keys that must NEVER be self-assignable — privilege escalation vectors.
|
|
21
|
+
*/
|
|
22
|
+
const BLOCKED_CLAIM_KEYS = new Set([
|
|
23
|
+
'admin',
|
|
24
|
+
'isAdmin',
|
|
25
|
+
'isSuper',
|
|
26
|
+
'isSuperAdmin',
|
|
27
|
+
'role',
|
|
28
|
+
'roles',
|
|
29
|
+
'permissions',
|
|
30
|
+
'superuser',
|
|
31
|
+
'moderator',
|
|
32
|
+
'staff',
|
|
33
|
+
'elevated',
|
|
34
|
+
]);
|
|
35
|
+
|
|
36
|
+
export interface SetCustomClaimsHandlerOptions {
|
|
37
|
+
/** Explicit allowlist of claim keys users may set on themselves. Required. */
|
|
38
|
+
allowedClaimKeys: string[];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Factory that returns a set-custom-claims handler scoped to allowed keys.
|
|
43
|
+
* Consumers MUST specify which claim keys are self-assignable.
|
|
44
|
+
*/
|
|
45
|
+
export function createSetCustomClaimsHandler(
|
|
46
|
+
options: SetCustomClaimsHandlerOptions
|
|
22
47
|
) {
|
|
23
|
-
|
|
24
|
-
return res.status(405).json({ error: 'Method not allowed' });
|
|
25
|
-
}
|
|
48
|
+
const allowedKeys = new Set(options.allowedClaimKeys);
|
|
26
49
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
50
|
+
// Fail fast if consumer accidentally allows a privilege key
|
|
51
|
+
for (const key of allowedKeys) {
|
|
52
|
+
if (BLOCKED_CLAIM_KEYS.has(key)) {
|
|
53
|
+
throw new Error(
|
|
54
|
+
`Claim key "${key}" is blocked — it is a privilege-escalation vector and cannot be self-assigned.`
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
33
58
|
|
|
34
|
-
|
|
35
|
-
|
|
59
|
+
return async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
60
|
+
if (req.method !== 'POST') {
|
|
61
|
+
return res.status(405).json({ error: 'Method not allowed' });
|
|
36
62
|
}
|
|
37
63
|
|
|
38
|
-
|
|
39
|
-
|
|
64
|
+
try {
|
|
65
|
+
const uid = await verifyAuthToken(req);
|
|
66
|
+
const { customClaims } = req.body;
|
|
40
67
|
|
|
41
|
-
|
|
42
|
-
|
|
68
|
+
if (!customClaims || typeof customClaims !== 'object') {
|
|
69
|
+
throw handleError(new Error('customClaims must be an object'));
|
|
70
|
+
}
|
|
43
71
|
|
|
44
|
-
|
|
72
|
+
const disallowedKeys = Object.keys(customClaims).filter(
|
|
73
|
+
(k) => !allowedKeys.has(k)
|
|
74
|
+
);
|
|
75
|
+
if (disallowedKeys.length > 0) {
|
|
76
|
+
throw handleError(
|
|
77
|
+
new Error(`Claim keys not allowed: ${disallowedKeys.join(', ')}`)
|
|
78
|
+
);
|
|
79
|
+
}
|
|
45
80
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
81
|
+
const user = await getFirebaseAdminAuth().getUser(uid);
|
|
82
|
+
const currentClaims = user.customClaims || {};
|
|
83
|
+
|
|
84
|
+
const updatedClaims = { ...currentClaims, ...customClaims };
|
|
85
|
+
|
|
86
|
+
await getFirebaseAdminAuth().setCustomUserClaims(uid, updatedClaims);
|
|
87
|
+
|
|
88
|
+
return res
|
|
89
|
+
.status(200)
|
|
90
|
+
.json({ success: true, customClaims: updatedClaims });
|
|
91
|
+
} catch (error) {
|
|
92
|
+
throw handleError(error);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
50
95
|
}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @fileoverview Vercel Cancel Subscription Handler
|
|
5
5
|
* @description Next.js API route wrapper for cancelSubscription
|
|
6
6
|
*
|
|
7
|
-
* @version 0.0
|
|
7
|
+
* @version 0.1.0
|
|
8
8
|
* @since 0.0.1
|
|
9
9
|
* @author AMBROISE PARK Consulting
|
|
10
10
|
*/
|
|
@@ -26,7 +26,7 @@ const cancelSubscriptionSchema = v.object({
|
|
|
26
26
|
* Business logic for canceling subscription
|
|
27
27
|
* Base function handles: validation, auth, rate limiting, monitoring
|
|
28
28
|
*
|
|
29
|
-
* @version 0.0
|
|
29
|
+
* @version 0.1.0
|
|
30
30
|
* @since 0.0.1
|
|
31
31
|
* @author AMBROISE PARK Consulting
|
|
32
32
|
*/
|
|
@@ -60,7 +60,7 @@ async function cancelSubscriptionLogic(
|
|
|
60
60
|
/**
|
|
61
61
|
* Cancel subscription function
|
|
62
62
|
*
|
|
63
|
-
* @version 0.0
|
|
63
|
+
* @version 0.1.0
|
|
64
64
|
* @since 0.0.1
|
|
65
65
|
* @author AMBROISE PARK Consulting
|
|
66
66
|
*/
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @fileoverview Vercel Change Plan Handler
|
|
5
5
|
* @description Next.js API route wrapper for changePlan
|
|
6
6
|
*
|
|
7
|
-
* @version 0.0
|
|
7
|
+
* @version 0.1.0
|
|
8
8
|
* @since 0.0.1
|
|
9
9
|
* @author AMBROISE PARK Consulting
|
|
10
10
|
*/
|
|
@@ -32,7 +32,7 @@ const changePlanSchema = v.object({
|
|
|
32
32
|
* Business logic for changing subscription plan
|
|
33
33
|
* Base function handles: validation, auth, rate limiting, monitoring
|
|
34
34
|
*
|
|
35
|
-
* @version 0.0
|
|
35
|
+
* @version 0.1.0
|
|
36
36
|
* @since 0.0.1
|
|
37
37
|
* @author AMBROISE PARK Consulting
|
|
38
38
|
*/
|
|
@@ -124,7 +124,7 @@ async function changePlanLogic(
|
|
|
124
124
|
/**
|
|
125
125
|
* Change subscription plan function
|
|
126
126
|
*
|
|
127
|
-
* @version 0.0
|
|
127
|
+
* @version 0.1.0
|
|
128
128
|
* @since 0.0.1
|
|
129
129
|
* @author AMBROISE PARK Consulting
|
|
130
130
|
*/
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @fileoverview Create checkout session API handler
|
|
5
5
|
* @description Vercel API route for creating Stripe checkout sessions
|
|
6
6
|
*
|
|
7
|
-
* @version 0.0
|
|
7
|
+
* @version 0.1.0
|
|
8
8
|
* @since 0.0.1
|
|
9
9
|
* @author AMBROISE PARK Consulting
|
|
10
10
|
*/
|
|
@@ -27,7 +27,7 @@ import type * as v from 'valibot';
|
|
|
27
27
|
* Business logic for creating checkout sessions
|
|
28
28
|
* Base function handles: validation, auth, rate limiting, monitoring
|
|
29
29
|
*
|
|
30
|
-
* @version 0.0
|
|
30
|
+
* @version 0.1.0
|
|
31
31
|
* @since 0.0.1
|
|
32
32
|
* @author AMBROISE PARK Consulting
|
|
33
33
|
*/
|
|
@@ -81,7 +81,7 @@ async function createCheckoutSessionLogic(
|
|
|
81
81
|
* Vercel API handler for creating checkout sessions
|
|
82
82
|
* Base function handles all common concerns automatically
|
|
83
83
|
*
|
|
84
|
-
* @version 0.0
|
|
84
|
+
* @version 0.1.0
|
|
85
85
|
* @since 0.0.1
|
|
86
86
|
* @author AMBROISE PARK Consulting
|
|
87
87
|
*/
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @fileoverview Stripe webhook handler API
|
|
5
5
|
* @description Vercel API route for handling Stripe webhook events
|
|
6
6
|
*
|
|
7
|
-
* @version 0.0
|
|
7
|
+
* @version 0.1.0
|
|
8
8
|
* @since 0.0.1
|
|
9
9
|
* @author AMBROISE PARK Consulting
|
|
10
10
|
*/
|
|
@@ -23,7 +23,7 @@ import type { NextApiRequest, NextApiResponse } from 'next';
|
|
|
23
23
|
/**
|
|
24
24
|
* Create Vercel webhook handler from billing config
|
|
25
25
|
*
|
|
26
|
-
* @version 0.0
|
|
26
|
+
* @version 0.1.0
|
|
27
27
|
* @since 0.0.1
|
|
28
28
|
* @author AMBROISE PARK Consulting
|
|
29
29
|
*/
|
|
@@ -107,7 +107,7 @@ async function getRawBody(req: NextApiRequest): Promise<Buffer> {
|
|
|
107
107
|
/**
|
|
108
108
|
* Next.js API route configuration
|
|
109
109
|
*
|
|
110
|
-
* @version 0.0
|
|
110
|
+
* @version 0.1.0
|
|
111
111
|
* @since 0.0.1
|
|
112
112
|
* @author AMBROISE PARK Consulting
|
|
113
113
|
*/
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @fileoverview Firebase Functions configuration constants
|
|
5
5
|
* @description DRY configuration for regions, timeouts, and other function settings
|
|
6
6
|
*
|
|
7
|
-
* @version 0.0
|
|
7
|
+
* @version 0.1.0
|
|
8
8
|
* @since 0.0.1
|
|
9
9
|
* @author AMBROISE PARK Consulting
|
|
10
10
|
*/
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
* Default to eu-west1 for European users (France)
|
|
15
15
|
* Override with FIREBASE_REGION environment variable
|
|
16
16
|
*
|
|
17
|
-
* @version 0.0
|
|
17
|
+
* @version 0.1.0
|
|
18
18
|
* @since 0.0.1
|
|
19
19
|
* @author AMBROISE PARK Consulting
|
|
20
20
|
*/
|
|
@@ -23,7 +23,7 @@ export const FIREBASE_REGION = process.env.FIREBASE_REGION || 'europe-west1';
|
|
|
23
23
|
/**
|
|
24
24
|
* Common function configuration options
|
|
25
25
|
*
|
|
26
|
-
* @version 0.0
|
|
26
|
+
* @version 0.1.0
|
|
27
27
|
* @since 0.0.1
|
|
28
28
|
* @author AMBROISE PARK Consulting
|
|
29
29
|
*/
|
|
@@ -39,7 +39,7 @@ export const FUNCTION_CONFIG = {
|
|
|
39
39
|
/**
|
|
40
40
|
* Stripe-specific configuration
|
|
41
41
|
*
|
|
42
|
-
* @version 0.0
|
|
42
|
+
* @version 0.1.0
|
|
43
43
|
* @since 0.0.1
|
|
44
44
|
* @author AMBROISE PARK Consulting
|
|
45
45
|
*/
|
|
@@ -52,7 +52,7 @@ export const STRIPE_CONFIG = {
|
|
|
52
52
|
/**
|
|
53
53
|
* Auth-specific configuration
|
|
54
54
|
*
|
|
55
|
-
* @version 0.0
|
|
55
|
+
* @version 0.1.0
|
|
56
56
|
* @since 0.0.1
|
|
57
57
|
* @author AMBROISE PARK Consulting
|
|
58
58
|
*/
|
|
@@ -65,7 +65,7 @@ export const AUTH_CONFIG = {
|
|
|
65
65
|
/**
|
|
66
66
|
* Generic CRUD configuration
|
|
67
67
|
*
|
|
68
|
-
* @version 0.0
|
|
68
|
+
* @version 0.1.0
|
|
69
69
|
* @since 0.0.1
|
|
70
70
|
* @author AMBROISE PARK Consulting
|
|
71
71
|
*/
|
|
@@ -78,7 +78,7 @@ export const CRUD_CONFIG = {
|
|
|
78
78
|
/**
|
|
79
79
|
* Available Firebase regions for reference
|
|
80
80
|
*
|
|
81
|
-
* @version 0.0
|
|
81
|
+
* @version 0.1.0
|
|
82
82
|
* @since 0.0.1
|
|
83
83
|
* @author AMBROISE PARK Consulting
|
|
84
84
|
*/
|
|
@@ -105,7 +105,7 @@ export const AVAILABLE_REGIONS = {
|
|
|
105
105
|
/**
|
|
106
106
|
* Environment validation
|
|
107
107
|
*
|
|
108
|
-
* @version 0.0
|
|
108
|
+
* @version 0.1.0
|
|
109
109
|
* @since 0.0.1
|
|
110
110
|
* @author AMBROISE PARK Consulting
|
|
111
111
|
*/
|
|
@@ -127,7 +127,7 @@ export const validateEnvironment = () => {
|
|
|
127
127
|
/**
|
|
128
128
|
* Type for Firebase region keys
|
|
129
129
|
*
|
|
130
|
-
* @version 0.0
|
|
130
|
+
* @version 0.1.0
|
|
131
131
|
* @since 0.0.1
|
|
132
132
|
* @author AMBROISE PARK Consulting
|
|
133
133
|
*/
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @fileoverview Firebase Admin SDK initialization for Vercel functions
|
|
5
5
|
* @description Re-exports provider functions for Vercel-specific access
|
|
6
6
|
*
|
|
7
|
-
* @version 0.0
|
|
7
|
+
* @version 0.1.0
|
|
8
8
|
* @since 0.0.1
|
|
9
9
|
* @author AMBROISE PARK Consulting
|
|
10
10
|
*/
|
|
@@ -17,7 +17,7 @@ import type { App } from 'firebase-admin/app';
|
|
|
17
17
|
* Initialize Firebase Admin SDK for Vercel environment
|
|
18
18
|
* Uses provider which handles service account credentials from environment variables
|
|
19
19
|
*
|
|
20
|
-
* @version 0.0
|
|
20
|
+
* @version 0.1.0
|
|
21
21
|
* @since 0.0.1
|
|
22
22
|
* @author AMBROISE PARK Consulting
|
|
23
23
|
*/
|
|
@@ -29,7 +29,7 @@ export async function initializeFirebaseAdmin(): Promise<App> {
|
|
|
29
29
|
* Get the initialized Firebase Admin app
|
|
30
30
|
* Initializes if not already done
|
|
31
31
|
*
|
|
32
|
-
* @version 0.0
|
|
32
|
+
* @version 0.1.0
|
|
33
33
|
* @since 0.0.1
|
|
34
34
|
* @author AMBROISE PARK Consulting
|
|
35
35
|
*/
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @fileoverview Vercel API Functions
|
|
5
5
|
* @description Barrel exports for Vercel API route handlers. Provides centralized access to all Vercel API function modules.
|
|
6
6
|
*
|
|
7
|
-
* @version 0.0
|
|
7
|
+
* @version 0.1.0
|
|
8
8
|
* @since 0.0.1
|
|
9
9
|
* @author AMBROISE PARK Consulting
|
|
10
10
|
*/
|