Skip to content

Commit

Permalink
Fix JSON API conditions (#602)
Browse files Browse the repository at this point in the history
  • Loading branch information
KPrasch authored Nov 7, 2024
2 parents 6b8184f + 9fac7b2 commit 63b0352
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
21 changes: 21 additions & 0 deletions packages/taco/src/conditions/context/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
38 changes: 37 additions & 1 deletion packages/taco/test/taco.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
);
})}
);

0 comments on commit 63b0352

Please sign in to comment.