package rbac import ( "net/http" "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" "github.rackspace.com/gorax/tools/passkey" ) // swagger:model GinAuthError type GinAuthError struct { // Exact error message encountered // Required: true // Example: you do not have the correct permission level to perform this action Error string `json:"error"` // And error was encountered while trying to retrieve user's Active Directory information // Required: false // Example: error binding to active directory Message string `json:"message"` } // GinAuthorization : gin middleware for active directory role based access control // Requires a couple variables to be available in the gin context: // "log" = *logrus.Entry // "passkey" = *passkey.Client // "username" = string // Username to check role of func GinAuthorization(role int) gin.HandlerFunc { return func(c *gin.Context) { log := c.MustGet("log").(*logrus.Entry) pk := c.MustGet("passkey").(*passkey.Client) username := c.MustGet("username").(string) log.Debug("Checking user's RBAC permissions") // Get the RBAC level of the user as authenticated via coreAPI ad, err := BindAD(username, pk) if err != nil { c.AbortWithStatusJSON(http.StatusInternalServerError, GinAuthError{ Error: err.Error(), Message: "error binding to active directory", }) return } rbacMsg, ok := Allowed(ad, role) log.Debug(rbacMsg) if !ok { c.AbortWithStatusJSON(http.StatusForbidden, GinAuthError{Error: ErrForbidden}) return } } }