Skip to content

Commit 5cda06c

Browse files
committed
remove iasResource list support & fix tests
1 parent 08313ba commit 5cda06c

File tree

5 files changed

+31
-178
lines changed

5 files changed

+31
-178
lines changed

packages/connectivity/src/scp-cf/client-credentials-token-cache.spec.ts

Lines changed: 0 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -249,149 +249,4 @@ describe('ClientCredentialsTokenCache', () => {
249249
expect(key).toBe('tenant-123:client-id');
250250
});
251251
});
252-
253-
describe('Multiple resources (array) support', () => {
254-
const validToken = {
255-
access_token: 'multi-resource-token',
256-
token_type: 'Bearer',
257-
expires_in: oneHourInSeconds * 3,
258-
jti: '',
259-
scope: ''
260-
};
261-
262-
beforeEach(() => {
263-
clientCredentialsTokenCache.clear();
264-
});
265-
266-
it('should cache and retrieve token with array of resource clientIds', () => {
267-
const resources = [
268-
{ clientId: 'client-1', tenantId: 'tenant-1' },
269-
{ clientId: 'client-2' },
270-
{ name: 'app3' }
271-
];
272-
273-
clientCredentialsTokenCache.cacheToken(
274-
'subscriber-tenant',
275-
'clientid',
276-
resources,
277-
validToken
278-
);
279-
280-
const cached = clientCredentialsTokenCache.getToken(
281-
'subscriber-tenant',
282-
'clientid',
283-
resources
284-
);
285-
286-
expect(cached).toEqual(validToken);
287-
});
288-
it('should treat resource array as a set (order-independent)', () => {
289-
const resources1 = [{ name: 'app-1' }, { name: 'app-2' }];
290-
const resources2 = [{ name: 'app-2' }, { name: 'app-1' }];
291-
292-
clientCredentialsTokenCache.cacheToken(
293-
'subscriber-tenant',
294-
'clientid',
295-
resources1,
296-
validToken
297-
);
298-
299-
const cached1 = clientCredentialsTokenCache.getToken(
300-
'subscriber-tenant',
301-
'clientid',
302-
resources1
303-
);
304-
const cached2 = clientCredentialsTokenCache.getToken(
305-
'subscriber-tenant',
306-
'clientid',
307-
resources2
308-
);
309-
310-
expect(cached1).toEqual(validToken);
311-
expect(cached2).toEqual(validToken);
312-
});
313-
314-
it('should isolate cache by number of resources in array', () => {
315-
const resources1 = [{ name: 'app-1' }];
316-
const resources2 = [{ name: 'app-1' }, { name: 'app-2' }];
317-
318-
clientCredentialsTokenCache.cacheToken(
319-
'subscriber-tenant',
320-
'clientid',
321-
resources1,
322-
validToken
323-
);
324-
325-
const cached1 = clientCredentialsTokenCache.getToken(
326-
'subscriber-tenant',
327-
'clientid',
328-
resources1
329-
);
330-
const cached2 = clientCredentialsTokenCache.getToken(
331-
'subscriber-tenant',
332-
'clientid',
333-
resources2
334-
);
335-
336-
expect(cached1).toEqual(validToken);
337-
expect(cached2).toBeUndefined();
338-
});
339-
340-
it('should not treat single resource and single-element array differently', () => {
341-
const singleResource = { name: 'app-1' };
342-
const arrayResource = [{ name: 'app-1' }];
343-
344-
clientCredentialsTokenCache.cacheToken(
345-
'subscriber-tenant',
346-
'clientid',
347-
singleResource,
348-
validToken
349-
);
350-
351-
const cachedSingle = clientCredentialsTokenCache.getToken(
352-
'subscriber-tenant',
353-
'clientid',
354-
singleResource
355-
);
356-
const cachedArray = clientCredentialsTokenCache.getToken(
357-
'subscriber-tenant',
358-
'clientid',
359-
arrayResource
360-
);
361-
362-
expect(cachedSingle).toEqual(validToken);
363-
expect(cachedArray).toEqual(validToken);
364-
});
365-
366-
it('should generate correct cache key with array of resource names', () => {
367-
const key = getCacheKey('tenant-123', 'client-id', [
368-
{ name: 'app-1' },
369-
{ name: 'app-2' }
370-
]);
371-
expect(key).toBe('tenant-123:client-id:name=app-1::name=app-2');
372-
});
373-
374-
it('should generate correct cache key with array of resource clientIds', () => {
375-
const key = getCacheKey('tenant-123', 'client-id', [
376-
{ clientId: 'client-1' },
377-
{ clientId: 'client-2', tenantId: 'tenant-2' }
378-
]);
379-
expect(key).toBe(
380-
'tenant-123:client-id:clientid=client-1::clientid=client-2:tenantid=tenant-2'
381-
);
382-
});
383-
384-
it('should generate correct cache key with array of mixed resource types', () => {
385-
const key = getCacheKey('tenant-123', 'client-id', [
386-
{ name: 'app-1' },
387-
{ clientId: 'client-2' }
388-
]);
389-
expect(key).toBe('tenant-123:client-id:clientid=client-2::name=app-1');
390-
});
391-
392-
it('should handle empty resource array', () => {
393-
const key = getCacheKey('tenant-123', 'client-id', []);
394-
expect(key).toBe('tenant-123:client-id');
395-
});
396-
});
397252
});

