{"id":13564149,"url":"https://github.com/node-casbin/koa-authz","last_synced_at":"2025-04-12T00:31:41.890Z","repository":{"id":32895785,"uuid":"144095710","full_name":"node-casbin/koa-authz","owner":"node-casbin","description":"koa-authz is an authorization middleware for Koa2 based on Casbin","archived":false,"fork":false,"pushed_at":"2023-07-06T15:00:44.000Z","size":627,"stargazers_count":40,"open_issues_count":1,"forks_count":14,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-10-16T10:31:06.714Z","etag":null,"topics":["authorization","casbin","koa2","koajs","middleware","node-casbin"],"latest_commit_sha":null,"homepage":"https://casbin.org/","language":"JavaScript","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}},"created_at":"2018-08-09T03:18:48.000Z","updated_at":"2023-12-08T16:58:45.000Z","dependencies_parsed_at":"2024-01-19T07:13:09.343Z","dependency_job_id":"882f4aef-0b3a-4424-a6b9-caba5bffb438","html_url":"https://github.com/node-casbin/koa-authz","commit_stats":{"total_commits":36,"total_committers":6,"mean_commits":6.0,"dds":"0.36111111111111116","last_synced_commit":"bb38ecb392eabadae4ab975aa0db9c57183676e3"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-casbin%2Fkoa-authz","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-casbin%2Fkoa-authz/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-casbin%2Fkoa-authz/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-casbin%2Fkoa-authz/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/node-casbin","download_url":"https://codeload.github.com/node-casbin/koa-authz/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247270952,"owners_count":20911583,"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","koa2","koajs","middleware","node-casbin"],"created_at":"2024-08-01T13:01:27.240Z","updated_at":"2025-04-12T00:31:41.609Z","avatar_url":"https://github.com/node-casbin.png","language":"JavaScript","readme":"Koa-Authz \n====\n[![NPM version][npm-image]][npm-url]\n[![NPM download][download-image]][download-url]\n[![codebeat badge](https://codebeat.co/badges/9defa882-898c-4dcb-91a6-7e8f061ccaac)](https://codebeat.co/projects/github-com-node-casbin-koa-authz-master)\n[![Build Status](https://travis-ci.org/node-casbin/koa-authz.svg?branch=master)](https://travis-ci.org/node-casbin/koa-authz)\n[![Coverage Status](https://coveralls.io/repos/github/node-casbin/koa-authz/badge.svg?branch=master)](https://coveralls.io/github/node-casbin/koa-authz?branch=master)\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/koa-authz.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/koa-authz\n[download-image]: https://img.shields.io/npm/dm/koa-authz.svg?style=flat-square\n[download-url]: https://npmjs.org/package/koa-authz\n\nKoa-Authz is an authorization middleware for [Koa](https://github.com/koajs/koa), it's based on ``Node-Casbin``: [https://github.com/casbin/node-casbin](https://github.com/casbin/node-casbin).\n\n## Installation\n### use casbin v2.x\n```shell\nnpm install casbin@2 koa-authz@2 --save\n```\n\n### use casbin v3.x\n```shell\nnpm install casbin@3 koa-authz@3 --save\n```\n\n## Simple Example\n\n```js\nconst casbin = require('casbin')\nconst Koa = require('koa')\nconst app = new Koa()\nconst authz = require('koa-authz')\n\n// response\napp.use(async (ctx, next) =\u003e {\n  const start = new Date()\n  await next()\n  console.log(new Date() - start)\n})\n\n// use authz middleware\napp.use(authz({\n  newEnforcer: async() =\u003e {\n    // load the casbin model and policy from files, database is also supported.\n    const enforcer = await casbin.newEnforcer('authz_model.conf', 'authz_policy.csv')\n    return enforcer\n  }\n}))\n\n// reload routes\nconst router = require('koa-router')({prefix: '/user'})\nrouter.get('/', (ctx) =\u003e {\n  ctx.body = {name: 'Chalin', age: 26}\n})\nrouter.put('/', (ctx) =\u003e {\n  ctx.body = {status: 'success'}\n})\napp.use(router.routes(), router.allowedMethods())\n\napp.listen(3000)\n```\n\n## Use a customized authorizer\n\nThis package provides ``BasicAuthorizer``, it uses ``HTTP Basic Authentication`` as the authentication method.\nIf you want to use another authentication method like OAuth, you needs to extends ``BasicAuthorizer`` as below:\n\n```js\nclass MyAuthorizer extends BasicAuthorizer {\n  // override function\n  getUserName () {\n    const { username } = this.ctx.state.user\n    return username\n  }\n}\n\napp.use(authz({\n  newEnforcer: async () =\u003e {\n    // load the casbin model and policy from files, database is also supported.\n    const enforcer = await casbin.newEnforcer('examples/authz_model.conf', 'examples/authz_policy.csv')\n    return enforcer\n  },\n  authorizer: (ctx, option) =\u003e new MyAuthorizer(ctx, option)\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\n## License\n\nThis project is licensed under the [Apache 2.0 license](LICENSE).\n","funding_links":[],"categories":["JavaScript","仓库"],"sub_categories":["中间件"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnode-casbin%2Fkoa-authz","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnode-casbin%2Fkoa-authz","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnode-casbin%2Fkoa-authz/lists"}