chakra

package module
v0.0.0-...-4ade35b Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2015 License: MIT Imports: 4 Imported by: 0

README

chakra

GoDoc Widget

chakra allows you to easily add resource/operation based access control to https://github.com/pressly/chi

You can define any access control rules you want as a middleware - chakra will make sure they are checked.

Forcing security as you code with minimum overhead

chakra will:

  • build resource routes (identifiers) for your access checks
  • inject access control right before the last handlers in chain - not optional
  • not let you run your code without providing access control function - it will panic
  • not let you create new router without providing the next part of resource route or explicitly telling it to use parent one - it will panic

It's not airtight - you can still escape the added security, but it requires more effort than being secure. You can write a buggy access control function too - it can't help with that - but at least you won't forget to use it.

How to use it?

Almost exactly the same way you'd use chi

chi without access control

r := chi.NewRouter()
r.Mount("/api", func(r chi.Router){
    r = chi.NewRouter()
    r.Post("/endpoint1", handler1)
    ...
})

chakra - chi with access control

chakra.SetAC(myAccessControlRules) // you only do this once

r := chakra.NewRouter(chakra.UseParentRoute)
r.Mount("/api", func(r chi.Router){
    r = chakra.NewRouter("secure_api")
    r.Post("/endpoint1", handler1)
    ...
})

And you are DONE! - myAccessControlRules will be called right before handler1 to check permissions to POST to {"secure_api", "endpoint1"} resource

Examples

Example access control function

Credits

TODO

  • More examples
  • A lot of unit tests (and then some!)

Contributions are always welcome - fork it, do your thing, open a pull request!

License

Copyright (c) 2015 Maciej Lisiewski

Licensed under MIT License

Documentation

Index

Constants

View Source
const UseParentRoute = "#"

UseParentRoute passed as route to NewRouter results in nothing being added to acl.route on subrouter creation - parent route is used instead

Variables

This section is empty.

Functions

func Route

func Route(part string) func(chi.Handler) chi.Handler

Route builds a resource identifier from the middleware chain. This resource identifier along with the operation (HTTP verb) can be used for determining access to a resource

func SetAC

func SetAC(fn func(chi.Handler) chi.Handler)

SetAC sets the function that is going to be used by chakra for resource control

Types

type Mux

type Mux struct {
	*chi.Mux
}

Mux implements chi.Router and makes access control with chi a bit easier

func NewRouter

func NewRouter(route string) *Mux

NewRouter creates new chakra Mux and appends its route to acl.route, unless the passed route is chakra.UseParentRoute.

func (*Mux) Connect

func (r *Mux) Connect(pattern string, handlers ...interface{})

Connect is a wrapper of chi.Connect for chakra.Mux. It appends the pattern passed to it to acl.route and injects access control middleware as penultimate one - just before handler(s)

func (*Mux) Delete

func (r *Mux) Delete(pattern string, handlers ...interface{})

Delete is a wrapper of chi.Delete for chakra.Mux. It appends the pattern passed to it to acl.route and injects access control middleware as penultimate one - just before handler(s)

func (*Mux) Get

func (r *Mux) Get(pattern string, handlers ...interface{})

Get is a wrapper of chi.Get for chakra.Mux. It appends the pattern passed to it to acl.route and injects access control middleware as penultimate one - just before handler(s)

func (*Mux) Group

func (r *Mux) Group(fn func(r chi.Router)) chi.Router

Group is a wrapper of chi.Group for chakra.Mux. It works exactly the same as the underlying chi method

func (*Mux) Handle

func (r *Mux) Handle(pattern string, handlers ...interface{})

Handle is a wrapper of chi.Handle for chakra.Mux. It appends the pattern passed to it to acl.route and injects access control middleware as penultimate one - just before handler(s)

func (*Mux) Head

func (r *Mux) Head(pattern string, handlers ...interface{})

Head is a wrapper of chi.Head for chakra.Mux. It appends the pattern passed to it to acl.route and injects access control middleware as penultimate one - just before handler(s)

func (*Mux) Mount

func (r *Mux) Mount(path string, handlers ...interface{})

Mount is a wrapper of chi.Mount for chakra.Mux. It works exactly the same as the underlying chi method

func (*Mux) Options

func (r *Mux) Options(pattern string, handlers ...interface{})

Options is a wrapper of chi.Options for chakra.Mux. It appends the pattern passed to it to acl.route and injects access control middleware as penultimate one - just before handler(s)

func (*Mux) Patch

func (r *Mux) Patch(pattern string, handlers ...interface{})

Patch is a wrapper of chi.Patch for chakra.Mux. It appends the pattern passed to it to acl.route and injects access control middleware as penultimate one - just before handler(s)

func (*Mux) Post

func (r *Mux) Post(pattern string, handlers ...interface{})

Post is a wrapper of chi.Post for chakra.Mux. It appends the pattern passed to it to acl.route and injects access control middleware as penultimate one - just before handler(s)

func (*Mux) Put

func (r *Mux) Put(pattern string, handlers ...interface{})

Put a wrapper of chi.Put for chakra.Mux. It appends the pattern passed to it to acl.route and injects access control middleware as penultimate one - just before handler(s)

func (*Mux) Route

func (r *Mux) Route(pattern string, fn func(r chi.Router)) chi.Router

Route is a wrapper of chi.Route for chakra.Mux. It works exactly the same as the underlying chi method

func (*Mux) Trace

func (r *Mux) Trace(pattern string, handlers ...interface{})

Trace is a wrapper of chi.Trace for chakra.Mux. It appends the pattern passed to it to acl.route and injects access control middleware as penultimate one - just before handler(s)

func (*Mux) Use

func (r *Mux) Use(middlewares ...interface{})

Use appends middleware(s) to chi middleware stack

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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