Skip to content

Commit 8c7fb8b

Browse files
committed
addressing review comments - claude assisted
1 parent 81a0ad8 commit 8c7fb8b

6 files changed

Lines changed: 38 additions & 10 deletions

File tree

packages/b2c-tooling-sdk/src/auth/oauth-jwt.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
invalidateCachedOAuthToken,
2323
decodeJWT,
2424
} from './oauth.js';
25+
import {globalAuthMiddlewareRegistry, applyAuthRequestMiddleware, applyAuthResponseMiddleware} from './middleware.js';
2526

2627
/**
2728
* Configuration for JWT Bearer authentication.
@@ -325,15 +326,24 @@ export class JwtOAuthStrategy implements AuthStrategy {
325326
'[JwtOAuthStrategy] Sending JWT Bearer token request',
326327
);
327328

328-
const response = await fetch(tokenUrl, {
329+
// Build request object for middleware
330+
let request = new Request(tokenUrl, {
329331
method: 'POST',
330332
headers: {
331333
'Content-Type': 'application/x-www-form-urlencoded',
332-
// NO Authorization header - this is key difference from client_credentials
333334
},
334335
body: params.toString(),
335336
});
336337

338+
// Apply auth middleware (e.g., User-Agent)
339+
const middleware = globalAuthMiddlewareRegistry.getMiddleware();
340+
request = await applyAuthRequestMiddleware(request, middleware);
341+
342+
let response = await fetch(request);
343+
344+
// Apply auth response middleware
345+
response = await applyAuthResponseMiddleware(request, response, middleware);
346+
337347
if (!response.ok) {
338348
const errorText = await response.text();
339349
this.logger.error(

packages/b2c-tooling-sdk/src/clients/cdn-zones.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import createClient, {type Client} from 'openapi-fetch';
1717
import type {AuthStrategy} from '../auth/types.js';
1818
import {OAuthStrategy} from '../auth/oauth.js';
19+
import {JwtOAuthStrategy} from '../auth/oauth-jwt.js';
1920
import type {paths, components} from './cdn-zones.generated.js';
2021
import {createAuthMiddleware, createLoggingMiddleware} from './middleware.js';
2122
import {globalMiddlewareRegistry, type MiddlewareRegistry} from './middleware-registry.js';
@@ -219,8 +220,11 @@ export function createCdnZonesClient(
219220
const domainScopes = options?.readWrite ? CDN_ZONES_RW_SCOPES : CDN_ZONES_READ_SCOPES;
220221
const requiredScopes = config.scopes ?? [...domainScopes, buildTenantScope(config.tenantId)];
221222

222-
// If OAuth strategy, add required scopes; otherwise use as-is
223-
const scopedAuth = auth instanceof OAuthStrategy ? auth.withAdditionalScopes(requiredScopes) : auth;
223+
// If auth supports scopes, add required scopes; otherwise use as-is
224+
const scopedAuth =
225+
auth instanceof OAuthStrategy || auth instanceof JwtOAuthStrategy
226+
? auth.withAdditionalScopes(requiredScopes)
227+
: auth;
224228

225229
// Core middleware: auth first
226230
client.use(createAuthMiddleware(scopedAuth));

packages/b2c-tooling-sdk/src/clients/cip.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {createRequire} from 'node:module';
99
import protobuf from 'protobufjs';
1010
import type {AuthStrategy, FetchInit} from '../auth/types.js';
1111
import {OAuthStrategy} from '../auth/oauth.js';
12+
import {JwtOAuthStrategy} from '../auth/oauth-jwt.js';
1213
import {getLogger} from '../logging/logger.js';
1314
import {globalMiddlewareRegistry, type MiddlewareRegistry, type UnifiedMiddleware} from './middleware-registry.js';
1415

@@ -790,6 +791,7 @@ export class CipClient {
790791
*/
791792
export function createCipClient(config: CipClientConfig, auth: AuthStrategy): CipClient {
792793
const cipScope = `SALESFORCE_COMMERCE_API:${config.instance}`;
793-
const scopedAuth = auth instanceof OAuthStrategy ? auth.withAdditionalScopes([cipScope]) : auth;
794+
const scopedAuth =
795+
auth instanceof OAuthStrategy || auth instanceof JwtOAuthStrategy ? auth.withAdditionalScopes([cipScope]) : auth;
794796
return new CipClient(config, scopedAuth);
795797
}

