auth

package
v0.1.16 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2025 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrForbidden = errors.New("forbidden")
View Source
var ErrNeedAuth = errors.New("authentication required")

Functions

func AuthMiddleware

func AuthMiddleware(c Config) func(next http.Handler) http.Handler

Provide middleware for authentication Checks if api key allowed or token is valid Get user and role from headers or token if request is anonymous, check if it allowed and add role

func ContextWithAuthInfo

func ContextWithAuthInfo(ctx context.Context, info *AuthInfo) context.Context

func ContextWithFullAccess

func ContextWithFullAccess(ctx context.Context) context.Context

func GenerateToken added in v0.1.9

func GenerateToken(privateKey []byte, claims jwt.MapClaims) (string, error)

func IsFullAccess

func IsFullAccess(ctx context.Context) bool

func ParsePrivateKey added in v0.1.9

func ParsePrivateKey(key []byte) (interface{}, error)

Types

type AnonymousConfig

type AnonymousConfig struct {
	Allowed bool   `json:"allowed" yaml:"allowed"`
	Role    string `json:"role" yaml:"role"`
}

type AnonymousProvider

type AnonymousProvider struct {
	Config AnonymousConfig
}

func NewAnonymous

func NewAnonymous(config AnonymousConfig) *AnonymousProvider

func (*AnonymousProvider) Authenticate

func (p *AnonymousProvider) Authenticate(r *http.Request) (*AuthInfo, error)

func (*AnonymousProvider) Name added in v0.1.9

func (p *AnonymousProvider) Name() string

func (*AnonymousProvider) Type added in v0.1.9

func (p *AnonymousProvider) Type() string

type ApiKeyConfig

type ApiKeyConfig struct {
	Key         string `json:"key" yaml:"key"`
	Header      string `json:"header" yaml:"header"`
	DefaultRole string `json:"default_role" yaml:"default-role"`

	Headers UserAuthInfoConfig `json:"headers" yaml:"headers"`
}

type ApiKeyProvider

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

func NewApiKey

func NewApiKey(name string, config ApiKeyConfig) *ApiKeyProvider

func (*ApiKeyProvider) Authenticate

func (p *ApiKeyProvider) Authenticate(r *http.Request) (*AuthInfo, error)

func (*ApiKeyProvider) Name

func (p *ApiKeyProvider) Name() string

func (*ApiKeyProvider) Type added in v0.1.9

func (p *ApiKeyProvider) Type() string

type AuthInfo

type AuthInfo struct {
	Role     string
	UserId   string
	UserName string

	AuthType     string
	AuthProvider string
	Token        string
}

func AuthInfoFromContext

func AuthInfoFromContext(ctx context.Context) *AuthInfo

type AuthProvider

type AuthProvider interface {
	Authenticate(r *http.Request) (*AuthInfo, error)
	Name() string
	Type() string
}

type Config

type Config struct {
	Providers []AuthProvider

	RedirectLoginPaths []string
	LoginUrl           string
	RedirectUrl        string
	DBApiKeysEnabled   bool
}

func (*Config) Info added in v0.1.9

func (c *Config) Info() []ProviderInfo

type CookieExtractor added in v0.1.9

type CookieExtractor string

func (CookieExtractor) ExtractToken added in v0.1.9

func (c CookieExtractor) ExtractToken(r *http.Request) (string, error)

type DBApiKey added in v0.1.9

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

func NewDBApiKey added in v0.1.9

func NewDBApiKey(qe types.Querier, name, headerName string) *DBApiKey

func (*DBApiKey) Authenticate added in v0.1.9

func (p *DBApiKey) Authenticate(r *http.Request) (*AuthInfo, error)

func (*DBApiKey) Name added in v0.1.9

func (p *DBApiKey) Name() string

func (*DBApiKey) Type added in v0.1.9

func (p *DBApiKey) Type() string

type JwtConfig

type JwtConfig struct {
	Issuer    string `json:"issuer" yaml:"issuer"`
	PublicKey []byte `json:"public_key" yaml:"public-key"`

	CookieName string `json:"cookie_name" yaml:"cookie-name"`

	ScopeRolePrefix string `json:"scope_role_prefix" yaml:"scope-role-prefix"`
	// RoleHeader is the header to check for role if not in claims than check that scope contains prefix+role (if the many roles a)
	RoleHeader string             `json:"role_header" yaml:"role-header"`
	Claims     UserAuthInfoConfig `json:"claims" yaml:"claims"`
}

type JwtProvider

type JwtProvider struct {
	Issuer string
	// contains filtered or unexported fields
}

func NewJwt

func NewJwt(config *JwtConfig) (*JwtProvider, error)

func (*JwtProvider) Authenticate

func (p *JwtProvider) Authenticate(r *http.Request) (*AuthInfo, error)

func (*JwtProvider) Name added in v0.1.9

func (p *JwtProvider) Name() string

func (*JwtProvider) Type added in v0.1.9

func (p *JwtProvider) Type() string

type ProviderInfo added in v0.1.9

type ProviderInfo struct {
	Name string `json:"name" yaml:"name"`
	Type string `json:"type" yaml:"type"`
}

type UserAuthInfoConfig

type UserAuthInfoConfig struct {
	Role     string `json:"role" yaml:"role"`
	UserId   string `json:"user_id" yaml:"user-id"`
	UserName string `json:"user_name" yaml:"user-name"`
}

Jump to

Keyboard shortcuts

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