authorizer

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	TokenNotFoundError = httpx.UnauthorizedError(
		errors.New("AuthorizerError:TOKEN_NOT_FOUND"),
		"没有提供有效的认证信息",
	)
	NeedLoginError = httpx.UnauthorizedError(
		errors.New("AuthorizerError:NEED_LOGIN"),
		"需要登录才能访问",
	)
	PermissionError = httpx.ForbiddenError(
		errors.New("AuthorizerError:PERMISSION_DENIED"),
		"没有权限访问该资源",
	)
)

Functions

func WithAuthData added in v0.0.3

func WithAuthData[I UID](ctx context.Context, data Data[I]) context.Context

Types

type Claims

type Claims[T UID] interface {
	GetUID() (T, error)
	GetSubject() (string, error)
	GetRoles() ([]string, error)
}

Claims represents the interface for extracting user information from authentication tokens. Implementations should provide methods to extract user ID, subject, and roles.

type ContextUtils

type ContextUtils[I UID] struct{}

ContextUtils provides utility functions for working with authentication context. It is parameterized by the user ID type for type safety.

func (ContextUtils[I]) CheckAuthID

func (c ContextUtils[I]) CheckAuthID(ctx context.Context, id I) error

CheckAuthID verifies that the current user ID matches the provided ID. It ensures the user can only access resources belonging to them.

func (ContextUtils[I]) CheckAuthStatus

func (c ContextUtils[I]) CheckAuthStatus(ctx context.Context) error

CheckAuthStatus verifies that the user is authenticated. It returns an error if authentication is required but not present.

func (ContextUtils[I]) GetCurrentID

func (ContextUtils[I]) GetCurrentID(ctx context.Context) (I, error)

GetCurrentID retrieves the current user ID from the context. It returns NeedLoginError if the user is not authenticated or the ID type is incorrect.

func (ContextUtils[I]) GetCurrentRoles added in v0.0.3

func (c ContextUtils[I]) GetCurrentRoles(ctx context.Context) []string

GetCurrentRoles retrieves the current user's roles from the context. It returns nil if no roles are set or if the user is not authenticated.

func (ContextUtils[I]) GetCurrentSubject added in v0.0.3

func (c ContextUtils[I]) GetCurrentSubject(ctx context.Context) (string, error)

GetCurrentSubject retrieves the current user's subject/username from the context. It returns NeedLoginError if the user is not authenticated.

type Data added in v0.0.3

type Data[I UID] struct {
	UID     I        `json:"uid"`
	Subject string   `json:"subject"`
	Roles   []string `json:"roles"`
}

func GetAuthData added in v0.0.3

func GetAuthData[I UID](ctx context.Context) (Data[I], bool)

type Generator

type Generator[I UID, T Claims[I]] interface {
	GenerateToken(ctx context.Context, claims T) (string, error)
}

Generator defines the interface for generating authentication tokens from claims.

type Parser

type Parser[I UID, T Claims[I]] interface {
	ParseToken(ctx context.Context, token string) (T, error)
}

Parser defines the interface for parsing authentication tokens into claims.

type ParserFunc

type ParserFunc[I UID, T Claims[I]] func(ctx context.Context, token string) (T, error)

ParserFunc is a function type that implements the Parser interface. This allows functions to be used directly as parsers without defining new types.

func (ParserFunc[I, T]) ParseToken

func (f ParserFunc[I, T]) ParseToken(ctx context.Context, token string) (T, error)

ParseToken implements the Parser interface for ParserFunc.

type TokenAuthorizer

type TokenAuthorizer[I UID, T Claims[I]] interface {
	Parser[I, T]
	Generator[I, T]
}

TokenAuthorizer combines token parsing and generation capabilities.

type UID

type UID interface {
	constraints.Integer | ~string
}

UID represents valid user identifier types, supporting both integer and string IDs.

Jump to

Keyboard shortcuts

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