@revenexx/cover 0.2.38 → 0.2.40
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/app/app.config.ts +17 -0
- package/app/components/account/address/AccountAddressCard.vue +16 -3
- package/app/components/account/address/AccountAddressView.vue +16 -1
- package/app/components/account/organization/AccountOrganization.vue +12 -0
- package/app/components/account/profile/AccountProfileForm.vue +15 -1
- package/app/components/account/team/AccountTeam.vue +15 -1
- package/app/components/checkout/onepage/CheckoutAddressPickerModal.vue +4 -0
- package/app/composables/useMasterDataEditable.ts +14 -0
- package/i18n/locales/de/account.json +4 -0
- package/i18n/locales/en/account.json +4 -0
- package/nuxt.config.ts +17 -1
- package/package.json +1 -1
- package/server/api/account/address/[id].delete.ts +1 -0
- package/server/api/account/address/[id].put.ts +1 -0
- package/server/api/account/address/index.post.ts +1 -0
- package/server/api/account/organization.put.ts +1 -0
- package/server/api/account/profile.get.ts +37 -2
- package/server/api/account/profile.put.ts +1 -0
- package/server/api/account/team/[id].put.ts +1 -0
- package/server/api/account/team.post.ts +1 -0
- package/server/routes/punchout/cxml/[account].ts +10 -0
- package/server/routes/punchout/ids.ts +13 -0
- package/server/routes/punchout/oci/[account].ts +11 -0
- package/server/utils/masterData.ts +23 -0
- package/server/utils/punchoutEntryGateway.ts +97 -0
- package/server/utils/punchoutEntryHandler.ts +185 -0
- package/server/utils/punchoutPassThrough.ts +306 -0
package/app/app.config.ts
CHANGED
|
@@ -27,6 +27,11 @@ export default defineAppConfig({
|
|
|
27
27
|
orderService: "mock",
|
|
28
28
|
inventoryService: "mock",
|
|
29
29
|
orderListService: "mock",
|
|
30
|
+
// Off by default like every other live domain. It was the one domain the
|
|
31
|
+
// registry resolved without declaring — `resolveServiceKey` fell through to
|
|
32
|
+
// its own mockKey, so promotions worked, but a storefront reading this list
|
|
33
|
+
// to see what it may switch on could not find it.
|
|
34
|
+
promotionsService: "mock",
|
|
30
35
|
themeService: "mock",
|
|
31
36
|
// Where a live catalogue keeps a manufacturer's logo. This is a per-tenant
|
|
32
37
|
// PIM attribute on the product document, not a platform contract, so a
|
|
@@ -95,6 +100,18 @@ export default defineAppConfig({
|
|
|
95
100
|
checkout: {
|
|
96
101
|
earliestDeliveryDays: 0,
|
|
97
102
|
},
|
|
103
|
+
// Master data another system owns — an ERP, usually — is shown in the
|
|
104
|
+
// account and not changed there. A merchant whose ERP is the record for a
|
|
105
|
+
// customer's addresses, profile or company sets the matching flag to
|
|
106
|
+
// false: the account shows the data read-only, the checkout offers its
|
|
107
|
+
// addresses for SELECTION only, and the BFF refuses the write (403) so no
|
|
108
|
+
// forgotten button can reach it. Changes then go through the merchant.
|
|
109
|
+
account: {
|
|
110
|
+
addresses: { editable: true },
|
|
111
|
+
profile: { editable: true },
|
|
112
|
+
organization: { editable: true },
|
|
113
|
+
team: { editable: true },
|
|
114
|
+
},
|
|
98
115
|
ui: {
|
|
99
116
|
icons: nuxtUiIcons(DEFAULT_ICON_WEIGHT),
|
|
100
117
|
},
|
|
@@ -4,7 +4,7 @@ import { COUNTRIES } from "../../../config/countries";
|
|
|
4
4
|
|
|
5
5
|
const { icon } = useIcons();
|
|
6
6
|
|
|
7
|
-
const props = defineProps<{
|
|
7
|
+
const props = withDefaults(defineProps<{
|
|
8
8
|
address: AccountAddress;
|
|
9
9
|
isDefaultLocked: boolean;
|
|
10
10
|
isDeleteDisabled: boolean;
|
|
@@ -12,7 +12,17 @@ const props = defineProps<{
|
|
|
12
12
|
isSaving: boolean;
|
|
13
13
|
saveError: string | null;
|
|
14
14
|
typeOptions: { label: string; value: string }[];
|
|
15
|
-
|
|
15
|
+
/**
|
|
16
|
+
* False when another system owns the address: no edit, no delete.
|
|
17
|
+
* Unset, it follows account.addresses.editable — so a page that renders
|
|
18
|
+
* the card without passing it (a theme's own copy of the address page)
|
|
19
|
+
* still hides what the storefront must not offer.
|
|
20
|
+
*/
|
|
21
|
+
editable?: boolean;
|
|
22
|
+
}>(), { editable: undefined });
|
|
23
|
+
|
|
24
|
+
const configEditable = useMasterDataEditable("addresses");
|
|
25
|
+
const canEdit = computed(() => props.editable ?? configEditable.value);
|
|
16
26
|
|
|
17
27
|
const formState = defineModel<AccountAddressFormState>("formState", { required: true });
|
|
18
28
|
|
|
@@ -63,7 +73,10 @@ function confirmDelete() {
|
|
|
63
73
|
{{ typeLabel }}
|
|
64
74
|
</p>
|
|
65
75
|
</div>
|
|
66
|
-
<div
|
|
76
|
+
<div
|
|
77
|
+
v-if="canEdit"
|
|
78
|
+
class="flex items-center gap-1"
|
|
79
|
+
>
|
|
67
80
|
<UTooltip :text="t('actions.edit')">
|
|
68
81
|
<UButton
|
|
69
82
|
:icon="icon('edit')"
|
|
@@ -19,6 +19,9 @@ const toast = useToast();
|
|
|
19
19
|
const {
|
|
20
20
|
addresses, status, error, updateAddress, createAddress, deleteAddress,
|
|
21
21
|
} = useAccountAddresses();
|
|
22
|
+
// Addresses another system owns are shown, never changed here (the BFF
|
|
23
|
+
// refuses the write as well — see account.addresses.editable).
|
|
24
|
+
const editable = useMasterDataEditable("addresses");
|
|
22
25
|
|
|
23
26
|
const editingIds = reactive<Record<string, boolean>>({});
|
|
24
27
|
const formStates = reactive<Record<string, AccountAddressFormState>>({});
|
|
@@ -226,7 +229,17 @@ async function onAddAddress() {
|
|
|
226
229
|
|
|
227
230
|
<template>
|
|
228
231
|
<div class="space-y-6">
|
|
229
|
-
<
|
|
232
|
+
<UAlert
|
|
233
|
+
v-if="!editable"
|
|
234
|
+
color="info"
|
|
235
|
+
variant="soft"
|
|
236
|
+
:icon="icon('info')"
|
|
237
|
+
:description="t('addresses.locked')"
|
|
238
|
+
/>
|
|
239
|
+
<div
|
|
240
|
+
v-else
|
|
241
|
+
class="flex justify-end"
|
|
242
|
+
>
|
|
230
243
|
<UButton
|
|
231
244
|
:icon="icon('plus')"
|
|
232
245
|
:label="t('addresses.actions.add')"
|
|
@@ -271,6 +284,7 @@ async function onAddAddress() {
|
|
|
271
284
|
:address="address"
|
|
272
285
|
:is-default-locked="defaultLockedIds.has(address.id)"
|
|
273
286
|
:is-delete-disabled="deleteDisabledIds.has(address.id)"
|
|
287
|
+
:editable="editable"
|
|
274
288
|
:is-editing="!!editingIds[address.id]"
|
|
275
289
|
:is-saving="!!savingIds[address.id]"
|
|
276
290
|
:save-error="saveErrors[address.id] ?? null"
|
|
@@ -289,6 +303,7 @@ async function onAddAddress() {
|
|
|
289
303
|
:address="address"
|
|
290
304
|
:is-default-locked="defaultLockedIds.has(address.id)"
|
|
291
305
|
:is-delete-disabled="deleteDisabledIds.has(address.id)"
|
|
306
|
+
:editable="editable"
|
|
292
307
|
:is-editing="!!editingIds[address.id]"
|
|
293
308
|
:is-saving="!!savingIds[address.id]"
|
|
294
309
|
:save-error="saveErrors[address.id] ?? null"
|
|
@@ -11,6 +11,9 @@ const { icon } = useIcons();
|
|
|
11
11
|
const { t } = useI18n();
|
|
12
12
|
const toast = useToast();
|
|
13
13
|
const { organization, status, error, updateOrganization } = useAccountOrganization();
|
|
14
|
+
// Company data another system owns is shown, never changed here (the BFF
|
|
15
|
+
// refuses the write as well — see account.organization.editable).
|
|
16
|
+
const editable = useMasterDataEditable("organization");
|
|
14
17
|
|
|
15
18
|
const editing = ref(false);
|
|
16
19
|
const saving = ref(false);
|
|
@@ -74,6 +77,7 @@ async function save(): Promise<void> {
|
|
|
74
77
|
{{ t('organization.companySection') }}
|
|
75
78
|
</h2>
|
|
76
79
|
<UButton
|
|
80
|
+
v-if="editable"
|
|
77
81
|
color="neutral"
|
|
78
82
|
variant="outline"
|
|
79
83
|
size="sm"
|
|
@@ -82,6 +86,14 @@ async function save(): Promise<void> {
|
|
|
82
86
|
@click="openEdit"
|
|
83
87
|
/>
|
|
84
88
|
</div>
|
|
89
|
+
<UAlert
|
|
90
|
+
v-if="!editable"
|
|
91
|
+
class="m-4 sm:m-5 mb-0 sm:mb-0"
|
|
92
|
+
color="info"
|
|
93
|
+
variant="soft"
|
|
94
|
+
:icon="icon('info')"
|
|
95
|
+
:description="t('organization.locked')"
|
|
96
|
+
/>
|
|
85
97
|
<dl class="p-4 sm:p-5 grid grid-cols-1 sm:grid-cols-2 gap-x-6 gap-y-4">
|
|
86
98
|
<div>
|
|
87
99
|
<dt class="text-xs text-muted">
|
|
@@ -10,6 +10,9 @@ const { icon } = useIcons();
|
|
|
10
10
|
const { t } = useI18n();
|
|
11
11
|
const toast = useToast();
|
|
12
12
|
const { user, updateProfile } = useAccount();
|
|
13
|
+
// Details another system owns are shown, never changed here (the BFF refuses
|
|
14
|
+
// the write as well — see account.profile.editable).
|
|
15
|
+
const editable = useMasterDataEditable("profile");
|
|
13
16
|
|
|
14
17
|
const saving = ref(false);
|
|
15
18
|
const saveError = ref<string | null>(null);
|
|
@@ -84,7 +87,10 @@ async function onSubmit() {
|
|
|
84
87
|
{{ t('profile.form.description') }}
|
|
85
88
|
</p>
|
|
86
89
|
</div>
|
|
87
|
-
<UTooltip
|
|
90
|
+
<UTooltip
|
|
91
|
+
v-if="editable"
|
|
92
|
+
:text="t('actions.edit')"
|
|
93
|
+
>
|
|
88
94
|
<UButton
|
|
89
95
|
:icon="icon('edit')"
|
|
90
96
|
variant="ghost"
|
|
@@ -94,6 +100,14 @@ async function onSubmit() {
|
|
|
94
100
|
/>
|
|
95
101
|
</UTooltip>
|
|
96
102
|
</div>
|
|
103
|
+
<UAlert
|
|
104
|
+
v-if="!editable"
|
|
105
|
+
class="mt-3"
|
|
106
|
+
color="info"
|
|
107
|
+
variant="soft"
|
|
108
|
+
:icon="icon('info')"
|
|
109
|
+
:description="t('profile.locked')"
|
|
110
|
+
/>
|
|
97
111
|
</template>
|
|
98
112
|
|
|
99
113
|
<FormKit
|
|
@@ -14,6 +14,9 @@ const { icon } = useIcons();
|
|
|
14
14
|
const { t } = useI18n();
|
|
15
15
|
const toast = useToast();
|
|
16
16
|
const { members, status, error, inviteMember, updateMemberRole } = useAccountTeam();
|
|
17
|
+
// Contacts another system owns are listed, never invited or changed here
|
|
18
|
+
// (the BFF refuses the write as well — see account.team.editable).
|
|
19
|
+
const editable = useMasterDataEditable("team");
|
|
17
20
|
const { tableUi } = useDataTable();
|
|
18
21
|
|
|
19
22
|
const ROLES: AccountTeamRole[] = ["buyer", "approver", "admin", "requester"];
|
|
@@ -141,12 +144,20 @@ async function saveEdit(): Promise<void> {
|
|
|
141
144
|
/>
|
|
142
145
|
|
|
143
146
|
<template v-else>
|
|
147
|
+
<UAlert
|
|
148
|
+
v-if="!editable"
|
|
149
|
+
color="info"
|
|
150
|
+
variant="soft"
|
|
151
|
+
:icon="icon('info')"
|
|
152
|
+
:description="t('team.locked')"
|
|
153
|
+
/>
|
|
144
154
|
<UiTableToolbar
|
|
145
155
|
v-model:search="search"
|
|
146
156
|
:search-placeholder="t('team.searchPlaceholder')"
|
|
147
157
|
>
|
|
148
158
|
<template #actions>
|
|
149
159
|
<UButton
|
|
160
|
+
v-if="editable"
|
|
150
161
|
color="primary"
|
|
151
162
|
tabindex="0"
|
|
152
163
|
:icon="icon('plus')"
|
|
@@ -206,7 +217,10 @@ async function saveEdit(): Promise<void> {
|
|
|
206
217
|
/>
|
|
207
218
|
</template>
|
|
208
219
|
<template #actions-cell="{ row }">
|
|
209
|
-
<div
|
|
220
|
+
<div
|
|
221
|
+
v-if="editable"
|
|
222
|
+
class="flex items-center justify-end gap-1"
|
|
223
|
+
>
|
|
210
224
|
<UTooltip :text="t('team.editTitle')">
|
|
211
225
|
<UButton
|
|
212
226
|
:icon="icon('edit')"
|
|
@@ -23,6 +23,9 @@ const emit = defineEmits<{
|
|
|
23
23
|
}>();
|
|
24
24
|
|
|
25
25
|
const { t } = useI18n();
|
|
26
|
+
// Addresses another system owns are picked from, never added to here: the
|
|
27
|
+
// order export takes the address the buyer selects, not one typed in.
|
|
28
|
+
const canAddAddress = useMasterDataEditable("addresses");
|
|
26
29
|
const toast = useToast();
|
|
27
30
|
const { form, profile, refreshProfile, validateField } = useCheckoutOnePage();
|
|
28
31
|
|
|
@@ -209,6 +212,7 @@ const addressLine = (address: Address): string =>
|
|
|
209
212
|
</p>
|
|
210
213
|
|
|
211
214
|
<UButton
|
|
215
|
+
v-if="canAddAddress"
|
|
212
216
|
variant="soft"
|
|
213
217
|
color="primary"
|
|
214
218
|
size="sm"
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ComputedRef } from "vue";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Whether the storefront may change this part of the customer's master data.
|
|
5
|
+
*
|
|
6
|
+
* `account.<area>.editable` in the app config, editable unless a merchant
|
|
7
|
+
* turned it off because another system — usually the ERP — owns the record.
|
|
8
|
+
* The BFF enforces the same flag (`assertMasterDataEditable`); this only
|
|
9
|
+
* decides what the UI offers.
|
|
10
|
+
*/
|
|
11
|
+
export function useMasterDataEditable(area: "addresses" | "profile" | "organization" | "team"): ComputedRef<boolean> {
|
|
12
|
+
const config = useAppConfig() as { account?: Partial<Record<typeof area, { editable?: boolean }>> };
|
|
13
|
+
return computed(() => config.account?.[area]?.editable !== false);
|
|
14
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"addresses": {
|
|
3
3
|
"title": "Ihre Adressen",
|
|
4
|
+
"locked": "Ihre Adressen werden von uns gepflegt. Für eine Änderung wenden Sie sich bitte an Ihren Ansprechpartner.",
|
|
4
5
|
"description": "Verwalten Sie Ihre gespeicherten Liefer- und Rechnungsadressen.",
|
|
5
6
|
"errorTitle": "Adressen konnten nicht geladen werden",
|
|
6
7
|
"errorDescription": "Bitte versuchen Sie es später erneut.",
|
|
@@ -145,6 +146,7 @@
|
|
|
145
146
|
},
|
|
146
147
|
"profile": {
|
|
147
148
|
"title": "Ihre Stammdaten",
|
|
149
|
+
"locked": "Ihre Stammdaten werden von uns gepflegt. Für eine Änderung wenden Sie sich bitte an Ihren Ansprechpartner.",
|
|
148
150
|
"description": "Verwalten Sie Ihre persönlichen Daten und Zugangsdaten.",
|
|
149
151
|
"form": {
|
|
150
152
|
"title": "Stammdaten bearbeiten",
|
|
@@ -501,6 +503,7 @@
|
|
|
501
503
|
},
|
|
502
504
|
"organization": {
|
|
503
505
|
"title": "Firmendaten",
|
|
506
|
+
"locked": "Ihre Firmendaten werden von uns gepflegt. Für eine Änderung wenden Sie sich bitte an Ihren Ansprechpartner.",
|
|
504
507
|
"description": "Daten Ihres Unternehmens ansehen und bearbeiten.",
|
|
505
508
|
"companySection": "Unternehmen",
|
|
506
509
|
"edit": "Bearbeiten",
|
|
@@ -527,6 +530,7 @@
|
|
|
527
530
|
},
|
|
528
531
|
"team": {
|
|
529
532
|
"title": "Benutzer",
|
|
533
|
+
"locked": "Die Benutzer Ihres Unternehmens werden von uns gepflegt. Für einen neuen Zugang wenden Sie sich bitte an Ihren Ansprechpartner.",
|
|
530
534
|
"description": "Verwalten Sie die Benutzer Ihres Unternehmens und deren Rollen.",
|
|
531
535
|
"searchPlaceholder": "Nach Name, E-Mail oder Rolle suchen …",
|
|
532
536
|
"invite": "Benutzer einladen",
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"addresses": {
|
|
3
3
|
"title": "Your addresses",
|
|
4
|
+
"locked": "Your addresses are maintained by us. To change one, please contact your account manager.",
|
|
4
5
|
"description": "Manage your saved shipping and billing addresses.",
|
|
5
6
|
"errorTitle": "Addresses could not be loaded",
|
|
6
7
|
"errorDescription": "Please try again later.",
|
|
@@ -145,6 +146,7 @@
|
|
|
145
146
|
},
|
|
146
147
|
"profile": {
|
|
147
148
|
"title": "Your profile",
|
|
149
|
+
"locked": "Your details are maintained by us. To change them, please contact your account manager.",
|
|
148
150
|
"description": "Manage your personal details and login credentials.",
|
|
149
151
|
"form": {
|
|
150
152
|
"title": "Edit master data",
|
|
@@ -501,6 +503,7 @@
|
|
|
501
503
|
},
|
|
502
504
|
"organization": {
|
|
503
505
|
"title": "Company",
|
|
506
|
+
"locked": "Your company details are maintained by us. To change them, please contact your account manager.",
|
|
504
507
|
"description": "View and edit your company details.",
|
|
505
508
|
"companySection": "Company",
|
|
506
509
|
"edit": "Edit",
|
|
@@ -527,6 +530,7 @@
|
|
|
527
530
|
},
|
|
528
531
|
"team": {
|
|
529
532
|
"title": "Users",
|
|
533
|
+
"locked": "Your company's users are maintained by us. For a new login, please contact your account manager.",
|
|
530
534
|
"description": "Manage your company's users and their roles.",
|
|
531
535
|
"searchPlaceholder": "Search by name, email or role…",
|
|
532
536
|
"invite": "Invite user",
|
package/nuxt.config.ts
CHANGED
|
@@ -87,6 +87,19 @@ export default defineNuxtConfig({
|
|
|
87
87
|
// request context, and a populated map here would let the cover-tenant
|
|
88
88
|
// cookie pick which tenant's API key the BFF authenticates with.
|
|
89
89
|
revenexxTenants: "",
|
|
90
|
+
// Punchout entry (ADR-0002): the ERP-facing address lives on the
|
|
91
|
+
// storefront host, because the gateway authenticates every call and app
|
|
92
|
+
// functions have no public ingress. The `modules:done` hook below adds
|
|
93
|
+
// the locale keys to this same object at build time.
|
|
94
|
+
punchout: {
|
|
95
|
+
/**
|
|
96
|
+
* Entry calls allowed per minute, per source IP × host, with a
|
|
97
|
+
* burst on top (ADR-0002 §6). The edge's WAF is the first line;
|
|
98
|
+
* this is the storefront's own share of the layered protection.
|
|
99
|
+
*/
|
|
100
|
+
entryRatePerMinute: 60,
|
|
101
|
+
entryRateBurst: 20,
|
|
102
|
+
},
|
|
90
103
|
typesenseHost: "localhost",
|
|
91
104
|
typesensePort: 8108,
|
|
92
105
|
typesenseProtocol: "http",
|
|
@@ -169,7 +182,10 @@ export default defineNuxtConfig({
|
|
|
169
182
|
const nuxt = useNuxt();
|
|
170
183
|
const i18n = (nuxt.options as { i18n?: { strategy?: string; defaultLocale?: string } }).i18n;
|
|
171
184
|
nuxt.options.runtimeConfig.punchout = {
|
|
172
|
-
|
|
185
|
+
// Spread the declared defaults, NOT a widened cast: the entry
|
|
186
|
+
// rate limits live in the same object and a cast to
|
|
187
|
+
// Record<string, unknown> drops them from the type.
|
|
188
|
+
...nuxt.options.runtimeConfig.punchout,
|
|
173
189
|
localeStrategy: i18n?.strategy ?? "no_prefix",
|
|
174
190
|
defaultLocale: i18n?.defaultLocale ?? "",
|
|
175
191
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@revenexx/cover",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.40",
|
|
4
4
|
"description": "Cover \u2014 revenexx design system for Nuxt. Distributed as a Nuxt layer: generic UI components, theming tokens and stores shared by the demo shop, custom storefronts and the Blokkli theme.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./nuxt.config.ts",
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { AccountAddressListResponse } from "../../../../app/interfaces/account/address-list";
|
|
2
2
|
|
|
3
3
|
export default defineEventHandler(async (event): Promise<AccountAddressListResponse> => {
|
|
4
|
+
assertMasterDataEditable("addresses");
|
|
4
5
|
const id = getRouterParam(event, "id");
|
|
5
6
|
if (!id) {
|
|
6
7
|
throw createError({ statusCode: 400, statusMessage: "Missing address id" });
|
|
@@ -26,6 +26,7 @@ function requireString(value: unknown, field: string): string {
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
export default defineEventHandler(async (event): Promise<AccountAddress> => {
|
|
29
|
+
assertMasterDataEditable("addresses");
|
|
29
30
|
const id = getRouterParam(event, "id");
|
|
30
31
|
if (!id) {
|
|
31
32
|
throw createError({ statusCode: 400, statusMessage: "Missing address id" });
|
|
@@ -4,6 +4,7 @@ import type {
|
|
|
4
4
|
} from "../../../../app/interfaces/account/address-list";
|
|
5
5
|
|
|
6
6
|
export default defineEventHandler(async (event): Promise<AccountAddress> => {
|
|
7
|
+
assertMasterDataEditable("addresses");
|
|
7
8
|
const body = await readBody<AccountAddressFormState>(event);
|
|
8
9
|
|
|
9
10
|
const required = [
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { AccountOrganization, AccountOrganizationFormState } from "../../../app/interfaces/account/organization";
|
|
2
2
|
|
|
3
3
|
export default defineEventHandler(async (event): Promise<AccountOrganization> => {
|
|
4
|
+
assertMasterDataEditable("organization");
|
|
4
5
|
const body = await readBody<AccountOrganizationFormState>(event);
|
|
5
6
|
return getOrganizationService(event).update(event, {
|
|
6
7
|
name: String(body?.name ?? "").trim(),
|
|
@@ -18,6 +18,24 @@ interface AccountProfileRaw {
|
|
|
18
18
|
};
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
+
/** The contact's own phone, or "" — a missing number is not an error. */
|
|
22
|
+
async function contactPhone(contactId: string | undefined): Promise<string> {
|
|
23
|
+
if (!contactId) {
|
|
24
|
+
return "";
|
|
25
|
+
}
|
|
26
|
+
try {
|
|
27
|
+
const contact = await useRevenexxSdk().call<{ phone?: string | null }>(
|
|
28
|
+
"GET",
|
|
29
|
+
`/v1/customers/contacts/${encodeURIComponent(contactId)}`,
|
|
30
|
+
);
|
|
31
|
+
return contact.phone ?? "";
|
|
32
|
+
}
|
|
33
|
+
catch (err) {
|
|
34
|
+
getLogService().error("Profile: contact phone lookup failed", apiErrorContext(err));
|
|
35
|
+
return "";
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
21
39
|
export default defineEventHandler(async (event): Promise<AccountProfileResponse> => {
|
|
22
40
|
// Session guard — the profile endpoint always requires a login, regardless
|
|
23
41
|
// of which identity implementation resolves the user below.
|
|
@@ -42,8 +60,25 @@ export default defineEventHandler(async (event): Promise<AccountProfileResponse>
|
|
|
42
60
|
// resolves the real customer through the public API.
|
|
43
61
|
const user = await getAccountService(event).getUser(event);
|
|
44
62
|
|
|
45
|
-
//
|
|
46
|
-
//
|
|
63
|
+
// Live: the buyer's own phone from their contact, and the company from
|
|
64
|
+
// their organisation. Both used to come from the demo store whatever the
|
|
65
|
+
// mode, so every live buyer saw the demo profile's company and number.
|
|
66
|
+
const live = resolveServiceKey(event, { domain: "accountService", mockKey: "mock", liveKey: "api" }) === "api";
|
|
67
|
+
if (live) {
|
|
68
|
+
const [phone, organization] = await Promise.all([
|
|
69
|
+
contactPhone(session.contactId),
|
|
70
|
+
getOrganizationService(event).get(event).catch(() => null),
|
|
71
|
+
]);
|
|
72
|
+
return {
|
|
73
|
+
name: user.name,
|
|
74
|
+
email: user.email,
|
|
75
|
+
phone,
|
|
76
|
+
company: organization?.name ?? "",
|
|
77
|
+
initials: user.initials,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Mock: the phone and company of the demo profile.
|
|
47
82
|
const profileData = await readCoverMutableJson<AccountProfileRaw>("account/profile.json");
|
|
48
83
|
|
|
49
84
|
return {
|
|
@@ -59,6 +59,7 @@ function validateProfileValue(value: string, maxLength: number, field: string):
|
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
export default defineEventHandler(async (event): Promise<AccountProfileResponse> => {
|
|
62
|
+
assertMasterDataEditable("profile");
|
|
62
63
|
const rawSession = getCookie(event, SESSION_COOKIE_NAME);
|
|
63
64
|
if (!rawSession) {
|
|
64
65
|
throw createError({ statusCode: 401, statusMessage: "Unauthorized" });
|
|
@@ -7,6 +7,7 @@ interface TeamUpdateBody {
|
|
|
7
7
|
const ROLES: AccountTeamRole[] = ["buyer", "approver", "admin", "requester"];
|
|
8
8
|
|
|
9
9
|
export default defineEventHandler(async (event): Promise<AccountTeamMember> => {
|
|
10
|
+
assertMasterDataEditable("team");
|
|
10
11
|
const id = getRouterParam(event, "id");
|
|
11
12
|
if (!id) {
|
|
12
13
|
throw createError({ statusCode: 400, statusMessage: "Missing member id" });
|
|
@@ -7,6 +7,7 @@ function toRole(value: unknown): AccountTeamRole {
|
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
export default defineEventHandler(async (event): Promise<AccountTeamMember> => {
|
|
10
|
+
assertMasterDataEditable("team");
|
|
10
11
|
const body = await readBody<Partial<AccountTeamMemberFormState>>(event);
|
|
11
12
|
const email = String(body?.email ?? "").trim();
|
|
12
13
|
if (!email) {
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { punchoutEntryHandler } from "../../../utils/punchoutEntryHandler";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* `/punchout/cxml/{account_code}` — cXML entry.
|
|
5
|
+
*
|
|
6
|
+
* The per-account segment is V8's `externalIdentifier`. The answer is the
|
|
7
|
+
* `PunchOutSetupResponse` itself, in the same HTTP response, which is the whole
|
|
8
|
+
* reason ADR-0002 exists.
|
|
9
|
+
*/
|
|
10
|
+
export default punchoutEntryHandler;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { punchoutEntryHandler } from "../../utils/punchoutEntryHandler";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* `/punchout/ids` — IDS 2.5 entry, on one shared address.
|
|
5
|
+
*
|
|
6
|
+
* IDS names its account in the call (`kndnr`/`name_kunde`/`pw_kunde`) rather
|
|
7
|
+
* than in the URL, because that is how trade software is configured. It is
|
|
8
|
+
* POST-only with `multipart/form-data` — the standard rules GET out, a cart
|
|
9
|
+
* does not fit in a query string — but this mount takes every method anyway:
|
|
10
|
+
* refusing the wrong one is the punchout app's answer to give, and the entry
|
|
11
|
+
* probe arrives here as a GET.
|
|
12
|
+
*/
|
|
13
|
+
export default punchoutEntryHandler;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { punchoutEntryHandler } from "../../../utils/punchoutEntryHandler";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* `/punchout/oci/{account_code}` — OCI entry.
|
|
5
|
+
*
|
|
6
|
+
* The account segment is V8's `externalIdentifier`, so an existing ERP
|
|
7
|
+
* configuration migrates unchanged. Every method: OCI entry typically arrives
|
|
8
|
+
* as a GET with its credentials in the query string, and the entry probe is a
|
|
9
|
+
* GET too.
|
|
10
|
+
*/
|
|
11
|
+
export default punchoutEntryHandler;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which parts of a customer's master data the storefront may change.
|
|
3
|
+
*
|
|
4
|
+
* Read from `account.<area>.editable` in the app config (default: editable).
|
|
5
|
+
* A merchant whose ERP owns the data turns it off; every BFF route that
|
|
6
|
+
* writes that data calls {@link assertMasterDataEditable} first, so the
|
|
7
|
+
* guard does not depend on the UI having hidden the last button.
|
|
8
|
+
*/
|
|
9
|
+
export type MasterDataArea = "addresses" | "profile" | "organization" | "team";
|
|
10
|
+
|
|
11
|
+
export function isMasterDataEditable(area: MasterDataArea): boolean {
|
|
12
|
+
const account = (useAppConfig() as { account?: Partial<Record<MasterDataArea, { editable?: boolean }>> }).account;
|
|
13
|
+
return account?.[area]?.editable !== false;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function assertMasterDataEditable(area: MasterDataArea): void {
|
|
17
|
+
if (!isMasterDataEditable(area)) {
|
|
18
|
+
throw createError({
|
|
19
|
+
statusCode: 403,
|
|
20
|
+
message: `This storefront does not change ${area}: they are maintained by the merchant.`,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import type { H3Event } from "h3";
|
|
2
|
+
import type { PunchoutEntryEnvelope } from "./punchoutPassThrough";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The one gateway call this layer makes, and who it makes it as.
|
|
6
|
+
*
|
|
7
|
+
* This is a deliberately small copy of what `@revenexx/cover` does in
|
|
8
|
+
* `server/utils/revenexxCredentials.ts`, and it is a copy on purpose: ADR-0002
|
|
9
|
+
* §8 wants a theme that carries no design system to be able to serve punchout
|
|
10
|
+
* entry by adding ONE dependency. A layer that pulled the whole design system
|
|
11
|
+
* in to resolve two request headers would not be that. So this file resolves
|
|
12
|
+
* the tenant itself and POSTs with `$fetch` — no SDK, no runtime dependency.
|
|
13
|
+
*
|
|
14
|
+
* A theme that carries both layers configures one set of values: the
|
|
15
|
+
* `NUXT_REVENEXX_*` names are the same on both sides.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** Who this request talks to the gateway as. Exactly one of jwt/apiKey is set. */
|
|
19
|
+
interface EntryCredentials {
|
|
20
|
+
readonly tenant: string;
|
|
21
|
+
readonly jwt: string;
|
|
22
|
+
readonly apiKey: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The platform's per-invocation tenant context (atlas ADR-0057 §8, ADR-0062):
|
|
27
|
+
* on a deployed Site the entrypoint resolves the tenant from the request Host,
|
|
28
|
+
* STRIPS any inbound tenant headers and injects its own — which is the whole
|
|
29
|
+
* reason an uncredentialed ERP call can be tenant-resolved at all, and why
|
|
30
|
+
* this layer never reads a tenant out of the URL.
|
|
31
|
+
*
|
|
32
|
+
* The env pair below it is local development and the demo shop; a deployed
|
|
33
|
+
* Site never reaches it.
|
|
34
|
+
*/
|
|
35
|
+
function entryCredentials(event: H3Event): EntryCredentials {
|
|
36
|
+
const config = useRuntimeConfig(event);
|
|
37
|
+
const brokeredTenant = getRequestHeader(event, "x-revenexx-tenant");
|
|
38
|
+
if (brokeredTenant) {
|
|
39
|
+
const jwt = getRequestHeader(event, "x-revenexx-context") ?? "";
|
|
40
|
+
return {
|
|
41
|
+
tenant: brokeredTenant,
|
|
42
|
+
jwt,
|
|
43
|
+
// A tenant header with no context token means the tenant is known
|
|
44
|
+
// but the auth is not: take the configured key only when it names
|
|
45
|
+
// the SAME tenant, or the call authenticates as somebody else.
|
|
46
|
+
apiKey: jwt || String(config.revenexxTenant || "") !== brokeredTenant ? "" : String(config.revenexxApiKey || ""),
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
tenant: String(config.revenexxTenant || ""),
|
|
51
|
+
jwt: "",
|
|
52
|
+
apiKey: String(config.revenexxApiKey || ""),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* POST the entry envelope to one of the `punchout.entry.*` capabilities.
|
|
58
|
+
*
|
|
59
|
+
* An ordinary authenticated capability call — the public, uncredentialed half
|
|
60
|
+
* ends at this layer's own route (ADR-0002 §2). Throws when the gateway is not
|
|
61
|
+
* configured or answers a failure; the caller turns that into a 502, because
|
|
62
|
+
* it is this storefront failing and not the external system.
|
|
63
|
+
*/
|
|
64
|
+
export async function punchoutEntryCall(
|
|
65
|
+
event: H3Event,
|
|
66
|
+
capabilityPath: string,
|
|
67
|
+
envelope: PunchoutEntryEnvelope,
|
|
68
|
+
timeoutMs: number,
|
|
69
|
+
): Promise<unknown> {
|
|
70
|
+
const { tenant, jwt, apiKey } = entryCredentials(event);
|
|
71
|
+
if (!tenant || (!jwt && !apiKey)) {
|
|
72
|
+
throw new Error(
|
|
73
|
+
"punchout entry: no gateway credentials — a deployed Site gets them from the brokered "
|
|
74
|
+
+ "request context, local development from NUXT_REVENEXX_TENANT / NUXT_REVENEXX_API_KEY",
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const config = useRuntimeConfig(event);
|
|
79
|
+
const endpoint = String(config.revenexxApiUrl || "https://api.revenexx.com").replace(/\/+$/, "");
|
|
80
|
+
|
|
81
|
+
return await $fetch(endpoint + capabilityPath, {
|
|
82
|
+
method: "POST",
|
|
83
|
+
headers: {
|
|
84
|
+
"content-type": "application/json",
|
|
85
|
+
"x-revenexx-tenant": tenant,
|
|
86
|
+
...(jwt ? { authorization: `Bearer ${jwt}` } : { "x-revenexx-api-key": apiKey }),
|
|
87
|
+
},
|
|
88
|
+
body: envelope,
|
|
89
|
+
// The entry contracts carry `timeout_ms: 8000`; an ERP holds its call
|
|
90
|
+
// open for seconds, so waiting longer than the capability itself does
|
|
91
|
+
// only turns one timeout into two.
|
|
92
|
+
timeout: timeoutMs,
|
|
93
|
+
// The answer is a JSON envelope; a failure must surface as a throw so
|
|
94
|
+
// it is never written out as if it were a protocol answer.
|
|
95
|
+
responseType: "json",
|
|
96
|
+
});
|
|
97
|
+
}
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import type { H3Event } from "h3";
|
|
2
|
+
import {
|
|
3
|
+
createError,
|
|
4
|
+
defineEventHandler,
|
|
5
|
+
getRequestHeader,
|
|
6
|
+
getRequestHost,
|
|
7
|
+
getRequestIP,
|
|
8
|
+
readRawBody,
|
|
9
|
+
send,
|
|
10
|
+
setResponseHeader,
|
|
11
|
+
setResponseStatus,
|
|
12
|
+
} from "h3";
|
|
13
|
+
|
|
14
|
+
import { punchoutEntryCall } from "./punchoutEntryGateway";
|
|
15
|
+
import {
|
|
16
|
+
PunchoutEntryRateLimiter,
|
|
17
|
+
punchoutEntryAddress,
|
|
18
|
+
punchoutEntryAnswer,
|
|
19
|
+
punchoutEntryEnvelope,
|
|
20
|
+
punchoutEntryRefusal,
|
|
21
|
+
} from "./punchoutPassThrough";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Punchout entry — one route, and it knows what neither OCI nor cXML is.
|
|
25
|
+
*
|
|
26
|
+
* This is the public, uncredentialed address an ERP calls. It serves the three
|
|
27
|
+
* addresses ADR-0002 §1 fixes:
|
|
28
|
+
*
|
|
29
|
+
* /punchout/oci/{account_code}
|
|
30
|
+
* /punchout/cxml/{account_code}
|
|
31
|
+
* /punchout/ids
|
|
32
|
+
*
|
|
33
|
+
* and it is a pipe, not a policy (§3). It forwards method, query, raw body
|
|
34
|
+
* bytes, content type and client IP as the entry envelope, and writes the
|
|
35
|
+
* punchout app's answer back out byte for byte — status, headers (including
|
|
36
|
+
* `Location`) and body. It authenticates nothing, refuses nothing, parses no
|
|
37
|
+
* JSON, requires no session and no CSRF token, redirects nobody to a login,
|
|
38
|
+
* and does not follow the 302 it hands back.
|
|
39
|
+
*
|
|
40
|
+
* Because it is protocol-agnostic, the entry-URL probe (RAD-49) needs no code
|
|
41
|
+
* here at all: `punchout_probe=<token>` is an ordinary query parameter that
|
|
42
|
+
* travels in the envelope, and the app's JSON handshake is an ordinary answer
|
|
43
|
+
* that is written out unaltered. That the probe passes is the proof that this
|
|
44
|
+
* route works — and while it fails, no punchout account on this tenant can be
|
|
45
|
+
* enabled at all.
|
|
46
|
+
*
|
|
47
|
+
* ## One handler, three mounts
|
|
48
|
+
*
|
|
49
|
+
* RAD-44 asks for one protocol-agnostic catch-all route, and this is it — one
|
|
50
|
+
* handler that parses the address out of the path and knows nothing else about
|
|
51
|
+
* the three protocols. What it is NOT is a Nitro catch-all under
|
|
52
|
+
* `/punchout/**`: server routes are matched before the page renderer, so a
|
|
53
|
+
* glob there swallows the shop's own punchout pages — the transfer
|
|
54
|
+
* interstitial lands on a 404 from this file instead of rendering. So the
|
|
55
|
+
* three addresses ADR-0002 §1 fixes are mounted as three route files, each of
|
|
56
|
+
* them a single line that exports this handler, and the protocol-agnosticism
|
|
57
|
+
* lives here rather than in the router.
|
|
58
|
+
*/
|
|
59
|
+
|
|
60
|
+
/** The capability contracts carry `timeout_ms: 8000`; waiting longer waits twice. */
|
|
61
|
+
const ENTRY_TIMEOUT_MS = 8000;
|
|
62
|
+
|
|
63
|
+
/** Methods that carry no body — reading one from them is an error, not empty bytes. */
|
|
64
|
+
const BODYLESS_METHODS = new Set(["GET", "HEAD"]);
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* One limiter per process, built on first use so the configured values apply.
|
|
68
|
+
* Module state rather than a per-request object — a counter that forgot every
|
|
69
|
+
* request would count nothing.
|
|
70
|
+
*/
|
|
71
|
+
let rateLimiter: PunchoutEntryRateLimiter | null = null;
|
|
72
|
+
|
|
73
|
+
function limiter(punchout: { entryRatePerMinute?: number; entryRateBurst?: number }): PunchoutEntryRateLimiter {
|
|
74
|
+
if (!rateLimiter) {
|
|
75
|
+
rateLimiter = new PunchoutEntryRateLimiter(
|
|
76
|
+
Number(punchout.entryRatePerMinute ?? 60),
|
|
77
|
+
Number(punchout.entryRateBurst ?? 20),
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
return rateLimiter;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Write one answer out as it stands.
|
|
85
|
+
*
|
|
86
|
+
* `send()` rather than a returned value: Nitro JSON-encodes a returned object
|
|
87
|
+
* and guesses a content type for a returned string, and this body is whatever
|
|
88
|
+
* the protocol says it is — an XML document, a line of text, or nothing at all
|
|
89
|
+
* behind a redirect.
|
|
90
|
+
*/
|
|
91
|
+
async function writeOut(
|
|
92
|
+
event: H3Event,
|
|
93
|
+
answer: { readonly status: number; readonly headers: Record<string, string>; readonly body: string },
|
|
94
|
+
): Promise<void> {
|
|
95
|
+
setResponseStatus(event, answer.status);
|
|
96
|
+
for (const [name, value] of Object.entries(answer.headers)) {
|
|
97
|
+
setResponseHeader(event, name, value);
|
|
98
|
+
}
|
|
99
|
+
// Entry is never cacheable: the answer carries a session handle.
|
|
100
|
+
setResponseHeader(event, "cache-control", "no-store");
|
|
101
|
+
await send(event, answer.body);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export const punchoutEntryHandler = defineEventHandler(async (event) => {
|
|
105
|
+
const path = event.path.split("?")[0] ?? "";
|
|
106
|
+
const address = punchoutEntryAddress(path);
|
|
107
|
+
if (!address) {
|
|
108
|
+
throw createError({ statusCode: 404, statusMessage: "Not Found" });
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const config = useRuntimeConfig(event);
|
|
112
|
+
|
|
113
|
+
// Counted per source IP × host: one procurement system hammering one shop
|
|
114
|
+
// must not shut the door for another shop on the same runtime.
|
|
115
|
+
const clientIp = getRequestHeader(event, "x-revenexx-client-ip")
|
|
116
|
+
?? getRequestIP(event, { xForwardedFor: true })
|
|
117
|
+
?? "";
|
|
118
|
+
const retryAfter = limiter(config.punchout ?? {}).check(
|
|
119
|
+
`${clientIp}|${getRequestHost(event)}`,
|
|
120
|
+
);
|
|
121
|
+
if (retryAfter !== null) {
|
|
122
|
+
return await writeOut(event, punchoutEntryRefusal("rate-limited", retryAfter));
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Bytes, never text and never JSON: `readRawBody(event, false)` answers a
|
|
126
|
+
// Buffer. h3 would otherwise parse a urlencoded or JSON body and the
|
|
127
|
+
// document would arrive re-serialised — which breaks Secure OCI's HMAC and
|
|
128
|
+
// whatever encoding a cXML document declares.
|
|
129
|
+
//
|
|
130
|
+
// GET and HEAD are asked for explicitly rather than left to `readRawBody`,
|
|
131
|
+
// which throws 405 for them. An OCI entry call IS typically a GET with its
|
|
132
|
+
// credentials in the query string, and the entry probe is always one — a
|
|
133
|
+
// pass-through that rejected GET would fail the one check that gates
|
|
134
|
+
// enabling a punchout account at all.
|
|
135
|
+
const body = BODYLESS_METHODS.has(event.method)
|
|
136
|
+
? Buffer.alloc(0)
|
|
137
|
+
: ((await readRawBody(event, false)) ?? Buffer.alloc(0));
|
|
138
|
+
if (body.byteLength > address.bodyCap) {
|
|
139
|
+
return await writeOut(event, punchoutEntryRefusal("too-large"));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const envelope = punchoutEntryEnvelope({
|
|
143
|
+
method: event.method,
|
|
144
|
+
query: new URL(event.path, "http://storefront.invalid").searchParams,
|
|
145
|
+
headers: event.headers,
|
|
146
|
+
body,
|
|
147
|
+
contentType: getRequestHeader(event, "content-type") ?? "",
|
|
148
|
+
clientIp,
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
let answered: unknown;
|
|
152
|
+
try {
|
|
153
|
+
answered = await punchoutEntryCall(event, address.capabilityPath, envelope, ENTRY_TIMEOUT_MS);
|
|
154
|
+
}
|
|
155
|
+
catch (error) {
|
|
156
|
+
// This storefront failed to reach the app that owns entry. Saying so as
|
|
157
|
+
// a 502 is the honest answer: a 200 with an error page in it is read by
|
|
158
|
+
// a procurement system as a catalogue.
|
|
159
|
+
console.error("[punchout] entry call failed", {
|
|
160
|
+
path: address.capabilityPath,
|
|
161
|
+
message: error instanceof Error ? error.message : String(error),
|
|
162
|
+
});
|
|
163
|
+
return await writeOut(event, {
|
|
164
|
+
status: 502,
|
|
165
|
+
headers: { "content-type": "text/plain; charset=utf-8" },
|
|
166
|
+
body: "punchout: this shop could not reach its punchout service",
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const answer = punchoutEntryAnswer(answered);
|
|
171
|
+
if (!answer) {
|
|
172
|
+
console.error("[punchout] entry answer was not a PunchoutEntryResponse", { path: address.capabilityPath });
|
|
173
|
+
return await writeOut(event, {
|
|
174
|
+
status: 502,
|
|
175
|
+
headers: { "content-type": "text/plain; charset=utf-8" },
|
|
176
|
+
body: "punchout: this shop's punchout service answered something it could not write out",
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return await writeOut(event, {
|
|
181
|
+
status: answer.status,
|
|
182
|
+
headers: answer.headers,
|
|
183
|
+
body: answer.body ?? "",
|
|
184
|
+
});
|
|
185
|
+
});
|
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The pass-through's own decisions — everything it does before and after the
|
|
3
|
+
* one call it makes, kept pure so it can be tested without a server.
|
|
4
|
+
*
|
|
5
|
+
* ADR-0002 §3 is the whole discipline, and every rule in it has a failure
|
|
6
|
+
* behind it:
|
|
7
|
+
*
|
|
8
|
+
* raw bytes a cXML or IDS document must survive byte for byte, and
|
|
9
|
+
* Secure OCI's HMAC is computed over what was actually
|
|
10
|
+
* sent — so the body is carried as base64 and never
|
|
11
|
+
* re-serialised
|
|
12
|
+
* no JSON parsing an entry body is a form or an XML document; parsing it
|
|
13
|
+
* would corrupt the evidence and reject valid calls
|
|
14
|
+
* no session, no CSRF the caller is a procurement system, not a browser with
|
|
15
|
+
* one of our cookies
|
|
16
|
+
* no login redirect an ERP follows a 302 to a login form and reports a
|
|
17
|
+
* broken catalogue
|
|
18
|
+
* the 302 is not followed here — it is the answer, for the buyer's browser
|
|
19
|
+
*
|
|
20
|
+
* The pass-through authenticates nothing and refuses nothing: credentials are
|
|
21
|
+
* the punchout app's business (§4). What it does own is the shape of the
|
|
22
|
+
* envelope, the body caps and the per-source rate limit (§6).
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/** One entry address this layer serves. */
|
|
26
|
+
export interface PunchoutEntryAddress {
|
|
27
|
+
/** The protocol suffix of the capability route — `oci`, `cxml` or `ids`. */
|
|
28
|
+
readonly protocol: "oci" | "cxml" | "ids";
|
|
29
|
+
/** The account segment OCI and cXML carry in their URL; "" for IDS. */
|
|
30
|
+
readonly accountCode: string;
|
|
31
|
+
/** Gateway path of the capability that answers it. */
|
|
32
|
+
readonly capabilityPath: string;
|
|
33
|
+
/** Largest body this address accepts, in bytes (ADR-0002 §6). */
|
|
34
|
+
readonly bodyCap: number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The envelope `punchout.entry.*` takes — the ERP's call, forwarded verbatim.
|
|
39
|
+
* Field for field the capability contract; nothing here is interpreted.
|
|
40
|
+
*/
|
|
41
|
+
export interface PunchoutEntryEnvelope {
|
|
42
|
+
readonly method: string;
|
|
43
|
+
readonly query: Record<string, string>;
|
|
44
|
+
readonly headers: Record<string, string>;
|
|
45
|
+
readonly body_b64: string;
|
|
46
|
+
readonly content_type: string;
|
|
47
|
+
readonly client_ip: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** What the punchout app answers, and the storefront writes out unchanged. */
|
|
51
|
+
export interface PunchoutEntryAnswer {
|
|
52
|
+
readonly status: number;
|
|
53
|
+
readonly headers: Record<string, string>;
|
|
54
|
+
readonly body: string | null;
|
|
55
|
+
readonly psid: string | null;
|
|
56
|
+
readonly start_page_url: string | null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Body caps per protocol (ADR-0002 §6). A cXML `PunchOutSetupRequest` is an
|
|
61
|
+
* order of magnitude larger than an OCI form, and an IDS `WKS` carries a whole
|
|
62
|
+
* cart as XML — one cap for all three would either reject real carts or accept
|
|
63
|
+
* anything.
|
|
64
|
+
*/
|
|
65
|
+
const BODY_CAPS = {
|
|
66
|
+
oci: 64 * 1024,
|
|
67
|
+
cxml: 256 * 1024,
|
|
68
|
+
ids: 2 * 1024 * 1024,
|
|
69
|
+
} as const;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Headers that must not be forwarded.
|
|
73
|
+
*
|
|
74
|
+
* Two groups, for two different reasons. The hop-by-hop ones describe THIS
|
|
75
|
+
* connection and mean nothing one hop further in (and `content-length` would
|
|
76
|
+
* describe a body that is now base64 inside a JSON envelope). The second group
|
|
77
|
+
* is the storefront's own: a cookie, an `authorization` the browser happens to
|
|
78
|
+
* carry, and the platform's brokered context headers — forwarding those would
|
|
79
|
+
* put this storefront's credentials into a record the punchout app keeps as
|
|
80
|
+
* evidence, and would let a caller present headers the gateway hop mints
|
|
81
|
+
* itself.
|
|
82
|
+
*/
|
|
83
|
+
const DROPPED_HEADERS = new Set([
|
|
84
|
+
"connection",
|
|
85
|
+
"keep-alive",
|
|
86
|
+
"proxy-authenticate",
|
|
87
|
+
"proxy-authorization",
|
|
88
|
+
"te",
|
|
89
|
+
"trailer",
|
|
90
|
+
"transfer-encoding",
|
|
91
|
+
"upgrade",
|
|
92
|
+
"content-length",
|
|
93
|
+
"expect",
|
|
94
|
+
"cookie",
|
|
95
|
+
"authorization",
|
|
96
|
+
"x-revenexx-tenant",
|
|
97
|
+
"x-revenexx-context",
|
|
98
|
+
"x-revenexx-market",
|
|
99
|
+
]);
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Which address was called, or null when this path is not an entry address.
|
|
103
|
+
*
|
|
104
|
+
* The three shapes are fixed by ADR-0002 §1 and are all this layer knows about
|
|
105
|
+
* the protocols: OCI and cXML keep a per-account URL segment (V8's
|
|
106
|
+
* `externalIdentifier`, so an existing ERP configuration migrates unchanged),
|
|
107
|
+
* IDS has one shared address and names its account in the call instead.
|
|
108
|
+
*
|
|
109
|
+
* Deliberately NOT a catch-all over `/punchout/*`: the shop's own punchout
|
|
110
|
+
* routes (`/punchout/start`, the transfer interstitial) live on the same
|
|
111
|
+
* prefix, and an address this layer does not serve must fall through to them
|
|
112
|
+
* rather than be answered here.
|
|
113
|
+
*/
|
|
114
|
+
export function punchoutEntryAddress(path: string): PunchoutEntryAddress | null {
|
|
115
|
+
const segments = path.replace(/^\/+/, "").split("/").map(part => decodeURIComponent(part));
|
|
116
|
+
if (segments[0] !== "punchout") {
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const protocol = segments[1];
|
|
121
|
+
if ((protocol === "oci" || protocol === "cxml") && segments.length === 3 && segments[2]) {
|
|
122
|
+
const accountCode = segments[2];
|
|
123
|
+
return {
|
|
124
|
+
protocol,
|
|
125
|
+
accountCode,
|
|
126
|
+
capabilityPath: `/v1/punchout/entry/${protocol}/${encodeURIComponent(accountCode)}`,
|
|
127
|
+
bodyCap: BODY_CAPS[protocol],
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
if (protocol === "ids" && segments.length === 2) {
|
|
131
|
+
return {
|
|
132
|
+
protocol: "ids",
|
|
133
|
+
accountCode: "",
|
|
134
|
+
capabilityPath: "/v1/punchout/entry/ids",
|
|
135
|
+
bodyCap: BODY_CAPS.ids,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Build the envelope from the parts of the request.
|
|
143
|
+
*
|
|
144
|
+
* `query` and `headers` arrive as multi-value structures and leave as flat
|
|
145
|
+
* maps, because that is what the contract declares. A repeated parameter keeps
|
|
146
|
+
* its FIRST value: OCI sends its credentials once, and a second `PASSWORD=`
|
|
147
|
+
* appended by a caller must not be the one that gets checked.
|
|
148
|
+
*/
|
|
149
|
+
export function punchoutEntryEnvelope(request: {
|
|
150
|
+
readonly method: string;
|
|
151
|
+
readonly query: Iterable<readonly [string, string]>;
|
|
152
|
+
readonly headers: Iterable<readonly [string, string]>;
|
|
153
|
+
readonly body: Uint8Array;
|
|
154
|
+
readonly contentType: string;
|
|
155
|
+
readonly clientIp: string;
|
|
156
|
+
}): PunchoutEntryEnvelope {
|
|
157
|
+
const query: Record<string, string> = {};
|
|
158
|
+
for (const [name, value] of request.query) {
|
|
159
|
+
if (!(name in query)) {
|
|
160
|
+
query[name] = value;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const headers: Record<string, string> = {};
|
|
165
|
+
for (const [name, value] of request.headers) {
|
|
166
|
+
const key = name.toLowerCase();
|
|
167
|
+
if (!DROPPED_HEADERS.has(key) && !(key in headers)) {
|
|
168
|
+
headers[key] = value;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return {
|
|
173
|
+
method: request.method.toUpperCase(),
|
|
174
|
+
query,
|
|
175
|
+
headers,
|
|
176
|
+
// Base64 rather than text: re-encoding a cXML or IDS document breaks
|
|
177
|
+
// signatures and encodings, and Secure OCI's HMAC is computed over the
|
|
178
|
+
// bytes that were actually sent.
|
|
179
|
+
body_b64: request.body.byteLength ? Buffer.from(request.body).toString("base64") : "",
|
|
180
|
+
content_type: request.contentType,
|
|
181
|
+
client_ip: request.clientIp,
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* A fixed-window counter per source IP × host (ADR-0002 §6: 60/min, burst 20).
|
|
187
|
+
*
|
|
188
|
+
* Per Nitro process, deliberately: a Site runs one process per instance and
|
|
189
|
+
* this is the storefront's SHARE of a layered protection, not the protection
|
|
190
|
+
* itself — the WAF and bot mitigation at the BunkerWeb edge are the first
|
|
191
|
+
* line, and the punchout app counts refusals per account behind it. A shared
|
|
192
|
+
* counter would need a store this layer must not require to exist.
|
|
193
|
+
*/
|
|
194
|
+
export class PunchoutEntryRateLimiter {
|
|
195
|
+
private readonly windows = new Map<string, { started: number; count: number }>();
|
|
196
|
+
|
|
197
|
+
constructor(
|
|
198
|
+
private readonly perMinute: number,
|
|
199
|
+
private readonly burst: number,
|
|
200
|
+
) {}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Count one call. Answers how many seconds to wait when it is over the
|
|
204
|
+
* limit, and null while it is within it.
|
|
205
|
+
*/
|
|
206
|
+
check(key: string, now: number = Date.now()): number | null {
|
|
207
|
+
const window = this.windows.get(key);
|
|
208
|
+
if (!window || now - window.started >= 60_000) {
|
|
209
|
+
this.windows.set(key, { started: now, count: 1 });
|
|
210
|
+
this.sweep(now);
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
window.count += 1;
|
|
215
|
+
if (window.count <= this.perMinute + this.burst) {
|
|
216
|
+
return null;
|
|
217
|
+
}
|
|
218
|
+
return Math.max(1, Math.ceil((window.started + 60_000 - now) / 1000));
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Drop windows that have run out. Without it the map grows with every
|
|
223
|
+
* source address ever seen, which on a public address is the whole
|
|
224
|
+
* internet.
|
|
225
|
+
*/
|
|
226
|
+
private sweep(now: number): void {
|
|
227
|
+
if (this.windows.size < 1024) {
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
for (const [key, window] of this.windows) {
|
|
231
|
+
if (now - window.started >= 60_000) {
|
|
232
|
+
this.windows.delete(key);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* The answer to a call this layer turns away itself — over the body cap, or
|
|
240
|
+
* over the rate limit.
|
|
241
|
+
*
|
|
242
|
+
* ADR-0002 §6 asks for these to be protocol-shaped. They are not, and cannot
|
|
243
|
+
* be here: a pass-through that minted a `PunchOutSetupResponse` would have to
|
|
244
|
+
* know what cXML is, which is exactly what §3 and §8 forbid it to know, and
|
|
245
|
+
* the shape of a refusal is something only the punchout app can decide (it
|
|
246
|
+
* keeps the reason and answers every protocol in its own words). So the
|
|
247
|
+
* refusal is the narrowest honest one — the right status, `Retry-After` where
|
|
248
|
+
* there is something to wait for, and a line of text, which is what the app
|
|
249
|
+
* itself answers for a refused OCI call.
|
|
250
|
+
*/
|
|
251
|
+
export function punchoutEntryRefusal(kind: "too-large" | "rate-limited", retryAfterSeconds?: number): {
|
|
252
|
+
readonly status: number;
|
|
253
|
+
readonly headers: Record<string, string>;
|
|
254
|
+
readonly body: string;
|
|
255
|
+
} {
|
|
256
|
+
if (kind === "too-large") {
|
|
257
|
+
return {
|
|
258
|
+
status: 413,
|
|
259
|
+
headers: { "content-type": "text/plain; charset=utf-8" },
|
|
260
|
+
body: "punchout: the entry call is larger than this address accepts",
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
return {
|
|
264
|
+
status: 429,
|
|
265
|
+
headers: {
|
|
266
|
+
"content-type": "text/plain; charset=utf-8",
|
|
267
|
+
...(retryAfterSeconds ? { "retry-after": String(retryAfterSeconds) } : {}),
|
|
268
|
+
},
|
|
269
|
+
body: "punchout: too many entry calls from this address",
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Read the answer the punchout app gave, defensively.
|
|
275
|
+
*
|
|
276
|
+
* Anything that is not the declared shape is a fault on our side of the
|
|
277
|
+
* gateway, not the ERP's, and must not be written out as if it were a protocol
|
|
278
|
+
* answer — the caller turns a null into a 502 of its own.
|
|
279
|
+
*/
|
|
280
|
+
export function punchoutEntryAnswer(raw: unknown): PunchoutEntryAnswer | null {
|
|
281
|
+
if (typeof raw !== "object" || raw === null) {
|
|
282
|
+
return null;
|
|
283
|
+
}
|
|
284
|
+
const answer = raw as Record<string, unknown>;
|
|
285
|
+
const status = Number(answer.status);
|
|
286
|
+
if (!Number.isInteger(status) || status < 100 || status > 599) {
|
|
287
|
+
return null;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const headers: Record<string, string> = {};
|
|
291
|
+
if (typeof answer.headers === "object" && answer.headers !== null) {
|
|
292
|
+
for (const [name, value] of Object.entries(answer.headers as Record<string, unknown>)) {
|
|
293
|
+
if (value != null) {
|
|
294
|
+
headers[name.toLowerCase()] = String(value);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
return {
|
|
300
|
+
status,
|
|
301
|
+
headers,
|
|
302
|
+
body: typeof answer.body === "string" ? answer.body : null,
|
|
303
|
+
psid: typeof answer.psid === "string" ? answer.psid : null,
|
|
304
|
+
start_page_url: typeof answer.start_page_url === "string" ? answer.start_page_url : null,
|
|
305
|
+
};
|
|
306
|
+
}
|