Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add actix-casbin-auth to Actix official middleware list #92

Closed
hsluoyz opened this issue Apr 7, 2020 · 21 comments
Closed

Add actix-casbin-auth to Actix official middleware list #92

hsluoyz opened this issue Apr 7, 2020 · 21 comments
Assignees
Labels
enhancement New feature or request

Comments

@hsluoyz
Copy link
Member

hsluoyz commented Apr 7, 2020

We have very active development for Casbin-RS now. However, our popularity is still under my estimation (129 stars). I hope we can do more advertisement in future to let people know and use our project.

The first thing we should do is to add Casbin middleware: https://github.com/actix/examples/tree/master/casbin to Actix official middleware list: https://actix.rs/docs/middleware/ . So Actix website visitors will also come to our GitHub page.

Can anyone work on this?

@hsluoyz hsluoyz pinned this issue Apr 7, 2020
@hsluoyz hsluoyz self-assigned this Apr 7, 2020
@hsluoyz hsluoyz added enhancement New feature or request help wanted Extra attention is needed labels Apr 7, 2020
@GopherJ
Copy link
Member

GopherJ commented Apr 7, 2020

I'll consider making a good actix middleware

@GopherJ
Copy link
Member

GopherJ commented Apr 8, 2020

I had a thought on this. The first thing we need is an actix casbin actor.

struct CasbinActor {
  enforcer: Option<Enforcer>
}

we use casbin::TryIntoAdapter, casbin::TryIntoModel for creating it.

use casbin::{TryIntoAdapter, TryIntoModel};
use actix::{Addr, Supervisor};

impl CasbinActor {
   pub async fn new<M: TryIntoModel, A: TryIntoAdapter>(m: M, a: A) 
   -> Result<Addr<Enforcer>> {
     let enforcer: Enforcer = Enforcer::new(m, a).await?;
     Supervisor::start(|_| CasbinActor {
            enforcer: Some(enforcer)
        })
   }
}

@GopherJ
Copy link
Member

GopherJ commented Apr 8, 2020

Then we define our message:

use casbin::Result;
use actix::Message;

pub enum CasbinCmd {
   Enforce(Vec<String>),
   AddPolicy(Vec<String>),
   AddPolicies(Vec<Vec<String>>),
   RemovePolicy(Vec<String>),
   RemovePolicies(Vec<Vec<String>>)
   ...
}


impl Message for CasbinActor {
    type Result = Result<bool>;
}

@GopherJ
Copy link
Member

GopherJ commented Apr 8, 2020

At last we should implement:

impl Actor for CasbinActor {
...
}

impl Supervised for CasbinActor {
    fn restarting(&mut self, _: &mut Self::Context) {
        self.enforcer.take();
    }
}

impl Handler<CasbinCmd> for CasbinActor {
    type Result = ResponseActFuture<Self, Result<bool>>;

    fn handle(&mut self, msg: CasbinCmd, _: &mut Self::Context) -> Self::Result {
    ...
    }
}

@GopherJ
Copy link
Member

GopherJ commented Apr 8, 2020

Users can then register CasbinActor on actix-wb::App.

In our middleware we access casbin enforcer by:

    if let Some(state) = req.app_data::<Enforcer>() {
     // use casbin for access control
    } else {
     // deny the request
    }

@GopherJ
Copy link
Member

GopherJ commented Apr 8, 2020

The main problem of this idea is Users need to register casbin enforcer on app_data.

How can we register an app data while creating a middleware?

@hackerchai
Copy link
Member

I am working on this issue. @hsluoyz @GopherJ

@GopherJ
Copy link
Member

GopherJ commented Apr 8, 2020

See also: actix/actix-web#1444

@GopherJ GopherJ changed the title Add actix-casbin to Actix official middleware list for better advertising Add actix-casbin to Actix official middleware list Apr 8, 2020
@hackerchai
Copy link
Member

The actor is finished. see casbin-rs/actix-casbin#3.

@rakeen01
Copy link

rakeen01 commented Apr 27, 2020

I was interested to work on this middleware Casbin posted (casbin/Casbin.NET#24) however working on a good actix middleware also seems interesting. Is there already a teambase working on this I can connect with?

@hsluoyz
Copy link
Member Author

hsluoyz commented Apr 28, 2020

Have we put our product here? https://actix.rs/docs/middleware/

We can find the source code of that website and make a PR for it to add.

@hackerchai
Copy link
Member

hackerchai commented Apr 28, 2020

Have we put our product here? https://actix.rs/docs/middleware/

We can find the source code of that website and make a PR for it to add.

@hsluoyz As far as I know, the only repo of Actix example is https://github.com/actix/examples/tree/master/casbin . I have finished the actor while the middleware is still work-in-progress. When the middleware is done, I will send a PR to https://github.com/actix/examples/tree/master/casbin. The previous version submitted by @PsiACE seems not using Actix middleware.

@PsiACE
Copy link
Contributor

PsiACE commented Apr 28, 2020

@hackerchai You can provide a separate example with middleware. In addition, the example also needs to be added to casbin-rs/examples.

@hsluoyz
Copy link
Member Author

hsluoyz commented Apr 28, 2020

I'm talking about how to advertise Casbin-RS better on Actix website: https://github.com/actix/actix-website

https://github.com/actix/actix-website/blob/master/content/docs/middleware.md is the source code of https://actix.rs/docs/middleware/

Can anyone make a PR to add Casbin to their website?

@PsiACE
Copy link
Contributor

PsiACE commented Apr 28, 2020

@hsluoyz The link you gave is about official middleware, but we are a third party, so it may not be suitable.

@hackerchai
Copy link
Member

@hackerchai You can provide a separate example with middleware. In addition, the example also needs to be added to casbin-rs/examples.

@PsiACE BTW, there is a mistake in the middleware page of the casbin official website. The actix-casbin link in Rust section is not a middleware but a simple example using actix-web. The actix-casbin now refers to the Actix runtime actor. We should call the official casbin middleware actix-casbin-auth. Maybe you can rename actix-casbin to actix-simple-example or something else.

@hackerchai
Copy link
Member

#82 Middleware has been implemented.

@hsluoyz
Copy link
Member Author

hsluoyz commented May 4, 2020

@hackerchai thanks.

Waiting for Actix guys' decision: actix/actix-website#170

@hsluoyz hsluoyz changed the title Add actix-casbin to Actix official middleware list Add actix-casbin-auth to Actix official middleware list May 4, 2020
@hackerchai
Copy link
Member

@hackerchai thanks.

Waiting for Actix guys' decision: actix/actix-website#170

@hsluoyz Thanks for your work. I will track that issue and implement an actix example using actix-casbin-middleware as soon as possible.

@hsluoyz hsluoyz unpinned this issue May 4, 2020
@hsluoyz
Copy link
Member Author

hsluoyz commented Dec 19, 2020

We should modify this page: https://github.com/actix/actix-website/blob/master/content/docs/middleware.md to add Casbin into it as an authorization middleware.

@hsluoyz
Copy link
Member Author

hsluoyz commented Dec 20, 2020

Made a PR: actix/actix-website#205

@hsluoyz hsluoyz removed the help wanted Extra attention is needed label Dec 20, 2020
@hsluoyz hsluoyz closed this as completed Dec 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants