Skip to content

Commit 7ccc9a3

Browse files
authored
feat: Add default transform function for service bindings (#5195)
1 parent 555dbe1 commit 7ccc9a3

File tree

4 files changed

+74
-4
lines changed

4 files changed

+74
-4
lines changed

.changeset/neat-parents-search.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sap-cloud-sdk/connectivity': minor
3+
---
4+
5+
[New Functionality] Add transform function to create OAuth2ClientCredentials destinations from service bindings.

packages/connectivity/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ export {
5959
ServiceBindingTransformOptions,
6060
transformServiceBindingToDestination,
6161
getAllDestinationsFromDestinationService,
62-
getTenantId
62+
getTenantId,
63+
transformServiceBindingToClientCredentialsDestination
6364
} from './scp-cf';
6465

6566
export type {

packages/connectivity/src/scp-cf/destination/service-binding-to-destination.spec.ts

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { serviceToken } from '../token-accessor';
22
import { resolveServiceBinding } from '../environment-accessor/service-bindings';
33
import { decodeJwt } from '../jwt';
4-
import { transformServiceBindingToDestination } from './service-binding-to-destination';
4+
import {
5+
transformServiceBindingToClientCredentialsDestination,
6+
transformServiceBindingToDestination
7+
} from './service-binding-to-destination';
58

69
const services = {
710
destination: [
@@ -68,10 +71,10 @@ const services = {
6871
name: 'some-service1',
6972
label: 'some-service',
7073
tags: ['some-service'],
74+
url: 'some-service-url',
7175
credentials: {
7276
clientid: 'some-service-clientid',
73-
clientsecret: 'some-service-clientsecret',
74-
uri: 'some-service-uri'
77+
clientsecret: 'some-service-clientsecret'
7578
}
7679
}
7780
],
@@ -211,4 +214,45 @@ describe('service binding to destination', () => {
211214
'"The provided service binding of type some-service is not supported out of the box for destination transformation."'
212215
);
213216
});
217+
218+
it('transforms a generic service binding to a client credentials destination', async () => {
219+
const destination =
220+
await transformServiceBindingToClientCredentialsDestination(
221+
resolveServiceBinding('some-service')
222+
);
223+
expect(destination).toEqual(
224+
expect.objectContaining({
225+
url: 'some-service-url',
226+
name: 'some-service1',
227+
authentication: 'OAuth2ClientCredentials',
228+
authTokens: expect.arrayContaining([
229+
expect.objectContaining({
230+
value: 'access-token',
231+
type: 'bearer'
232+
})
233+
])
234+
})
235+
);
236+
});
237+
238+
it('transforms a generic service binding to a client credentials destination with custom url', async () => {
239+
const destination =
240+
await transformServiceBindingToClientCredentialsDestination(
241+
resolveServiceBinding('some-service'),
242+
{ url: 'some-custom-service-url' }
243+
);
244+
expect(destination).toEqual(
245+
expect.objectContaining({
246+
url: 'some-custom-service-url',
247+
name: 'some-service1',
248+
authentication: 'OAuth2ClientCredentials',
249+
authTokens: expect.arrayContaining([
250+
expect.objectContaining({
251+
value: 'access-token',
252+
type: 'bearer'
253+
})
254+
])
255+
})
256+
);
257+
});
214258
});

packages/connectivity/src/scp-cf/destination/service-binding-to-destination.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,26 @@ export async function transformServiceBindingToDestination(
5656
);
5757
}
5858

59+
/**
60+
* Convenience function to create a destination from the provided service binding.
61+
* Transforms a service binding to a destination of type OAuth2ClientCredentials.
62+
* If a JWT is provided as part of the options, the tenant in the JWT is used for the client credentials grant, else the provider tenant is used, wherever applicable.
63+
* @param service - The service binding to transform.
64+
* @param options - Options used to transform the service binding.
65+
* @returns A promise returning the transformed destination on success.
66+
*/
67+
export async function transformServiceBindingToClientCredentialsDestination(
68+
service: Service,
69+
options?: ServiceBindingTransformOptions & { url?: string }
70+
): Promise<Destination> {
71+
const token = await serviceToken(service, options);
72+
return buildClientCredentialsDestination(
73+
token,
74+
options?.url ?? service.url,
75+
service.name
76+
);
77+
}
78+
5979
async function aicoreToDestination(
6080
service: Service,
6181
options?: ServiceBindingTransformOptions

0 commit comments

Comments
 (0)