auth

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: MIT Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

pkg/auth/jwt.go

Index

Constants

View Source
const (
	ActionCreate = "create"
	ActionRead   = "read"
	ActionUpdate = "update"
	ActionDelete = "delete"
)

Content actions checked against a role.

View Source
const TokenTTL = 72 * time.Hour

TokenTTL is how long an issued token stays valid.

Variables

View Source
var ErrInvalidToken = errors.New("invalid token")

ErrInvalidToken is returned for any token that fails parsing or validation.

Functions

func GenerateJWT

func GenerateJWT(username, role string) (string, error)

func HasPermission

func HasPermission(role, action string) bool

HasPermission reports whether role may perform action on content.

func InitializeJWT

func InitializeJWT(secret string)

func RoleFromContext

func RoleFromContext(ctx context.Context) string

RoleFromContext returns the role stored by WithRole, or "".

func WithRole

func WithRole(ctx context.Context, role string) context.Context

WithRole returns a context carrying the authenticated user's role.

Types

type Claims

type Claims struct {
	Username string `json:"username"`
	Role     string `json:"role"`

	jwt.RegisteredClaims
}

func ParseJWT

func ParseJWT(tokenStr string) (*Claims, error)

ParseJWT validates signature, algorithm (HS256 only) and expiry. It never returns nil claims together with a nil error.

Jump to

Keyboard shortcuts

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