// authHandler - does the authentication for the routes
func authHandler(h http.Handler, providers []auth.Provider) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var principalFound error
for _, provider := range providers {
principal, err := provider.GetPrincipal(r)
if principal != nil {
log.Debug("We found one. HOORAY!")
// we found our principal, stop looking
break
}
if err != nil {
principalFound = err
}
}
// if we went through the providers and found no principals. We will
// have found an error
if principalFound != nil {
log.Debug("no principal found")
writeResponse(w, http.StatusUnauthorized, broker.ErrorResponse{Description: principalFound.Error()})
return
}
h.ServeHTTP(w, r)
})
}
There are times we will want to add an auth handler or other http handler middleware to allow for authentication before accessing the broker.
https://github.com/openshift/ansible-service-broker/blob/master/pkg/handler/handler.go#L196