http: add client certificate user auth middleware

This populates the authenticated user from the client certificate
common name.

Also added tests for the existing client certificate functionality.
This commit is contained in:
Peter Fern
2023-05-26 15:26:13 +10:00
committed by Nick Craig-Wood
parent 7751d5a00b
commit 1cfed18aa7
14 changed files with 458 additions and 29 deletions

View File

@ -226,8 +226,6 @@ func NewServer(ctx context.Context, options ...Option) (*Server, error) {
s.mux.Use(MiddlewareStripPrefix(s.cfg.BaseURL))
}
s.initAuth()
err := s.initTemplate()
if err != nil {
return nil, err
@ -238,6 +236,8 @@ func NewServer(ctx context.Context, options ...Option) (*Server, error) {
return nil, err
}
s.initAuth()
for _, addr := range s.cfg.ListenAddr {
var url string
var network = "tcp"
@ -293,9 +293,17 @@ func NewServer(ctx context.Context, options ...Option) (*Server, error) {
}
func (s *Server) initAuth() {
s.usingAuth = false
authCertificateUserEnabled := s.tlsConfig != nil && s.tlsConfig.ClientAuth != tls.NoClientCert && s.auth.HtPasswd == "" && s.auth.BasicUser == ""
if authCertificateUserEnabled {
s.usingAuth = true
s.mux.Use(MiddlewareAuthCertificateUser())
}
if s.auth.CustomAuthFn != nil {
s.usingAuth = true
s.mux.Use(MiddlewareAuthCustom(s.auth.CustomAuthFn, s.auth.Realm))
s.mux.Use(MiddlewareAuthCustom(s.auth.CustomAuthFn, s.auth.Realm, authCertificateUserEnabled))
return
}
@ -310,7 +318,6 @@ func (s *Server) initAuth() {
s.mux.Use(MiddlewareAuthBasic(s.auth.BasicUser, s.auth.BasicPass, s.auth.Realm, s.auth.Salt))
return
}
s.usingAuth = false
}
func (s *Server) initTemplate() error {