{"id":21158157,"url":"https://github.com/node-casbin/hapi-authz","last_synced_at":"2025-07-09T12:33:45.471Z","repository":{"id":42871568,"uuid":"248787987","full_name":"node-casbin/hapi-authz","owner":"node-casbin","description":"hapi-authz is an authorization middleware for Hapi.js based on Casbin","archived":false,"fork":false,"pushed_at":"2023-08-08T05:46:21.000Z","size":194,"stargazers_count":3,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-25T03:04:32.259Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://casbin.org/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/node-casbin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-03-20T15:22:41.000Z","updated_at":"2024-03-13T14:35:29.000Z","dependencies_parsed_at":"2022-09-23T16:11:35.712Z","dependency_job_id":null,"html_url":"https://github.com/node-casbin/hapi-authz","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-casbin%2Fhapi-authz","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-casbin%2Fhapi-authz/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-casbin%2Fhapi-authz/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-casbin%2Fhapi-authz/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/node-casbin","download_url":"https://codeload.github.com/node-casbin/hapi-authz/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225550929,"owners_count":17487211,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-20T12:16:49.770Z","updated_at":"2024-11-20T12:16:50.442Z","avatar_url":"https://github.com/node-casbin.png","language":"TypeScript","readme":"# Hapi Authz\n\n[![NPM version][npm-image]][npm-url]\n[![NPM download][download-image]][download-url]\n[![codebeat badge](https://codebeat.co/badges/45934fbd-c678-4164-a05a-6098b0c96250)](https://codebeat.co/projects/github-com-node-casbin-hapi-authz-master)\n[![Build Status](https://travis-ci.org/node-casbin/hapi-authz.svg?branch=master)](https://travis-ci.org/github/node-casbin/hapi-authz)\n[![Release](https://img.shields.io/github/release/node-casbin/hapi-authz.svg)](https://github.com/node-casbin/hapi-authz/releases/latest)\n[![Discord](https://img.shields.io/discord/1022748306096537660?logo=discord\u0026label=discord\u0026color=5865F2)](https://discord.gg/S5UjpzGZjN)\n\n[npm-image]: https://img.shields.io/npm/v/@casbin/hapi-authz.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/@casbin/hapi-authz\n[download-image]: https://img.shields.io/npm/dm/@casbin/hapi-authz.svg?style=flat-square\n[download-url]: https://www.npmjs.com/package/@casbin/hapi-authz\n\nThis 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).\n\n## Installation\n\n```shell\nnpm i casbin @casbin/hapi-authz --save\n```\n\n## Integration\n\n- Register the plugin inside your index.js file.\n```javascript\nconst { newEnforcer } = require('casbin');\nconst hapiauthz = require('@casbin/hapi-authz');\n\n...\n\nconst init = async () =\u003e {\n    ...\n    const enforcer = await newEnforcer('model.conf', 'policy.csv') // replace with your model and policy file location\n\n    await server.register({  \n    plugin: hapiauthz.Hapiauthz,\n    options: {\n      newEnforcer: enforcer\n    }\n\n    ...\n  })\n}\n```\n\n## Use a customized authorizer\n\nThis package provides ``BasicAuthorizer``, which checks the Authorization header for the username.\nIf you want to use another authentication method like OAuth, you needs to extends ``BasicAuthorizer`` as below:\n\n```js\nclass MyAuthorizer extends hapiauthz.BasicAuthorizer {\n  constructor(request, enforcer) {\n    super(request, enforcer);\n  }\n\n  getUserName () {\n    const { username } = this.request.credentials.username\n    return username\n  }\n}\n\nconst init = async () =\u003e {\n    ...\n    const enforcer = await newEnforcer('model.conf', 'policy.csv') // replace with your model and policy file location\n\n    await server.register({  \n    plugin: hapiauthz.Hapiauthz,\n    options: {\n      newEnforcer: enforcer,\n      authorizer: (request, option) =\u003e new MyAuthorizer(request, option)\n    }\n\n    ...\n  })\n}\n```\n\n\n## How to control the access\n\nThe 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:\n\n1. ``subject``: the logged-on user name\n2. ``object``: the URL path for the web resource like \"dataset1/item1\"\n3. ``action``: HTTP method like GET, POST, PUT, DELETE, or the high-level actions you defined like \"read-file\", \"write-blog\"\n\n\nFor how to write authorization policy and other details, please refer to [the Casbin's documentation](https://casbin.org).\n\n## Getting Help\n\n- [Node-Casbin](https://github.com/casbin/node-casbin)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnode-casbin%2Fhapi-authz","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnode-casbin%2Fhapi-authz","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnode-casbin%2Fhapi-authz/lists"}