auth

package
v1.10.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 4, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RoleAdmin  = "admin"
	RoleEditor = "editor"
	RoleViewer = "viewer"
)

Role constants for RBAC.

Variables

ValidRoles is the set of recognised role strings.

Functions

func ContextWithUser

func ContextWithUser(ctx context.Context, user *User) context.Context

ContextWithUser returns a new context with the given user stored in it. This is primarily useful for testing.

func GenerateToken

func GenerateToken(user *User, secret []byte, expiry time.Duration) (string, error)

GenerateToken creates a signed JWT for the given user.

func Middleware

func Middleware(secret []byte) func(http.Handler) http.Handler

Middleware returns HTTP middleware that validates JWT tokens from the Authorization header. Requests to health and login endpoints are allowed through without authentication.

func RequireRole

func RequireRole(roles ...string) func(http.Handler) http.Handler

RequireRole returns middleware that checks whether the authenticated user has one of the specified roles. Returns 403 Forbidden otherwise.

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

func UserFromContext(ctx context.Context) *User

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL