Documentation
¶
Index ¶
- Constants
- Variables
- func ContextWithUser(ctx context.Context, user *User) context.Context
- func GenerateToken(user *User, secret []byte, expiry time.Duration) (string, error)
- func Middleware(secret []byte) func(http.Handler) http.Handler
- func RequireRole(roles ...string) func(http.Handler) http.Handler
- type Claims
- type User
- type UserStore
Constants ¶
View Source
const ( RoleAdmin = "admin" RoleEditor = "editor" RoleViewer = "viewer" )
Role constants for RBAC.
Variables ¶
View Source
var ValidRoles = map[string]bool{ RoleAdmin: true, RoleEditor: true, RoleViewer: true, }
ValidRoles is the set of recognised role strings.
Functions ¶
func ContextWithUser ¶
ContextWithUser returns a new context with the given user stored in it. This is primarily useful for testing.
func GenerateToken ¶
GenerateToken creates a signed JWT for the given user.
func Middleware ¶
Middleware returns HTTP middleware that validates JWT tokens from the Authorization header. Requests to health and login endpoints are allowed through without authentication.
Types ¶
type Claims ¶
type Claims struct {
jwt.RegisteredClaims
UserID string `json:"user_id"`
Email string `json:"email"`
Role string `json:"role"`
TenantID string `json:"tenant_id,omitempty"`
}
Claims are the JWT claims embedded in each token.
type User ¶
type User struct {
ID string `json:"id"`
Email string `json:"email"`
Name string `json:"name"`
Role string `json:"role"`
TenantID string `json:"tenant_id,omitempty"`
PasswordHash string `json:"-"`
CreatedAt time.Time `json:"created_at"`
}
User represents an authenticated user.
func UserFromContext ¶
UserFromContext returns the authenticated user stored in the request context, or nil if no user is present.
type UserStore ¶
type UserStore interface {
Create(ctx context.Context, user *User) error
GetByEmail(ctx context.Context, email string) (*User, error)
GetByID(ctx context.Context, id string) (*User, error)
List(ctx context.Context) ([]User, error)
UpdateRole(ctx context.Context, id, role string) error
}
UserStore is the interface for user persistence.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package dynamostore implements auth.UserStore backed by DynamoDB via the generic dynamostore package.
|
Package dynamostore implements auth.UserStore backed by DynamoDB via the generic dynamostore package. |
Click to show internal directories.
Click to hide internal directories.