2016-10-25 15:20:06 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2017-06-26 20:30:33 +00:00
|
|
|
"fmt"
|
2016-10-25 15:20:06 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/influxdata/chronograf"
|
|
|
|
)
|
|
|
|
|
2017-02-15 20:07:33 +00:00
|
|
|
// AuthRoute are the routes for each type of OAuth2 provider
|
|
|
|
type AuthRoute struct {
|
|
|
|
Name string `json:"name"` // Name uniquely identifies the provider
|
|
|
|
Label string `json:"label"` // Label is a user-facing string to present in the UI
|
|
|
|
Login string `json:"login"` // Login is the route to the login redirect path
|
|
|
|
Logout string `json:"logout"` // Logout is the route to the logout redirect path
|
|
|
|
Callback string `json:"callback"` // Callback is the route the provider calls to exchange the code/state
|
|
|
|
}
|
|
|
|
|
2017-02-15 22:28:17 +00:00
|
|
|
// AuthRoutes contains all OAuth2 provider routes.
|
|
|
|
type AuthRoutes []AuthRoute
|
|
|
|
|
|
|
|
// Lookup searches all the routes for a specific provider
|
|
|
|
func (r *AuthRoutes) Lookup(provider string) (AuthRoute, bool) {
|
|
|
|
for _, route := range *r {
|
|
|
|
if route.Name == provider {
|
|
|
|
return route, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return AuthRoute{}, false
|
|
|
|
}
|
|
|
|
|
2016-10-25 15:20:06 +00:00
|
|
|
type getRoutesResponse struct {
|
2017-06-13 20:36:53 +00:00
|
|
|
Layouts string `json:"layouts"` // Location of the layouts endpoint
|
|
|
|
Mappings string `json:"mappings"` // Location of the application mappings endpoint
|
|
|
|
Sources string `json:"sources"` // Location of the sources endpoint
|
|
|
|
Me string `json:"me"` // Location of the me endpoint
|
|
|
|
Dashboards string `json:"dashboards"` // Location of the dashboards endpoint
|
|
|
|
Auth []AuthRoute `json:"auth"` // Location of all auth routes.
|
|
|
|
Logout *string `json:"logout,omitempty"` // Location of the logout route for all auth routes
|
|
|
|
ExternalLinks getExternalLinksResponse `json:"external"` // All external links for the client to use
|
2016-10-25 15:20:06 +00:00
|
|
|
}
|
|
|
|
|
2017-06-13 20:36:53 +00:00
|
|
|
// AllRoutes is a handler that returns all links to resources in Chronograf server, as well as
|
|
|
|
// external links for the client to know about, such as for JSON feeds or custom side nav buttons.
|
2017-05-31 00:21:46 +00:00
|
|
|
// Optionally, routes for authentication can be returned.
|
|
|
|
type AllRoutes struct {
|
2017-06-26 20:30:33 +00:00
|
|
|
AuthRoutes []AuthRoute // Location of all auth routes. If no auth, this can be empty.
|
|
|
|
LogoutLink string // Location of the logout route for all auth routes. If no auth, this can be empty.
|
|
|
|
StatusFeed string // External link to the JSON Feed for the News Feed on the client's Status Page
|
2017-06-26 21:13:23 +00:00
|
|
|
CustomLinks map[string]string // Custom external links for client's User menu, as passed in via CLI/ENV
|
2017-06-23 22:12:02 +00:00
|
|
|
Logger chronograf.Logger
|
2017-06-13 20:36:53 +00:00
|
|
|
}
|
|
|
|
|
2017-06-26 20:35:21 +00:00
|
|
|
// ServeHTTP returns all top level routes and external links within chronograf
|
2017-05-31 00:21:46 +00:00
|
|
|
func (a *AllRoutes) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2017-06-26 20:30:33 +00:00
|
|
|
customLinks, err := NewCustomLinks(a.CustomLinks)
|
|
|
|
if err != nil {
|
|
|
|
msg := fmt.Sprintf("Invalid CustomLinks input: %v", customLinks)
|
2017-06-26 20:44:56 +00:00
|
|
|
Error(w, http.StatusInternalServerError, msg, a.Logger)
|
2017-06-26 20:30:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-25 15:20:06 +00:00
|
|
|
routes := getRoutesResponse{
|
2017-06-15 20:51:22 +00:00
|
|
|
Sources: "/chronograf/v1/sources",
|
|
|
|
Layouts: "/chronograf/v1/layouts",
|
|
|
|
Me: "/chronograf/v1/me",
|
|
|
|
Mappings: "/chronograf/v1/mappings",
|
|
|
|
Dashboards: "/chronograf/v1/dashboards",
|
|
|
|
Auth: make([]AuthRoute, len(a.AuthRoutes)), // We want to return at least an empty array, rather than null
|
|
|
|
ExternalLinks: getExternalLinksResponse{
|
2017-06-23 22:12:02 +00:00
|
|
|
StatusFeed: &a.StatusFeed,
|
2017-06-26 20:30:33 +00:00
|
|
|
CustomLinks: customLinks,
|
2017-06-15 20:51:22 +00:00
|
|
|
},
|
2017-02-15 20:07:33 +00:00
|
|
|
}
|
2017-05-31 00:21:46 +00:00
|
|
|
|
|
|
|
// The JSON response will have no field present for the LogoutLink if there is no logout link.
|
|
|
|
if a.LogoutLink != "" {
|
|
|
|
routes.Logout = &a.LogoutLink
|
2017-05-03 23:34:05 +00:00
|
|
|
}
|
2017-02-15 20:07:33 +00:00
|
|
|
|
2017-05-31 00:21:46 +00:00
|
|
|
for i, route := range a.AuthRoutes {
|
2017-02-15 20:07:33 +00:00
|
|
|
routes.Auth[i] = route
|
2016-10-25 15:20:06 +00:00
|
|
|
}
|
|
|
|
|
2017-05-31 00:21:46 +00:00
|
|
|
encodeJSON(w, http.StatusOK, routes, a.Logger)
|
2016-10-25 15:20:06 +00:00
|
|
|
}
|