https://github.com/node-casbin/hapi-authz
hapi-authz is an authorization middleware for Hapi.js based on Casbin
https://github.com/node-casbin/hapi-authz
Last synced: 8 months ago
JSON representation
hapi-authz is an authorization middleware for Hapi.js based on Casbin
- Host: GitHub
- URL: https://github.com/node-casbin/hapi-authz
- Owner: node-casbin
- License: apache-2.0
- Created: 2020-03-20T15:22:41.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2023-08-08T05:46:21.000Z (over 2 years ago)
- Last Synced: 2024-10-25T03:04:32.259Z (over 1 year ago)
- Language: TypeScript
- Homepage: https://casbin.org/
- Size: 189 KB
- Stars: 3
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Hapi Authz
[![NPM version][npm-image]][npm-url]
[![NPM download][download-image]][download-url]
[](https://codebeat.co/projects/github-com-node-casbin-hapi-authz-master)
[](https://travis-ci.org/github/node-casbin/hapi-authz)
[](https://github.com/node-casbin/hapi-authz/releases/latest)
[](https://discord.gg/S5UjpzGZjN)
[npm-image]: https://img.shields.io/npm/v/@casbin/hapi-authz.svg?style=flat-square
[npm-url]: https://npmjs.org/package/@casbin/hapi-authz
[download-image]: https://img.shields.io/npm/dm/@casbin/hapi-authz.svg?style=flat-square
[download-url]: https://www.npmjs.com/package/@casbin/hapi-authz
This is a authorization middleware for [Hapi js](https://github.com/hapijs/hapi), and it is based on [Node-Casbin](https://github.com/casbin/node-casbin).
## Installation
```shell
npm i casbin @casbin/hapi-authz --save
```
## Integration
- Register the plugin inside your index.js file.
```javascript
const { newEnforcer } = require('casbin');
const hapiauthz = require('@casbin/hapi-authz');
...
const init = async () => {
...
const enforcer = await newEnforcer('model.conf', 'policy.csv') // replace with your model and policy file location
await server.register({
plugin: hapiauthz.Hapiauthz,
options: {
newEnforcer: enforcer
}
...
})
}
```
## Use a customized authorizer
This package provides ``BasicAuthorizer``, which checks the Authorization header for the username.
If you want to use another authentication method like OAuth, you needs to extends ``BasicAuthorizer`` as below:
```js
class MyAuthorizer extends hapiauthz.BasicAuthorizer {
constructor(request, enforcer) {
super(request, enforcer);
}
getUserName () {
const { username } = this.request.credentials.username
return username
}
}
const init = async () => {
...
const enforcer = await newEnforcer('model.conf', 'policy.csv') // replace with your model and policy file location
await server.register({
plugin: hapiauthz.Hapiauthz,
options: {
newEnforcer: enforcer,
authorizer: (request, option) => new MyAuthorizer(request, option)
}
...
})
}
```
## How to control the access
The authorization determines a request based on ``{subject, object, action}``, which means what ``subject`` can perform what ``action`` on what ``object``. In this plugin, the meanings are:
1. ``subject``: the logged-on user name
2. ``object``: the URL path for the web resource like "dataset1/item1"
3. ``action``: HTTP method like GET, POST, PUT, DELETE, or the high-level actions you defined like "read-file", "write-blog"
For how to write authorization policy and other details, please refer to [the Casbin's documentation](https://casbin.org).
## Getting Help
- [Node-Casbin](https://github.com/casbin/node-casbin)