Giter VIP home page Giter VIP logo

casbin4d's Introduction

Casbin4D

Made With Delphi CodeCoverage Version Discord

casbin Logo

Casbin4D is a cross platform (FireMonkey) implementation for Delphi/Pascal of the popular authorisation library Casbin. It provides support for enforcing authorization based on various access control models.

You are cordially invited to share, fork, review and improve this library. Please feel free to comment and offer suggestions. If you want to contribute, check the wiki page for developers first

All the languages supported by Casbin:

golang java nodejs php
Casbin jCasbin node-Casbin PHP-Casbin
production-ready production-ready production-ready production-ready
python delphi dotnet rust
PyCasbin Casbin4D Casbin-Net Casbin-RS
production-ready experimental production-ready production-ready

Table of contents

Supported models

  1. ACL (Access Control List)
  2. ACL with superuser
  3. ACL without users: especially useful for systems that don't have authentication or user log-ins.
  4. ACL without resources: some scenarios may target for a type of resources instead of an individual resource by using permissions like write-article, read-log. It doesn't control the access to a specific article or log.
  5. RBAC (Role-Based Access Control)
  6. RBAC with resource roles: both users and resources can have roles (or groups) at the same time.
  7. RBAC with domains/tenants: users can have different role sets for different domains/tenants.
  8. ABAC (Attribute-Based Access Control): syntax sugar like resource.Owner can be used to get the attribute for a resource.
  9. RESTful: supports paths like /res/*, /res/:id and HTTP methods like GET, POST, PUT, DELETE.
  10. Deny-override: both allow and deny authorizations are supported, deny overrides the allow.
  11. Priority: the policy rules can be prioritized like firewall rules.

How it works

In Casbin, an access control model is abstracted into a CONF file based on the PERM metamodel (Policy, Effect, Request, Matchers). So switching or upgrading the authorization mechanism for a project is just as simple as modifying a configuration. You can customize your own access control model by combining the available models. For example, you can get RBAC roles and ABAC attributes together inside one model and share one set of policy rules.

The most basic and simplest model in Casbin is ACL. ACL's model CONF looks like this:

# Request definition
[request_definition]
r = sub, obj, act

# Policy definition
[policy_definition]
p = sub, obj, act

# Policy effect
[policy_effect]
e = some(where (p.eft == allow))

# Matchers
[matchers]
m = r.sub == p.sub && r.obj == p.obj && r.act == p.act

The above configuration follows the Go language.

Casbin4D understands it but you can also use the typical Delphi/Pascal style:

...
[matchers]
m = r.sub = p.sub and r.obj = p.sub and r.act = p.act

An example policy for ACL model is like:

p, alice, data1, read
p, bob, data2, write

For Casbin this means that:

  • alice can read data1
  • bob can write data2

Then, in your application instantiate a new Casbin (interfaced) object and pass the required files:

var
  casbin: ICasbin;
begin
  casbin:=TCasbin.Create ('model.conf', 'policies.csv');
  ...
end

and, finally, test (enforce) an assertion:

  ...
  if casbin.enforce(['alice,data1,read']) then
    // Alice is super happy as she can read data1
  else
    // Alice is sad
  ...

Features

What Casbin does:

  1. enforce the policy in the classic {subject, object, action} form or a customized form as you defined, both allow and deny authorizations are supported.
  2. manage the role-user mappings and role-role mappings (aka role hierarchy in RBAC).
  3. support built-in superuser like root or administrator. A superuser can do anything without explict permissions.
  4. multiple built-in operators to support the rule matching. For example, keyMatch can map a resource key /foo/bar to the pattern /foo*

What Casbin does NOT do:

  1. authentication (aka verify username and password when a user logs in)
  2. manage the list of users or roles. This is left to the application that uses Casbin. Keep in mind that Casbin is not designed as a password container. However, Casbin stores the user-role mapping for the RBAC scenario

Installation

Casbin4D comes in a package (currently for Delphi 10.3 Rio) and you can install it in the IDE. However, there are no visual components which means that you can use the units independently of packages. Just import the units in your project (assuming you do not mind the number of them)

Documentation

Please see the wiki pages

Online editor

You can also use the online editor (http://casbin.org/editor/) to write your Casbin model and policy in your web browser. It provides functionality such as syntax highlighting and code completion, just like an IDE for a programming language.

You can, also, use the Main Demo to test the scripts and the assertions. See the Demos

Demos

See the Demos folder. The Examples folder contains the example configuration and policy files from the original Go implementation

The main demo is under Demos/Main folder. You can, also, find an executable file in this folder so you can download it and try Casbin4D

Main Demo

Get started

Please see the Documentation

Policy management

Casbin4D provides one point of access to manage permissions via the IPolicyManager. If you are familiar with other implementations, you will notice that they have two sets of APIs (Management API and RBAC API). This implementation combines both of them under the Policy Manager

Policy persistence

In Casbin4D, the policy storage is abstracted via the concept of the adapter. The consumer of Casbin4D is free to implement the management of policy storage as they see fit to their needs. For convenience, Casbin4D provides two adapters: one for text files (.csv) and one memory adapter. You are welcome (and invited) to contribute any new adapters with broader usage. Please let us know

Multi-threading

Casbin4D is designed with multi-threading in mind. The current implementation achieves this at the Enforcer level

Benchmarks

There is a benchmark project located in Benchmarks folder which tests the policy enforcement.

The project was executed in the following machine/installation:

* Dell XPS 15 9560: Intel(R) Core(TM) i7-7700HQ CPU @2.80GHz, 2801MHz, 4 Core(s), 8 Logical Processor(s)

* Windows: Windows 10 Pro 64-bit, 10.0.16299 Build 16299

The results are shown in the following table. The time overhead is calculated per operation (op) which represents a single call to TCasbin.enforce[..]

Test case Size Time overhead Memory overhead
Raw Enforce 2 Rules (2 Users) 0.000090 sec/op 0 KB
Basic Model 2 Rules (2 Users) 0.000466 sec/op 432 B
RBAC 5 Rules (2 Users, 1 Role) 0.000872 sec/op 352 B
RBAC (Small) 1,100 Rules (1,000 Users, 100 roles) 0.238945 sec/op 1.49 MB
RBAC (Medium) 11,000 Rules (10,000 Users, 1,000 Roles) 9.745707 sec/op 15.7 MB
RBAC (With Resource Roles) 6 Rules (2 Users, 2 Roles) 0.000658 sec/op 352 B
RBAC (With Domains/Tenants) 6 Rules (2 Users, 1 Role, 2 Domains) 0.000670 sec/op 352 B
RBAC (With Deny) 6 Rules (2 Users, 1 Role) 0.001260 sec/op 380 B
ABAC 0 Rules (0 Users) 0.000181 sec/op 120 B
KeyMatch 2 Rules (2 Users) 0.000782 sec/op 352 B
Priority 9 Rules (2 Users, 2 Roles) 0.001124 sec/op 380 B

Examples

Model Model file Policy file
ACL basic_model.conf basic_policy.csv
ACL with superuser basic_model_with_root.conf basic_policy.csv
ACL without users basic_model_without_users.conf basic_policy_without_users.csv
ACL without resources basic_model_without_resources.conf basic_policy_without_resources.csv
RBAC rbac_model.conf rbac_policy.csv
RBAC with resource roles rbac_model_with_resource_roles.conf rbac_policy_with_resource_roles.csv
RBAC with domains/tenants rbac_model_with_domains.conf rbac_policy_with_domains.csv
ABAC abac_model.conf N/A
RESTful keymatch_model.conf keymatch_policy.csv
Deny-override rbac_model_with_deny.conf rbac_policy_with_deny.csv
Priority priority_model.conf priority_policy.csv

Tests

The vast majority of the tests of the original implementation in Go have been imported in Delphi. Please see the Tests

You can check the code coverage here for the up to date status. You are welcome to improve the tests and the coverage

License

This project is licensed under the Apache 2.0 license.

Contact

If you have any issues or feature requests, please contact us. PR is welcome

casbin4d's People

Contributors

divy9881 avatar erikuniformagri avatar gopherj avatar hsluoyz avatar jkour avatar kyle-mccarthy avatar pipcom avatar selflocking avatar wallymathieu avatar wiphi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

casbin4d's Issues

GetRolesForUser API

Is there GetRolesForUser API in Casbin4D?
I noticed there is GetRolesForUser API missing in Casbin website Get Started docs with Delphi example.

gwyHsI.png

performance is lower than expected

Intresting project, but why Pascal version is slower than Golang, it doesn't make sense.

Golang: RBAC (small) | 1100 rules (1000 users, 100 roles) | 0.164309 ms/op
Delphi: RBAC (Small) | 1,100 Rules (1,000 Users, 100 roles) | 238.945 ms/op

Test/Create Packages for older Delphi Versions

Hi everyone,
I do not have other versions of Delphi installed than 10.2 and 10.3
It would be great if you were able tot create and test packages in other versions. Then, I can merge them to the master branch
Thanks

To-Do List

  • Management API
  • RBAC API
  • ABAC (Proper implementation)
  • Custom Data (??)
  • Patterns in g/g2? (see TestRBACModelWithPattern)
  • Watchers (???)

RBAC with domains and explicit user policy

Hi folks,
I think there is a bug in the RBAC implementation.

Please consider the following model

[request_definition]
r = sub, dom, obj, act

[policy_definition]
p = sub, dom, obj, act

[role_definition]
g = _, _, _

[policy_effect]
e = some(where (p.eft == allow))

[matchers]
m = g(r.sub, p.sub, r.dom) && r.dom == p.dom && r.obj == p.obj && r.act == p.act

with the following policy-file:

p, admin, domain1, data1, read
p, admin, domain1, data1, write
p, admin, domain2, data2, read
p, admin, domain2, data2, write

p, alice, domain3, data3, read

g, alice, admin, domain1
g, bob, admin, domain2

If you test the follow request alice, domain3, data3, read or admin, domain1, data1, read in the casbon online editor (https://casbin.org/editor/) the enforcment result will be always true.

If you test this in the Casbin4D Demo, the result is always false. I think this is a bug, because you can have role permissions with explicit user policies in addition.

I debugged this a while, but I don't find any simple solution for that. I think we need a link to the role / user itself, or a better linkExists-logic in the policy. What did you think?

Best regards.

d10.2.3

make it work in d10.2 also and down

[dcc32 Error] Casbin.Policy.pas(604): E2010 Incompatible types: 'TStringDynArray' and 'System.TArray<System.string>'

Both console and VCL demo doesn't work as expected

I am using Delphi 10.4.2 to build the demo. I found that both demo doesn't return correct result:

Console demo fail at
Assert(casbin.enforce(['bob', 'user', 'delete']) = True);

VCL demo never always return Deny.

Why use Random*100 as a replacement?

Some times the result of an enforcement is wrong.
When I enforce a request on a simple RBAC model and there is no matching policy for my request. I still get result = true

You can see in the image botch "DELETE2" and "ADD" are replaced by 93. Then the string will match wrongly
Logging

Why is there a "Random*100" in TMatcher.addIdentifier? (And why does it have to be replaced anyway?)
Now there is a 1% change the result is wrong and if I have 1000+ policys the result is almost guaranteed wrong.

If you run the tests several times you will see some tests will fail random

Installation

Hi,
I am trying to instal in Delphi 10.2, i get error in unit Casbin.Adapter.Base in
procedure TBaseAdapter.load(const aFilter: TFilterArray);

for var str in aFilter do ==> var is reserved word in delphi

How to fix this poroblem ?

Thank's

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.