diff --git a/CHANGELOG.md b/CHANGELOG.md index 46670ed..e1fb563 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,18 @@ ## Unreleased +## 3.1.0 + +**Housekeeping:** + +- Slim down the default request context to `headers`, `ip`, and `library`. The + client id is already carried by `headers` (the `x-castle-client-id` header / + `__cid` cookie) and resolved by Castle server-side, so the SDK no longer + derives it separately. +- Remove the internal client-id extraction service and the now-unused cookie + plumbing (`HeadersGetCookieService`) in `ContextGetDefaultService` / + `ContextPrepareService`. + ## 3.0.0 **BREAKING CHANGES:** diff --git a/package.json b/package.json index 37a8adf..a75b525 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "$schema": "http://json.schemastore.org/package", "name": "@castleio/sdk", "description": "Castle SDK for Node", - "version": "3.0.0", + "version": "3.1.0", "main": "./dist/index.js", "module": "./dist/index.mjs", "types": "./dist/index.d.ts", diff --git a/src/client-id/client-id.module.ts b/src/client-id/client-id.module.ts deleted file mode 100644 index e371345..0000000 --- a/src/client-id/client-id.module.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './services'; diff --git a/src/client-id/services/client-id-extract.service.ts b/src/client-id/services/client-id-extract.service.ts deleted file mode 100644 index b44eb51..0000000 --- a/src/client-id/services/client-id-extract.service.ts +++ /dev/null @@ -1,12 +0,0 @@ -import type { IncomingHttpHeaders } from 'http2'; -import { HeadersGetCookieService } from '../../headers/headers.module'; - -export const ClientIdExtractService = { - call: (headers: IncomingHttpHeaders = {}, cookies?: string | string[]) => { - return ( - headers['x-castle-client-id'] || - HeadersGetCookieService.call(cookies, '__cid') || - '' - ); - }, -}; diff --git a/src/client-id/services/index.ts b/src/client-id/services/index.ts deleted file mode 100644 index 049b4ed..0000000 --- a/src/client-id/services/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './client-id-extract.service'; diff --git a/src/context/services/context-get-default.service.ts b/src/context/services/context-get-default.service.ts index 8e557fd..3860f18 100644 --- a/src/context/services/context-get-default.service.ts +++ b/src/context/services/context-get-default.service.ts @@ -1,43 +1,19 @@ -import { isEmpty, pickByTruthy } from '../../utils/object'; import { Configuration } from '../../configuration'; -import { ClientIdExtractService } from '../../client-id/client-id.module'; import { HeadersExtractService } from '../../headers/headers.module'; import { IPsExtractService } from '../../ips/ips.module'; import { version } from '../../../package.json'; import type { IncomingHttpHeaders } from 'http2'; -const requestContextData = ( - request: { headers: IncomingHttpHeaders }, - cookies: string | undefined, - configuration: Configuration -): { [key: string]: any } => { - if (isEmpty(request)) { - return {}; - } - - const cookiesForClientId = - cookies || (request.headers as { [key: string]: any })?.cookies; - return { - client_id: - ClientIdExtractService.call(request.headers, cookiesForClientId) || false, - active: true, - headers: HeadersExtractService.call(request.headers, configuration), - ip: IPsExtractService.call(request.headers, configuration), - }; -}; - export const ContextGetDefaultService = { call: ( request: { headers: IncomingHttpHeaders }, - cookies: string | undefined, configuration: Configuration - ): { [key: string]: any } => { - return { - ...pickByTruthy(requestContextData(request, cookies, configuration)), - library: { - name: 'castle-node', - version, - }, - }; - }, + ): { [key: string]: any } => ({ + headers: HeadersExtractService.call(request.headers, configuration), + ip: IPsExtractService.call(request.headers, configuration), + library: { + name: 'castle-node', + version, + }, + }), }; diff --git a/src/context/services/context-prepare.service.ts b/src/context/services/context-prepare.service.ts index fe67d76..c53bd08 100644 --- a/src/context/services/context-prepare.service.ts +++ b/src/context/services/context-prepare.service.ts @@ -11,7 +11,6 @@ export const ContextPrepareService = { ) => { const defaultContext = ContextGetDefaultService.call( request, - options?.cookies, configuration ); return deepMerge(defaultContext, options?.context); diff --git a/src/headers/services/headers-get-cookie.service.ts b/src/headers/services/headers-get-cookie.service.ts deleted file mode 100644 index d3695df..0000000 --- a/src/headers/services/headers-get-cookie.service.ts +++ /dev/null @@ -1,20 +0,0 @@ -export const HeadersGetCookieService = { - call: ( - cookies: string | string[] | undefined, - name: string - ): string | undefined => { - if (!cookies) { - return; - } - - const pattern = new RegExp(`(?:^|\\s+)\\s?${name}=(.*?)(?:;|$)`); - const results = cookies.toString().match(pattern); - - if (results && results.length === 2) { - // return the last match - return results[1]; - } - - return; - }, -}; diff --git a/src/headers/services/index.ts b/src/headers/services/index.ts index 9508f73..48cb18f 100644 --- a/src/headers/services/index.ts +++ b/src/headers/services/index.ts @@ -1,2 +1 @@ export * from './headers-extract.service'; -export * from './headers-get-cookie.service'; diff --git a/test/client-id/services/client-id-extract.service.test.ts b/test/client-id/services/client-id-extract.service.test.ts deleted file mode 100644 index b3e4e70..0000000 --- a/test/client-id/services/client-id-extract.service.test.ts +++ /dev/null @@ -1,53 +0,0 @@ -import { ClientIdExtractService } from '../../../src/client-id/client-id.module'; - -describe('ClientIdExtractService', () => { - describe('call', () => { - const expectedFromCookie = 'abcd'; - const expectedFromHeader = 'abcde'; - - describe('with client_id', () => { - it('extracts client_id from cookie', () => { - const headers = { - cookie: '__cid=abcd;other=efgh', - 'x-forwarded-for': '1.2.3.4', - }; - const received = ClientIdExtractService.call(headers, headers.cookie); - expect(received).toEqual(expectedFromCookie); - }); - }); - - describe('with x-castle-client-id header', () => { - it('extracts client_id from cookie', () => { - const headers = { - cookie: '', - 'x-forwarded-for': '1.2.3.4', - 'x-castle-client-id': expectedFromHeader, - }; - const received = ClientIdExtractService.call(headers, headers.cookie); - expect(received).toEqual(expectedFromHeader); - }); - }); - - describe('with cookies and headers undefined', () => { - it('returns empty string', () => { - const headers = { - cookie: '', - }; - const received = ClientIdExtractService.call(headers, headers.cookie); - expect(received).toEqual(''); - }); - }); - - describe('with headers and cookie defined', () => { - it('extracts client_id from headers', () => { - const headers = { - cookie: '__cid=abcd;other=efgh', - 'x-forwarded-for': '1.2.3.4', - 'x-castle-client-id': expectedFromHeader, - }; - const received = ClientIdExtractService.call(headers, headers.cookie); - expect(received).toEqual(expectedFromHeader); - }); - }); - }); -}); diff --git a/test/context/services/context-get-default.service.test.ts b/test/context/services/context-get-default.service.test.ts index eb0e8e7..0be33d7 100644 --- a/test/context/services/context-get-default.service.test.ts +++ b/test/context/services/context-get-default.service.test.ts @@ -14,7 +14,6 @@ describe('ContextGetDefaultService', () => { name: 'castle-node', version, }, - client_id: 'client_id', ip: '1.2.3.4', }; @@ -29,17 +28,12 @@ describe('ContextGetDefaultService', () => { headers: { 'x-forwarded-for': '1.2.3.4', 'x-castle-client-id': 'client_id', - cookies: 'client_id', }, body: {}, } as ExpressRequest; it('generates default context', () => { - const received = ContextGetDefaultService.call( - mockRequest, - undefined, - config - ); + const received = ContextGetDefaultService.call(mockRequest, config); expect(received).toMatchObject(expected); }); }); diff --git a/test/context/services/context-prepare.service.test.ts b/test/context/services/context-prepare.service.test.ts index ad44636..0f1e554 100644 --- a/test/context/services/context-prepare.service.test.ts +++ b/test/context/services/context-prepare.service.test.ts @@ -22,7 +22,6 @@ describe('ContextPrepareService', () => { }); const options = { - cookies: '__cid=abcd;', context: { client_id: 'client_id', active: true, diff --git a/test/headers/services/headers-get-cookie.service.test.ts b/test/headers/services/headers-get-cookie.service.test.ts deleted file mode 100644 index a66cd90..0000000 --- a/test/headers/services/headers-get-cookie.service.test.ts +++ /dev/null @@ -1,70 +0,0 @@ -import { HeadersGetCookieService } from '../../../src/headers/headers.module'; - -describe('HeadersGetCookieService', () => { - describe('call', () => { - it('doesnt get cookie from empty header', () => { - const headers = { - cookie: '', - }; - - expect( - HeadersGetCookieService.call(headers.cookie, '_key') - ).toBeUndefined(); - }); - - it('gets cookie with encoded value', () => { - const headers = { - cookie: - '__uid=21917; cfids187=; cf=\u002C\u003B; a_key=f195d29f3ee38e07bd95ae9c', - }; - - expect(HeadersGetCookieService.call(headers.cookie, 'cf')).toEqual(','); - }); - - it('gets cookie from the end', () => { - const headers = { - cookie: '__uid=21917; cfids187=; _key=f195d29f3ee38e07bd95ae9c', - }; - expect(HeadersGetCookieService.call(headers.cookie, '_key')).toEqual( - 'f195d29f3ee38e07bd95ae9c' - ); - }); - - it('gets cookie from the middle', () => { - const headers = { - cookie: - '__uid=21917; cfids187=; cf=abc; a_key=f195d29f3ee38e07bd95ae9c', - }; - expect(HeadersGetCookieService.call(headers.cookie, 'cf')).toEqual('abc'); - }); - - it('gets cookie from the start', () => { - const headers = { - cookie: - '__uid=21917; cfids187=; cf=abc; a_key=f195d29f3ee38e07bd95ae9c', - }; - expect(HeadersGetCookieService.call(headers.cookie, '__uid')).toEqual( - '21917' - ); - }); - - it('does not get cookie with similar prefix', () => { - const headers = { - cookie: '__uid=21917; cfids187=; a_key=f195d29f3ee38e07bd95ae9c', - }; - expect( - HeadersGetCookieService.call(headers.cookie, '_key') - ).toBeUndefined(); - }); - - it('doesnt get cookie from the middle when empty', () => { - const headers = { - cookie: - '__uid=21917; cfids187=; cf=abc; a_key=f195d29f3ee38e07bd95ae9c', - }; - expect(HeadersGetCookieService.call(headers.cookie, 'cfids187')).toEqual( - '' - ); - }); - }); -}); diff --git a/test/payload/services/payload-prepare-service.test.ts b/test/payload/services/payload-prepare-service.test.ts index 3fcb545..1f2a01a 100644 --- a/test/payload/services/payload-prepare-service.test.ts +++ b/test/payload/services/payload-prepare-service.test.ts @@ -11,7 +11,6 @@ describe('PayloadPrepareService', () => { version, }, client_id: '123', - active: true, }, user_id: '123', };