auth

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: May 23, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CredentialBearer  = "bearer"
	CredentialQuery   = "query"
	CredentialCookie  = "cookie"
	CredentialSession = "session"
)
View Source
const (
	ContextIdentityKey = "auth.identity"
	ContextIdentity    = ContextIdentityKey
)
View Source
const (
	MethodJWT       = "jwt"
	MethodSession   = "session"
	MethodAnonymous = "anonymous"
)

Variables

View Source
var (
	ErrInvalidCredentials = errors.New("invalid credentials")
	ErrAccountDisabled    = errors.New("account disabled")
	ErrUnauthorized       = errors.New("unauthorized")
)

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 AnonymousAuthenticator added in v0.1.2

type AnonymousAuthenticator struct{}

func (AnonymousAuthenticator) AuthenticateRequest added in v0.1.2

func (AnonymousAuthenticator) AuthenticateRequest(context.Context, *http.Request) (*Identity, error)

type ChainAuthenticator added in v0.1.2

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

func NewChainAuthenticator added in v0.1.2

func NewChainAuthenticator(authenticators ...RequestAuthenticator) *ChainAuthenticator

func (*ChainAuthenticator) AuthenticateRequest added in v0.1.2

func (a *ChainAuthenticator) AuthenticateRequest(ctx context.Context, r *http.Request) (*Identity, error)

type Credential added in v0.1.2

type Credential struct {
	Type     string
	Value    string
	Source   string
	Provider string
}

type Extractor added in v0.1.2

type Extractor interface {
	Extract(*http.Request) (Credential, bool)
}

func BearerTokenExtractor added in v0.1.2

func BearerTokenExtractor() Extractor

func CookieTokenExtractor added in v0.1.2

func CookieTokenExtractor(names ...string) Extractor

func FirstExtractor added in v0.1.2

func FirstExtractor(extractors ...Extractor) Extractor

func QueryTokenExtractor added in v0.1.2

func QueryTokenExtractor(name string) Extractor

type ExtractorFunc added in v0.1.2

type ExtractorFunc func(*http.Request) (Credential, bool)

func (ExtractorFunc) Extract added in v0.1.2

func (f ExtractorFunc) Extract(r *http.Request) (Credential, bool)

type Identity

type Identity struct {
	SubjectID   int64
	Subject     string
	SubjectType string
	TenantID    int64
	Username    string
	RoleID      int64
	Roles       []string
	Permissions []string
	Method      string
	Provider    string
	SessionID   string
	TokenID     string
	ExpiresAt   time.Time
	Extra       map[string]any
}

func CurrentIdentity

func CurrentIdentity(c identityContext) *Identity

func (*Identity) Authenticated added in v0.1.2

func (i *Identity) Authenticated() bool

func (*Identity) SubjectKey added in v0.1.2

func (i *Identity) SubjectKey() string

type IdentityFailureHook added in v0.1.2

type IdentityFailureHook func(ctx context.Context, r *http.Request, err error) error

type IdentityHook added in v0.1.2

type IdentityHook func(ctx context.Context, identity *Identity, raw any) error

type IdentityRefresher added in v0.1.2

type IdentityRefresher = IdentityHook

type IdentityValidator added in v0.1.2

type IdentityValidator = IdentityHook

type JWTAuthenticator added in v0.1.2

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

func NewJWTAuthenticator added in v0.1.2

func NewJWTAuthenticator(cfg *JWTConfig, opts ...JWTAuthenticatorOption) *JWTAuthenticator

func (*JWTAuthenticator) AuthenticateRequest added in v0.1.2

func (a *JWTAuthenticator) AuthenticateRequest(ctx context.Context, r *http.Request) (*Identity, error)

func (*JWTAuthenticator) Login added in v0.1.2

func (a *JWTAuthenticator) Login(ctx context.Context, identity *Identity) (*LoginResult, error)

type JWTAuthenticatorOption added in v0.1.2

type JWTAuthenticatorOption func(*JWTAuthenticator)

func WithJWTExtractor added in v0.1.2

func WithJWTExtractor(extractor Extractor) JWTAuthenticatorOption

func WithJWTProvider added in v0.1.2

func WithJWTProvider(provider string) JWTAuthenticatorOption

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 LoginResult

type LoginResult struct {
	Token    string
	Identity *Identity
}

type RequestAuthenticator added in v0.1.2

type RequestAuthenticator interface {
	AuthenticateRequest(ctx context.Context, r *http.Request) (*Identity, error)
}

type RequestAuthenticatorFunc added in v0.1.2

type RequestAuthenticatorFunc func(context.Context, *http.Request) (*Identity, error)

func (RequestAuthenticatorFunc) AuthenticateRequest added in v0.1.2

func (f RequestAuthenticatorFunc) AuthenticateRequest(ctx context.Context, r *http.Request) (*Identity, error)

type SessionAuthenticator added in v0.1.2

type SessionAuthenticator struct {
	Provider   string
	Key        string
	Reader     SessionReader
	Mapper     SessionMapper
	Validator  IdentityValidator
	Validators []IdentityHook
	Refresher  IdentityRefresher
	Refreshers []IdentityHook
	Failure    IdentityFailureHook
	Failures   []IdentityFailureHook
}

func NewSessionAuthenticator added in v0.1.2

func NewSessionAuthenticator(opts SessionAuthenticator) *SessionAuthenticator

func (*SessionAuthenticator) AuthenticateRequest added in v0.1.2

func (a *SessionAuthenticator) AuthenticateRequest(ctx context.Context, r *http.Request) (*Identity, error)

type SessionMapper added in v0.1.2

type SessionMapper func(ctx context.Context, raw any) (*Identity, error)

type SessionReader added in v0.1.2

type SessionReader interface {
	ReadSession(ctx context.Context, r *http.Request, key string) (any, bool, error)
}

Directories

Path Synopsis
adapter
gin

Jump to

Keyboard shortcuts

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