From 7868bc81a3e7d2fb7768b7ed3e62d9057e7b71fe Mon Sep 17 00:00:00 2001 From: James Campbell Date: Thu, 7 Nov 2024 16:47:42 +0700 Subject: [PATCH 1/2] Parse context parameters in JSON API condition --- .../taco/src/conditions/context/context.ts | 21 ++++++++++ packages/taco/test/taco.test.ts | 38 ++++++++++++++++++- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/packages/taco/src/conditions/context/context.ts b/packages/taco/src/conditions/context/context.ts index 116d04af..ffb5b0da 100644 --- a/packages/taco/src/conditions/context/context.ts +++ b/packages/taco/src/conditions/context/context.ts @@ -18,6 +18,7 @@ import { CONTEXT_PARAM_PREFIX, USER_ADDRESS_PARAMS, } from '../const'; +import { JsonApiConditionType } from '../schemas/json-api'; export type CustomContextParam = string | number | boolean; export type ContextParam = CustomContextParam | AuthSignature; @@ -178,6 +179,26 @@ export class ConditionContext { } } } + // If it's a JSON API condition, check url and query + if (condition.conditionType === JsonApiConditionType) { + const urlComponents = condition.endpoint.replace("https://", "").split("/"); + for (const param of urlComponents ?? []) { + if (this.isContextParameter(param)) { + requestedParameters.add(param); + } + } + if (condition.query) { + const queryParams = condition.query.match(":[a-zA-Z_]*"); + if (queryParams) { + for (const param of queryParams) { + requestedParameters.add(param); + } + } + } + if (this.isContextParameter(condition.authorizationToken)) { + requestedParameters.add(condition.authorizationToken); + } + } return requestedParameters; } diff --git a/packages/taco/test/taco.test.ts b/packages/taco/test/taco.test.ts index 5f8b59f3..539294bd 100644 --- a/packages/taco/test/taco.test.ts +++ b/packages/taco/test/taco.test.ts @@ -136,4 +136,40 @@ describe('taco', () => { new Set([customParamKey, USER_ADDRESS_PARAM_DEFAULT]), ); }); -}); + // test json api condition exposes requested parameters + it('jsonapi condition exposes requested parameters', async () => { + const mockedDkg = fakeDkgFlow(FerveoVariant.precomputed, 0, 4, 4); + const mockedDkgRitual = fakeDkgRitual(mockedDkg); + const provider = fakeProvider(aliceSecretKeyBytes); + const signer = provider.getSigner(); + const getFinalizedRitualSpy = mockGetActiveRitual(mockedDkgRitual); + + const jsonApiCondition = new conditions.base.jsonApi.JsonApiCondition({ + endpoint: 'https://api.example.com/:userId/data', + query: '$.data[?(@.owner == :userAddress)].value', + authorizationToken: ':authToken', + returnValueTest: { + comparator: '==', + value: true + } + }); + + const messageKit = await taco.encrypt( + provider, + domains.DEVNET, + message, + jsonApiCondition, + mockedDkg.ritualId, + signer, + ); + expect(getFinalizedRitualSpy).toHaveBeenCalled(); + + const conditionContext = ConditionContext.fromMessageKit(messageKit); + const requestedParameters = conditionContext.requestedContextParameters; + + // Verify all context parameters from endpoint, query and authToken are detected + expect(requestedParameters).toEqual( + new Set([':userId', ':userAddress', ':authToken']) + ); + })} +); From 9fac7b230638823657ac68f72fd33c2845dd84f8 Mon Sep 17 00:00:00 2001 From: James Campbell Date: Thu, 7 Nov 2024 10:55:45 +0100 Subject: [PATCH 2/2] Use + instead of * in regex * would match 0 - infinity Co-authored-by: KPrasch --- packages/taco/src/conditions/context/context.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/taco/src/conditions/context/context.ts b/packages/taco/src/conditions/context/context.ts index ffb5b0da..3f9cf1f6 100644 --- a/packages/taco/src/conditions/context/context.ts +++ b/packages/taco/src/conditions/context/context.ts @@ -188,7 +188,7 @@ export class ConditionContext { } } if (condition.query) { - const queryParams = condition.query.match(":[a-zA-Z_]*"); + const queryParams = condition.query.match(":[a-zA-Z_]+"); if (queryParams) { for (const param of queryParams) { requestedParameters.add(param);