Skip to content

RBAC(Role-Based Access Control) Support #2000

@goginenibhavani2000

Description

@goginenibhavani2000

Problem:

GoFr lacks built-in role-based access control, forcing developers to implement custom authorization logic for each route. This leads to inconsistent security implementations and code duplication.

Goal :

Create a declarative RBAC middleware that integrates seamlessly with GoFr.

  1. JSON Configuration: Define roles and permissions declaratively
  2. Multiple Usage Patterns:
  •   app.Use(rbac.Middleware(config))  // Global
    
  •  app.GET("/admin", rbac.RequireRole("admin")(handler)) // Route-specific
    
  1. Flexible Role Extraction: Support JWT claims, sessions, and custom extractors
  2. Pattern Matching: Handle wildcards () and path patterns (/users/)
  3. Helper Functions: IsAdmin(), HasRole(), GetUserRoles()

Implementation Plan

  • Models-

// RoleConfig defines the roles and their allowed paths/permissions.
type RoleConfig struct {
Roles map[string][]string json:"roles" // e.g., {"admin": ["*"], "editor": ["/edit", "/update"]}
}

// Options allow flexible configuration.
type Options struct {
RoleExtractor func(*Context) ([]string, error) // customized function can be passed
Config RoleConfig
}

  • func to load RoleConfig (LoadRoleConfig(path string) (rbac.RoleConfig, error)) and validate config
  • Implement func Middleware(opt Options) http.Handler for global use
  • Implement route-specific like RequireRole(role string) http.Handler
  • isAuthorized function should support pattern match(match(pattern, path string) bool) for wildcard and prefix matching
  • Implement helper functions: IsAdmin(c *Context) bool , HasRole(c *Context, role string), GetUserRoles(c *Context) ([]string, error)

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions