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

Add onAllow callback support #20

Merged
merged 1 commit into from
Apr 21, 2021
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ The API exposed by this plugin is the configuration options:
| `getObj` | `Request => string` | Extracts `obj` from the request | `r => r.url` |
| `getAct` | `Request => string` | Extracts `act` from the request | `r => r.method` |
| `onDeny` | `(Reply, { sub, obj, act, dom }) => any` | Invoked when Casbin's `enforce` resolves to false | Returns a `403 Forbidden` error |
| `onAllow`| `(Reply, { sub, obj, act, dom }) => any` | Invoked when Casbin's `enforce` resolves to true | noop |
| `log` | `(Fastify, Request, { sub, obj, act, dom }) => void` | Invoked before invoking Casbin's `enforce` | Logs using fastify.log.info |
| `hook` | `'onRequest', 'preParsing', 'preValidation', 'preHandler'` | Which lifecycle to use for performing the check | `'preHandler'` |

Expand Down
1 change: 1 addition & 0 deletions plugin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export interface FastifyCasbinRestOptions {
getObj?(request: FastifyRequest): string
getAct?(request: FastifyRequest): string
onDeny?(reply: FastifyReply, checkParams: Permission): void
onAllow?(reply: FastifyReply, checkParams: Permission): void
log?(fastify: FastifyInstance, request: FastifyRequest, checkParams: Permission ): void
hook?: Hook
}
Expand Down
6 changes: 5 additions & 1 deletion plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ async function fastifyCasbinRest (fastify, options) {
const isAuthorized = getDom ? await fastify.casbin.enforce(sub, dom, obj, act) : await fastify.casbin.enforce(sub, obj, act)

if (!isAuthorized) {
await options.onDeny(reply, { sub, dom, obj, act })
return await options.onDeny(reply, { sub, dom, obj, act })
}

if (options.onAllow) {
Copy link
Contributor Author

@kibertoad kibertoad Apr 20, 2021

Choose a reason for hiding this comment

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

I believe that executing this conditionally should be faster for noop cases than invoking an empty function always, but I'm happy to change to always executing an empty function by default if you prefer that approach.

await options.onAllow(reply, { sub, dom, obj, act })
}
})
}
Expand Down
38 changes: 38 additions & 0 deletions test/casbinRest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,44 @@ test('allows route where plugin is enabled and enforce resolves true with dom re
})
})

test('invokes onAllow callback if defined', t => {
t.plan(9)

const fastify = Fastify()

fastify.register(makeStubCasbin())
fastify.register(plugin, {
onAllow: (reply, { sub, obj, act }) => {
t.equal(sub, undefined)
t.equal(obj, '/')
t.equal(act, 'GET')
}
})

fastify.get('/', {
casbin: {
rest: true
}
}, () => 'ok')

fastify.ready(async err => {
t.error(err)

fastify.casbin.enforce.callsFake((sub, obj, act) => {
t.equal(sub, undefined)
t.equal(obj, '/')
t.equal(act, 'GET')
return Promise.resolve(true)
})

t.equal((await fastify.inject('/')).body, 'ok')

t.ok(fastify.casbin.enforce.called)

fastify.close()
})
})

test('forbids route where plugin is enabled and enforce resolves false', t => {
t.plan(6)

Expand Down
6 changes: 6 additions & 0 deletions test/plugin.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ server.register(casbinRest)

server.register(casbinRest, {
log: (fastify, request, { sub, obj, act }) => { fastify.log.info({ sub, obj, act }, 'Invoking casbin enforce') },
onAllow: (reply, { sub, obj, act }) => {
expectType<FastifyReply>(reply)
expectType<string>(sub)
expectType<string>(obj)
expectType<string>(act)
},
onDeny: (reply, { sub, obj, act }) => {
expectType<FastifyReply>(reply)
expectType<string>(sub)
Expand Down