All Projects → phatcher → Meerkat.Security

phatcher / Meerkat.Security

Licence: MIT license
RBAC library for .NET MVC and WebAPI projects

Programming Languages

C#
18002 projects
HTML
75241 projects
javascript
184084 projects - #8 most used programming language
CSS
56736 projects
F#
602 projects
Batchfile
5799 projects

Projects that are alternatives of or similar to Meerkat.Security

Active Directory Dotnet Native Desktop
A .NET 4.5 WPF application that authenticates a user and calls web API using Azure AD and OAuth 2.0 access tokens.
Stars: ✭ 94 (+347.62%)
Mutual labels:  webapi, asp-net-mvc
Active Directory Dotnet Daemon
A Windows console application that calls a web API using its app identity (instead of a user's identity) to get access tokens in an unattended job or process.
Stars: ✭ 107 (+409.52%)
Mutual labels:  webapi, asp-net-mvc
TraceHub
Centralized and distributed logging for Web applications and services, extending System.Diagnostics and Essential.Diagnostics, providing structured tracing and logging withou needing to change 1 line of your application codes
Stars: ✭ 22 (+4.76%)
Mutual labels:  webapi, asp-net-mvc
active-directory-dotnet-daemon-certificate-credential
A .NET 4.5 daemon application that uses a certificate to authenticate with Azure AD and get OAuth 2.0 access tokens.
Stars: ✭ 40 (+90.48%)
Mutual labels:  webapi, asp-net-mvc
Xclcms
XCLCMS is a lightweight CMS (content management system) background management system, Developed using asp.net MVC, it provides a simple and easy-to-use web API interface and supports multiple applications for a single merchant.
Stars: ✭ 107 (+409.52%)
Mutual labels:  webapi, asp-net-mvc
ByteScout-SDK-SourceCode
ALL source code samples for ByteScout SDKs and Web API API products.
Stars: ✭ 24 (+14.29%)
Mutual labels:  webapi, asp-net-mvc
actix-casbin-auth
Casbin Actix-web access control middleware
Stars: ✭ 40 (+90.48%)
Mutual labels:  rbac
GPONMonitor
GPON Monitoring tool for Dasan Networks GPON OLTs
Stars: ✭ 26 (+23.81%)
Mutual labels:  webapi
active-directory-javascript-nodejs-webapi-v2
A small Node.js Web API that is protected with Azure AD v2.0 to validate access tokens and accepts authorized calls using Passport.js
Stars: ✭ 76 (+261.9%)
Mutual labels:  webapi
aspnet-api-versioning
Provides a set of libraries which add service API versioning to ASP.NET Web API, OData with ASP.NET Web API, and ASP.NET Core.
Stars: ✭ 2,396 (+11309.52%)
Mutual labels:  webapi
saule
JSON API library for ASP.Net Web API 2.
Stars: ✭ 74 (+252.38%)
Mutual labels:  webapi
framboise
Framboise is a fuzzer for in-depth testing of WebAPIs.
Stars: ✭ 21 (+0%)
Mutual labels:  webapi
CellReport
CellReport 是一个netcore实现的、以复杂统计报表为核心目标的制作、运行工具。支持数据看板、大屏制作。你可以使用数据库、excel文件、api服务、已有报表等为数据源,通过内置的集合函数组织数据,以类excel界面设计最终呈现结果。
Stars: ✭ 196 (+833.33%)
Mutual labels:  asp-net-mvc
RRQMSocket
TouchSocket是.Net(包括 C# 、VB.Net、F#)的一个整合性的、超轻量级的网络通信框架。包含了 tcp、udp、ssl、http、websocket、rpc、jsonrpc、webapi、xmlrpc等一系列的通信模块。一键式解决 TCP 黏分包问题,udp大数据包分片组合问题等。使用协议模板,可快速实现「固定包头」、「固定长度」、「区间字符」等一系列的数据报文解析。
Stars: ✭ 286 (+1261.9%)
Mutual labels:  webapi
aspnet-core-web-api-using-odata
Demo application of my speech 'Add OData Support to Your Asp.Net Core Web Api' at Dotnet Konf İstanbul. http://dotnetkonf.com/
Stars: ✭ 28 (+33.33%)
Mutual labels:  webapi
webapiclientgenexamples
Code Examples for using WebApiClientGen
Stars: ✭ 43 (+104.76%)
Mutual labels:  webapi
EasyShiro
基于 RBAC 模型功能全面的 Shiro 安全集成&简化&扩展组件。Shiro integration & simplifies & Extension component based RBAC
Stars: ✭ 47 (+123.81%)
Mutual labels:  rbac
sdk-oauth
Fitbit SDK example application.
Stars: ✭ 66 (+214.29%)
Mutual labels:  webapi
flask-authz
Use Casbin in Flask, Casbin is a powerful and efficient open-source access control library.
Stars: ✭ 100 (+376.19%)
Mutual labels:  rbac
elmah.io
ELMAH error logger for sending errors to elmah.io.
Stars: ✭ 31 (+47.62%)
Mutual labels:  asp-net-mvc

Meerkat Security

This project provides Role Based Access Control (RBAC) to allow easier authorization of activities in your system and also provides support for this in ASP.NET MVC and WebAPI projects.

It is available as a set of NuGet packages Meerkat.Security, Meerkat.Security.Mvc and Meerkat.Security.WebApi

NuGet Build status

Welcome to contributions from anyone.

You can see the version history here.

Build the project

  • Windows: Run build.cmd

The tooling should be automatically installed by paket/Fake. The default build will compile and test the project, and also produce the nuget packages.

Library License

The library is available under the MIT License, for more information see the License file in the GitHub repository.

Introduction

Security is hard, and the goal of this project is to make it a bit easier to get a good level of default security with low effort into your project. Having said that, your requirements will be different from mine so make sure that you first understand what you are trying to do and that this project is a good fit.

Basic Concepts

Here we cover off the key building blocks of the framework

Security Models

There are four classic models of access control:

  • Discretionary Access Control (DAC): Control based on restricting access based on the identity of subjects and/or groups to which they belong. The controls are discretionary in the sense that a subject with certain access permission is capable of passing that permission on to any other subject.
  • Mandatory Access Control (MAC): Any operation by any subject on any object is tested against the set of authorization rules (aka policy) to determine if the operation is allowed. The ability to grant permissions itself comes under MAC and this distinguishes it from the simpler DAC model.
  • Role Based Access Control (RBAC): This is a policy neutral access control mechanism around roles and permissions and although different from DAC and MAC can easily be used to enforce such constraints
  • Attribute Based Access Control (ABAC): This evolved from RBAC to consider additional attribute of either the subject or secured resource in addition to roles and groups and is policy-based in the sense that it uses policies rather than static permissions to define what is authorized.

This framework is most closely aligned to RBAC but has some ABAC capabilities as well, e.g. you can secure an operation based on additional attributes about the subject and also use hierarchies to implement more fine-grained permissions.

The main advantage of the framework is its ease of implementation and speed since operations will take place in memory, and this leads us to its main limitation in that it is intended to secure activities on the class of the resource rather than individual resources i.e. we can easily secure Order.Create or Order.Edit to a role SalesClerk but restricting editing of Order 231 for example would require domain knowledge and/or database access to know who can edit this particular order and so it out of scope.

Principals and Claims

Most developers are aware that .NET processes execute under a user context called a Principal, this is easily accessed via Thread.CurrentPrincipal or in a web application via the controllers User property. Since .NET 4.5 these are all ClaimsPrincipals which can contain multiple ClaimsIdentities each holding Claims. A claim is a security attribute with some basic properties...

  • Type: The type of claim, e.g. Role, Name, Team, Age
  • Value: The value of the claim, e.g. Admin, 18, Team A
  • Issuer: Who says this claim is true, can be important to ensure the integrity of the system
  • ValueType: What type of data is the claim, default is string but can be any type e.g. integer, date

In this model, roles and the user's name are not anything special, just particular types of claim.

How we arrive at these claims is out of scope for this project but in general they are the responsibility of the authentication system where users, groups and roles are allocated and can also be further enhanced by a process called claims transformation which happens as part of the ASP.NET pipeline.

Resources and Activities

A lot of examples of securing .NET applications show security against activities such as CreateUser and UpdateUser and one issue I have with this is that the number of securable items can grow very rapidly e.g. in a fairly small system of 20 resource types with just CRUD operations you have 80 securable items and this quite rapidly becomes difficult to manage. This project took a slightly different perspective akin to how REST models things and explicitly separated the resource from the activity that is being secured.

This allows you to define granular but wide-ranging permissions e.g. grant Delete permission to the Admin role on all resources or grant all permissions on Order to the SalesClerk role.

Grant vs Deny Permissions

One concept that simplifies security maintenance is the concept of Deny permissions which take precedence over any Grant. For example, your normal sales clerk Alice is off for the say so you grant Bob the SalesClerk role, but you don't want him to be able to do too much damage so you also say Bob Deny Order.Delete, now this Deny permission for Bob will take precedence over the SalesClerk's standard permissions to be able to do anything to an Order.

Defining the security model

Following on from the idea of resources and activities, your security model should be focused on your business functionality rather than how you map this to a particular software implementation. If you do this, it will be much easier to explain to the business sponsors what is being secured and why, and should more naturally flow from any analysis discussions e.g. "We only want managers to be able to approve invoices".

Another way of developing this structure is rather than resource/activity is noun/verb; nouns represent objects that need securing and verbs are the operations that users will perform. You can stray from this pattern, but it should be intentional rather than accidental.

Lets take a simple business model

  • Orders: Orders placed in the system
  • Invoices: Invoiced orders
  • Sales Clerk: Enters/amends orders in the system
  • Invoice Clerk: Processes invoices
  • Sales Manager: Supervises sales clerks
  • Finance Manager: Supervises invoice clerks
  • Finance Director: Supervises managers

So what we want to implement is the following

  • Sales Clerk: Can read, create, amend, ship and cancel Orders but can't delete them, has no rights to Invoices
  • Sales Manager: Can read, ship and cancel Orders but can only read Invoices
  • Invoice Clerk: Can read, create, amend and cancel Invoices but not approve or delete them, has no rights on Orders
  • Finance Manger: Can read, approve or cancel Invoices but only read Orders
  • Finance Director: Can read and delete Orders and Invoices but has no rights to create, amend or approve

This gives a reasonable separation of powers where it requires at least two actors to colude to create an order, raise an invoice and get it approved.

This can be represented in the following table, blank nodes means no rights

Resource Activity SC SM IC FM FD
Order Read x x x x
Order Create x
Order Edit x
Order Ship x x
Order Cancel x x
Order Delete x
Invoice Read x x x x
Invoice Create x
Invoice Edit x
Invoice Approve x
Invoice Cancel x
Invoice Delete x

We can define the model in either XML or JSON according to preference, here's the XML representation

<financeAuthorization name="Finance" allowUnauthenticated="false">
    <activity name="Order.Read">
        <allow roles="SalesManager, FinanceManager, FinanceDirector" />
    </activity>
    <activity name="Order">
        <allow roles="SalesClerk" />
    </activity>
    <activity name="Order.Ship">
        <allow roles="SalesManager" />
    </activity>
    <activity name="Order.Cancel">
        <allow roles="SalesManager" />
    </activity>
    <activity name="Order.Delete">
        <allow roles="FinanceDirector" />
        <deny roles="SalesClerk" />
    </activity>
    <activity name="Invoice.Read">
        <allow roles="SalesManager, FinanceManager, FinanceDirector" />
    </activity>
    <activity name="Invoice">
        <allow roles="InvoiceClerk" />
    </activity>
    <activity name="Invoice.Approve">
        <deny roles="InvoiceClerk" />
        <allow roles="FinanceManager" />
    </activity>
    <activity name="Invoice.Cancel">
        <deny roles="InvoiceClerk" />
        <allow roles="FinanceManager" />
    </activity>
    <activity name="Invoice.Delete">
        <deny roles="InvoiceClerk" />
        <approve roles="FinanceDirector" />
    </activity>
</financeAuthorization>

It does not matter what order the activities are defined in, once they are loaded into memory we produce a canoncial ordering so we always process from most specific to least specific, see hiearchies for more details.

This allow us to define wider rules and then pare them back with exclusions e.g.

    <activity name="Invoice">
        <allow roles="InvoiceClerk" />
    </activity>
    <activity name="Invoice.Approve">
        <deny roles="InvoiceClerk" />
        <allow roles="FinanceManager" />
    </activity>

This grants all activities on Invoice to the InvoiceClerk, but then specifically disallows them from Approve whilst granting that right to the FinanceManager.

One general principle is not to have hierarchies of roles, if you do so it makes it much more difficult to see the implications of changes to the security model and can lead to inadvertent breaches of separation of concern rules. For example, if instead of explicitly denoting a FinanceDirector's permissions we also said that they also inherit the other roles, then a bad actor could create an order, invoice it and approve it without the security model complaining.

Hierarchies

Sometimes you need more fine-grained security, for example you want to provide some property-level permissioning so employee information is available but their salary information is not. Another example might be that you want people to being to print some reports but exclude them from more sensitive data; to do this we have the concept of both resource and activity hierarchies.

This is achieved by using a path separator "/" in either the resource, the activity or both, the authorization system then checks them using a canonical ordering from most specific to least specific to determine the permission to apply, and this first one to make a decision is the one applied. For example given the following security fragment...

   <activity name="Reports.Print" authorized="true" />
   <activity name="Reports/Sales.Print" authorized="false">
       <allow roles="Sales" />
   </activity>
   <activity name="Reports/Employees.Print" authorized="false">
       <allow roles="HR" />
   </activity>

This says that all authenticated users are allow to print reports, but only Sales are allowed to print Sales reports and only HR are allowed to print Employee reports.

So if we ask to authorize Reports/Sales.Print the following activities would be considered in this order

Reports/Sales.Print -> Reports.Print -> .Print

By using 'authorized="false"' at the sublevels we are forcing the decision to be evaluated there, otherwise it would bubble up to Reports.Print. The other alternative is to use Deny permissions but this can get tricky as a Deny is an immediate block on further evaluation, e.g. given

   <activity name="Reports.Print">
       <allow roles="Users" />
   </activity>
   <activity name="Reports/Sales.Print" authorized="false">
       <deny roles="Users" />
       <allow roles="Sales" />
   </activity>
   <activity name="Reports/Employees.Print" authorized="false">
       <deny roles="Users" />
       <allow roles="HR" />
   </activity>

The problem is if you make a HR user a member of Users, they would be locked out of their reports due to the Deny taking precedence. This also brings up a good point of generally avoiding groups such as Users. We effectively get this overall group by virtue of users being authenticated and you can include everyone with for an activity with a 'authorized="true"' clause or even 'allowUnauthenticated="true"', for example one standard entry I have in most security files is

<activity name="Home.Index" authorized="true" allowUnauthenticated="true" />

To use the hierarchies effectively you might need to make more than one call to the authorization system...

  • A employee MVC controller is secured with Employee.Read but you do an additional check on Employee/Salary.Read before returning that data.
  • The top level Reports link might be secured with Reports.Print but a particular report might be secured with Reports/Salary.Print.

Securing an Application

To secure application we need to:

  • Tell the system to enforce the security model
  • Map the controller/action to the resource/activity security model

Some of this code is generic and is in the Meerkat.Security assembly, but the MVC specific code is held in Meerkat.Security.Mvc and this should be referenced from your MVC project. The process to secure a WebApi application is the same as that for an MVC application, but a different assembly must be referenced as MVC and WebApi do not share the same code base.

Enforce security model

We want the security model to be enforced with as little programmer intervention as possible, so the easy way is to introduce a custom attribute similar to the built-in Authorize attribute. Our one is called ActivityAuthorize and can either be registered via a global filter, or explicitly placed on a controller. If introduced as a global filter, the system automatically determines the relevant controller/action information from the routing information.

It is possible to put multiple ActivityAuthorize on a controller action but be aware that all such attributes must evaluate to true for the user to be granted access; this may be useful where more complex security requirements arise.

Mapping controller/action

There is a fairly natural mapping from controller/action to resource/activity but it sometimes needs a little help so that we do not unnecessarily multiple up activities. For a standard MVC controller you will typically have the following actions, the relevant activity is shown afterwards

  • Index: Displays the list of data - Read
  • Details: Displays a single item - Read
  • Create: Creates an entity - Create
  • Edit: Modifies an entity - Update
  • Delete: Deletes an entity - Delete

For a WebAPI controller we have a slightly different set if we are following REST API rules

  • Get: Displays the list of data - Read
  • Get Displays a single item - Read
  • Post: Creates an entity - Create
  • Put: Modifies an entity - Update
  • Patch: Modifies an entity - Update
  • Delete: Deletes an entity - Delete

If you are using an OData controller you may also need to support sub-types of your main entities

  • GetFromXXX: Displays the list of data - Resource: XXXX, Read
  • GetXXX Displays a single item - Resource: XXXX, Read
  • PostXXX: Creates an entity - Resource: XXXX, Create
  • PutXXX: Modifies an entity - Resource: XXXX, Update
  • PatchXXX: Modifies an entity - Resource: XXXX, Update

To avoid having to specify the changed values on every controller, we introduce a new service IControllerActivityMapper with a default implementation that performs the basic mappings shown above.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].