{"id":19298256,"url":"https://github.com/casbin/mux-authz","last_synced_at":"2025-04-22T09:32:27.445Z","repository":{"id":57537583,"uuid":"285667098","full_name":"casbin/mux-authz","owner":"casbin","description":"gorilla/mux's RBAC \u0026 ABAC Authorization middleware based on Casbin","archived":false,"fork":false,"pushed_at":"2023-08-06T13:59:02.000Z","size":5,"stargazers_count":6,"open_issues_count":1,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-29T12:34:10.974Z","etag":null,"topics":["abac","acl","authz","casbin","gorilla-mux","middleware","mux","plugin","rbac","security"],"latest_commit_sha":null,"homepage":"https://github.com/casbin/casbin","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/casbin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2020-08-06T20:39:13.000Z","updated_at":"2022-11-10T15:08:27.000Z","dependencies_parsed_at":"2023-09-26T09:07:53.719Z","dependency_job_id":null,"html_url":"https://github.com/casbin/mux-authz","commit_stats":{"total_commits":3,"total_committers":1,"mean_commits":3.0,"dds":0.0,"last_synced_commit":"86a2975cf31edc9f27a7fcdbbe91e9d1cbd65983"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casbin%2Fmux-authz","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casbin%2Fmux-authz/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casbin%2Fmux-authz/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casbin%2Fmux-authz/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/casbin","download_url":"https://codeload.github.com/casbin/mux-authz/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223768700,"owners_count":17199394,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["abac","acl","authz","casbin","gorilla-mux","middleware","mux","plugin","rbac","security"],"created_at":"2024-11-09T23:07:24.873Z","updated_at":"2024-11-09T23:07:25.466Z","avatar_url":"https://github.com/casbin.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Mux-authz [![Build Status](https://travis-ci.org/casbin/mux-authz.svg?branch=master)](https://travis-ci.org/casbin/mux-authz)[![Coverage Status](https://coveralls.io/repos/github/casbin/mux-authz/badge.svg?branch=master)](https://coveralls.io/github/casbin/mux-authz?branch=master)\n\nMux-authz is an authorization middleware for [Mux](https://github.com/gorilla/mux), it’s based on [Casbin](https://github.com/casbin/casbin). If you have better suggestions, please submit the issue.\n\n## Installation\n\n```\ngo get github.com/casbin/mux-authz\n```\n\n## Prepare\n\nThis repo is based on [Casbin](http://github.com/casbin/casbin), so you need to prepare two files in advance.\n\nThe Casbin model file describes access control models like ACL, RBAC, ABAC, etc. \n\nThe Casbin policy file describes the authorization policy rules. \n\nFor how to write these files, please refer to: https://github.com/casbin/casbin#get-started\n\n## How to use\n\n1. Create your Casbin model file [authz_model.conf](https://github.com/casbin/mux-authz/blob/master/authz_model.conf) and Casbin policy file [authz_policy.csv](https://github.com/casbin/mux-authz/blob/master/authz_policy.csv) into this folder. \n\n2. Load model and policy \n\n   ```go\n   c := new(authz.CasbinAuthorizer)\n   err :=c.Load(\"authz_model.conf\", \"authz_policy.csv\")\n   if err != nil {\n   \tfmt.Println(err.Error())\n   }\n   ```\n\n3. Use Middleware\n\n   ```go\n   r :=mux.NewRouter()\n   r.HandleFunc(\"/{url:[A-Za-z0-9\\\\/]+}\", handler)\n   r.Use(c.Middleware)\n   ```\n\n   Note: Now we only support check the whole path. So we recommend using path with regular expressions in the HandleFunc(). In this way, you don't have to worry about 404 due to the number of ‘/‘.For example `/book1/1` and `/bookshelf1/book1/1`.\n\nIf you have any questions, you can refer to [mux-authz_test.go](https://github.com/casbin/mux-authz/blob/master/mux-authz_test.go).\n\n## Complete Example.\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\tauthz \"github.com/casbin/mux-authz\"\n\t\"github.com/gorilla/mux\"\n\t\"log\"\n\t\"net/http\"\n)\n\nfunc main() {\n\tc := new(authz.CasbinAuthorizer)\n\terr :=c.Load(\"authz_model.conf\", \"authz_policy.csv\")\n\tif err != nil {\n\t\tfmt.Println(err.Error())\n\t}\n\n\t// A very simple health check handler.\n\thandler := http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) {\n\t\tw.Header().Set(\"Content-Type\", \"application/json\")\n\t\tw.WriteHeader(http.StatusOK)\n\t})\n\n\tr :=mux.NewRouter()\n\tr.HandleFunc(\"/{url:[A-Za-z0-9\\\\/]+}\", handler)\n\tr.Use(c.Middleware)\n\tlog.Fatal(http.ListenAndServe(\":8080\",r))\n}\n```\n\nNote: This plugin only supports HTTP basic authentication to get the logged-in user name, if you use other kinds of authentication like OAuth, LDAP, etc, you may need to customize this plugin.\n\n## How to control the access\n\nThe authorization determines a request based on `{subject, object, action}`, which means what `subject` can perform what `action` on what `object`. In this plugin, the meanings are:\n\n1. `subject`: the logged-on user name\n2. `object`: the URL path for the web resource like \"dataset1/item1\"\n3. `action`: HTTP method like GET, POST, PUT, DELETE, or the high-level actions you defined like \"read-file\", \"write-blog\"\n\nFor how to write authorization policy and other details, please refer to [the Casbin's documentation](https://github.com/casbin/casbin).\n\n## Getting Help\n\n- [Casbin](https://github.com/casbin/casbin)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcasbin%2Fmux-authz","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcasbin%2Fmux-authz","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcasbin%2Fmux-authz/lists"}