libauth

package
v1.0.0-rc.11 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: Apache-2.0 Imports: 5 Imported by: 0 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

Index

Constants

View Source
const ContextTokenKey = contextKey("jwtToken")

Variables

View Source
var (
	ErrNotAuthorized           = errors.New("libauth: not authorized")
	ErrTokenExpired            = errors.New("libauth: token has expired")
	ErrIssuedAtMissing         = errors.New("libauth: issued at claim is missing")
	ErrIssuedAtInFuture        = errors.New("libauth: token is malformed: issued-at time is in the future")
	ErrIdentityMissing         = errors.New("libauth: identity claim is missing")
	ErrInvalidTokenClaims      = errors.New("libauth: invalid token claims")
	ErrTokenMissing            = errors.New("libauth: token is missing")
	ErrUnexpectedSigningMethod = errors.New("libauth: unexpected signing method")
	ErrTokenParsingFailed      = errors.New("libauth: token parsing failed")
	ErrTokenSigningFailed      = errors.New("libauth: token signing failed")
	ErrEmptyJWTSecret          = errors.New("libauth: JWT secret cannot be empty")
	ErrEmptyIdentity           = errors.New("libauth: identity cannot be empty")
)

Functions

func CheckAuthorisation

func CheckAuthorisation[T Authz](ctx context.Context, jwtSecret string, forResource string, permission int) (bool, error)

func CreateToken

func CreateToken[T Authz](cfg CreateTokenArgs, identity string, payload T) (string, time.Time, error)

func GetClaims

func GetClaims[T Authz](ctx context.Context, jwtSecret string) (T, error)

func GetIdentity

func GetIdentity[T Authz](ctx context.Context, jwtSecret string) (string, error)

func RefreshToken

func RefreshToken[T Authz](cfg CreateTokenArgs, oldToken string) (string, time.Time, error)

func RefreshTokenWithGracePeriod

func RefreshTokenWithGracePeriod[T Authz](cfg CreateTokenArgs, oldToken string, gracePeriod time.Duration) (string, bool, time.Time, error)

Types

type AuthClaims

type AuthClaims[T Authz] struct {
	jwt.RegisteredClaims
	Identity    string `json:"identity"`
	Permissions T      `json:"permissions"`
}

func ValidateToken

func ValidateToken[T Authz](ctx context.Context, tokenStr string, jwtSecret string) (*AuthClaims[T], error)

func (AuthClaims[T]) Valid

func (c AuthClaims[T]) Valid() error

func (AuthClaims[T]) Validate

func (c AuthClaims[T]) Validate() error

type Authz

type Authz interface {
	RequireAuthorisation(forResource string, permission int) (bool, error)
}

type CreateTokenArgs

type CreateTokenArgs struct {
	JWTSecret string
	JWTExpiry time.Duration
}

Jump to

Keyboard shortcuts

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