You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs: document and exemplify route-based authorization with Gin and JWT (#356)
- Add comprehensive documentation and examples for the Authorizer function, including its usage patterns, function signature, role/path/method-based access control, recommendations, and advanced usage in English, Simplified Chinese, and Traditional Chinese
- Introduce a new example project that demonstrates route-based authorization using Gin and JWT, including example `go.mod`, `go.sum`, and a fully working `main.go` with three-tier access (admin, user, guest) and related route protection logic
- Expand README navigation with sections for Authorizer understanding, practical examples, and best practices
Signed-off-by: appleboy <[email protected]>
The `Authorizer` function is a crucial component for implementing role-based access control in your application. It determines whether an authenticated user has permission to access specific protected routes.
619
+
620
+
### How Authorizer Works
621
+
622
+
The `Authorizer` is called **automatically** during the JWT middleware processing for any route that uses `MiddlewareFunc()`. Here's the execution flow:
623
+
624
+
1.**Token Validation**: JWT middleware validates the token
625
+
2.**Identity Extraction**: `IdentityHandler` extracts user identity from token claims
626
+
3.**Authorization Check**: `Authorizer` determines if the user can access the resource
627
+
4.**Route Access**: If authorized, request proceeds; otherwise, `Unauthorized` is called
628
+
629
+
### Authorizer Function Signature
630
+
631
+
```go
632
+
func(c *gin.Context, dataany) bool
633
+
```
634
+
635
+
- `c *gin.Context`: The Gin context containing request information
636
+
- `data any`: User identity data returned by `IdentityHandler`
637
+
- Returns `bool`: `true` for authorized access, `false` to deny access
638
+
639
+
### Basic Usage Examples
640
+
641
+
#### Example 1: Role-Based Authorization
642
+
643
+
```go
644
+
func authorizeHandler() func(c *gin.Context, data any) bool {
645
+
returnfunc(c *gin.Context, data any) bool {
646
+
ifv, ok:= data.(*User); ok && v.UserName == "admin" {
647
+
returntrue// Only admin users can access
648
+
}
649
+
returnfalse
650
+
}
651
+
}
652
+
```
653
+
654
+
#### Example 2: Path-Based Authorization
655
+
656
+
```go
657
+
funcauthorizeHandler() func(c *gin.Context, data any) bool {
658
+
returnfunc(c *gin.Context, data any) bool {
659
+
user, ok:= data.(*User)
660
+
if !ok {
661
+
returnfalse
662
+
}
663
+
664
+
path:= c.Request.URL.Path
665
+
666
+
// Admin can access all routes
667
+
if user.Role == "admin" {
668
+
returntrue
669
+
}
670
+
671
+
// Regular users can only access /auth/profile and /auth/hello
### Setting Up Different Authorization for Different Routes
718
+
719
+
To implement different authorization rules for different route groups, you can create multiple middleware instances or use path checking within a single Authorizer:
0 commit comments