diff --git a/packages/sdks/core-js-sdk/src/httpClient/helpers/getClientSessionId.ts b/packages/sdks/core-js-sdk/src/httpClient/helpers/getClientSessionId.ts new file mode 100644 index 000000000..8aeabf69c --- /dev/null +++ b/packages/sdks/core-js-sdk/src/httpClient/helpers/getClientSessionId.ts @@ -0,0 +1,28 @@ +let sessionId: string; + +export const getClientSessionId = (): string => { + if (sessionId) { + return sessionId; + } + const currentDate = new Date(); + const utcString = `${currentDate.getUTCFullYear().toString()}-${( + currentDate.getUTCMonth() + 1 + ) + .toString() + .padStart(2, '0')}-${currentDate + .getUTCDate() + .toString() + .padStart(2, '0')}-${currentDate + .getUTCHours() + .toString() + .padStart(2, '0')}:${currentDate + .getUTCMinutes() + .toString() + .padStart(2, '0')}:${currentDate + .getUTCSeconds() + .toString() + .padStart(2, '0')}:${currentDate.getUTCMilliseconds().toString()}`; + const randomSuffix = Math.floor(1000 + Math.random() * 9000); + sessionId = `${utcString}-${randomSuffix}`; + return sessionId; +}; diff --git a/packages/sdks/core-js-sdk/src/httpClient/helpers/index.ts b/packages/sdks/core-js-sdk/src/httpClient/helpers/index.ts index 21de8535e..9bc7ae325 100644 --- a/packages/sdks/core-js-sdk/src/httpClient/helpers/index.ts +++ b/packages/sdks/core-js-sdk/src/httpClient/helpers/index.ts @@ -1,4 +1,5 @@ export { default as createFetchLogger } from './createFetchLogger'; +export { getClientSessionId } from './getClientSessionId'; export function transformSetCookie(setCookieHeader: string) { // Split the header by semicolons to separate different attributes diff --git a/packages/sdks/core-js-sdk/src/httpClient/index.ts b/packages/sdks/core-js-sdk/src/httpClient/index.ts index 889779812..e7f09db63 100644 --- a/packages/sdks/core-js-sdk/src/httpClient/index.ts +++ b/packages/sdks/core-js-sdk/src/httpClient/index.ts @@ -1,4 +1,4 @@ -import { transformSetCookie } from './helpers'; +import { getClientSessionId, transformSetCookie } from './helpers'; import createFetchLogger from './helpers/createFetchLogger'; import { CreateHttpClientConfig, @@ -35,6 +35,7 @@ declare const BUILD_VERSION: string; */ const createDescopeHeaders = () => { return { + 'x-descope-sdk-session-id': getClientSessionId(), 'x-descope-sdk-name': 'core-js', 'x-descope-sdk-version': BUILD_VERSION, }; diff --git a/packages/sdks/core-js-sdk/test/httpClient.test.ts b/packages/sdks/core-js-sdk/test/httpClient.test.ts index 26c5dc036..c163b981b 100644 --- a/packages/sdks/core-js-sdk/test/httpClient.test.ts +++ b/packages/sdks/core-js-sdk/test/httpClient.test.ts @@ -1,5 +1,6 @@ import { DEFAULT_BASE_API_URL } from '../src/constants'; import createHttpClient from '../src/httpClient'; +import { getClientSessionId } from '../src/httpClient/helpers'; import createFetchLogger from '../src/httpClient/helpers/createFetchLogger'; import { ExtendedResponse } from '../src/httpClient/types'; @@ -11,6 +12,7 @@ const afterRequestHook = jest.fn(); const descopeHeaders = { 'x-descope-sdk-name': 'core-js', 'x-descope-sdk-version': globalThis.BUILD_VERSION, + 'x-descope-sdk-session-id': getClientSessionId(), }; const httpClient = createHttpClient({ @@ -160,8 +162,8 @@ describe('httpClient', () => { test2: '123', test: '123', Authorization: 'Bearer 456', + ...descopeHeaders, 'x-descope-sdk-name': 'lulu', - 'x-descope-sdk-version': globalThis.BUILD_VERSION, }), method: 'GET', }, @@ -205,6 +207,25 @@ describe('httpClient', () => { cookiePolicy: null, }); + httpClient.get('1/2/3/4', { + headers: { test2: '123' }, + queryParams: { test2: '123' }, + }); + + expect(mockFetch).toHaveBeenCalledWith( + 'http://descope.com/1/2/3/4?test2=123', + { + body: undefined, + headers: new Headers({ + test2: '123', + test: '123', + Authorization: 'Bearer 456', + ...descopeHeaders, + }), + method: 'GET', + }, + ); + httpClient.get('1/2/3', { headers: { test2: '123' }, queryParams: { test2: '123' }, diff --git a/packages/sdks/web-js-sdk/test/persistTokens.test.ts b/packages/sdks/web-js-sdk/test/persistTokens.test.ts index 3cf6e4a31..b3c8272aa 100644 --- a/packages/sdks/web-js-sdk/test/persistTokens.test.ts +++ b/packages/sdks/web-js-sdk/test/persistTokens.test.ts @@ -4,9 +4,16 @@ import createSdk from '../src/index'; import { authInfo } from './mocks'; import { createMockReturnValue } from './testUtils'; +globalThis.Headers = class Headers { + constructor(obj: object) { + return Object.assign({}, obj); + } +} as any; + const descopeHeaders = { 'x-descope-sdk-name': 'web-js', 'x-descope-sdk-version': global.BUILD_VERSION, + 'x-descope-sdk-session-id': expect.any(String), }; const mockFetch = jest.fn().mockReturnValueOnce(new Promise(() => {}));