Auth Overview
Added: v1.6.0
Zen's auth system is built around the Authenticator interface - any authentication strategy can be plugged in by implementing a single method.
Interface
go
type Authenticator interface {
Authenticate(r *http.Request) (*User, error)
}Return a *User on success or an error to trigger the configured error handler.
Usage
Apply authentication as middleware. Optionally skip routes with SkipPaths:
go
r.EnableAuth(jwtAuth)
r.EnableAuth(jwtAuth, zen.SkipPaths("/health"))User Struct
go
type User struct {
ID string
Username string
Roles []string
Claims map[string]any
}- Role-Based Access (RBAC) -
r.EnableRBAC,RequireRole, anduser.HasRole("admin") - Permission-Based Access (PBAC) -
RequirePermission,RequireAnyPermission,RequireAllPermissions, anduser.HasPermission(...) - Claim-Based Access -
RequireClaimfor enforcing specific claim values Roleslist role names assigned to the user; permissions are resolved through the role registryClaimscarry provider-specific data (e.g., OIDC claims).
Retrieving the User
go
user := c.GetUser()Returns the authenticated *User from the context, or nil if not authenticated.
Password Helpers
Use the built-in password helpers for hashing and validation:
go
hashed, err := auth.HashPassword("secret")
hashed, err := auth.HashPassword("secret", 12) // optional bcrypt cost
ok := auth.ValidatePassword(hashed, "secret")