From 63073580449e5112ad867d49093b588f7b07bd8d Mon Sep 17 00:00:00 2001 From: arsalan-arshad Date: Fri, 6 Nov 2026 21:08:17 +0500 Subject: [PATCH] fix(auth): send expiring: 1 on OAuth code grant in server.js --- PROJECT_CONTEXT.md | 15 ++++++++ README.md | 4 +-- docs/BUILD_PLAN.md | 2 +- server.js | 23 ++++++++---- test/server.test.js | 88 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 123 insertions(+), 9 deletions(-) diff --git a/PROJECT_CONTEXT.md b/PROJECT_CONTEXT.md index e165fc5..0d6eccc 100644 --- a/PROJECT_CONTEXT.md +++ b/PROJECT_CONTEXT.md @@ -1164,6 +1164,21 @@ Run the five spikes in a day or two on a dev store; write the answers into secti - 581 unit tests passing across 132 suites (`npm test`). Zero failures. - 592 tests across 133 suites in `npm run test:all` (581 passing, 11 skipped live DB integration tests). +### 2026-11-06: Expiring Offline Access Tokens on OAuth Code Grant (`server.js`) (Branch fix/oauth-expiring-token) +1. **Root Cause Analysis (Partner Dashboard "Deprecated offline tokens" Alert):** + - Shopify Partner Dashboard displayed a "Deprecated offline tokens" warning banner because prior installs and fallback OAuth authorization code grants in `server.js` requested non-expiring offline tokens (`expiring: 0` / omitted). + - While token exchange in `exchange.js` had been updated to send `expiring: 1`, the manual OAuth authorization code callback in `server.js` (`POST /admin/oauth/access_token`) did not send `expiring: 1` and omitted expiry tracking on the `Session` record. +2. **Implementation:** + - Updated `server.js` OAuth authorization code grant exchange body to explicitly include `expiring: 1` per Shopify's migration guide for authorization code grant. + - Parsed `expires_in` (defaulting to 3600 seconds) and computed `expiresDate = new Date(Date.now() + expiresIn * 1000)`. + - Updated the `Session` insert query in `server.js` to persist `expires: expiresDate` with `refreshToken: NULL` and `refreshTokenExpires: NULL` (matching the minimal-secrets-at-rest pattern in `exchange.js`). + - Allowed optional `fetchFn` in `createRequestListener(options)` for deterministic testing. +3. **Automated Test Coverage:** + - Added unit test in `test/server.test.js` verifying `GET /api/auth/callback` sends `expiring: 1` to `https://${shop}/admin/oauth/access_token` and persists the computed `expires` Date in the `Session` table. + - 582 unit tests passing across 132 suites (`npm test`). Zero failures. + - 593 tests across 133 suites in `npm run test:all` (582 passing, 11 skipped live DB integration tests). + + diff --git a/README.md b/README.md index ca33823..6cfb5c4 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ > **Open-Source (MIT)** | Built for the Shopify Merchant & Developer Community | Open for PRs & Discussions > -> ⚙️ **Status: Complete Engine, Admin & Storefront Modules Verified (581/581 Unit Tests Passing across 132 Suites)** | **Live PostgreSQL Integration (11 integration tests run against a live PostgreSQL instance (they skip with a notice when none is reachable))** | **Live Railway Hosting with Postgres** | **Dev Store Spikes Verified (S2, S3, S4, S5 Answered; S1 Answered on Plus Dev Store Only; S6 Answered on Dev Store)** | **Milestones M9a, M9b-1, M9b-2, M9b-3a & M9b-3b Verified Live on Dev Store** +> ⚙️ **Status: Complete Engine, Admin & Storefront Modules Verified (582/582 Unit Tests Passing across 132 Suites)** | **Live PostgreSQL Integration (11 integration tests run against a live PostgreSQL instance (they skip with a notice when none is reachable))** | **Live Railway Hosting with Postgres** | **Dev Store Spikes Verified (S2, S3, S4, S5 Answered; S1 Answered on Plus Dev Store Only; S6 Answered on Dev Store)** | **Milestones M9a, M9b-1, M9b-2, M9b-3a & M9b-3b Verified Live on Dev Store**