packages/connectivity/src/scp-cf/client-credentials-token-cache.ts

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ const ClientCredentialsTokenCache = (
1414
getToken: (
1515
tenantId: string | undefined,
1616
clientId: string,
17-
resource?: IasResource | IasResource[]
17+
resource?: IasResource
1818
): ClientCredentialsResponse | undefined =>
1919
cache.get(getCacheKey(tenantId, clientId, resource)),
2020

2121
cacheToken: (
2222
tenantId: string | undefined,
2323
clientId: string,
24-
resource: IasResource | IasResource[] | undefined,
24+
resource: IasResource | undefined,
2525
token: ClientCredentialsResponse
2626
): void => {
2727
cache.set(getCacheKey(tenantId, clientId, resource), {
@@ -43,29 +43,19 @@ const ClientCredentialsTokenCache = (
4343
* @returns Normalized resource string or empty string if not provided.
4444
* @internal
4545
*/
46-
function normalizeResource(
47-
resource?: IasResource | IasResource[]
48-
): string | undefined {
46+
function normalizeResource(resource?: IasResource): string | undefined {
4947
if (!resource) {
5048
return undefined;
5149
}
50+
if ('name' in resource) {
51+
return `name=${resource.name}`;
52+
}
5253

53-
const resources = Array.isArray(resource) ? resource : [resource];
54-
55-
return resources
56-
.map(r => {
57-
if ('name' in r) {
58-
return `name=${r.name}`;
59-
}
60-
61-
let normalized = `clientid=${r.clientId}`;
62-
if (r.tenantId) {
63-
normalized += `:tenantid=${r.tenantId}`;
64-
}
65-
return normalized;
66-
})
67-
.sort()
68-
.join('::');
54+
let normalized = `clientid=${resource.clientId}`;
55+
if (resource.tenantId) {
56+
normalized += `:tenantid=${resource.tenantId}`;
57+
}
58+
return normalized;
6959
}
7060

7161
/** *
@@ -78,7 +68,7 @@ function normalizeResource(
7868
export function getCacheKey(
7969
tenantId: string | undefined,
8070
clientId: string,
81-
resource?: IasResource | IasResource[]
71+
resource?: IasResource
8272
): string | undefined {
8373
if (!tenantId) {
8474
logger.warn(

packages/connectivity/src/scp-cf/destination/destination-from-vcap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ interface IasOptionsBase {
141141
* Either provide the app name (common case) or the provider client ID
142142
* and tenant ID (optional).
143143
*/
144-
resource?: IasResource | IasResource[];
144+
resource?: IasResource;
145145
/**
146146
* The consumer (BTP) tenant ID of the application.
147147
* May be required for multi-tenant communication.

packages/connectivity/src/scp-cf/identity-service.spec.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ describe('getIasClientCredentialsToken', () => {
9999
aud: 'test-audience',
100100
ias_apis: ['dummy']
101101
});
102-
expect(mockFetchClientCredentialsToken).toHaveBeenCalledWith({});
102+
expect(mockFetchClientCredentialsToken).toHaveBeenCalledWith({
103+
token_format: 'jwt'
104+
});
103105
});
104106

105107
it('fetches IAS token with client secret authentication', async () => {
@@ -125,7 +127,9 @@ describe('getIasClientCredentialsToken', () => {
125127
aud: 'test-audience',
126128
ias_apis: ['dummy']
127129
});
128-
expect(mockFetchClientCredentialsToken).toHaveBeenCalledWith({});
130+
expect(mockFetchClientCredentialsToken).toHaveBeenCalledWith({
131+
token_format: 'jwt'
132+
});
129133
});
130134

131135
it('includes resource parameter for app2app flow', async () => {
@@ -136,7 +140,8 @@ describe('getIasClientCredentialsToken', () => {
136140
});
137141

138142
expect(mockFetchClientCredentialsToken).toHaveBeenCalledWith({
139-
resource: 'urn:sap:identity:application:provider:name:my-app'
143+
resource: 'urn:sap:identity:application:provider:name:my-app',
144+
token_format: 'jwt'
140145
});
141146
});
142147

@@ -162,7 +167,8 @@ describe('getIasClientCredentialsToken', () => {
162167

163168
expect(mockFetchClientCredentialsToken).toHaveBeenCalledWith({
164169
resource: 'urn:sap:identity:application:provider:name:my-app',
165-
app_tid: 'tenant-123'
170+
app_tid: 'tenant-123',
171+
token_format: 'jwt'
166172
});
167173
});
168174

@@ -174,6 +180,7 @@ describe('getIasClientCredentialsToken', () => {
174180
});
175181

176182
expect(mockFetchClientCredentialsToken).toHaveBeenCalledWith({
183+
token_format: 'jwt',
177184
custom_param: 'custom_value'
178185
});
179186
});
@@ -222,7 +229,9 @@ describe('getIasClientCredentialsToken', () => {
222229
assertion: userAssertion
223230
});
224231

225-
expect(mockFetchJwtBearerToken).toHaveBeenCalledWith(userAssertion, {});
232+
expect(mockFetchJwtBearerToken).toHaveBeenCalledWith(userAssertion, {
233+
token_format: 'jwt'
234+
});
226235
expect(mockFetchClientCredentialsToken).not.toHaveBeenCalled();
227236
});
228237

@@ -254,7 +263,8 @@ describe('getIasClientCredentialsToken', () => {
254263
expect(mockFetchJwtBearerToken).toHaveBeenCalledWith(userAssertion, {
255264
resource: 'urn:sap:identity:application:provider:name:my-app',
256265
app_tid: 'tenant-123',
257-
refresh_token: '0'
266+
refresh_token: '0',
267+
token_format: 'jwt'
258268
});
259269
});
260270
});

packages/connectivity/src/scp-cf/identity-service.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,9 @@ async function getIasClientCredentialsTokenImpl(
146146
token_format: 'jwt'
147147
};
148148

149-
// Stringify resource(s)
149+
// Stringify resource
150150
if (arg.resource) {
151-
tokenOptions.resource = Array.isArray(arg.resource)
152-
? arg.resource.map(convertResourceToUrn)
153-
: convertResourceToUrn(arg.resource);
151+
tokenOptions.resource = convertResourceToUrn(arg.resource);
154152
}
155153

156154
if (arg.appTid) {

0 commit comments

Comments
 (0)