{"id":26482618,"url":"https://github.com/casbin-rs/poem-casbin","last_synced_at":"2025-06-12T18:32:40.410Z","repository":{"id":39118242,"uuid":"502951862","full_name":"casbin-rs/poem-casbin","owner":"casbin-rs","description":"Casbin Poem access control middleware ","archived":false,"fork":false,"pushed_at":"2022-07-10T03:27:15.000Z","size":14,"stargazers_count":7,"open_issues_count":0,"forks_count":3,"subscribers_count":6,"default_branch":"main","last_synced_at":"2025-04-05T19:51:12.273Z","etag":null,"topics":["abac","acl","auth","authz","casbin","middleware","poem","rbac","rust"],"latest_commit_sha":null,"homepage":"","language":"Rust","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/casbin-rs.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}},"created_at":"2022-06-13T12:42:39.000Z","updated_at":"2024-06-20T03:13:58.000Z","dependencies_parsed_at":"2022-07-12T17:43:40.684Z","dependency_job_id":null,"html_url":"https://github.com/casbin-rs/poem-casbin","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/casbin-rs/poem-casbin","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casbin-rs%2Fpoem-casbin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casbin-rs%2Fpoem-casbin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casbin-rs%2Fpoem-casbin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casbin-rs%2Fpoem-casbin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/casbin-rs","download_url":"https://codeload.github.com/casbin-rs/poem-casbin/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casbin-rs%2Fpoem-casbin/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259519255,"owners_count":22870327,"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":["abac","acl","auth","authz","casbin","middleware","poem","rbac","rust"],"created_at":"2025-03-20T04:14:30.127Z","updated_at":"2025-06-12T18:32:40.380Z","avatar_url":"https://github.com/casbin-rs.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Poem Casbin Middleware\n\n[Casbin](https://github.com/casbin/casbin-rs) access control middleware for [poem](https://github.com/poem-web/poem) framework\n\n## Install\n\nAdd it to `Cargo.toml`\n\n```toml\npoem = \"1.3.31\"\npoem-casbin-auth = \"0.x.x\"\ntokio = { version = \"1.17.0\", features = [\"rt-multi-thread\", \"macros\"] }\n```\n\n## Requirement\n\n**Casbin only takes charge of permission control**, so you need to implement an `Authentication Middleware` to identify user.\n\nYou should put `poem_casbin_auth::CasbinVals` which contains `subject`(username) and `domain`(optional) into [Extension](https://docs.rs/http/0.2.8/http/struct.Extensions.html).\n\nFor example:\n```rust\nuse poem::{\n    Endpoint, EndpointExt, Middleware, Request, Result,\n};\nuse poem_casbin_auth::CasbinVals;\n\npub struct FakeAuth;\n\npub struct FakeAuthMiddleware\u003cE\u003e {\n    ep: E,\n}\n\nimpl\u003cE: Endpoint\u003e Middleware\u003cE\u003e for FakeAuth {\n    type Output = FakeAuthMiddleware\u003cE\u003e;\n\n    fn transform(\u0026self, ep: E) -\u003e Self::Output {\n        FakeAuthMiddleware { ep }\n    }\n}\n\n#[poem::async_trait]\nimpl\u003cE: Endpoint\u003e Endpoint for FakeAuthMiddleware\u003cE\u003e {\n    type Output = E::Output;\n\n    async fn call(\u0026self, mut req: Request) -\u003e Result\u003cSelf::Output\u003e {\n        let vals = CasbinVals {\n            subject: String::from(\"alice\"),\n            domain: None,\n        };\n        req.extensions_mut().insert(vals);\n        self.ep.call(req).await\n    }\n}\n```\n\n## Example\n```rust\nuse poem_casbin_auth::casbin::{DefaultModel, FileAdapter, Result};\nuse poem_casbin_auth::CasbinService;\nuse poem::{get, handler, listener::TcpListener, web::Path, Route, Server};\nuse poem_casbin_auth::casbin::function_map::key_match2;\n\n#[allow(dead_code)]\nmod fake_auth;\n\n#[handler]\nfn endpoint() -\u003e () {}\n\n#[tokio::main]\nasync fn main() -\u003e Result\u003c(), std::io::Error\u003e {\n    let m = DefaultModel::from_file(\"examples/rbac_with_pattern_model.conf\")\n        .await\n        .unwrap();\n    let a = FileAdapter::new(\"examples/rbac_with_pattern_policy.csv\");\n\n    let casbin_middleware = CasbinService::new(m, a).await.unwrap();\n\n    casbin_middleware\n        .write()\n        .await\n        .get_role_manager()\n        .write()\n        .matching_fn(Some(key_match2), None);\n\n    let app = Route::new()\n        .at(\"/pen/1\", get(endpoint))\n        .at(\"/pen/2\", get(endpoint))\n        .at(\"/book/:id\", get(endpoint))\n        .with(casbin_middleware)\n        .with(FakeAuth);\n\n    Server::new(TcpListener::bind(\"127.0.0.1:8080\"))\n        .run(app)\n        .await\n}\n```\n\n## License\n\nThis project is licensed under\n\n* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or [http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0))\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcasbin-rs%2Fpoem-casbin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcasbin-rs%2Fpoem-casbin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcasbin-rs%2Fpoem-casbin/lists"}