auth

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: May 22, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package auth provides service-agnostic authentication orchestration.

Index

Constants

View Source
const (
	ContextIdentityKey = "auth.identity"
	ContextIdentity    = ContextIdentityKey
)

Variables

View Source
var (
	ErrInvalidCredentials = errors.New("invalid credentials")
	ErrAccountDisabled    = errors.New("account disabled")
	ErrUnauthorized       = errors.New("unauthorized")
	ErrHookNotImplemented = errors.New("auth hook not implemented")
)

Functions

func HasPermission

func HasPermission(identity *Identity, permission string) bool

func RoleID

func RoleID(c identityContext) int64

func UserID

func UserID(c identityContext) int64

Types

type Auth

type Auth struct {
	// contains filtered or unexported fields
}
var Default *Auth

Default is the global auth manager injected by bootstrap or service startup.

func NewWithGuard

func NewWithGuard(guard Guard, hooks Hooks) *Auth

func (*Auth) Authenticate

func (a *Auth) Authenticate(ctx context.Context, credential string) (*Identity, error)

func (*Auth) Guard

func (a *Auth) Guard() Guard

func (*Auth) LoginByCredentials

func (a *Auth) LoginByCredentials(ctx context.Context, credentials Credentials) (*LoginResult, error)

func (*Auth) Logout

func (a *Auth) Logout(ctx context.Context, credential string) error

func (*Auth) Mode

func (a *Auth) Mode() Mode

type Credentials

type Credentials struct {
	Username string
	Password string
}

type Guard

type Guard interface {
	Mode() Mode
	Login(ctx context.Context, identity *Identity) (*LoginResult, error)
	Logout(ctx context.Context, credential string) error
	Authenticate(ctx context.Context, credential string) (*Identity, error)
}

type HookFuncs

type HookFuncs struct {
	FindUserFunc         func(ctx context.Context, username string) (any, error)
	CheckPasswordFunc    func(ctx context.Context, user any, password string) error
	BuildIdentityFunc    func(ctx context.Context, user any) (*Identity, error)
	AfterLoginFunc       func(ctx context.Context, user any, identity *Identity) error
	AfterLoginFailedFunc func(ctx context.Context, username string, err error)
}

func (HookFuncs) AfterLogin

func (h HookFuncs) AfterLogin(ctx context.Context, user any, identity *Identity) error

func (HookFuncs) AfterLoginFailed

func (h HookFuncs) AfterLoginFailed(ctx context.Context, username string, err error)

func (HookFuncs) BuildIdentity

func (h HookFuncs) BuildIdentity(ctx context.Context, user any) (*Identity, error)

func (HookFuncs) CheckPassword

func (h HookFuncs) CheckPassword(ctx context.Context, user any, password string) error

func (HookFuncs) FindUser

func (h HookFuncs) FindUser(ctx context.Context, username string) (any, error)

type Hooks

type Hooks interface {
	FindUser(ctx context.Context, username string) (any, error)
	CheckPassword(ctx context.Context, user any, password string) error
	BuildIdentity(ctx context.Context, user any) (*Identity, error)
	AfterLogin(ctx context.Context, user any, identity *Identity) error
	AfterLoginFailed(ctx context.Context, username string, err error)
}

type Identity

type Identity struct {
	SubjectID   int64
	SubjectType string
	Username    string
	RoleID      int64
	Permissions []string
	Extra       map[string]any
}

func CurrentIdentity

func CurrentIdentity(c identityContext) *Identity

type JWTClaims

type JWTClaims = jwtx.SubjectClaims

func Claims

func Claims(c identityContext) *JWTClaims

type JWTConfig

type JWTConfig struct {
	Secret string `mapstructure:"secret" yaml:"secret"`
	Issuer string `mapstructure:"issuer" yaml:"issuer"`
	Expire int    `mapstructure:"expire" yaml:"expire"`
}

type JWTGuard

type JWTGuard struct {
	// contains filtered or unexported fields
}

func NewJWTGuard

func NewJWTGuard(cfg *JWTConfig) *JWTGuard

func (*JWTGuard) Authenticate

func (g *JWTGuard) Authenticate(ctx context.Context, credential string) (*Identity, error)

func (*JWTGuard) Login

func (g *JWTGuard) Login(ctx context.Context, identity *Identity) (*LoginResult, error)

func (*JWTGuard) Logout

func (g *JWTGuard) Logout(ctx context.Context, credential string) error

func (*JWTGuard) Mode

func (g *JWTGuard) Mode() Mode

type LoginResult

type LoginResult struct {
	Mode     Mode
	Token    string
	Identity *Identity
}

type Mode

type Mode string
const (
	ModeJWT Mode = "jwt"
)

Directories

Path Synopsis
adapter
gin

Jump to

Keyboard shortcuts

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