Skip to content

Commit

Permalink
Merge pull request #12 from hackerchai/master
Browse files Browse the repository at this point in the history
Feat: bump version 0.3.1
  • Loading branch information
hackerchai authored Jul 29, 2020
2 parents ba1ff1b + f201bc4 commit 31e6808
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "actix-casbin-auth"
version = "0.3.0"
version = "0.3.1"
authors = ["Eason Chai <[email protected]>","Cheng JIANG <[email protected]>"]
edition = "2018"
license = "Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
Add it to `Cargo.toml`

```rust
actix-casbin-auth = "0.3.0"
actix-casbin-auth = "0.3.1"
actix-rt = "1.1.1"
actix-web = "2.0.0"
```
Expand Down
6 changes: 5 additions & 1 deletion src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@ impl CasbinService {
}
}

pub async fn get_enforcer(&self) -> Arc<RwLock<CachedEnforcer>> {
pub async fn get_enforcer(&mut self) -> Arc<RwLock<CachedEnforcer>> {
self.enforcer.clone()
}

pub async fn set_enforcer(e: Arc<RwLock<CachedEnforcer>>) -> Self {
CasbinService { enforcer: e }
}
}

impl<S, B> Transform<S> for CasbinService
Expand Down
107 changes: 107 additions & 0 deletions tests/test_set_enforcer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
use std::cell::RefCell;
use std::pin::Pin;
use std::rc::Rc;
use std::task::{Context, Poll};

use actix_service::{Service, Transform};
use actix_web::{dev::ServiceRequest, dev::ServiceResponse, Error, HttpMessage, HttpResponse};
use futures::future::{ok, Future, Ready};

use actix_casbin_auth::{CasbinService, CasbinVals};

use actix_web::{test, web, App};
use casbin::{CachedEnforcer, CoreApi, DefaultModel, FileAdapter};

use std::sync::Arc;

#[cfg(feature = "runtime-tokio")]
use tokio::sync::RwLock;

#[cfg(feature = "runtime-async-std")]
use async_std::sync::RwLock;

pub struct FakeAuth;

impl<S: 'static, B> Transform<S> for FakeAuth
where
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S::Future: 'static,
B: 'static,
{
type Request = ServiceRequest;
type Response = ServiceResponse<B>;
type Error = Error;
type InitError = ();
type Transform = FakeAuthMiddleware<S>;
type Future = Ready<Result<Self::Transform, Self::InitError>>;

fn new_transform(&self, service: S) -> Self::Future {
ok(FakeAuthMiddleware {
service: Rc::new(RefCell::new(service)),
})
}
}

pub struct FakeAuthMiddleware<S> {
service: Rc<RefCell<S>>,
}

impl<S, B> Service for FakeAuthMiddleware<S>
where
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
S::Future: 'static,
B: 'static,
{
type Request = ServiceRequest;
type Response = ServiceResponse<B>;
type Error = Error;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;

fn poll_ready(&mut self, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
self.service.poll_ready(cx)
}

fn call(&mut self, req: ServiceRequest) -> Self::Future {
let mut svc = self.service.clone();

Box::pin(async move {
let vals = CasbinVals {
subject: String::from("alice"),
domain: None,
};
req.extensions_mut().insert(vals);
svc.call(req).await
})
}
}

#[actix_rt::test]
async fn test_middleware() {
let m = DefaultModel::from_file("examples/rbac_restful_keymatch2_model.conf")
.await
.unwrap();
let a = FileAdapter::new("examples/rbac_restful_keymatch2_policy.csv");

let enforcer = Arc::new(RwLock::new(CachedEnforcer::new(m, a).await.unwrap()));

let casbin_middleware = CasbinService::set_enforcer(enforcer).await;

casbin_middleware.write().await;

let mut app = test::init_service(
App::new()
.wrap(casbin_middleware)
.wrap(FakeAuth)
.route("/pen/1", web::get().to(|| HttpResponse::Ok()))
.route("/book/{id}", web::get().to(|| HttpResponse::Ok())),
)
.await;

let req_pen = test::TestRequest::get().uri("/pen/1").to_request();
let resp_pen = test::call_service(&mut app, req_pen).await;
assert!(resp_pen.status().is_success());

let req_book = test::TestRequest::get().uri("/book/2").to_request();
let resp_book = test::call_service(&mut app, req_book).await;
assert!(resp_book.status().is_success());
}

0 comments on commit 31e6808

Please sign in to comment.