Documentation
¶
Overview ¶
Package auth provides authentication and authorization functionality. It includes JWT token handling, OIDC integration, HTTP middleware, and role-based access control.
Package auth provides authentication and authorization functionality. This file contains compatibility functions for the old auth/errors package.
Index ¶
- Variables
- func GetContext(err error, key string) (interface{}, bool)
- func GetMessage(err error) (string, bool)
- func GetOp(err error) (string, bool)
- func GetUserIDFromContext(ctx context.Context) (string, bool)
- func GetUserRolesFromContext(ctx context.Context) ([]string, bool)
- func IsAuthenticated(ctx context.Context) bool
- func ValidateConfig(config Config) *validation.ValidationResult
- func WithContext(err error, key string, value interface{}) error
- func WithMessage(err error, message string) error
- func WithOp(err error, op string) error
- func WithUserID(ctx context.Context, userID string) context.Context
- func WithUserRoles(ctx context.Context, roles []string) context.Context
- func Wrap(err error, message string) error
- type Auth
- func (a *Auth) GenerateToken(ctx context.Context, userID string, roles []string, scopes []string, ...) (string, error)
- func (a *Auth) GetUserID(ctx context.Context) (string, error)
- func (a *Auth) GetUserRoles(ctx context.Context) ([]string, error)
- func (a *Auth) HasRole(ctx context.Context, role string) (bool, error)
- func (a *Auth) IsAdmin(ctx context.Context) (bool, error)
- func (a *Auth) IsAuthorized(ctx context.Context, operation string) (bool, error)
- func (a *Auth) Middleware() func(http.Handler) http.Handler
- func (a *Auth) ValidateToken(ctx context.Context, tokenString string) (*jwt.Claims, error)
- type Config
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidToken is returned when a token is invalid ErrInvalidToken = errors.NewAuthenticationError("invalid token", "", nil) // ErrExpiredToken is returned when a token has expired ErrExpiredToken = errors.NewAuthenticationError("token expired", "", nil) // ErrMissingToken is returned when a token is missing ErrMissingToken = errors.NewAuthenticationError("token missing", "", nil) // ErrInvalidSignature is returned when a token has an invalid signature ErrInvalidSignature = errors.NewAuthenticationError("invalid token signature", "", nil) // ErrInvalidClaims is returned when a token has invalid claims ErrInvalidClaims = errors.NewAuthenticationError("invalid token claims", "", nil) ErrUnauthorized = errors.ErrUnauthorized // ErrForbidden is returned when a user is forbidden from performing an operation ErrForbidden = errors.ErrForbidden // ErrInvalidConfig is returned when the configuration is invalid ErrInvalidConfig = errors.NewConfigurationError("invalid configuration", "", "", nil) // ErrInternal is returned when an internal error occurs ErrInternal = errors.ErrInternal // ErrNotImplemented is returned when a feature is not implemented ErrNotImplemented = errors.New(errors.InternalErrorCode, "not implemented") )
Error constants for backward compatibility
Functions ¶
func GetContext ¶ added in v1.4.0
GetContext gets a context value from an error for backward compatibility
func GetMessage ¶ added in v1.4.0
GetMessage gets the message from an error for backward compatibility
func GetUserIDFromContext ¶
GetUserIDFromContext retrieves the user ID from the context.
func GetUserRolesFromContext ¶
GetUserRolesFromContext retrieves the user roles from the context.
func IsAuthenticated ¶
IsAuthenticated checks if the user is authenticated.
func ValidateConfig ¶
func ValidateConfig(config Config) *validation.ValidationResult
ValidateConfig validates the configuration for the auth module.
func WithContext ¶ added in v1.4.0
WithContext adds context to an error for backward compatibility
func WithMessage ¶ added in v1.4.0
WithMessage adds a message to an error for backward compatibility
func WithUserID ¶
WithUserID returns a new context with the user ID.
func WithUserRoles ¶
WithUserRoles returns a new context with the user roles.
Types ¶
type Auth ¶
type Auth struct {
// contains filtered or unexported fields
}
Auth provides authentication and authorization functionality.
func (*Auth) GenerateToken ¶
func (a *Auth) GenerateToken(ctx context.Context, userID string, roles []string, scopes []string, resources []string) (string, error)
GenerateToken generates a new JWT token for a user with the specified roles, scopes, and resources.
func (*Auth) GetUserRoles ¶
GetUserRoles retrieves the user roles from the context.
func (*Auth) IsAuthorized ¶
IsAuthorized checks if the user is authorized to perform the operation.
func (*Auth) Middleware ¶
Middleware returns the HTTP middleware for authentication.
type Config ¶
type Config struct {
// JWT configuration
JWT struct {
// SecretKey is the key used to sign and verify JWT tokens
SecretKey string
// TokenDuration is the validity period for generated tokens
TokenDuration time.Duration
// Issuer identifies the entity that issued the token
Issuer string
// Remote validation configuration
Remote struct {
// Enabled determines if remote validation should be used
Enabled bool
// ValidationURL is the URL of the remote validation endpoint
ValidationURL string
// ClientID is the client ID for the remote validation service
ClientID string
// ClientSecret is the client secret for the remote validation service
ClientSecret string
// Timeout is the timeout for remote validation operations
Timeout time.Duration
}
}
// OIDC configuration
OIDC struct {
// IssuerURL is the URL of the OIDC provider
IssuerURL string
// ClientID is the client ID for the OIDC provider
ClientID string
// ClientSecret is the client secret for the OIDC provider
ClientSecret string
// RedirectURL is the redirect URL for the OIDC provider
RedirectURL string
// Scopes are the OAuth2 scopes to request
Scopes []string
// Timeout is the timeout for OIDC operations
Timeout time.Duration
}
// Middleware configuration
Middleware struct {
// SkipPaths are paths that should skip authentication
SkipPaths []string
// RequireAuth determines if authentication is required for all requests
RequireAuth bool
}
// Service configuration
Service struct {
// AdminRoleName is the name of the admin role
AdminRoleName string
// ReadOnlyRoleName is the name of the read-only role
ReadOnlyRoleName string
// ReadOperationPrefixes are prefixes for read-only operations
ReadOperationPrefixes []string
}
}
Config holds the configuration for the auth module.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns the default configuration for the auth module.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package config provides adapters for auth configuration.
|
Package config provides adapters for auth configuration. |
|
Package errors provides comprehensive error handling for the auth module.
|
Package errors provides comprehensive error handling for the auth module. |
|
Package jwt provides JWT token handling for the auth module.
|
Package jwt provides JWT token handling for the auth module. |
|
Package middleware provides HTTP middleware for authentication.
|
Package middleware provides HTTP middleware for authentication. |
|
Package oidc provides OpenID Connect integration for the auth module.
|
Package oidc provides OpenID Connect integration for the auth module. |
|
Package service provides authorization services for the auth module.
|
Package service provides authorization services for the auth module. |