Documentation
¶
Index ¶
- Constants
- func ConfigureNTLM(server *http.Server)
- func GetCertificateFunc(certSubject string, store CertStore, ...) (func(*tls.ClientHelloInfo) (*tls.Certificate, error), io.Closer, error)
- func SetUser(r *http.Request, username string) *http.Request
- func SetUserGroups(r *http.Request, groups []string) *http.Request
- func User(r *http.Request) (string, bool)
- func UserGroups(r *http.Request) ([]string, bool)
- type AuthErrorHandler
- type AuthErrorHandlers
- type CertStore
- type CertificateSource
- type LDAPOption
- func WithLDAPAddress(addr string) LDAPOption
- func WithLDAPConnectionTTL(d time.Duration) LDAPOption
- func WithLDAPErrorHandlers(h AuthErrorHandlers) LDAPOption
- func WithLDAPServiceAccountSPN(spn string) LDAPOption
- func WithLDAPTimeout(d time.Duration) LDAPOption
- func WithLDAPUsersDN(dn string) LDAPOption
- type LDAPProvider
- type SSPIOption
- type SSPIProvider
Constants ¶
const ( // CertStoreLocalMachine searches the LocalMachine certificate store (default). CertStoreLocalMachine CertStore = icert.StoreLocalMachine // CertStoreCurrentUser searches the CurrentUser certificate store. CertStoreCurrentUser CertStore = icert.StoreCurrentUser // DefaultRefreshThreshold is the window before certificate expiry at which // GetCertificateFunc triggers a background refresh. Pass this value to // GetCertificateFunc when you do not need a custom refresh window. DefaultRefreshThreshold = 7 * 24 * time.Hour // DefaultRetryInterval is the minimum time between background refresh // attempts. If a refresh fails (e.g. the renewed certificate is not yet in // the store), subsequent requests within the refresh window are served from // the cache without spawning new goroutines until this interval elapses. DefaultRetryInterval = 5 * time.Minute // DefaultLdapTimeout is the per-operation timeout applied to every LDAP // call (searches, health-check probes, etc.). In a corporate Active // Directory environment LDAP round-trips are typically sub-100 ms; five // seconds is generous while still failing fast against a hung server. DefaultLdapTimeout = 5 * time.Second // DefaultLdapTTL is the default maximum lifetime for a pooled LDAP connection. // In Active Directory, Kerberos tickets typically expire after 10 hours. // Rotating connections every 1 hour ensures they never encounter an expired ticket. DefaultLdapTTL = 1 * time.Hour )
Variables ¶
This section is empty.
Functions ¶
func ConfigureNTLM ¶
ConfigureNTLM sets the ConnContext on server so that each connection is assigned a unique ID. This ID is required by the NTLM handler to correlate the two-round token exchange across separate HTTP requests on the same keep-alive connection. Only required when using NTLM authentication.
func GetCertificateFunc ¶
func GetCertificateFunc(certSubject string, store CertStore, refreshThreshold, retryInterval time.Duration) (func(*tls.ClientHelloInfo) (*tls.Certificate, error), io.Closer, error)
GetCertificateFunc fetches the named certificate from the Windows store immediately — surfacing any configuration error at startup rather than on the first TLS handshake — and returns a tls.Config.GetCertificate callback that transparently refreshes the certificate in a background goroutine when it is within refreshThreshold of expiry, enabling zero-downtime rotation. Pass DefaultRefreshThreshold for the standard 7-day window.
retryInterval is the minimum time between background refresh attempts. If the store is temporarily unavailable (e.g. the renewed certificate has not been deployed yet), requests that arrive within the refresh window would otherwise each spawn a new goroutine. retryInterval rate-limits that behaviour so that at most one attempt runs per interval. Pass DefaultRetryInterval for the standard 5-minute window.
The returned io.Closer releases the Windows store handles for the currently-cached certificate. Call it after http.Server.Shutdown returns to ensure all active connections have already finished.
func SetUser ¶
SetUser injects a username into the request context, normalising it first. Use this to resume a session without re-running SSPI authentication. An empty username is stored as-is; User(r) will return ("", false) for it since empty strings are treated as "no authenticated user."
func SetUserGroups ¶
SetUserGroups injects group memberships into the request context. Use this to resume a session with cached groups without re-running LDAP.
func User ¶
User returns the authenticated username from the request context. The second return value is false if no user has been set.
func UserGroups ¶
UserGroups returns the authenticated user's group memberships from the request context. The second return value is false if no groups are present. After the LDAP middleware runs, it returns ([]string{}, true) for users with no group memberships, distinguishing "no groups" from "LDAP didn't run."
Types ¶
type AuthErrorHandler ¶
type AuthErrorHandler = iauth.AuthErrorHandler
AuthErrorHandler is a function type for handling an authentication or authorisation error. Assign one to any field of AuthErrorHandlers to override the default behaviour for that specific error category.
type AuthErrorHandlers ¶
type AuthErrorHandlers = iauth.AuthErrorHandlers
AuthErrorHandlers configures the error-handling behaviour of the authentication middleware. Pass one to WithSSPIErrorHandlers or WithLDAPErrorHandlers. Any field left nil falls back to the built-in default for that category; set OnGeneralError as a single catch-all.
type CertStore ¶
CertStore identifies which Windows certificate store to search. Use CertStoreLocalMachine or CertStoreCurrentUser.
type CertificateSource ¶
type CertificateSource = icert.CertificateSource
CertificateSource holds a TLS certificate retrieved from the Windows store. Call Close when the certificate is no longer needed (e.g. on server shutdown).
func GetWin32Cert ¶
func GetWin32Cert(subject string, store CertStore) (*CertificateSource, error)
GetWin32Cert retrieves a certificate from the Windows certificate store by Common Name and returns a CertificateSource. The certificate is validated before being returned: it must not be expired and must carry the ExtKeyUsageServerAuth extended key usage.
The caller must call Close on the returned CertificateSource when it is no longer needed to release Windows store handles.
For servers that need zero-downtime certificate rotation, use GetCertificateFunc instead.
type LDAPOption ¶ added in v0.2.0
type LDAPOption func(*ldapConfig)
LDAPOption configures an LDAPProvider.
func WithLDAPAddress ¶ added in v0.2.0
func WithLDAPAddress(addr string) LDAPOption
WithLDAPAddress sets the address of the LDAP server (host:port).
func WithLDAPConnectionTTL ¶ added in v0.2.0
func WithLDAPConnectionTTL(d time.Duration) LDAPOption
WithLDAPConnectionTTL sets the maximum lifetime of a pooled LDAP connection. This prevents stale Kerberos tickets from causing failures on long-lived connections. Zero disables the TTL.
func WithLDAPErrorHandlers ¶ added in v0.2.0
func WithLDAPErrorHandlers(h AuthErrorHandlers) LDAPOption
WithLDAPErrorHandlers overrides the default error-handling behaviour of the LDAP middleware. Any field left nil falls back to the built-in default.
func WithLDAPServiceAccountSPN ¶ added in v0.2.0
func WithLDAPServiceAccountSPN(spn string) LDAPOption
WithLDAPServiceAccountSPN sets the Service Principal Name of the account used to bind to the LDAP server via GSSAPI/Kerberos.
func WithLDAPTimeout ¶ added in v0.2.0
func WithLDAPTimeout(d time.Duration) LDAPOption
WithLDAPTimeout sets the per-operation timeout applied to every LDAP call on each connection (searches, health-check probes, etc.). Zero is treated as DefaultLdapTimeout.
func WithLDAPUsersDN ¶ added in v0.2.0
func WithLDAPUsersDN(dn string) LDAPOption
WithLDAPUsersDN sets the Distinguished Name under which users are searched.
type LDAPProvider ¶ added in v0.2.0
type LDAPProvider struct {
// contains filtered or unexported fields
}
LDAPProvider enriches an authenticated request's context with the user's Active Directory group memberships. Create one with NewLDAPProvider, then register its Middleware method with your router or wrap handlers manually. It must be placed after SSPIProvider in the middleware chain.
func NewLDAPProvider ¶ added in v0.2.0
func NewLDAPProvider(opts ...LDAPOption) (*LDAPProvider, error)
NewLDAPProvider returns an LDAPProvider configured by the given options. LDAP connections are established lazily per request after initial validation.
func (*LDAPProvider) Close ¶ added in v0.2.0
func (p *LDAPProvider) Close() error
Close drains the LDAP connection pool, closing all idle connections. Call this on server shutdown after the HTTP server has stopped accepting new requests.
func (*LDAPProvider) Middleware ¶ added in v0.2.0
func (p *LDAPProvider) Middleware(next http.Handler) http.Handler
Middleware satisfies func(http.Handler) http.Handler and can be passed directly to any router's Use() method or used to wrap a handler manually:
router.Use(ldapProvider.Middleware) handler := ldapProvider.Middleware(myHandler)
type SSPIOption ¶ added in v0.2.0
type SSPIOption func(*sspiConfig)
SSPIOption configures an SSPIProvider.
func WithNTLM ¶ added in v0.2.0
func WithNTLM() SSPIOption
WithNTLM configures the SSPIProvider to use NTLM instead of Kerberos. Required for non-domain or localhost scenarios.
func WithSSPIErrorHandlers ¶ added in v0.2.0
func WithSSPIErrorHandlers(h AuthErrorHandlers) SSPIOption
WithSSPIErrorHandlers overrides the default error-handling behaviour of the SSPI middleware. Any field left nil falls back to the built-in default.
type SSPIProvider ¶ added in v0.2.0
type SSPIProvider struct {
// contains filtered or unexported fields
}
SSPIProvider authenticates requests using Windows SSPI (Kerberos or NTLM). Create one with NewSSPIProvider, then register its Middleware method with your router's Use() method or wrap handlers manually.
func NewSSPIProvider ¶ added in v0.2.0
func NewSSPIProvider(opts ...SSPIOption) (*SSPIProvider, error)
NewSSPIProvider acquires the required Windows SSPI credentials and returns a provider whose Middleware method satisfies func(http.Handler) http.Handler. Credential acquisition happens once here so that any configuration error is surfaced at startup rather than on the first request.
func (*SSPIProvider) Close ¶ added in v0.2.0
func (p *SSPIProvider) Close() error
Close releases the Windows SSPI credentials held by this provider. Call this on server shutdown.
func (*SSPIProvider) Middleware ¶ added in v0.2.0
func (p *SSPIProvider) Middleware(next http.Handler) http.Handler
Middleware satisfies func(http.Handler) http.Handler and can be passed directly to any router's Use() method or used to wrap a handler manually:
router.Use(sspiProvider.Middleware) handler := sspiProvider.Middleware(myHandler)
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
min-win-server
command
|
|
|
quick-start
command
|
|
|
sec-win-server
command
|
|
|
integration_tests
|
|
|
cmd/testserver
command
|
|
|
internal
|
|