lib/http: add UsingAuth method

This commit is contained in:
Nick Craig-Wood 2022-12-10 10:51:57 +00:00
parent 3167292c2f
commit a9ce86f9a3
1 changed files with 10 additions and 0 deletions

View File

@ -144,6 +144,7 @@ type Server struct {
cfg Config cfg Config
template *TemplateConfig template *TemplateConfig
htmlTemplate *template.Template htmlTemplate *template.Template
usingAuth bool // set if we are using auth middleware
} }
// Option allows customizing the server // Option allows customizing the server
@ -272,19 +273,23 @@ func NewServer(ctx context.Context, options ...Option) (*Server, error) {
func (s *Server) initAuth() { func (s *Server) initAuth() {
if s.auth.CustomAuthFn != nil { 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))
return return
} }
if s.auth.HtPasswd != "" { if s.auth.HtPasswd != "" {
s.usingAuth = true
s.mux.Use(MiddlewareAuthHtpasswd(s.auth.HtPasswd, s.auth.Realm)) s.mux.Use(MiddlewareAuthHtpasswd(s.auth.HtPasswd, s.auth.Realm))
return return
} }
if s.auth.BasicUser != "" { if s.auth.BasicUser != "" {
s.usingAuth = true
s.mux.Use(MiddlewareAuthBasic(s.auth.BasicUser, s.auth.BasicPass, s.auth.Realm, s.auth.Salt)) s.mux.Use(MiddlewareAuthBasic(s.auth.BasicUser, s.auth.BasicPass, s.auth.Realm, s.auth.Salt))
return return
} }
s.usingAuth = false
} }
func (s *Server) initTemplate() error { func (s *Server) initTemplate() error {
@ -426,3 +431,8 @@ func (s *Server) URLs() []string {
} }
return out return out
} }
// UsingAuth returns true if authentication is required
func (s *Server) UsingAuth() bool {
return s.usingAuth
}