pmorie / osb-broker-lib

A go library for developing an Open Service Broker
Apache License 2.0
28 stars 23 forks source link

Need ability to add middleware handler functions for extension #15

Open jmrodri opened 6 years ago

jmrodri commented 6 years ago

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

// 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)
    })
}
pmorie commented 6 years ago

Seems like a very good idea