-
Notifications
You must be signed in to change notification settings - Fork 0
Add Facebook Login for Business + Commerce Platform integration with multi-tenant support #156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
- Add FacebookConnection Prisma model with encrypted token storage - Implement src/lib/facebook.ts helper library with: - OAuth state encoding/verification (HMAC-signed) - Token exchange (short-lived to long-lived) - Graph API asset discovery - Webhook signature verification - AES-256-GCM token encryption/decryption - Add OAuth routes: - /api/auth/facebook/start (initiate OAuth flow) - /api/auth/facebook/callback (handle authorization) - Add webhook endpoint: - /api/facebook/webhook (GET verification, POST events) - Update .env.example with required Facebook variables - Add comprehensive Facebook integration docs to README - Create database migration for FacebookConnection model - All type checks and builds pass successfully Co-authored-by: syed-reza98 <71028588+syed-reza98@users.noreply.github.com>
|
|
||
| // 2. Get and verify state | ||
| const state = searchParams.get("state"); | ||
| if (!state) { |
Check failure
Code scanning / CodeQL
User-controlled bypass of security check High
action
user-provided value
| const searchParams = request.nextUrl.searchParams; | ||
| const tenantParam = searchParams.get("tenant"); | ||
|
|
||
| if (!tenantParam) { |
Check failure
Code scanning / CodeQL
User-controlled bypass of security check High
action
user-provided value
- Add docs/FACEBOOK_INTEGRATION.md with complete setup guide - Document OAuth flow, webhook configuration, and security features - Include testing procedures and troubleshooting guide - Provide multi-tenant isolation patterns and best practices - Add App Review requirements and maintenance procedures Co-authored-by: syed-reza98 <71028588+syed-reza98@users.noreply.github.com>
- Use timing-safe comparison for OAuth state signatures - Generate random salt per encryption operation (instead of static salt) - Store salt with encrypted data (format: iv:salt:authTag:encrypted) - Throw errors instead of fallback to plain text when ENCRYPTION_KEY missing - Validate Facebook Business ID exists before storing connection - All type checks pass successfully Co-authored-by: syed-reza98 <71028588+syed-reza98@users.noreply.github.com>
| ], | ||
| }); | ||
|
|
||
| console.log("[facebook/callback] Connection stored successfully for tenant:", tenantId); |
Check warning
Code scanning / CodeQL
Log injection Medium
user-provided value
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 4 days ago
In general, to fix log injection you should sanitize any user-controlled value before logging, typically by stripping \r and \n characters (and optionally other control characters), and/or clearly delimiting user input. In this case, the only problematic use is the logging of tenantId, not its use as an identifier for DB operations, so we must avoid changing the value used in business logic and only sanitize what is passed to console.log.
The best targeted fix here is:
- Introduce a local, sanitized variable (e.g.,
safeTenantIdForLog) that is derived fromtenantIdusingString(tenantId).replace(/[\r\n]/g, ""). - Use this sanitized value exclusively in the log statement on line 132.
- Keep
tenantIditself unchanged for all other uses (e.g.,storeFacebookConnection).
This requires only a minimal change in src/app/api/auth/facebook/callback/route.ts, near the existing log call, and no new imports or helper functions are strictly needed.
-
Copy modified lines R132-R136
| @@ -129,7 +129,11 @@ | ||
| ], | ||
| }); | ||
|
|
||
| console.log("[facebook/callback] Connection stored successfully for tenant:", tenantId); | ||
| const safeTenantIdForLog = String(tenantId).replace(/[\r\n]/g, ""); | ||
| console.log( | ||
| "[facebook/callback] Connection stored successfully for tenant:", | ||
| safeTenantIdForLog | ||
| ); | ||
|
|
||
| // 10. Redirect to success page | ||
| const successUrl = new URL("/settings/integrations", request.nextUrl.origin); |
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
|
|
||
| // 1. Check for errors | ||
| const error = searchParams.get("error"); | ||
| if (error) { |
Check failure
Code scanning / CodeQL
User-controlled bypass of security check High
Implements server-side Facebook OAuth and webhook integration for multi-tenant commerce platform, enabling stores to connect Facebook Business accounts, pages, and product catalogs.
Core Implementation
Helper Library (
src/lib/facebook.ts)API Routes
POST /api/auth/facebook/start?tenant={storeId}- Initiates OAuth with signed stateGET /api/auth/facebook/callback- Handles authorization, exchanges tokens, discovers assets, stores encrypted connectionGET|POST /api/facebook/webhook- Webhook verification challenge and event deliveryDatabase Model
Security
storeId, state includes signed tenant IDENCRYPTION_KEY(no plain text fallback)Usage
Environment Variables
Documentation
docs/FACEBOOK_INTEGRATION.md- Complete setup guide, webhook configuration, troubleshooting.env.example- Required variables with generation instructionsOriginal prompt
Summary
The Meta (Facebook) integration included in PR #155 is failing in production. This pull request will add a robust, multi-tenant-friendly implementation of "Facebook Login for Business" + Commerce Platform integration in the StormCom UI repository so tenants can connect their Facebook Business and Commerce assets. The changes include OAuth start/callback routes, webhook endpoint with signature verification, a helper library for Graph API token exchange & asset discovery, Prisma schema to persist tenant connections, and .env example updates. The code is implemented to integrate with the repository's existing Prisma client at src/lib/prisma.ts and Next.js App Router conventions (route handlers). It validates state, exchanges short-lived tokens for long-lived tokens, persists encrypted tokens (placeholder encryption function), and verifies webhook signatures. The PR is targeted at the main branch.
Goals
What I will change (files to add / modify)
Add src/lib/facebook.ts — helper library implementing:
Add app/router route handlers (Next.js 16 App Router):
These routes require the tenantId query param when starting (e.g. /api/auth/facebook/start?tenant=) and will use signed state to persist tenant context.
Modify prisma/schema.prisma — add a FacebookConnection model:
Update .env.example and README.md (or docs/CONTRIBUTING.md) to include:
Add a short note in .github/copilot-instructions.md about the new integration and environment variable requirements.
Implementation details & assumptions
import prisma from '@/lib/prisma'.${process.env.NEXT_PUBLIC_APP_ORIGIN || process.env.NEXTAUTH_URL}/api/auth/facebook/callback— ensure that value is registered in Facebook App settings.npm run prisma:generate && npm run prisma:migrate:devwith env vars sourced.Security / App Review notes
Files to be added / modified in the PR (short list)
Additional notes for reviewers
This pull request was created from Copilot chat.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.