auth

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MethodSession = "session"
	MethodAPIKey  = "api_key"
	MethodJWT     = "jwt"
)

Variables

This section is empty.

Functions

func GroupName

func GroupName(id string) string

func IsNoCredentials

func IsNoCredentials(err error) bool

func JWTSign

func JWTSign(secret []byte, claims core.Map) (string, error)

JWTSign signs HS256 JWT claims.

func Provider

func Provider() runaprovider.Provider

Provider registers the auth registry.

func ShortName

func ShortName(id string) string

Types

type AuthFunc

type AuthFunc func(ctx any) (*Info, error)

AuthFunc adapts a function to Authenticator.

func (AuthFunc) Authenticate

func (fn AuthFunc) Authenticate(ctx any) (*Info, error)

type Authenticator

type Authenticator interface {
	Authenticate(ctx any) (*Info, error)
}

Authenticator resolves request identity.

func APIKeyAuth

func APIKeyAuth(verify func(string) (core.Map, bool, error), sources ...TokenSource) Authenticator

APIKeyAuth authenticates a request token with a verifier callback.

func AuthMux

func AuthMux(items ...Authenticator) Authenticator

AuthMux tries multiple authenticators in order.

func JWTAuth

func JWTAuth(secret []byte, sources ...TokenSource) Authenticator

JWTAuth authenticates HS256 JWT tokens without forcing a large dependency.

func SessionAuth

func SessionAuth(name string) Authenticator

SessionAuth authenticates when a named session contains auth data.

type Context

type Context interface {
	Context() context.Context
	Request() *http.Request
	CookieValue(string) (string, bool)
	Session(...string) *session.Session
}

Context is the minimal request context needed by authenticators.

type ErrNoCredentials

type ErrNoCredentials struct{}

ErrNoCredentials marks missing credentials.

func (ErrNoCredentials) Error

func (ErrNoCredentials) Error() string

type Info

type Info struct {
	Name   string
	Method string
	Data   core.Map
}

Info is request auth information.

type PermissionChecker

type PermissionChecker interface {
	Check(ctx any, info *Info, id string) error
}

PermissionChecker checks one permission id.

func DefaultPermissionChecker

func DefaultPermissionChecker() PermissionChecker

DefaultPermissionChecker checks Info.Data permission collections.

type PermissionFunc

type PermissionFunc func(ctx any, info *Info, id string) error

PermissionFunc adapts a function to PermissionChecker.

func (PermissionFunc) Check

func (fn PermissionFunc) Check(ctx any, info *Info, id string) error

type PermissionInfo

type PermissionInfo struct {
	ID          string
	Name        string
	Label       string
	Group       string
	GroupLabel  string
	Method      string
	Path        string
	Tags        []string
	Description string
	Meta        core.Map
}

PermissionInfo describes one route permission.

type Registry

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

Registry stores named authenticators and default permission checker.

func Default

func Default() *Registry

Default returns the default app auth registry.

func New

func New() *Registry

New creates a registry.

func (*Registry) Auth

func (registry *Registry) Auth(name string, authenticator Authenticator)

func (*Registry) Checker

func (registry *Registry) Checker() PermissionChecker

func (*Registry) MustOf

func (registry *Registry) MustOf(name string) Authenticator

func (*Registry) Names

func (registry *Registry) Names() []string

func (*Registry) Of

func (registry *Registry) Of(name string) Authenticator

func (*Registry) Permission

func (registry *Registry) Permission(checker PermissionChecker)

type TokenSource

type TokenSource interface {
	Name() string
	Token(ctx any) string
}

TokenSource resolves token from a request context.

func Cookie(name string) TokenSource

Cookie reads token from a cookie.

func Header(name string) TokenSource

Header reads token from a header. Authorization removes Bearer prefix.

func Query

func Query(name string) TokenSource

Query reads token from a query parameter.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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