authz

package module
v0.0.0-...-cc3fff6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 22, 2021 License: Apache-2.0 Imports: 9 Imported by: 0

README

Caddy-authz

Caddy-authz is an authorization middleware for Caddy, it's based on https://github.com/casbin/casbin. It includes basicauth checks agains a bcrypt password file.

Installation

go get github.com/casbin/caddy-authz

Caddyfile syntax

http://localhost:80 {
    authz "/folder/to/caddy_binary/authz_model.conf" /folder/to/caddy_binary/authz_policy.csv" AuthRealm authfile
    ...
}

The authz directive specifies the path to Casbin model file (.conf) and Casbin policy file (.csv). 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

A working example

  1. cd into the folder of caddy binary.

  2. Put your Casbin model file authz_model.conf and Casbin policy file authz_policy.csv into this folder.

  3. Add authz directive to your Caddyfile like:

http://localhost:80 {
    authz "authz_model.conf" "authz_policy.csv" MyRealm bcrypt.pass
    root "/my-website.net"
}
  1. Run caddy and enjoy.

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

License

This project is under Apache 2.0 License. See the LICENSE file for the full license text.

Documentation

Index

Constants

View Source
const (
	// MustAuthenticate is returned if authentication is required.
	MustAuthenticate = 0
	// AccessAllowed is returned if the user has access to the resource.
	AccessAllowed = 1
	// AccessDenied is returned if the user has no access to the resource.
	AccessDenied = 2
	// AnonymousAccess is returned if the access is authorized for anonymous access.
	AnonymousAccess = 1
	// IdentifiedAccess is returned if the access is authorized for an identified user.
	IdentifiedAccess = 2
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Authorizer

type Authorizer struct {
	AuthConfig struct {
		ModelPath    string
		PolicyPath   string
		Realm        string
		PasswordFile string
	}

	Enforcer      *casbin.Enforcer
	PasswordCheck authfile.IAuthenticationService
}

func (Authorizer) CaddyModule

func (Authorizer) CaddyModule() caddy.ModuleInfo

CaddyModule returns the Caddy module information.

func (*Authorizer) CheckPermission

func (a *Authorizer) CheckPermission(r *http.Request) int

CheckPermission checks the user/method/path combination from the request. Returns true (permission granted) or false (permission forbidden)

func (*Authorizer) Provision

func (a *Authorizer) Provision(ctx caddy.Context) error

Provision implements caddy.Provisioner.

func (Authorizer) ServeHTTP

func (a Authorizer) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error

ServeHTTP implements caddyhttp.MiddlewareHandler.

func (*Authorizer) UnmarshalCaddyfile

func (a *Authorizer) UnmarshalCaddyfile(d *caddyfile.Dispenser) error

UnmarshalCaddyfile implements caddyfile.Unmarshaler.

func (*Authorizer) Validate

func (a *Authorizer) Validate() error

Validate implements caddy.Validator.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL