Giter Club home page Giter Club logo

mux-authz's Introduction

Mux-authz Build StatusCoverage Status

Mux-authz is an authorization middleware for Mux, it’s based on Casbin. If you have better suggestions, please submit the issue.

Installation

go get github.com/casbin/mux-authz

Prepare

This repo is based on Casbin, so you need to prepare two files in advance.

The Casbin model file describes access control models like ACL, RBAC, ABAC, etc.

The Casbin policy file describes the authorization policy rules.

For how to write these files, please refer to: https://github.com/casbin/casbin#get-started

How to use

  1. Create your Casbin model file authz_model.conf and Casbin policy file authz_policy.csv into this folder.

  2. Load model and policy

    c := new(authz.CasbinAuthorizer)
    err :=c.Load("authz_model.conf", "authz_policy.csv")
    if err != nil {
    	fmt.Println(err.Error())
    }
  3. Use Middleware

    r :=mux.NewRouter()
    r.HandleFunc("/{url:[A-Za-z0-9\\/]+}", handler)
    r.Use(c.Middleware)

    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.

If you have any questions, you can refer to mux-authz_test.go.

Complete Example.

package main

import (
	"fmt"
	authz "github.com/casbin/mux-authz"
	"github.com/gorilla/mux"
	"log"
	"net/http"
)

func main() {
	c := new(authz.CasbinAuthorizer)
	err :=c.Load("authz_model.conf", "authz_policy.csv")
	if err != nil {
		fmt.Println(err.Error())
	}

	// A very simple health check handler.
	handler := http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")
		w.WriteHeader(http.StatusOK)
	})

	r :=mux.NewRouter()
	r.HandleFunc("/{url:[A-Za-z0-9\\/]+}", handler)
	r.Use(c.Middleware)
	log.Fatal(http.ListenAndServe(":8080",r))
}

Note: 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.

How to control the access

The 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:

  1. subject: the logged-on user name
  2. object: the URL path for the web resource like "dataset1/item1"
  3. action: HTTP method like GET, POST, PUT, DELETE, or the high-level actions you defined like "read-file", "write-blog"

For how to write authorization policy and other details, please refer to the Casbin's documentation.

Getting Help

mux-authz's People

Contributors

00lt00 avatar hsluoyz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

mux-authz's Issues

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.