Skip to content

Commit

Permalink
chore: Improve client credentials cache and tenant retrieval (#4774)
Browse files Browse the repository at this point in the history
* chore: Improve client credentials cache and tenant retrieval

* fix tests

* fix tests

* Apply suggestions from code review

Co-authored-by: Deeksha Sinha <[email protected]>

* fix integration test

* improvements from review

---------

Co-authored-by: Deeksha Sinha <[email protected]>
  • Loading branch information
marikaner and deekshas8 authored Jun 28, 2024
1 parent 99b5009 commit 54a46a3
Show file tree
Hide file tree
Showing 16 changed files with 310 additions and 335 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-cherries-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sap-cloud-sdk/connectivity': minor
---

[Improvement] Use tenant ID instead of tenant-aware authentication URL for the client credentials cache.
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ describe('ClientCredentialsTokenCache', () => {
};

clientCredentialsTokenCache.cacheToken(
'https://url_valid',
'subscriber-tenant',
'clientid',
validToken
);

jest.advanceTimersByTime(oneHourInSeconds * 2 * 1000);

const valid = clientCredentialsTokenCache.getToken(
'https://url_valid',
'subscriber-tenant',
'clientid'
);

Expand All @@ -43,14 +43,14 @@ describe('ClientCredentialsTokenCache', () => {
};

clientCredentialsTokenCache.cacheToken(
'https://url_expired',
'subscriber-tenant',
'clientid',
expiredToken
);
jest.advanceTimersByTime(oneHourInSeconds * 2 * 1000);

const expired = clientCredentialsTokenCache.getToken(
'https://url_expired',
'subscriber-tenant',
'clientid'
);

Expand All @@ -70,21 +70,21 @@ describe('ClientCredentialsTokenCache', () => {
};

clientCredentialsTokenCache.cacheToken(
'https://url_valid',
'subscriber-tenant',
'clientid',
validToken
);

jest.advanceTimersByTime(oneHourInSeconds * 2 * 1000);

const invalid = clientCredentialsTokenCache.getToken(
'https://url_valid',
'subscriber-tenant',
undefined as any
);

expect(invalid).toBeUndefined();
expect(warn).toBeCalledWith(
'Cannot get cache key. The ClientId was undefined.'
expect(warn).toHaveBeenCalledWith(
'Cannot create cache key for client credentials token cache. The given client ID is undefined.'
);
});
});
30 changes: 20 additions & 10 deletions packages/connectivity/src/scp-cf/client-credentials-token-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ const logger = createLogger({
const ClientCredentialsTokenCache = (
cache: Cache<ClientCredentialsResponse>
) => ({
getToken: (url, clientId: string): ClientCredentialsResponse | undefined =>
cache.get(getCacheKey(url, clientId)),
getToken: (
tenantId: string | undefined,
clientId: string
): ClientCredentialsResponse | undefined =>
cache.get(getCacheKey(tenantId, clientId)),

cacheToken: (
url,
tenantId: string | undefined,
clientId: string,
token: ClientCredentialsResponse
): void => {
cache.set(getCacheKey(url, clientId), {
cache.set(getCacheKey(tenantId, clientId), {
entry: token,
expires: token.expires_in
? Date.now() + token.expires_in * 1000
Expand All @@ -33,20 +36,27 @@ const ClientCredentialsTokenCache = (

/** *
* @internal
* @param url - URL from where the token is fetched
* @param tenantId - The ID of the tenant to cache the token for.
* @param clientId - ClientId to fetch the token
* @returns the token
*/
export function getCacheKey(url: string, clientId: string): string | undefined {
if (!url) {
logger.warn('Cannot get cache key. The url was undefined.');
export function getCacheKey(
tenantId: string | undefined,
clientId: string
): string | undefined {
if (!tenantId) {
logger.warn(
'Cannot create cache key for client credentials token cache. The given tenant ID is undefined.'
);
return;
}
if (!clientId) {
logger.warn('Cannot get cache key. The ClientId was undefined.');
logger.warn(
'Cannot create cache key for client credentials token cache. The given client ID is undefined.'
);
return;
}
return [url, clientId].join(':');
return [tenantId, clientId].join(':');
}

/**
Expand Down
Loading

0 comments on commit 54a46a3

Please sign in to comment.