packages/b2c-tooling-sdk/src/clients/custom-apis.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import createClient, {type Client} from 'openapi-fetch';
1616
import type {AuthStrategy} from '../auth/types.js';
1717
import {OAuthStrategy} from '../auth/oauth.js';
18+
import {JwtOAuthStrategy} from '../auth/oauth-jwt.js';
1819
import type {paths, components} from './custom-apis.generated.js';
1920
import {createAuthMiddleware, createLoggingMiddleware} from './middleware.js';
2021
import {globalMiddlewareRegistry, type MiddlewareRegistry} from './middleware-registry.js';
@@ -148,8 +149,11 @@ export function createCustomApisClient(config: CustomApisClientConfig, auth: Aut
148149
// Build required scopes: domain scope + tenant-specific scope
149150
const requiredScopes = config.scopes ?? [...CUSTOM_APIS_DEFAULT_SCOPES, buildTenantScope(config.tenantId)];
150151

151-
// If OAuth strategy, add required scopes; otherwise use as-is
152-
const scopedAuth = auth instanceof OAuthStrategy ? auth.withAdditionalScopes(requiredScopes) : auth;
152+
// If auth supports scopes, add required scopes; otherwise use as-is
153+
const scopedAuth =
154+
auth instanceof OAuthStrategy || auth instanceof JwtOAuthStrategy
155+
? auth.withAdditionalScopes(requiredScopes)
156+
: auth;
153157

154158
// Core middleware: auth first
155159
client.use(createAuthMiddleware(scopedAuth));

packages/b2c-tooling-sdk/src/clients/granular-replications.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {paths, components} from './granular-replications.generated.js';
99
import {createAuthMiddleware, createLoggingMiddleware, createRateLimitMiddleware} from './middleware.js';
1010
import {globalMiddlewareRegistry, type MiddlewareRegistry} from './middleware-registry.js';
1111
import {OAuthStrategy} from '../auth/oauth.js';
12+
import {JwtOAuthStrategy} from '../auth/oauth-jwt.js';
1213
import {buildTenantScope, toOrganizationId, normalizeTenantId} from './custom-apis.js';
1314

1415
export {toOrganizationId, normalizeTenantId, buildTenantScope};
@@ -88,7 +89,10 @@ export function createGranularReplicationsClient(
8889

8990
// Build required scopes: domain scope + tenant-specific scope
9091
const requiredScopes = config.scopes ?? ['sfcc.granular-replications.rw', buildTenantScope(config.tenantId)];
91-
const scopedAuth = auth instanceof OAuthStrategy ? auth.withAdditionalScopes(requiredScopes) : auth;
92+
const scopedAuth =
93+
auth instanceof OAuthStrategy || auth instanceof JwtOAuthStrategy
94+
? auth.withAdditionalScopes(requiredScopes)
95+
: auth;
9296

9397
client.use(createAuthMiddleware(scopedAuth));
9498

packages/b2c-tooling-sdk/src/clients/scapi-schemas.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import createClient, {type Client} from 'openapi-fetch';
1616
import type {AuthStrategy} from '../auth/types.js';
1717
import {OAuthStrategy} from '../auth/oauth.js';
18+
import {JwtOAuthStrategy} from '../auth/oauth-jwt.js';
1819
import type {paths, components} from './scapi-schemas.generated.js';
1920
import {createAuthMiddleware, createLoggingMiddleware} from './middleware.js';
2021
import {globalMiddlewareRegistry, type MiddlewareRegistry} from './middleware-registry.js';
@@ -185,8 +186,11 @@ export function createScapiSchemasClient(config: ScapiSchemasClientConfig, auth:
185186
// Build required scopes: domain scope + tenant-specific scope
186187
const requiredScopes = config.scopes ?? [...SCAPI_SCHEMAS_DEFAULT_SCOPES, buildTenantScope(config.tenantId)];
187188

188-
// If OAuth strategy, add required scopes; otherwise use as-is
189-
const scopedAuth = auth instanceof OAuthStrategy ? auth.withAdditionalScopes(requiredScopes) : auth;
189+
// If auth supports scopes, add required scopes; otherwise use as-is
190+
const scopedAuth =
191+
auth instanceof OAuthStrategy || auth instanceof JwtOAuthStrategy
192+
? auth.withAdditionalScopes(requiredScopes)
193+
: auth;
190194

191195
// Core middleware: auth first
192196
client.use(createAuthMiddleware(scopedAuth));

0 commit comments

Comments
 (0)