wauth

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

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

Go to latest
Published: Jan 9, 2022 License: MIT Imports: 4 Imported by: 0

README

wauth

GitHub Repo stars GitHub GitHub go.mod Go version GitHub CI Status Go Report Card Go.Dev reference codecov

wauth is an authorization middleware for Gin, it's based on https://github.com/casbin/casbin.

Installation

go get github.com/wyy-go/wauth

Simple Example

package main

import (
	"net/http"

	"github.com/casbin/casbin/v2"
	"github.com/gin-gonic/gin"

	"github.com/wyy-go/wauth"
)

func main() {
	e, err := casbin.NewEnforcer("../casbin_model.conf", "../casbin_policy.csv")
	if err != nil {
		panic(err)
	}

	router := gin.New()
	router.Use(
		func(c *gin.Context) {
			wauth.CtxWithSubject(c, "alice")
		},
		wauth.NewAuthorizer(e),
	)
	router.GET("/dataset1/resource1", func(c *gin.Context) {
		c.String(http.StatusOK, "alice own this resource")
	})
	router.GET("/dataset2/resource1", func(c *gin.Context) {
		c.String(http.StatusOK, "alice do not own this resource")
	})
	router.Run(":8080")
}

Documentation

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 MIT License. See the LICENSE file for the full license text.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CtxWithSubject

func CtxWithSubject(c *gin.Context, subject string)

func NewAuthorizer

func NewAuthorizer(e casbin.IEnforcer, opts ...Option) gin.HandlerFunc

NewAuthorizer returns the authorizer, uses a Casbin enforcer as input

func Subject

func Subject(c *gin.Context) string

Types

type BasicAuthorizer

type BasicAuthorizer struct {
	// contains filtered or unexported fields
}

BasicAuthorizer stores the casbin handler

type Option

type Option func(*Options)

func WithErrorFn

func WithErrorFn(fn func(*gin.Context, error)) Option

func WithForbiddenFn

func WithForbiddenFn(fn func(*gin.Context)) Option

func WithSkipAuthFn

func WithSkipAuthFn(fn func(*gin.Context) bool) Option

func WithSubjectFn

func WithSubjectFn(fn func(*gin.Context) string) Option

type Options

type Options struct {
	// contains filtered or unexported fields
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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