Skip to content

Commit 650db88

Browse files
authored
feat: implement enableAcceptJsonRequest API (#497)
1 parent 2de2dba commit 650db88

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

src/coreEnforcer.ts

+24-3
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export class CoreEnforcer {
6161
protected autoSave = true;
6262
protected autoBuildRoleLinks = true;
6363
protected autoNotifyWatcher = true;
64+
protected acceptJsonRequest = false;
6465
protected fs?: FileSystem;
6566

6667
/**
@@ -351,6 +352,15 @@ export class CoreEnforcer {
351352
this.autoNotifyWatcher = enable;
352353
}
353354

355+
/**
356+
* enableAcceptJsonRequest determines whether to attempt parsing request args as JSON
357+
*
358+
* @param enable whether to attempt parsing request args as JSON
359+
*/
360+
public enableAcceptJsonRequest(enable: boolean): void {
361+
this.acceptJsonRequest = enable;
362+
}
363+
354364
/**
355365
* enableAutoBuildRoleLinks controls whether to save a policy rule
356366
* automatically to the adapter when it is added or removed.
@@ -477,9 +487,20 @@ export class CoreEnforcer {
477487
throw new Error(`invalid request size: expected ${rTokensLen}, got ${rvals.length}, rvals: ${rvals}"`);
478488
}
479489

480-
rTokens.forEach((token, j) => {
481-
parameters[token] = rvals[j];
482-
});
490+
if (this.acceptJsonRequest) {
491+
// Attempt to parse each request parameter as JSON; continue with string if failed
492+
rTokens.forEach((token, j) => {
493+
try {
494+
parameters[token] = JSON.parse(rvals[j]);
495+
} catch {
496+
parameters[token] = rvals[j];
497+
}
498+
});
499+
} else {
500+
rTokens.forEach((token, j) => {
501+
parameters[token] = rvals[j];
502+
});
503+
}
483504

484505
p?.tokens.forEach((token, j) => {
485506
parameters[token] = p?.policy[i][j];

test/enforcer.test.ts

+32
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,10 @@ class TestSub {
560560
this.Name = name;
561561
this.Age = age;
562562
}
563+
564+
toJSONString(): string {
565+
return JSON.stringify(this);
566+
}
563567
}
564568

565569
test('test ABAC Scaling', async () => {
@@ -837,3 +841,31 @@ test('TestEnforceWithMatcher', async () => {
837841
expect(await e.enforceWithMatcher(m2, 'data2_admin', 'data1', 'read')).toBe(true);
838842
expect(await e.enforceWithMatcher(m2, 'data2_admin', 'data1', 'write')).toBe(true);
839843
});
844+
845+
test('TestEnforceWithEnableAcceptJsonRequest', async () => {
846+
const e = await newEnforcer('examples/abac_rule_model.conf', 'examples/abac_rule_policy.csv');
847+
e.enableAcceptJsonRequest(true);
848+
849+
const sub1 = new TestSub('alice', 16).toJSONString();
850+
const sub2 = new TestSub('alice', 20).toJSONString();
851+
const sub3 = new TestSub('alice', 65).toJSONString();
852+
853+
await testEnforce(e, sub1, '/data1', 'read', false);
854+
await testEnforce(e, sub1, '/data2', 'read', false);
855+
await testEnforce(e, sub1, '/data1', 'write', false);
856+
await testEnforce(e, sub1, '/data2', 'write', true);
857+
await testEnforce(e, sub2, '/data1', 'read', true);
858+
await testEnforce(e, sub2, '/data2', 'read', false);
859+
await testEnforce(e, sub2, '/data1', 'write', false);
860+
await testEnforce(e, sub2, '/data2', 'write', true);
861+
await testEnforce(e, sub3, '/data1', 'read', true);
862+
await testEnforce(e, sub3, '/data2', 'read', false);
863+
await testEnforce(e, sub3, '/data1', 'write', false);
864+
await testEnforce(e, sub3, '/data2', 'write', false);
865+
866+
e.enableAcceptJsonRequest(false);
867+
await testEnforce(e, sub1, '/data2', 'write', false);
868+
await testEnforce(e, sub2, '/data1', 'read', false);
869+
await testEnforce(e, sub2, '/data2', 'write', false);
870+
await testEnforce(e, sub3, '/data1', 'read', false);
871+
});

0 commit comments

Comments
 (0)