Documentation ¶
Overview ¶
Package http provides a registration interface for http services
Index ¶
- Variables
- func AddAuthFlagsPrefix(flagSet *pflag.FlagSet, prefix string, cfg *AuthConfig)
- func AddHTTPFlagsPrefix(flagSet *pflag.FlagSet, prefix string, cfg *Config)
- func AddTemplateFlagsPrefix(flagSet *pflag.FlagSet, prefix string, cfg *TemplateConfig)
- func AfterEpoch(t time.Time) bool
- func AuthHelp(prefix string) string
- func CtxGetAuth(ctx context.Context) interface{}
- func CtxGetUser(ctx context.Context) (string, bool)
- func CtxSetUser(ctx context.Context, value string) context.Context
- func GetTemplate(tmpl string) (*template.Template, error)
- func Help(prefix string) string
- func IsAuthenticated(r *http.Request) bool
- func IsUnixSocket(r *http.Request) bool
- func NewBaseContext(ctx context.Context, url string) func(l net.Listener) context.Context
- func PublicURL(r *http.Request) string
- func TemplateHelp(prefix string) string
- type AuthConfig
- type Config
- type CustomAuthFn
- type LoggedBasicAuth
- type Middleware
- func MiddlewareAuthBasic(user, pass, realm, salt string) Middleware
- func MiddlewareAuthCertificateUser() Middleware
- func MiddlewareAuthCustom(fn CustomAuthFn, realm string, userFromContext bool) Middleware
- func MiddlewareAuthHtpasswd(path, realm string) Middleware
- func MiddlewareCORS(allowOrigin string) Middleware
- func MiddlewareStripPrefix(prefix string) Middleware
- type Option
- type Server
- type TemplateConfig
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidMinTLSVersion - hard coded errors, allowing for easier testing ErrInvalidMinTLSVersion = errors.New("invalid value for --min-tls-version") // ErrTLSBodyMismatch - hard coded errors, allowing for easier testing ErrTLSBodyMismatch = errors.New("need both TLSCertBody and TLSKeyBody to use TLS") // ErrTLSFileMismatch - hard coded errors, allowing for easier testing ErrTLSFileMismatch = errors.New("need both --cert and --key to use TLS") // ErrTLSParseCA - hard coded errors, allowing for easier testing ErrTLSParseCA = errors.New("unable to parse client certificate authority") )
var Assets embed.FS
Assets holds the embedded filesystem for the default template
var AuthConfigInfo = fs.Options{{
Name: "htpasswd",
Default: "",
Help: "A htpasswd file - if not provided no authentication is done",
}, {
Name: "realm",
Default: "",
Help: "Realm for authentication",
}, {
Name: "user",
Default: "",
Help: "User name for authentication",
}, {
Name: "pass",
Default: "",
Help: "Password for authentication",
}, {
Name: "salt",
Default: "dlPL2MqE",
Help: "Password hashing salt",
}}
AuthConfigInfo descripts the Options in use
var ConfigInfo = fs.Options{{ Name: "addr", Default: []string{"127.0.0.1:8080"}, Help: "IPaddress:Port or :Port to bind server to", }, { Name: "server_read_timeout", Default: 1 * time.Hour, Help: "Timeout for server reading data", }, { Name: "server_write_timeout", Default: 1 * time.Hour, Help: "Timeout for server writing data", }, { Name: "max_header_bytes", Default: 4096, Help: "Maximum size of request header", }, { Name: "cert", Default: "", Help: "TLS PEM key (concatenation of certificate and CA certificate)", }, { Name: "key", Default: "", Help: "TLS PEM Private key", }, { Name: "client_ca", Default: "", Help: "Client certificate authority to verify clients with", }, { Name: "baseurl", Default: "", Help: "Prefix for URLs - leave blank for root", }, { Name: "min_tls_version", Default: "tls1.0", Help: "Minimum TLS version that is acceptable", }, { Name: "allow_origin", Default: "", Help: "Origin which cross-domain request (CORS) can be executed from", }}
ConfigInfo descripts the Options in use
var TemplateConfigInfo = fs.Options{{
Name: "template",
Default: "",
Help: "User-specified template",
}}
TemplateConfigInfo descripts the Options in use
Functions ¶
func AddAuthFlagsPrefix ¶ added in v1.61.0
func AddAuthFlagsPrefix(flagSet *pflag.FlagSet, prefix string, cfg *AuthConfig)
AddAuthFlagsPrefix adds flags to the flag set for AuthConfig
func AddHTTPFlagsPrefix ¶ added in v1.61.0
AddHTTPFlagsPrefix adds flags for the httplib
func AddTemplateFlagsPrefix ¶ added in v1.61.0
func AddTemplateFlagsPrefix(flagSet *pflag.FlagSet, prefix string, cfg *TemplateConfig)
AddTemplateFlagsPrefix for the templating functionality
func AfterEpoch ¶ added in v1.61.0
AfterEpoch returns the time since the epoch for the given time
func AuthHelp ¶ added in v1.61.0
AuthHelp returns text describing the http authentication to add to the command help.
func CtxGetAuth ¶ added in v1.61.0
CtxGetAuth is a wrapper over the private Auth context key
func CtxGetUser ¶ added in v1.61.0
CtxGetUser is a wrapper over the private User context key
func CtxSetUser ¶ added in v1.61.0
CtxSetUser is a test helper that injects a User value into context
func GetTemplate ¶ added in v1.61.0
GetTemplate returns the HTML template for serving directories via HTTP/WebDAV
func IsAuthenticated ¶ added in v1.61.0
IsAuthenticated checks if this request was authenticated via a middleware
func IsUnixSocket ¶ added in v1.61.0
IsUnixSocket checks if the request was received on a unix socket, used to skip auth & CORS
func NewBaseContext ¶ added in v1.61.0
NewBaseContext initializes the context for all requests, adding info for use in middleware and handlers
func PublicURL ¶ added in v1.61.0
PublicURL returns the URL defined in NewBaseContext, used for logging & CORS
func TemplateHelp ¶ added in v1.61.0
TemplateHelp returns a string that describes how to use a custom template
Types ¶
type AuthConfig ¶ added in v1.61.0
type AuthConfig struct { HtPasswd string `config:"htpasswd"` // htpasswd file - if not provided no authentication is done Realm string `config:"realm"` // realm for authentication BasicUser string `config:"user"` // single username for basic auth if not using Htpasswd BasicPass string `config:"pass"` // password for BasicUser Salt string `config:"salt"` // password hashing salt CustomAuthFn CustomAuthFn `json:"-" config:"-"` // custom Auth (not set by command line flags) }
AuthConfig contains options for the http authentication
func DefaultAuthCfg ¶ added in v1.61.0
func DefaultAuthCfg() AuthConfig
DefaultAuthCfg returns a new config which can be customized by command line flags
Note that this needs to be kept in sync with AuthConfigInfo above and can be removed when all callers have been converted.
func (*AuthConfig) AddFlagsPrefix ¶ added in v1.61.0
func (cfg *AuthConfig) AddFlagsPrefix(flagSet *pflag.FlagSet, prefix string)
AddFlagsPrefix adds flags to the flag set for AuthConfig
type Config ¶ added in v1.61.0
type Config struct { ListenAddr []string `config:"addr"` // Port to listen on BaseURL string `config:"baseurl"` // prefix to strip from URLs ServerReadTimeout time.Duration `config:"server_read_timeout"` // Timeout for server reading data ServerWriteTimeout time.Duration `config:"server_write_timeout"` // Timeout for server writing data MaxHeaderBytes int `config:"max_header_bytes"` // Maximum size of request header TLSCert string `config:"cert"` // Path to TLS PEM key (concatenation of certificate and CA certificate) TLSKey string `config:"key"` // Path to TLS PEM Private key TLSCertBody []byte `config:"-"` // TLS PEM key (concatenation of certificate and CA certificate) body, ignores TLSCert TLSKeyBody []byte `config:"-"` // TLS PEM Private key body, ignores TLSKey ClientCA string `config:"client_ca"` // Client certificate authority to verify clients with MinTLSVersion string `config:"min_tls_version"` // MinTLSVersion contains the minimum TLS version that is acceptable. AllowOrigin string `config:"allow_origin"` // AllowOrigin sets the Access-Control-Allow-Origin header }
Config contains options for the http Server
func DefaultCfg ¶ added in v1.61.0
func DefaultCfg() Config
DefaultCfg is the default values used for Config
Note that this needs to be kept in sync with ConfigInfo above and can be removed when all callers have been converted.
type CustomAuthFn ¶ added in v1.61.0
CustomAuthFn if used will be used to authenticate user, pass. If an error is returned then the user is not authenticated.
If a non nil value is returned then it is added to the context under the key
type LoggedBasicAuth ¶ added in v1.61.0
LoggedBasicAuth simply wraps the goauth.BasicAuth struct
func NewLoggedBasicAuthenticator ¶ added in v1.61.0
func NewLoggedBasicAuthenticator(realm string, secrets goauth.SecretProvider) *LoggedBasicAuth
NewLoggedBasicAuthenticator instantiates a new instance of LoggedBasicAuthenticator
type Middleware ¶
Middleware function signature required by chi.Router.Use()
func MiddlewareAuthBasic ¶ added in v1.61.0
func MiddlewareAuthBasic(user, pass, realm, salt string) Middleware
MiddlewareAuthBasic instantiates middleware that authenticates for a single user
func MiddlewareAuthCertificateUser ¶ added in v1.63.0
func MiddlewareAuthCertificateUser() Middleware
MiddlewareAuthCertificateUser instantiates middleware that extracts the authenticated user via client certificate common name
func MiddlewareAuthCustom ¶ added in v1.61.0
func MiddlewareAuthCustom(fn CustomAuthFn, realm string, userFromContext bool) Middleware
MiddlewareAuthCustom instantiates middleware that authenticates using a custom function
func MiddlewareAuthHtpasswd ¶ added in v1.61.0
func MiddlewareAuthHtpasswd(path, realm string) Middleware
MiddlewareAuthHtpasswd instantiates middleware that authenticates against the passed htpasswd file
func MiddlewareCORS ¶ added in v1.61.0
func MiddlewareCORS(allowOrigin string) Middleware
MiddlewareCORS instantiates middleware that handles basic CORS protections for rcd
func MiddlewareStripPrefix ¶ added in v1.61.0
func MiddlewareStripPrefix(prefix string) Middleware
MiddlewareStripPrefix instantiates middleware that removes the BaseURL from the path
type Option ¶ added in v1.61.0
type Option func(*Server)
Option allows customizing the server
func WithAuth ¶ added in v1.61.0
func WithAuth(cfg AuthConfig) Option
WithAuth option initializes the appropriate auth middleware
func WithConfig ¶ added in v1.61.0
WithConfig option applies the Config to the server, overriding defaults
func WithTemplate ¶ added in v1.61.0
func WithTemplate(cfg TemplateConfig) Option
WithTemplate option allows the parsing of a template
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server contains info about the running http server
func NewServer ¶
NewServer instantiates a new http server using provided listeners and options This function is provided if the default http server does not meet a services requirements and should not generally be used A http server can listen using multiple listeners. For example, a listener for port 80, and a listener for port 443. tlsListeners are ignored if opt.TLSKey is not provided
func (*Server) HTMLTemplate ¶ added in v1.61.0
HTMLTemplate returns the parsed template, if WithTemplate option was passed.
func (*Server) Serve ¶ added in v1.61.0
func (s *Server) Serve()
Serve starts the HTTP server on each listener
type TemplateConfig ¶ added in v1.61.0
type TemplateConfig struct {
Path string `config:"template"`
}
TemplateConfig for the templating functionality
func DefaultTemplateCfg ¶ added in v1.61.0
func DefaultTemplateCfg() TemplateConfig
DefaultTemplateCfg returns a new config which can be customized by command line flags
Note that this needs to be kept in sync with TemplateConfigInfo above and can be removed when all callers have been converted.
func (*TemplateConfig) AddFlagsPrefix ¶ added in v1.61.0
func (cfg *TemplateConfig) AddFlagsPrefix(flagSet *pflag.FlagSet, prefix string)
AddFlagsPrefix for the templating functionality