-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from hackerchai/master
Feat: bump version 0.3.1
- Loading branch information
Showing
4 changed files
with
114 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |