Skip to content

Commit

Permalink
feat: Add default transform function for service bindings (#5195)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomfrenken authored Nov 21, 2024
1 parent 555dbe1 commit 7ccc9a3
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/neat-parents-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sap-cloud-sdk/connectivity': minor
---

[New Functionality] Add transform function to create OAuth2ClientCredentials destinations from service bindings.
3 changes: 2 additions & 1 deletion packages/connectivity/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ export {
ServiceBindingTransformOptions,
transformServiceBindingToDestination,
getAllDestinationsFromDestinationService,
getTenantId
getTenantId,
transformServiceBindingToClientCredentialsDestination
} from './scp-cf';

export type {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { serviceToken } from '../token-accessor';
import { resolveServiceBinding } from '../environment-accessor/service-bindings';
import { decodeJwt } from '../jwt';
import { transformServiceBindingToDestination } from './service-binding-to-destination';
import {
transformServiceBindingToClientCredentialsDestination,
transformServiceBindingToDestination
} from './service-binding-to-destination';

const services = {
destination: [
Expand Down Expand Up @@ -68,10 +71,10 @@ const services = {
name: 'some-service1',
label: 'some-service',
tags: ['some-service'],
url: 'some-service-url',
credentials: {
clientid: 'some-service-clientid',
clientsecret: 'some-service-clientsecret',
uri: 'some-service-uri'
clientsecret: 'some-service-clientsecret'
}
}
],
Expand Down Expand Up @@ -211,4 +214,45 @@ describe('service binding to destination', () => {
'"The provided service binding of type some-service is not supported out of the box for destination transformation."'
);
});

it('transforms a generic service binding to a client credentials destination', async () => {
const destination =
await transformServiceBindingToClientCredentialsDestination(
resolveServiceBinding('some-service')
);
expect(destination).toEqual(
expect.objectContaining({
url: 'some-service-url',
name: 'some-service1',
authentication: 'OAuth2ClientCredentials',
authTokens: expect.arrayContaining([
expect.objectContaining({
value: 'access-token',
type: 'bearer'
})
])
})
);
});

it('transforms a generic service binding to a client credentials destination with custom url', async () => {
const destination =
await transformServiceBindingToClientCredentialsDestination(
resolveServiceBinding('some-service'),
{ url: 'some-custom-service-url' }
);
expect(destination).toEqual(
expect.objectContaining({
url: 'some-custom-service-url',
name: 'some-service1',
authentication: 'OAuth2ClientCredentials',
authTokens: expect.arrayContaining([
expect.objectContaining({
value: 'access-token',
type: 'bearer'
})
])
})
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ export async function transformServiceBindingToDestination(
);
}

/**
* Convenience function to create a destination from the provided service binding.
* Transforms a service binding to a destination of type OAuth2ClientCredentials.
* 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.
* @param service - The service binding to transform.
* @param options - Options used to transform the service binding.
* @returns A promise returning the transformed destination on success.
*/
export async function transformServiceBindingToClientCredentialsDestination(
service: Service,
options?: ServiceBindingTransformOptions & { url?: string }
): Promise<Destination> {
const token = await serviceToken(service, options);
return buildClientCredentialsDestination(
token,
options?.url ?? service.url,
service.name
);
}

async function aicoreToDestination(
service: Service,
options?: ServiceBindingTransformOptions
Expand Down

0 comments on commit 7ccc9a3

Please sign in to comment.