{"id":26482615,"url":"https://github.com/casbin-rs/axum-casbin","last_synced_at":"2025-04-05T18:06:07.833Z","repository":{"id":39814697,"uuid":"505272806","full_name":"casbin-rs/axum-casbin","owner":"casbin-rs","description":"Axum authorization middleware based on Casbin","archived":false,"fork":false,"pushed_at":"2025-03-12T15:13:19.000Z","size":39,"stargazers_count":49,"open_issues_count":0,"forks_count":7,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-03-29T17:12:42.239Z","etag":null,"topics":["abac","acl","auth","authentication","authz","axum","casbin","casbin-rs","middleware","permission","rbac","rust"],"latest_commit_sha":null,"homepage":"https://github.com/casbin/casbin-rs","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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-06-20T02:43:00.000Z","updated_at":"2025-03-28T22:00:11.000Z","dependencies_parsed_at":"2025-03-06T16:36:10.406Z","dependency_job_id":null,"html_url":"https://github.com/casbin-rs/axum-casbin","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casbin-rs%2Faxum-casbin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casbin-rs%2Faxum-casbin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casbin-rs%2Faxum-casbin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casbin-rs%2Faxum-casbin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/casbin-rs","download_url":"https://codeload.github.com/casbin-rs/axum-casbin/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247378140,"owners_count":20929296,"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","authentication","authz","axum","casbin","casbin-rs","middleware","permission","rbac","rust"],"created_at":"2025-03-20T04:14:28.827Z","updated_at":"2025-04-05T18:06:07.797Z","avatar_url":"https://github.com/casbin-rs.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# axum-casbin\n\n[![Crates.io](https://img.shields.io/crates/d/axum-casbin)](https://crates.io/crates/axum-casbin)\n[![Docs](https://docs.rs/axum-casbin/badge.svg)](https://docs.rs/axum-casbin)\n[![CI](https://github.com/casbin-rs/axum-casbin/actions/workflows/ci.yml/badge.svg)](https://github.com/casbin-rs/axum-casbin/actions/workflows/ci.yml)\n[![codecov](https://codecov.io/gh/casbin-rs/axum-casbin/branch/master/graph/badge.svg)](https://codecov.io/gh/casbin-rs/axum-casbin)\n\n[Casbin](https://github.com/casbin/casbin-rs) access control middleware for [axum](https://github.com/tokio-rs/axum) framework\n\n## Install\n\nAdd dependencies to `Cargo.toml`\n\n```bash\ncargo add axum\ncargo add axum-casbin\ncargo add tokio --features full\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 `axum_casbin::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 axum::{response::Response, BoxError};\nuse futures::future::BoxFuture;\n\nuse bytes::Bytes;\nuse http::{self, Request};\nuse http_body::Body as HttpBody;\nuse std::{\n    boxed::Box,\n    convert::Infallible,\n    task::{Context, Poll},\n};\nuse tower::{Layer, Service};\n\nuse axum_casbin::CasbinVals;\n\n#[derive(Clone)]\nstruct FakeAuthLayer;\n\nimpl\u003cS\u003e Layer\u003cS\u003e for FakeAuthLayer {\n    type Service = FakeAuthMiddleware\u003cS\u003e;\n\n    fn layer(\u0026self, inner: S) -\u003e Self::Service {\n        FakeAuthMiddleware { inner }\n    }\n}\n\n#[derive(Clone)]\nstruct FakeAuthMiddleware\u003cS\u003e {\n    inner: S,\n}\n\nimpl\u003cS, ReqBody, ResBody\u003e Service\u003cRequest\u003cReqBody\u003e\u003e for FakeAuthMiddleware\u003cS\u003e\nwhere\n    S: Service\u003cRequest\u003cReqBody\u003e, Response = Response\u003cResBody\u003e, Error = Infallible\u003e\n        + Clone\n        + Send\n        + 'static,\n    S::Future: Send + 'static,\n    ReqBody: Send + 'static,\n    Infallible: From\u003c\u003cS as Service\u003cRequest\u003cReqBody\u003e\u003e\u003e::Error\u003e,\n    ResBody: HttpBody\u003cData = Bytes\u003e + Send + 'static,\n    ResBody::Error: Into\u003cBoxError\u003e,\n{\n    type Response = S::Response;\n    type Error = S::Error;\n    // `BoxFuture` is a type alias for `Pin\u003cBox\u003cdyn Future + Send + 'a\u003e\u003e`\n    type Future = BoxFuture\u003c'static, Result\u003cSelf::Response, Self::Error\u003e\u003e;\n\n    fn poll_ready(\u0026mut self, cx: \u0026mut Context\u003c'_\u003e) -\u003e Poll\u003cResult\u003c(), Self::Error\u003e\u003e {\n        self.inner.poll_ready(cx)\n    }\n\n    fn call(\u0026mut self, mut req: Request\u003cReqBody\u003e) -\u003e Self::Future {\n        let not_ready_inner = self.inner.clone();\n        let mut inner = std::mem::replace(\u0026mut self.inner, not_ready_inner);\n\n        Box::pin(async move {\n            let vals = CasbinVals {\n                subject: String::from(\"alice\"),\n                domain: None,\n            };\n            req.extensions_mut().insert(vals);\n            inner.call(req).await\n        })\n    }\n}\n```\n\n## Example\n```rust\nuse axum::{routing::get, Router};\nuse axum_casbin::{CasbinAxumLayer};\nuse axum_casbin::casbin::function_map::key_match2;\nuse axum_casbin::casbin::{CoreApi, DefaultModel, FileAdapter, Result};\n\n// Handler that immediately returns an empty `200 OK` response.\nasync fn handler() {}\n\n#[tokio::main]\nasync fn main() -\u003e Result\u003c()\u003e {\n    let m = DefaultModel::from_file(\"examples/rbac_with_pattern_model.conf\")\n        .await\n        .unwrap();\n\n    let a = FileAdapter::new(\"examples/rbac_with_pattern_policy.csv\");\n\n    let casbin_middleware = CasbinAxumLayer::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 = Router::new()\n        .route(\"/pen/1\", get(handler))\n        .route(\"/pen/2\", get(handler))\n        .route(\"/book/:id\", get(handler))\n        .layer(casbin_middleware)\n        .layer(FakeAuthLayer);\n\n    axum::Server::bind(\u0026\"127.0.0.1:8080\".parse().unwrap())\n        .serve(app.into_make_service())\n        .await;\n    \n        Ok(())\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%2Faxum-casbin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcasbin-rs%2Faxum-casbin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcasbin-rs%2Faxum-casbin/lists"}