{"id":26482629,"url":"https://github.com/casbin-rs/actix-casbin-auth","last_synced_at":"2025-03-20T04:14:33.237Z","repository":{"id":40415120,"uuid":"254035673","full_name":"casbin-rs/actix-casbin-auth","owner":"casbin-rs","description":"Casbin Actix-web access control middleware","archived":false,"fork":false,"pushed_at":"2024-05-06T01:01:31.000Z","size":91,"stargazers_count":55,"open_issues_count":1,"forks_count":16,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-08-08T19:41:18.037Z","etag":null,"topics":["abac","acl","actix","actix-web","auth","authentication","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":null,"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":null,"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":"2020-04-08T08:56:51.000Z","updated_at":"2024-06-21T07:02:43.000Z","dependencies_parsed_at":"2024-04-08T02:54:52.956Z","dependency_job_id":null,"html_url":"https://github.com/casbin-rs/actix-casbin-auth","commit_stats":{"total_commits":61,"total_committers":8,"mean_commits":7.625,"dds":0.5737704918032787,"last_synced_commit":"071a2467c2b1ab6591c3c58fe3b2ecd73afa8250"},"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casbin-rs%2Factix-casbin-auth","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casbin-rs%2Factix-casbin-auth/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casbin-rs%2Factix-casbin-auth/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casbin-rs%2Factix-casbin-auth/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/casbin-rs","download_url":"https://codeload.github.com/casbin-rs/actix-casbin-auth/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244547562,"owners_count":20470105,"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","actix","actix-web","auth","authentication","casbin","casbin-rs","middleware","permission","rbac","rust"],"created_at":"2025-03-20T04:14:32.698Z","updated_at":"2025-03-20T04:14:33.226Z","avatar_url":"https://github.com/casbin-rs.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Actix Casbin Middleware\n\n[![Crates.io](https://img.shields.io/crates/d/actix-casbin-auth)](https://crates.io/crates/actix-casbin-auth)\n[![Docs](https://docs.rs/actix-casbin-auth/badge.svg)](https://docs.rs/actix-casbin-auth)\n[![CI](https://github.com/casbin-rs/actix-casbin-auth/actions/workflows/ci.yml/badge.svg)](https://github.com/casbin-rs/actix-casbin-auth/actions/workflows/ci.yml)\n[![codecov](https://codecov.io/gh/casbin-rs/actix-casbin-auth/branch/master/graph/badge.svg)](https://codecov.io/gh/casbin-rs/actix-casbin-auth)\n\n[Casbin](https://github.com/casbin/casbin-rs) access control middleware for [actix-web](https://github.com/actix/actix-web) framework\n\n## Install\n\nAdd dependencies\n\n```bash\ncargo add actix-rt\ncargo add actix-web\ncargo add actix-casbin --no-default-features --features runtime-async-std\ncargo add actix-casbin-auth --no-default-features --features runtime-async-std\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 `actix_casbin_auth::CasbinVals` which contains `subject`(username) and `domain`(optional) into [Extension](https://docs.rs/actix-web/2.0.0/actix_web/dev/struct.Extensions.html).\n\nFor example:\n\n```rust\nuse std::cell::RefCell;\nuse std::pin::Pin;\nuse std::rc::Rc;\nuse std::task::{Context, Poll};\n\nuse actix_service::{Service, Transform};\nuse actix_web::{dev::ServiceRequest, dev::ServiceResponse, Error, HttpMessage};\nuse futures::future::{ok, Future, Ready};\n\nuse actix_casbin_auth::CasbinVals;\n\n\npub struct FakeAuth;\n\nimpl\u003cS: 'static, B\u003e Transform\u003cS\u003e for FakeAuth\n    where\n        S: Service\u003cServiceRequest, Response = ServiceResponse\u003cB\u003e, Error = Error\u003e,\n        S::Future: 'static,\n        B: 'static,\n{\n    type Response = ServiceResponse\u003cB\u003e;\n    type Error = Error;\n    type InitError = ();\n    type Transform = FakeAuthMiddleware\u003cS\u003e;\n    type Future = Ready\u003cResult\u003cSelf::Transform, Self::InitError\u003e\u003e;\n\n    fn new_transform(\u0026self, service: S) -\u003e Self::Future {\n        ok(FakeAuthMiddleware {\n            service: Rc::new(RefCell::new(service)),\n        })\n    }\n}\n\npub struct FakeAuthMiddleware\u003cS\u003e {\n    service: Rc\u003cRefCell\u003cS\u003e\u003e,\n}\n\nimpl\u003cS, B\u003e Service for FakeAuthMiddleware\u003cS\u003e\n    where\n        S: Service\u003cServiceRequest, Response = ServiceResponse\u003cB\u003e, Error = Error\u003e + 'static,\n        S::Future: 'static,\n        B: 'static,\n{\n    type Response = ServiceResponse\u003cB\u003e;\n    type Error = Error;\n    type Future = Pin\u003cBox\u003cdyn Future\u003cOutput = Result\u003cSelf::Response, Self::Error\u003e\u003e\u003e\u003e;\n\n    fn poll_ready(\u0026mut self, cx: \u0026mut Context) -\u003e Poll\u003cResult\u003c(), Self::Error\u003e\u003e {\n        self.service.poll_ready(cx)\n    }\n\n    fn call(\u0026mut self, req: ServiceRequest) -\u003e Self::Future {\n        let mut svc = self.service.clone();\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            svc.call(req).await\n        })\n    }\n}\n````\n\n\n## Example\n\n```rust\nuse actix_casbin_auth::casbin::{DefaultModel, FileAdapter, Result};\nuse actix_casbin_auth::CasbinService;\nuse actix_web::{web, App, HttpResponse, HttpServer};\nuse actix_casbin_auth::casbin::function_map::key_match2;\n\n#[allow(dead_code)]\nmod fake_auth;\n\n#[actix_rt::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    let a = FileAdapter::new(\"examples/rbac_with_pattern_policy.csv\");  //You can also use diesel-adapter or sqlx-adapter\n\n    let casbin_middleware = CasbinService::new(m, a).await?;\n\n    casbin_middleware\n        .write()\n        .await\n        .get_role_manager()\n        .write()\n        .unwrap()\n        .matching_fn(Some(key_match2), None);\n\n    HttpServer::new(move || {\n        App::new()\n            .wrap(casbin_middleware.clone())\n            .wrap(FakeAuth)          \n            .route(\"/pen/1\", web::get().to(|| HttpResponse::Ok()))\n            .route(\"/book/{id}\", web::get().to(|| HttpResponse::Ok()))\n    })\n    .bind(\"127.0.0.1:8080\")?\n    .run()\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%2Factix-casbin-auth","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcasbin-rs%2Factix-casbin-auth","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcasbin-rs%2Factix-casbin-auth/lists"}