server: fix the path registration for user APIs at non-root URLs

This commit is contained in:
Eric Chiang 2016-08-18 16:05:55 -07:00
parent bef9f3c221
commit b466ae7a70

View file

@ -215,6 +215,8 @@ func (s *Server) HTTPHandler() http.Handler {
clock := clockwork.NewRealClock() clock := clockwork.NewRealClock()
mux := http.NewServeMux() mux := http.NewServeMux()
// Handler methods which register handlers at prefixed paths.
handle := func(urlPath string, h http.Handler) { handle := func(urlPath string, h http.Handler) {
p := path.Join(s.IssuerURL.Path, urlPath) p := path.Join(s.IssuerURL.Path, urlPath)
// path.Join always trims trailing slashes (https://play.golang.org/p/GRr0jDd9P7). // path.Join always trims trailing slashes (https://play.golang.org/p/GRr0jDd9P7).
@ -227,6 +229,14 @@ func (s *Server) HTTPHandler() http.Handler {
handleFunc := func(urlPath string, hf http.HandlerFunc) { handleFunc := func(urlPath string, hf http.HandlerFunc) {
handle(urlPath, hf) handle(urlPath, hf)
} }
handleStripPrefix := func(urlPath string, h http.Handler) {
if s.IssuerURL.Path != "" {
handle(urlPath, http.StripPrefix(s.IssuerURL.Path, h))
} else {
handle(urlPath, h)
}
}
handleFunc(httpPathDiscovery, handleDiscoveryFunc(s.ProviderConfig())) handleFunc(httpPathDiscovery, handleDiscoveryFunc(s.ProviderConfig()))
handleFunc(httpPathAuth, handleAuthFunc(s, s.Connectors, s.LoginTemplate, s.EnableRegistration)) handleFunc(httpPathAuth, handleAuthFunc(s, s.Connectors, s.LoginTemplate, s.EnableRegistration))
handleFunc(httpPathOOB, handleOOBFunc(s, s.OOBTemplate)) handleFunc(httpPathOOB, handleOOBFunc(s, s.OOBTemplate))
@ -293,7 +303,7 @@ func (s *Server) HTTPHandler() http.Handler {
usersAPI := usersapi.NewUsersAPI(s.UserManager, s.ClientManager, s.RefreshTokenRepo, s.UserEmailer, s.localConnectorID) usersAPI := usersapi.NewUsersAPI(s.UserManager, s.ClientManager, s.RefreshTokenRepo, s.UserEmailer, s.localConnectorID)
handler := NewUserMgmtServer(usersAPI, s.JWTVerifierFactory(), s.UserManager, s.ClientManager).HTTPHandler() handler := NewUserMgmtServer(usersAPI, s.JWTVerifierFactory(), s.UserManager, s.ClientManager).HTTPHandler()
handle(apiBasePath+"/", handler) handleStripPrefix(apiBasePath+"/", handler)
return http.Handler(mux) return http.Handler(mux)
} }