authorizer

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ContextKeyUID     = "uid"
	ContextKeySubject = "subject"
	ContextKeyRoles   = "roles"
)

Variables

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

Functions

func GetCurrentRoles

func GetCurrentRoles(ctx context.Context) []string

func GetCurrentSubject

func GetCurrentSubject(ctx context.Context) (string, error)

Types

type Claims

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

type ContextUtils

type ContextUtils[I UID] struct{}

func (ContextUtils[I]) CheckAuthID

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

func (ContextUtils[I]) CheckAuthStatus

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

func (ContextUtils[I]) GetCurrentID

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

type Generator

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

type Parser

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

type ParserFunc

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

func (ParserFunc[I, T]) ParseToken

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

type TokenAuthorizer

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

type UID

type UID interface {
	constraints.Integer | ~string
}

Jump to

Keyboard shortcuts

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