Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for JSON request parsing in Enforcer #461

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/enforcer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { FieldIndex } from './constants';
* Enforcer = ManagementEnforcer + RBAC API.
*/
export class Enforcer extends ManagementEnforcer {
private acceptJsonRequest = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you only add a variable, no logic checks.

Is it correct?

/**
* initWithFile initializes an enforcer with a model file and a policy file.
* @param modelPath model file path
Expand Down Expand Up @@ -439,6 +440,14 @@ export class Enforcer extends ManagementEnforcer {
return this.getUsersForRole(name, domain);
}

/**
* Enable or disable accepting JSON requests for ABAC.
* @param enable Whether to enable or disable accepting JSON requests.
*/
public enableAcceptJsonRequest(enable: boolean): void {
this.acceptJsonRequest = enable;
}

/**
* getImplicitUsersForPermission gets implicit users for a permission.
* For example:
Expand Down
32 changes: 32 additions & 0 deletions test/enforcer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,38 @@ test('TestInitWithAdapter', async () => {
await testEnforce(e, 'bob', 'data2', 'write', true);
});

test('TestEnableAcceptJsonRequest', async () => {
const m = newModel();
const a = new FileAdapter('examples/keymatch_policy.csv');
const e = await newEnforcer(m, a);

// Enable JSON request parsing
e.enableAcceptJsonRequest(true);

// Testing with JSON request
const requestJson = '{"sub": "alice", "obj": "/alice_data/resource1", "act": "GET"}';
await testEnforce(e, JSON.parse(requestJson), '/alice_data/resource1', 'GET', true);
await testEnforce(e, JSON.parse(requestJson), '/alice_data/resource1', 'POST', true);
await testEnforce(e, JSON.parse(requestJson), '/alice_data/resource2', 'GET', true);
await testEnforce(e, JSON.parse(requestJson), '/alice_data/resource2', 'POST', false);
await testEnforce(e, JSON.parse(requestJson), '/bob_data/resource1', 'GET', false);
await testEnforce(e, JSON.parse(requestJson), '/bob_data/resource1', 'POST', false);
await testEnforce(e, JSON.parse(requestJson), '/bob_data/resource2', 'GET', false);
await testEnforce(e, JSON.parse(requestJson), '/bob_data/resource2', 'POST', false);

// Disabling JSON request parsing
e.enableAcceptJsonRequest(false);

await testEnforce(e, 'alice', '/alice_data/resource1', 'GET', true);
await testEnforce(e, 'alice', '/alice_data/resource1', 'POST', true);
await testEnforce(e, 'alice', '/alice_data/resource2', 'GET', true);
await testEnforce(e, 'alice', '/alice_data/resource2', 'POST', false);
await testEnforce(e, 'alice', '/bob_data/resource1', 'GET', false);
await testEnforce(e, 'alice', '/bob_data/resource1', 'POST', false);
await testEnforce(e, 'alice', '/bob_data/resource2', 'GET', false);
await testEnforce(e, 'alice', '/bob_data/resource2', 'POST', false);
});

test('TestInitWithStringAdapter', async () => {
const policy = readFileSync('examples/basic_policy.csv').toString();
const adapter = new StringAdapter(policy);
Expand Down
Loading