Skip to content

Commit

Permalink
chore: Set default string as CC Token cache key when tenantId cannot …
Browse files Browse the repository at this point in the history
…be determined (#4922)

* set tenantcache

* fix log

* Changes from lint:fix

---------

Co-authored-by: cloud-sdk-js <[email protected]>
  • Loading branch information
deekshas8 and cloud-sdk-js authored Aug 16, 2024
1 parent fedf5df commit 188ced9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createLogger } from '@sap-cloud-sdk/util';
import { decodeJwt, decodeOrMakeJwt } from '../jwt';
import { decodeJwt, decodeOrMakeJwt, defaultTenantId } from '../jwt';
import { DestinationFetchOptions } from './destination-accessor-types';
import {
IsolationStrategy,
Expand All @@ -18,11 +18,6 @@ const logger = createLogger({
messageContext: 'register-destination'
});

/**
* @internal
*/
const defaultTenantId = 'provider-tenant';

/**
* Represents options to configure how a destination should be registered.
*/
Expand Down
17 changes: 17 additions & 0 deletions packages/connectivity/src/scp-cf/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ const logger = createLogger({
messageContext: 'jwt'
});

/**
* @internal
*/
export const defaultTenantId = 'provider-tenant';

function makeArray(val: string | string[] | undefined): string[] {
return val ? (Array.isArray(val) ? val : [val]) : [];
}
Expand All @@ -32,6 +37,18 @@ export function userId({ user_id }: JwtPayload): string {
return user_id;
}

/**
* @internal
* Get the default tenant ID.
* @returns The default tenant ID.
*/
export function getDefaultTenantId(): string {
logger.debug(
'Could not determine tenant from JWT nor XSUAA, identity or destination service binding. Client Credentials token is cached without tenant information.'
);
return defaultTenantId;
}

/**
* Get the tenant ID of a decoded JWT, based on its `zid` or if not available `app_tid` property.
* @param jwt - Token to read the tenant ID from.
Expand Down
9 changes: 7 additions & 2 deletions packages/connectivity/src/scp-cf/token-accessor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { ErrorWithCause } from '@sap-cloud-sdk/util';
import { JwtPayload } from './jsonwebtoken-type';
import { getSubdomain, getTenantId, getTenantIdFromBinding } from './jwt';
import {
getDefaultTenantId,
getSubdomain,
getTenantId,
getTenantIdFromBinding
} from './jwt';
import { CachingOptions } from './cache';
import { clientCredentialsTokenCache } from './client-credentials-token-cache';
import { resolveServiceBinding } from './environment-accessor';
Expand Down Expand Up @@ -39,7 +44,7 @@ export async function serviceToken(
const serviceCredentials = serviceBinding.credentials;
const tenantForCaching = options?.jwt
? getTenantId(options.jwt) || getSubdomain(options.jwt)
: getTenantIdFromBinding();
: getTenantIdFromBinding() || getDefaultTenantId();

if (opts.useCache) {
const cachedToken = clientCredentialsTokenCache.getToken(
Expand Down

0 comments on commit 188ced9

Please sign in to comment.