Documentation
¶
Index ¶
- Variables
- func AuthMiddleware(c Config) func(next http.Handler) http.Handler
- func ContextWithAuthInfo(ctx context.Context, info *AuthInfo) context.Context
- func ContextWithFullAccess(ctx context.Context) context.Context
- func GenerateToken(privateKey []byte, claims jwt.MapClaims) (string, error)
- func IsFullAccess(ctx context.Context) bool
- func IsImpersonated(ctx context.Context) bool
- func ParsePrivateKey(key []byte) (interface{}, error)
- func ScalarClaims(claims map[string]any) map[string]any
- type AnonymousConfig
- type AnonymousProvider
- type ApiKeyConfig
- type ApiKeyProvider
- type AuthInfo
- type AuthProvider
- type Config
- type CookieExtractor
- type DBApiKey
- type JwtConfig
- type JwtProvider
- type ProviderInfo
- type UserAuthInfoConfig
Constants ¶
This section is empty.
Variables ¶
var ErrForbidden = errors.New("forbidden")
var ErrInvalidKeyType = errors.New("invalid key type")
ErrInvalidKeyType is returned by a provider when a token is present but is not verifiable with that provider's key (wrong signing algorithm/key) — i.e. it was most likely issued for a different provider. The middleware treats it as a signal to try the next provider. Unlike ErrSkipAuth (no token at all), it also records that a token was rejected, so a failed authentication attempt is not silently downgraded to anonymous access.
var ErrNeedAuth = errors.New("authentication required")
var ErrSkipAuth = errors.New("skip authentication")
var ErrTokenExpired = errors.New("token expired")
Functions ¶
func AuthMiddleware ¶
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 GenerateToken ¶ added in v0.1.9
func IsFullAccess ¶
func IsImpersonated ¶ added in v0.3.21
IsImpersonated returns true if the current request is running under impersonation.
func ParsePrivateKey ¶ added in v0.1.9
func ScalarClaims ¶ added in v0.3.39
ScalarClaims returns the subset of claims whose values are scalars (string, bool, or number) — the only claims usable as [$auth.<claim>] substitution values in filters and mutation data. Nested objects and arrays are dropped. Returns nil when nothing scalar remains.
Types ¶
type AnonymousConfig ¶
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
// Claims holds the scalar claims carried by the authentication token
// (JWT/OIDC), exposed to permission filters and mutation data as
// [$auth.<claim>] placeholders. Nil for token-less auth (API key,
// anonymous) and for impersonated identities. Built-in placeholders
// (user_id, role, …) always take precedence over a same-named claim.
Claims map[string]any
// ImpersonatedBy holds the original admin identity when this request
// is running under impersonation. Nil means no impersonation.
ImpersonatedBy *AuthInfo
}
func AuthInfoFromContext ¶
func BuildImpersonatedAuthInfo ¶ added in v0.3.22
func BuildImpersonatedAuthInfo(original *AuthInfo, targetUserId, targetUserName, targetRole string) *AuthInfo
BuildImpersonatedAuthInfo constructs an AuthInfo representing an impersonated identity. The target identity (userId, userName, role) becomes the effective identity. The original AuthInfo is preserved in ImpersonatedBy for audit purposes.
func ImpersonatedByFromContext ¶ added in v0.3.21
ImpersonatedByFromContext returns the original admin identity if impersonation is active.
type AuthProvider ¶
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 (*DBApiKey) Authenticate ¶ added in v0.1.9
type JwtConfig ¶
type JwtConfig struct {
Issuer string `json:"issuer" yaml:"issuer"`
// PublicKey is the PEM-encoded public key. It is a string (not []byte) so
// it parses from a plain multi-line scalar in YAML/JSON configs; []byte
// would require YAML !!binary / JSON base64 encoding.
PublicKey string `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