@sgflabs/storefront-core 0.1.6 → 0.1.7
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/README.md
CHANGED
|
@@ -66,18 +66,19 @@ apex's single registered callback (state cookie and nonce helpers in `src/lib/he
|
|
|
66
66
|
A separate first-party storefront on its own host (not a subdomain of the apex) can log in with Google through the same
|
|
67
67
|
single callback, with no Google Console change.
|
|
68
68
|
|
|
69
|
-
- On the
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
69
|
+
- On the backend, give the app's publishable key its storefront URL (`POST /admin/opticomm/publishable-keys/{id}/storefront`)
|
|
70
|
+
and then enable the hub for it (`POST /admin/opticomm/publishable-keys/{id}/sso-hub` with `{ "enabled": true }`). The apex
|
|
71
|
+
asks `GET /store/opticomm/store-domains/sso-origin?host=` and accepts only that exact hostname (no wildcard or suffix
|
|
72
|
+
match); the answers are cached for 30 s (10 s for "no"), so a revoked host stops within that window. Registered hosts
|
|
73
|
+
receive customer JWTs that are valid across the backend, so only first-party hosts belong on the list.
|
|
73
74
|
- The backend `vendor_domains` feature must be on: handoff mint and redeem return 404 without it, so the login fails
|
|
74
75
|
after Google.
|
|
75
76
|
- Registered hosts must not log query strings on `/api/auth/handoff`: the one-time code is in the URL for up to 60 seconds.
|
|
76
77
|
- On the consuming app set `NEXT_PUBLIC_SSO_HUB_ORIGIN=<apex origin>` (build time and runtime). Do NOT set
|
|
77
|
-
`SSO_HUB_SECRET`
|
|
78
|
+
`SSO_HUB_SECRET` there. The app's `google-callback` refuses to exchange a code when this
|
|
78
79
|
variable is set.
|
|
79
80
|
- Re-export `auth/handoff` only: never `google-start`, and delete any `google-callback` re-export.
|
|
80
81
|
- Mount `<TenantHostProvider isTenantHost={false} googleSsoOrigin={ssoHubOrigin()}>`.
|
|
81
82
|
- `NEXT_PUBLIC_BASE_URL` must be the exact public https origin of the app, and its proxy must send
|
|
82
83
|
`x-forwarded-proto: https`. In this mode the handoff redeems for that configured host, never the `Host` header, so it works only for the
|
|
83
|
-
exact host of `NEXT_PUBLIC_BASE_URL`: an alternate hostname that is also
|
|
84
|
+
exact host of `NEXT_PUBLIC_BASE_URL`: an alternate hostname that is also enabled as an SSO hub return origin will not work.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sgflabs/storefront-core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
4
|
"description": "Shared storefront library consumed as raw TypeScript source by the brand apps.",
|
|
5
5
|
"license": "PolyForm-Shield-1.0.0",
|
|
6
6
|
"sideEffects": [
|
|
@@ -55,9 +55,9 @@
|
|
|
55
55
|
"devDependencies": {
|
|
56
56
|
"@sentry/nextjs": "^10.63.0",
|
|
57
57
|
"@sgflabs/artha-go-sdk": "^0.2.0",
|
|
58
|
-
"@sgflabs/gstin": "^0.1.
|
|
59
|
-
"@sgflabs/page-spec": "^0.1.
|
|
60
|
-
"@sgflabs/storefront-core-common": "^0.1.
|
|
58
|
+
"@sgflabs/gstin": "^0.1.7",
|
|
59
|
+
"@sgflabs/page-spec": "^0.1.7",
|
|
60
|
+
"@sgflabs/storefront-core-common": "^0.1.7",
|
|
61
61
|
"@testing-library/dom": "^10.4.1",
|
|
62
62
|
"@testing-library/jest-dom": "^7.0.0",
|
|
63
63
|
"@testing-library/react": "^16.3.2",
|
|
@@ -73,9 +73,9 @@
|
|
|
73
73
|
"peerDependencies": {
|
|
74
74
|
"@sentry/nextjs": "^10.63.0",
|
|
75
75
|
"@sgflabs/artha-go-sdk": "^0.2.0",
|
|
76
|
-
"@sgflabs/gstin": "^0.1.
|
|
77
|
-
"@sgflabs/page-spec": "^0.1.
|
|
78
|
-
"@sgflabs/storefront-core-common": "^0.1.
|
|
76
|
+
"@sgflabs/gstin": "^0.1.7",
|
|
77
|
+
"@sgflabs/page-spec": "^0.1.7",
|
|
78
|
+
"@sgflabs/storefront-core-common": "^0.1.7",
|
|
79
79
|
"next": "15.5.27",
|
|
80
80
|
"react": "^19.2.3",
|
|
81
81
|
"react-dom": "^19.2.3"
|
|
@@ -16,7 +16,7 @@ export function isHttpsRequest(request: Request): boolean {
|
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
18
|
* `nonce` is empty for an apex login (no handoff to bind); `state` is the OAuth state Google was handed.
|
|
19
|
-
* `registered` is true when google-start accepted the host via
|
|
19
|
+
* `registered` is true when google-start accepted the host via the backend sso-origin lookup (an admin-enabled publishable key); cookies without it read as not registered.
|
|
20
20
|
*/
|
|
21
21
|
export type SsoHubState = { host: string; locale: string; nonce: string; state: string; registered?: boolean }
|
|
22
22
|
|
|
@@ -103,21 +103,79 @@ function bareOrigin(value: string): URL | null {
|
|
|
103
103
|
const schemeAllowed = (url: URL): boolean =>
|
|
104
104
|
url.protocol === "https:" || (url.protocol === "http:" && new URL(getSiteBaseUrl()).protocol === "http:")
|
|
105
105
|
|
|
106
|
+
const SSO_ORIGIN_TTL_MS = 30_000
|
|
107
|
+
const SSO_ORIGIN_NEGATIVE_TTL_MS = 10_000
|
|
108
|
+
// A host confirmed within this long past its TTL is "known": it skips the cold-lookup budget (it is still re-asked at the backend).
|
|
109
|
+
const SSO_ORIGIN_STALE_WINDOW_MS = 300_000
|
|
110
|
+
const SSO_ORIGIN_CACHE_MAX = 500
|
|
111
|
+
// Backstop on cold lookups per instance per minute, so random hosts cannot turn this app into an unbounded backend load.
|
|
112
|
+
const SSO_ORIGIN_MAX_COLD_FETCHES_PER_MINUTE = 1000
|
|
113
|
+
const SSO_ORIGIN_BUDGET_WINDOW_MS = 60_000
|
|
114
|
+
// Positives and negatives live apart: junk misses can only evict other junk, never a confirmed host.
|
|
115
|
+
const ssoOriginHits = new Map<string, { origin: string; expiresAt: number }>()
|
|
116
|
+
const ssoOriginMisses = new Map<string, number>()
|
|
117
|
+
let coldBudget = { count: 0, resetAt: 0 }
|
|
118
|
+
|
|
119
|
+
export const clearSsoOriginCache = () => {
|
|
120
|
+
ssoOriginHits.clear()
|
|
121
|
+
ssoOriginMisses.clear()
|
|
122
|
+
coldBudget = { count: 0, resetAt: 0 }
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const remember = <V>(map: Map<string, V>, host: string, value: V) => {
|
|
126
|
+
map.delete(host)
|
|
127
|
+
map.set(host, value)
|
|
128
|
+
if (map.size > SSO_ORIGIN_CACHE_MAX) map.delete(map.keys().next().value as string)
|
|
129
|
+
}
|
|
130
|
+
|
|
106
131
|
/**
|
|
107
|
-
* The origin
|
|
108
|
-
*
|
|
109
|
-
*
|
|
132
|
+
* The return origin the backend permits for exactly this host, or null. `fetchOrigin` is the backend lookup: it
|
|
133
|
+
* resolves an origin, or throws (a 404 error means "not enabled"; anything else is an outage). Fails closed: an
|
|
134
|
+
* error, a malformed origin or an origin on another hostname is null. Hits live 30 s and misses 10 s so one login
|
|
135
|
+
* does not ask twice; errors are never cached. ponytail: per-instance caches and cold-lookup budget, all bounded; a
|
|
136
|
+
* revoked flag stops within 30 s. A hit moves its entry to the end (LRU-ish). Only a cold host (no confirmation in the
|
|
137
|
+
* last 5 minutes) is charged to the per-minute budget and is refused (null) above it without asking the backend; a
|
|
138
|
+
* known host, even past its TTL, is always re-checked at the backend, so revocation is honoured and junk cannot lock it out.
|
|
110
139
|
*/
|
|
111
|
-
export function
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
140
|
+
export async function ssoOriginForHost(
|
|
141
|
+
host: string,
|
|
142
|
+
fetchOrigin: (host: string) => Promise<string>,
|
|
143
|
+
now = Date.now()
|
|
144
|
+
): Promise<string | null> {
|
|
145
|
+
if (!isBareHostname(host) || host === apexHostname()) return null
|
|
146
|
+
const hit = ssoOriginHits.get(host)
|
|
147
|
+
if (hit && hit.expiresAt > now) {
|
|
148
|
+
remember(ssoOriginHits, host, hit)
|
|
149
|
+
return hit.origin
|
|
150
|
+
}
|
|
151
|
+
const missUntil = ssoOriginMisses.get(host)
|
|
152
|
+
if (missUntil !== undefined && missUntil > now) {
|
|
153
|
+
remember(ssoOriginMisses, host, missUntil)
|
|
154
|
+
return null
|
|
155
|
+
}
|
|
156
|
+
const known = hit !== undefined && hit.expiresAt + SSO_ORIGIN_STALE_WINDOW_MS > now
|
|
157
|
+
if (!known) {
|
|
158
|
+
if (coldBudget.resetAt <= now) coldBudget = { count: 0, resetAt: now + SSO_ORIGIN_BUDGET_WINDOW_MS }
|
|
159
|
+
if (++coldBudget.count > SSO_ORIGIN_MAX_COLD_FETCHES_PER_MINUTE) return null
|
|
160
|
+
}
|
|
161
|
+
try {
|
|
162
|
+
const raw = await fetchOrigin(host)
|
|
163
|
+
const url = bareOrigin(raw)
|
|
164
|
+
if (url && schemeAllowed(url) && url.hostname === host) {
|
|
165
|
+
ssoOriginMisses.delete(host)
|
|
166
|
+
remember(ssoOriginHits, host, { origin: raw, expiresAt: now + SSO_ORIGIN_TTL_MS })
|
|
167
|
+
return raw
|
|
168
|
+
}
|
|
169
|
+
} catch (error) {
|
|
170
|
+
const status = (error as { status?: unknown } | null)?.status
|
|
171
|
+
if (status !== 404) {
|
|
172
|
+
// Status only: neither the host nor the error text is logged.
|
|
173
|
+
console.warn(`sso-origin lookup failed (status ${typeof status === "number" ? status : "none"}); failing closed`)
|
|
174
|
+
return null
|
|
118
175
|
}
|
|
119
|
-
if (url.hostname === host) return entry
|
|
120
176
|
}
|
|
177
|
+
ssoOriginHits.delete(host)
|
|
178
|
+
remember(ssoOriginMisses, host, now + SSO_ORIGIN_NEGATIVE_TTL_MS)
|
|
121
179
|
return null
|
|
122
180
|
}
|
|
123
181
|
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { getOpticommSdk } from "../config"
|
|
2
|
+
import { getClientIpHeaders } from "./client-ip-headers"
|
|
3
|
+
import { ssoOriginForHost } from "./sso-hub"
|
|
4
|
+
|
|
5
|
+
/** The Google SSO hub return origin the backend permits for `host` (null when not enabled or on any failure). */
|
|
6
|
+
export const lookupSsoOrigin = (host: string) =>
|
|
7
|
+
ssoOriginForHost(host, async (h) => {
|
|
8
|
+
const sdk = await getOpticommSdk()
|
|
9
|
+
// Forward the visitor IP so the backend budgets per visitor, not per storefront server.
|
|
10
|
+
return (await sdk.store.opticomm.retrieveStoreDomainsSsoOrigin<{ origin: string }>({ host: h }, await getClientIpHeaders())).origin
|
|
11
|
+
})
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { NextResponse } from "next/server"
|
|
2
2
|
import { getOpticommSdk, getSdk } from "../../../lib/config"
|
|
3
3
|
import { finishGoogleLogin } from "../../../lib/data/finish-google-login"
|
|
4
|
+
import { lookupSsoOrigin } from "../../../lib/helpers/sso-origin-lookup"
|
|
4
5
|
import { getSiteBaseUrl } from "../../../lib/helpers/get-site-base-url"
|
|
5
6
|
import {
|
|
6
7
|
apexHostname,
|
|
@@ -9,7 +10,6 @@ import {
|
|
|
9
10
|
originForHost,
|
|
10
11
|
isHttpsRequest,
|
|
11
12
|
readCookie,
|
|
12
|
-
registeredReturnOrigin,
|
|
13
13
|
resolveLocale,
|
|
14
14
|
sameSecret,
|
|
15
15
|
SSO_HUB_COOKIE,
|
|
@@ -42,9 +42,9 @@ export async function GET(request: Request) {
|
|
|
42
42
|
// missing, tampered or expired one is a failed login: the code could be anyone's (login CSRF). Unset, no cookie is
|
|
43
43
|
// today's apex flow.
|
|
44
44
|
const locale = resolveLocale(hub ? hub.locale : url.searchParams.get("locale"))
|
|
45
|
-
// A cookie issued for a registered host stays valid only while that host is still registered:
|
|
46
|
-
//
|
|
47
|
-
const registeredOrigin = hub ?
|
|
45
|
+
// A cookie issued for a registered host stays valid only while that host is still registered: un-flagging it in
|
|
46
|
+
// the backend must stop in-flight handoffs, not fall back to the tenant path.
|
|
47
|
+
const registeredOrigin = hub?.registered ? await lookupSsoOrigin(hub.host) : null
|
|
48
48
|
const revoked = Boolean(hub?.registered) && !registeredOrigin
|
|
49
49
|
const tenantOrigin = hub && !revoked && hub.host !== apexHostname() ? (registeredOrigin ?? originForHost(hub.host)) : null
|
|
50
50
|
const fail = () =>
|
|
@@ -2,13 +2,13 @@ import { NextResponse } from "next/server"
|
|
|
2
2
|
import { getOpticommSdk } from "../../../lib/config"
|
|
3
3
|
import { getGoogleLoginUrl } from "../../../lib/data/customer"
|
|
4
4
|
import { getSiteBaseUrl } from "../../../lib/helpers/get-site-base-url"
|
|
5
|
+
import { lookupSsoOrigin } from "../../../lib/helpers/sso-origin-lookup"
|
|
5
6
|
import { forwardedVisitorIp } from "../../../lib/helpers/visitor-ip"
|
|
6
7
|
import {
|
|
7
8
|
apexHostname,
|
|
8
9
|
isBareHostname,
|
|
9
10
|
isHttpsRequest,
|
|
10
11
|
isNonce,
|
|
11
|
-
registeredReturnOrigin,
|
|
12
12
|
resolveLocale,
|
|
13
13
|
sealSsoHubState,
|
|
14
14
|
SSO_HUB_COOKIE,
|
|
@@ -37,19 +37,21 @@ function throttled(request: Request, now = Date.now()): boolean {
|
|
|
37
37
|
return entry.count > MAX_STARTS_PER_WINDOW
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
/** The primary host of a live tenant (or the apex itself), flagged when it was accepted via
|
|
40
|
+
/** The primary host of a live tenant (or the apex itself), flagged when it was accepted via the backend SSO-origin lookup; null for anything else. No open redirect: only this answer ever reaches the cookie. */
|
|
41
41
|
async function trustedReturnHost(raw: string): Promise<{ host: string; registered: boolean } | null> {
|
|
42
42
|
const host = raw.trim().toLowerCase()
|
|
43
43
|
if (!isBareHostname(host)) return null
|
|
44
44
|
if (host === apexHostname()) return { host, registered: false }
|
|
45
|
-
|
|
45
|
+
// Resolver first on purpose: a host that is both a live tenant and an SSO-flagged origin stays a tenant (the
|
|
46
|
+
// flag only adds hosts the resolver does not know), so enabling the flag never changes how an existing tenant logs in.
|
|
46
47
|
try {
|
|
47
48
|
const sdk = await getOpticommSdk()
|
|
48
49
|
const { primary_host } = await sdk.store.opticomm.retrieveStoreDomainsResolve<{ primary_host: string }>({ host })
|
|
49
|
-
|
|
50
|
+
if (isBareHostname(primary_host)) return { host: primary_host, registered: false }
|
|
50
51
|
} catch {
|
|
51
|
-
|
|
52
|
+
// Not a live tenant (or the resolver is down): fall through to the SSO-origin lookup, which fails closed.
|
|
52
53
|
}
|
|
54
|
+
return (await lookupSsoOrigin(host)) ? { host, registered: true } : null
|
|
53
55
|
}
|
|
54
56
|
|
|
55
57
|
/**
|