Skip to content

montedelgallo/gin_auth_roles

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gin_auth_roles

This package is inspired on Authz and modified so to not support only basic HTTP authentication. it's based on https://github.com/casbin/casbin.

Installation

go get github.com/giovapanasiti/gin_auth_roles

Simple Example

To make it work you have to set my_user_role in the context when you authenticate your user:

func UpdateContextUserModel(c *gin.Context, my_user_id uint) {
	var myUserModel UserModel
	if my_user_id != 0 {
		db := common.GetDB()
		db.First(&myUserModel, my_user_id)
	}
	c.Set("my_user_id", my_user_id)
	c.Set("my_user_model", myUserModel)
	c.Set("my_user_role", myUserModel.Role)
}
package main

import (
	"net/http"

	"github.com/casbin/casbin"
	"github.com/giovapanasiti/gin_auth_roles"
	"github.com/gin-gonic/gin"
)

func main() {
	// load the casbin model and policy from files, database is also supported.
	e := casbin.NewEnforcer("auth.conf", "policy.csv")

	// define your router, and use the Casbin authz middleware.
	// the access that is denied by authz will return HTTP 403 error.
    router := gin.New()
    router.Use(gin_auth_roles.NewAuthorizer(e))
}

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 role
  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.

About

Role-based authorization manager for Gin Gonic

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages