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 support for specifying custom logger #11

Merged
merged 2 commits into from
Jan 16, 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 @@ -60,6 +60,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) => any` | Invoked when Casbin's `enforce` resolves to false | Returns a `403 Forbidden` error |
| `log` | `(Fastify, Request, sub, obj, act => 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'` |

Note that extraction rules defined within route options take precedence over the rules defined in the plugin options.
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@
"http-errors": "^1.8.0"
},
"devDependencies": {
"@types/node": "^14.14.6",
"fastify": "^3.7.0",
"@types/node": "^14.14.21",
"fastify": "^3.10.1",
"pre-commit": "^1.2.2",
"sinon": "^9.2.1",
"sinon": "^9.2.3",
"snazzy": "^9.0.0",
"standard": "^16.0.1",
"tap": "^14.10.8",
"tsd": "^0.13.1",
"typescript": "^4.0.5"
"standard": "^16.0.3",
"tap": "^14.11.0",
"tsd": "^0.14.0",
"typescript": "^4.1.3"
}
}
3 changes: 2 additions & 1 deletion plugin.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <reference types="node" />

import { FastifyPluginAsync, FastifyReply, FastifyRequest } from 'fastify'
import { FastifyInstance, FastifyPluginAsync, FastifyReply, FastifyRequest } from 'fastify'

declare module 'fastify' {
interface RouteShorthandOptions {
Expand All @@ -25,6 +25,7 @@ export interface FastifyCasbinRestOptions {
getObj?(request: FastifyRequest): string
getAct?(request: FastifyRequest): string
onDeny?(reply: FastifyReply, sub: string, obj: string, act: string): void
log?(fastify: FastifyInstance, request: FastifyRequest, sub: string, obj: string, act: string): void
hook?: Hook
}

Expand Down
5 changes: 3 additions & 2 deletions plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ const defaultOptions = {
onDeny: (reply, sub, obj, act) => {
throw new Forbidden(`${sub} not allowed to ${act} ${obj}`)
},
log: (fastify, request, sub, obj, act) => { fastify.log.info({ sub, obj, act }, 'Invoking casbin enforce') },
hook: 'preHandler'
}

async function fastifyCasbinRest (fastify, options) {
options = { ...defaultOptions, ...options }
const { log } = options

fastify.addHook('onRoute', routeOptions => {
// add option to turn it on for all routes
Expand All @@ -39,8 +41,7 @@ async function fastifyCasbinRest (fastify, options) {
const obj = getObj(request)
const act = getAct(request)

fastify.log.info({ sub, obj, act }, 'Invoking casbin enforce')

log(fastify, request, sub, obj, act)
if (!(await fastify.casbin.enforce(sub, obj, act))) {
await options.onDeny(reply, sub, obj, act)
}
Expand Down
33 changes: 32 additions & 1 deletion test/casbinRest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ test('throws if no casbin decorator exists', t => {
fastify.register(plugin)

fastify.ready(err => {
t.is(err.message, "The decorator 'casbin' is not present in Fastify")
t.is(err.message, "The decorator 'casbin' required by 'fastify-casbin-rest' is not present in Fastify")

fastify.close()
})
Expand Down Expand Up @@ -205,6 +205,37 @@ test('supports specifying custom hooks', t => {
})
})

test('supports specifying custom logger', t => {
t.plan(5)

const fastify = Fastify()
fastify.register(makeStubCasbin())
fastify.register(plugin, {
log: (fastify, request, sub, obj, act) => {
t.equal(sub, 'a')
t.equal(obj, 'b')
t.equal(act, 'c')
},
getSub: _request => 'a',
getObj: _request => 'b',
getAct: _request => 'c'
})

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

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

fastify.casbin.enforce.resolves(true)

t.equal((await fastify.inject('/')).statusCode, 200)

fastify.close()
})
})

test('supports overriding plugin rules on route level', t => {
t.plan(4)

Expand Down
1 change: 1 addition & 0 deletions test/plugin.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const server = fastify()
server.register(casbinRest)

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