A curated list card of generic unbranded wellness products converging into a single coral QR code link node on deep teal background. @@ -28,7 +28,7 @@ **Privacy by design:** Protocols never accept or store health, medical, or diagnostic data. Browsing shoppers have zero personal information collected or stored. Under an explicit owner decision (dated 2026-11-05), the zero-PII rule is relaxed strictly for an explicit, opt-in "Stay in touch" contact submitted by the shopper on a protocol recommendation page (email, optional first name, timestamp, and verifiable consent text). No protocol, order, or product data is ever linked to the contact. Attribution is tracked solely via aggregate monthly counters. A public privacy policy is served at [`/privacy`](file:///Users/macbook/work/shopify/04-practitioner-protocols/server.js) documenting data handling, explicit consent, compliance webhooks, and sub-processors. -**Current build stage:** The pure engine logic, security controls, cryptographic primitives, Liquid renderers, catalog resolver, rate limiters, validation barriers, and PostgreSQL database queries are fully built and covered by 507 automated unit tests across 111 suites using mock database interfaces. Real PostgreSQL query execution, transaction behavior, constraints, row-level locking (`FOR UPDATE`), and migrations are verified against a live PostgreSQL instance via `npm run test:db` (11 integration tests run against a live PostgreSQL instance, skipping with a notice when none is reachable). The app is deployed live on Railway (`practitioner-protocols-production.up.railway.app`) with internal PostgreSQL and automated migration tracking. Spikes S2 (sitemap exclusion, `noindex`, and proxy header), S3 (hidden line property attribution persistence in storefront checkout and `orders/paid` webhook payload), S4 (20 lines in a single cart batch add), S5 (retail vs B2B pricing), and S6 (storefront predictive search and variant hydration contract; Option A chosen) are verified on live dev store `ai-agent-app-h9qwnwd8.myshopify.com`; Spike S1 is verified on the Plus dev store only. In production mode with `DATABASE_URL` configured, the embedded merchant admin is verified live on the dev store (Milestone M9a; `GET /`, `GET /admin`, `GET /app` served via App Bridge ID tokens and HS256 session verification), including the self-enrolment toggle and pending practitioner review table with customer admin deep links (Milestone M9b-1; verified live on dev store); for Milestone M9b-2, verified live on the dev store: enrol round trip (JSON POST through the App Proxy), pending portal state with requested name, admin pending row with customer deep link, merchant approval, approved portal display, form-encoded and `text/plain;application/json` POSTs returning 415, and unknown action returning 400 (the per-customer rate limit of 5/hr, the pending cap of 200, `customers/redact` profile deletion, and `Sec-Fetch-Site` handling are covered by automated unit tests and were not exercised live); for Milestone M9b-3a (protocol authoring and client page hydration), VERIFIED LIVE on the dev store: editor search with real products and CAD prices, protocol save, share-page hydration, Add all with _protocol_token/_protocol_id/_protocol_usage in the cart, checkout to orders/paid (webhook 200, stat row written), edit round trip (qty and note persisted, token unchanged), list card item count, handout page layout; migration adding nullable `productHandle` to `ProtocolLine`, validation and persistence, client-side predictive search (`/search/suggest.json`) and variant hydration (`/products/.js`) in the practitioner editor with `textContent` rendering, client page variant hydration from `/products/.js`, and removal of `createFakeVariantResolver` from the production App Proxy pipeline. Follow-up hotfix loaded protocol lines on portal queries (`json_agg` + `linesCount`) to eliminate edit data loss, removed fake retail total (`$0.00`) from cards to show only item count, and implemented print-friendly handout mode (`?handout=1`) in `renderProtocolLiquid`. Milestone M9b-3b (protocol lifecycle: archive, restore, duplicate-as-template for starters with CSRF, duplicate-from-starter, Rule 4 ownership isolation, in-page confirm UI, and Archived section) is VERIFIED LIVE on the dev store. Remaining NOT-verified live list: logged-out guest view, handoutText/footerDisclaimer rendering, real printing, live cross-owner/cross-shop denial, non-Latin handles (known limitation), discounted-order attribution live, and new CSP inside real admin iframe. Note: the admin attribution report page displayed zeros for live data until a contract mismatch between `getAttributionReport` and `renderAttributionTab` was resolved by unifying `{ month, summary, rows, byPractice, byProtocol }` across the data service, renderer, demo data, and RFC 4180 CSV export with `Intl.NumberFormat` currency formatting. Pre-submission hardening is complete with HMAC-signed OAuth state cookies, public `/privacy` page, `Strict-Transport-Security` on all responses, expanded embedded App Bridge CSP, admin starter catalog product search UX, individual mode settings label, and `.github/CODEOWNERS`. Milestone M11 implements shopper opt-in contact capture (server-defined canonical consent text, dedicated 5/hr rate limiter, case-insensitive email deduplication, RFC 4180 CSV export with `Verified = "no"`) and merchant contact & feedback (`ShopProfile` sync via Admin GraphQL, AES-256-GCM session token encryption at rest, "Contact & feedback" card with 1–5 rating and marketing opt-in checkbox, `scripts/contacts.mjs` and `scripts/stats.mjs`). Under Shopify Managed Installation, authentication uses App Bridge token exchange (RFC 8693) to acquire expiring offline access tokens (1-hour lifetime, `expiring: 1`) without interactive redirects, with encrypted storage at rest via AES-256-GCM, non-PII failure logging, friendly status 200 root page, and fallback legacy OAuth. The app holds a short-lived (1 hour) offline access token, re-acquired on admin load when missing or expiring within 5 minutes, and deleted on uninstallation; no refresh token is stored (fewer secrets at rest) and no separate refresh flow is built because the app makes no Admin API calls outside embedded-admin requests. Covered by 581 automated unit tests across 132 suites. +**Current build stage:** The pure engine logic, security controls, cryptographic primitives, Liquid renderers, catalog resolver, rate limiters, validation barriers, and PostgreSQL database queries are fully built and covered by 507 automated unit tests across 111 suites using mock database interfaces. Real PostgreSQL query execution, transaction behavior, constraints, row-level locking (`FOR UPDATE`), and migrations are verified against a live PostgreSQL instance via `npm run test:db` (11 integration tests run against a live PostgreSQL instance, skipping with a notice when none is reachable). The app is deployed live on Railway (`practitioner-protocols-production.up.railway.app`) with internal PostgreSQL and automated migration tracking. Spikes S2 (sitemap exclusion, `noindex`, and proxy header), S3 (hidden line property attribution persistence in storefront checkout and `orders/paid` webhook payload), S4 (20 lines in a single cart batch add), S5 (retail vs B2B pricing), and S6 (storefront predictive search and variant hydration contract; Option A chosen) are verified on live dev store `ai-agent-app-h9qwnwd8.myshopify.com`; Spike S1 is verified on the Plus dev store only. In production mode with `DATABASE_URL` configured, the embedded merchant admin is verified live on the dev store (Milestone M9a; `GET /`, `GET /admin`, `GET /app` served via App Bridge ID tokens and HS256 session verification), including the self-enrolment toggle and pending practitioner review table with customer admin deep links (Milestone M9b-1; verified live on dev store); for Milestone M9b-2, verified live on the dev store: enrol round trip (JSON POST through the App Proxy), pending portal state with requested name, admin pending row with customer deep link, merchant approval, approved portal display, form-encoded and `text/plain;application/json` POSTs returning 415, and unknown action returning 400 (the per-customer rate limit of 5/hr, the pending cap of 200, `customers/redact` profile deletion, and `Sec-Fetch-Site` handling are covered by automated unit tests and were not exercised live); for Milestone M9b-3a (protocol authoring and client page hydration), VERIFIED LIVE on the dev store: editor search with real products and CAD prices, protocol save, share-page hydration, Add all with _protocol_token/_protocol_id/_protocol_usage in the cart, checkout to orders/paid (webhook 200, stat row written), edit round trip (qty and note persisted, token unchanged), list card item count, handout page layout; migration adding nullable `productHandle` to `ProtocolLine`, validation and persistence, client-side predictive search (`/search/suggest.json`) and variant hydration (`/products/.js`) in the practitioner editor with `textContent` rendering, client page variant hydration from `/products/.js`, and removal of `createFakeVariantResolver` from the production App Proxy pipeline. Follow-up hotfix loaded protocol lines on portal queries (`json_agg` + `linesCount`) to eliminate edit data loss, removed fake retail total (`$0.00`) from cards to show only item count, and implemented print-friendly handout mode (`?handout=1`) in `renderProtocolLiquid`. Milestone M9b-3b (protocol lifecycle: archive, restore, duplicate-as-template for starters with CSRF, duplicate-from-starter, Rule 4 ownership isolation, in-page confirm UI, and Archived section) is VERIFIED LIVE on the dev store. Remaining NOT-verified live list: logged-out guest view, handoutText/footerDisclaimer rendering, real printing, live cross-owner/cross-shop denial, non-Latin handles (known limitation), discounted-order attribution live, and new CSP inside real admin iframe. Note: the admin attribution report page displayed zeros for live data until a contract mismatch between `getAttributionReport` and `renderAttributionTab` was resolved by unifying `{ month, summary, rows, byPractice, byProtocol }` across the data service, renderer, demo data, and RFC 4180 CSV export with `Intl.NumberFormat` currency formatting. Pre-submission hardening is complete with HMAC-signed OAuth state cookies, public `/privacy` page, `Strict-Transport-Security` on all responses, expanded embedded App Bridge CSP, admin starter catalog product search UX, individual mode settings label, and `.github/CODEOWNERS`. Milestone M11 implements shopper opt-in contact capture (server-defined canonical consent text, dedicated 5/hr rate limiter, case-insensitive email deduplication, RFC 4180 CSV export with `Verified = "no"`) and merchant contact & feedback (`ShopProfile` sync via Admin GraphQL, AES-256-GCM session token encryption at rest, "Contact & feedback" card with 1–5 rating and marketing opt-in checkbox, `scripts/contacts.mjs` and `scripts/stats.mjs`). Under Shopify Managed Installation, authentication uses App Bridge token exchange (RFC 8693) to acquire expiring offline access tokens (1-hour lifetime, `expiring: 1`) without interactive redirects, with encrypted storage at rest via AES-256-GCM, non-PII failure logging, friendly status 200 root page, and fallback legacy OAuth. The app holds a short-lived (1 hour) offline access token, re-acquired on admin load when missing or expiring within 5 minutes, and deleted on uninstallation; no refresh token is stored (fewer secrets at rest) and no separate refresh flow is built because the app makes no Admin API calls outside embedded-admin requests. Covered by 582 automated unit tests across 132 suites. --- diff --git a/docs/BUILD_PLAN.md b/docs/BUILD_PLAN.md index bc1de29..c1a6633 100644 --- a/docs/BUILD_PLAN.md +++ b/docs/BUILD_PLAN.md @@ -122,7 +122,7 @@ - Lifecycle: `app/uninstalled` deletes the `Session` row while keeping `ShopProfile` with `uninstalledAt` timestamp; `shop/redact` purges all shop records. - UI: Settings opt-in card displays a disabled checkbox with `"Your store contact email is still being loaded. Reload this page."` when `ShopProfile.email` is absent (never displays `"this email"`), and custom request cards prefill `replyEmail` when present. Shopper consent text uses `ShopProfile.shopName` when available. - Friendly root landing page: `GET /` (or `/admin`, `/app`) outside admin iframe with no session serves status 200 HTML with `"Practitioner Protocols runs inside your Shopify admin. Open it from your Shopify admin's Apps list."`, link to `/privacy`, and zero login or shop domain input fields. - - Automated unit test coverage: 581 passing tests across 132 suites (`npm test`). + - Automated unit test coverage: 582 passing tests across 132 suites (`npm test`). ## Later Practitioner application form · aggregate "client orders" count on protocol cards · protocol version history · Salesforce rollup via Bridge Recipes. diff --git a/server.js b/server.js index 4456db3..2f56956 100644 --- a/server.js +++ b/server.js @@ -666,6 +666,7 @@ export function createRequestListener(options = {}) { const apiKey = options.shopifyApiKey || (process.env.SHOPIFY_API_KEY || ''); const pool = options.pool || null; const tokenExchanger = options.tokenExchanger || null; + const fetchFn = options.fetchFn || globalThis.fetch; return async (req, res) => { try { @@ -795,19 +796,22 @@ export function createRequestListener(options = {}) { if (databaseUrl && pool) { let accessToken = ''; let scope = 'read_orders'; + let expiresIn = 3600; if (typeof tokenExchanger === 'function') { const tokenData = await tokenExchanger({ shop, code, apiKey, apiSecret }); accessToken = tokenData.accessToken; scope = tokenData.scope || scope; + expiresIn = typeof tokenData.expiresIn === 'number' ? tokenData.expiresIn : (tokenData.expires_in || 3600); } else { - const tokenRes = await fetch(`https://${shop}/admin/oauth/access_token`, { + const tokenRes = await fetchFn(`https://${shop}/admin/oauth/access_token`, { method: 'POST', headers: { 'Content-Type': 'application/json', Accept: 'application/json' }, body: JSON.stringify({ client_id: apiKey, client_secret: apiSecret, code, + expiring: 1, }), }); if (!tokenRes.ok) { @@ -818,26 +822,33 @@ export function createRequestListener(options = {}) { const tokenData = await tokenRes.json(); accessToken = tokenData.access_token; scope = tokenData.scope || scope; + expiresIn = typeof tokenData.expires_in === 'number' ? tokenData.expires_in : 3600; } const encryptedToken = encryptAccessToken(accessToken); + const expiresDate = new Date(Date.now() + expiresIn * 1000); // Do not store the state in Session.state; store "authenticated". // Store encrypted access token at rest using AES-256-GCM. + // Store expiry date for expiring offline tokens (Option 1 in Shopify Migration Guide). + // Refresh tokens are intentionally NOT stored (fewer secrets at rest). await pool.query( - `INSERT INTO "Session" ("id", "shop", "state", "isOnline", "scope", "accessToken") - VALUES ($1, $2, $3, false, $4, $5) + `INSERT INTO "Session" ("id", "shop", "state", "isOnline", "scope", "accessToken", "expires", "refreshToken", "refreshTokenExpires") + VALUES ($1, $2, $3, false, $4, $5, $6, NULL, NULL) ON CONFLICT ("id") DO UPDATE SET "shop" = EXCLUDED."shop", "state" = EXCLUDED."state", "scope" = EXCLUDED."scope", - "accessToken" = EXCLUDED."accessToken"`, - [`offline_${shop}`, shop, 'authenticated', scope, encryptedToken] + "accessToken" = EXCLUDED."accessToken", + "expires" = EXCLUDED."expires", + "refreshToken" = NULL, + "refreshTokenExpires" = NULL`, + [`offline_${shop}`, shop, 'authenticated', scope, encryptedToken, expiresDate] ); // Synchronize ShopProfile via Admin GraphQL shop query (non-blocking, never fails install) try { - await syncShopProfile(pool, { shop, accessToken }); + await syncShopProfile(pool, { shop, accessToken, fetchFn }); } catch (syncErr) { console.warn(`[ShopProfile] Failed to sync profile for ${shop} on install: ${syncErr.message}`); } diff --git a/test/server.test.js b/test/server.test.js index 07bdb2b..f1c4eb8 100644 --- a/test/server.test.js +++ b/test/server.test.js @@ -456,10 +456,98 @@ describe('Server & Configuration Hardening (server.js, shopify.app.toml, README. // Do not store the state in Session.state; store "authenticated" assert.equal(queriedRows[0].params[2], 'authenticated'); assert.equal(queriedRows[0].params[4], 'shpca_token_xyz_999'); + assert.ok(queriedRows[0].params[5] instanceof Date, 'Must persist expires timestamp as Date'); const setCookie = res.headers['Set-Cookie'] || res.headers['set-cookie']; assert.ok(setCookie.includes('Max-Age=0')); }); + + it('GET /api/auth/callback sends expiring: 1 when requesting access token from Shopify', async () => { + let tokenRequestUrl = ''; + let tokenRequestBody = null; + + const mockFetch = async (url, init) => { + const urlStr = String(url); + if (urlStr.includes('/admin/oauth/access_token')) { + tokenRequestUrl = urlStr; + if (init?.body) { + tokenRequestBody = JSON.parse(init.body); + } + return new Response(JSON.stringify({ + access_token: 'shpca_expiring_token_abc', + scope: 'read_orders', + expires_in: 3600, + refresh_token: 'shprt_test_refresh_token', + refresh_token_expires_in: 7776000 + }), { + status: 200, + headers: { 'Content-Type': 'application/json' } + }); + } + return new Response(JSON.stringify({ + data: { + shop: { + name: 'Expiring Store', + email: 'store@example.com', + shopOwnerName: 'Owner', + plan: { publicDisplayName: 'Basic' }, + shopAddress: { countryCodeV2: 'US' } + } + } + }), { + status: 200, + headers: { 'Content-Type': 'application/json' } + }); + }; + + const queriedRows = []; + const mockPool = { + async query(sql, params) { + queriedRows.push({ sql, params }); + return { rows: [] }; + }, + }; + + const prodListener = createRequestListener({ + databaseUrl: 'postgres://production.host:5432/db', + shopifyApiSecret: apiSecret, + shopifyApiKey: apiKey, + pool: mockPool, + fetchFn: mockFetch, + }); + + const state = 'nonce_expiring_state'; + const signedCookie = signOAuthState(state, apiSecret); + const params = { + code: 'valid_auth_code_expiring', + shop: 'expiring-store.myshopify.com', + state, + timestamp: '1729500000', + }; + const hmac = calculateOAuthSignature(params, apiSecret); + const queryString = new URLSearchParams({ ...params, hmac }).toString(); + + const req = createMockReq({ + method: 'GET', + url: `/api/auth/callback?${queryString}`, + headers: { cookie: `shopify_oauth_state=${signedCookie}` }, + }); + const res = createMockRes(); + await prodListener(req, res); + + assert.equal(res.statusCode, 302); + assert.ok(tokenRequestUrl.includes('/admin/oauth/access_token')); + assert.equal(tokenRequestBody.client_id, apiKey); + assert.equal(tokenRequestBody.client_secret, apiSecret); + assert.equal(tokenRequestBody.code, 'valid_auth_code_expiring'); + assert.equal(tokenRequestBody.expiring, 1, 'Must send expiring: 1 per Shopify migration requirement'); + + assert.ok(queriedRows.length >= 1); + const sessionRow = queriedRows.find(q => q.sql.includes('INSERT INTO "Session"')); + assert.ok(sessionRow, 'Must insert into Session table'); + assert.equal(sessionRow.params[0], 'offline_expiring-store.myshopify.com'); + assert.ok(sessionRow.params[5] instanceof Date, 'Must persist computed expires Date in Session'); + }); }); describe('Shopify App Store Security Headers (server.js)', () => {