{"id":21158148,"url":"https://github.com/node-casbin/express-authz","last_synced_at":"2025-07-09T12:33:44.450Z","repository":{"id":46937395,"uuid":"144398704","full_name":"node-casbin/express-authz","owner":"node-casbin","description":"express-authz is an authorization middleware for Express.js based on Casbin","archived":false,"fork":false,"pushed_at":"2023-07-06T15:01:30.000Z","size":340,"stargazers_count":62,"open_issues_count":0,"forks_count":9,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-11-01T09:49:14.290Z","etag":null,"topics":["authorization","casbin","express","expressjs","middleware","node-casbin"],"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","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-08-11T15:57:52.000Z","updated_at":"2024-08-30T06:59:29.000Z","dependencies_parsed_at":"2024-06-21T14:18:24.488Z","dependency_job_id":"545cc4ef-935c-481d-a7be-dcf04bdd6ac2","html_url":"https://github.com/node-casbin/express-authz","commit_stats":{"total_commits":48,"total_committers":9,"mean_commits":5.333333333333333,"dds":0.6041666666666667,"last_synced_commit":"ac649cd5901417a22a9c802f84659df580a4c253"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-casbin%2Fexpress-authz","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-casbin%2Fexpress-authz/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-casbin%2Fexpress-authz/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-casbin%2Fexpress-authz/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/node-casbin","download_url":"https://codeload.github.com/node-casbin/express-authz/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225550885,"owners_count":17487197,"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":["authorization","casbin","express","expressjs","middleware","node-casbin"],"created_at":"2024-11-20T12:16:17.346Z","updated_at":"2024-11-20T12:16:18.252Z","avatar_url":"https://github.com/node-casbin.png","language":"TypeScript","readme":"# Express-Authz\n\n[![NPM version][npm-image]][npm-url]\n[![NPM download][download-image]][download-url]\n[![codebeat badge](https://codebeat.co/badges/d179eb87-cf80-4ddb-ac94-a72a564a2fda)](https://codebeat.co/projects/github-com-node-casbin-express-authz-master)\n[![GitHub Actions](https://github.com/node-casbin/express-authz/workflows/main/badge.svg)](https://github.com/node-casbin/express-authz/actions)\n[![Coverage Status](https://coveralls.io/repos/github/node-casbin/express-authz/badge.svg?branch=master)](https://coveralls.io/github/node-casbin/express-authz?branch=master)\n[![Release](https://img.shields.io/github/release/node-casbin/express-authz.svg)](https://github.com/node-casbin/express-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-express-authz.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/casbin-express-authz\n[download-image]: https://img.shields.io/npm/dm/casbin-express-authz.svg?style=flat-square\n[download-url]: https://npmjs.org/package/casbin-express-authz\n\nExpress-Authz is an authorization middleware for [Express](https://github.com/expressjs/express), it's based on `Node-Casbin`: [https://github.com/casbin/node-casbin](https://github.com/casbin/node-casbin).\n\n## Installation\n\n### use casbin v2.x\n\n```shell\nnpm install casbin@2 casbin-express-authz@1 --save\n```\n\n### use casbin v3.x\n\n```shell\nnpm install casbin@3 casbin-express-authz@2 --save\n```\n\nor you can simply use,\n\n```shell\nnpm install express casbin casbin-express-authz --save\n```\n\n## Usage with Basic HTTP Authentication\n\nBy default casbin-authz supports HTTP Basic Authentication of the form `Authentication: Basic {Base64Encoded(username:password)}`\n\n## Usage with Other HTTP Authentication\n\nTo use other HTTP Authentication like `Bearer/Digest` you can use a custom middleware to define the `res.locals.username` variable and casbin-authz will automatically pick up the value from the variable.\n\n```js\nconst { newEnforcer } = require('casbin');\nconst express = require('express');\nconst { authz } = require('casbin-express-authz');\n\nconst app = express();\nconst enforcer = await newEnforcer('examples/authz_model.conf', 'examples/authz_policy.csv');\n\n// set userinfo\napp.use((req, res, next) =\u003e {\n  res.locals.username = getUsernameFromToken(); // Your custom function for retrieving username\n  next();\n});\n\n// use authz middleware\napp.use(authz({ newEnforcer: enforcer }));\n\n// response\napp.use((req, res, next) =\u003e {\n  res.status(200).json({ status: 'OK' });\n});\n\napp.listen(3000);\n```\n\n### Usage with customized authorizer\n\nThis package provides `BasicAuthorizer`, it uses HTTP Basic Authentication as the authentication method. If you want to use another authentication method like OAuth, you needs to implement Authorizer as below:\n\n```typescript\nimport { Enforcer, newEnforcer } from 'casbin';\nimport { authz, Authorizer } from 'casbin-express-authz';\nimport * as express from 'express';\n\nconst app = express();\n\nclass MyAuthorizer implements Authorizer {\n  private e: Enforcer;\n\n  constructor(e: Enforcer) {\n    this.e = e;\n  }\n\n  checkPermission(): Promise\u003cboolean\u003e {\n    // do something\n    return true;\n  }\n}\nconst e = await newEnforcer('examples/authz_model.conf', 'examples/authz_policy.csv');\n\napp.use(\n  authz({\n    newEnforcer: e,\n    authorizer: new MyAuthorizer(e),\n  })\n);\n\napp.listen(3000);\n```\n\n### Usage with customized authorizer class\n\nWhen the authorizer needs the request and response object to check the permission, one can pass the constructor of the customized `Authorizer` class instead of an instance.\n\n```typescript\nimport { Enforcer, newEnforcer } from 'casbin';\nimport { authz, AuthorizerConstructor } from 'casbin-express-authz';\nimport { Request, Response } from 'express';\n\nconst app = express();\n\nclass MyAuthorizer implements Authorizer {\n  private e: Enforcer;\n  private req: Request;\n  private res: Respons;\n\n  constructor(req:Request, res:Respons, e: Enforcer) {\n    this.e = e;\n    this.req = req\n    this.res = res\n  }\n\n  checkPermission(): Promise\u003cboolean\u003e {\n    // do something\n    return true;\n  }\n}\nconst e = await newEnforcer('examples/authz_model.conf', 'examples/authz_policy.csv');\n\napp.use(\n  authz({\n    newEnforcer: e,\n    authorizer: MyAuthorizer,\n  })\n);\n\napp.listen(3000);\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\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\n## License\n\nThis project is licensed under the [Apache 2.0 license](LICENSE).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnode-casbin%2Fexpress-authz","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnode-casbin%2Fexpress-authz","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnode-casbin%2Fexpress-authz/lists"}