Add a me endpoint for chronograf

Returns the currently signed in user from session or token
pull/10616/head
Brandon Farmer 2018-10-10 10:06:29 -07:00
parent ad74678200
commit d65008d24b
2 changed files with 37 additions and 0 deletions

View File

@ -200,6 +200,11 @@ func (h *APIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return return
} }
if strings.HasPrefix(r.URL.Path, "/api/v2/me") {
h.UserHandler.ServeHTTP(w, r)
return
}
if strings.HasPrefix(r.URL.Path, "/api/v2/orgs") { if strings.HasPrefix(r.URL.Path, "/api/v2/orgs") {
h.OrgHandler.ServeHTTP(w, r) h.OrgHandler.ServeHTTP(w, r)
return return

View File

@ -9,6 +9,7 @@ import (
"path" "path"
"github.com/influxdata/platform" "github.com/influxdata/platform"
platcontext "github.com/influxdata/platform/context"
kerrors "github.com/influxdata/platform/kit/errors" kerrors "github.com/influxdata/platform/kit/errors"
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
) )
@ -26,6 +27,7 @@ func NewUserHandler() *UserHandler {
} }
h.HandlerFunc("POST", "/api/v2/users", h.handlePostUser) h.HandlerFunc("POST", "/api/v2/users", h.handlePostUser)
h.HandlerFunc("GET", "/api/v2/me", h.handleGetMe)
h.HandlerFunc("GET", "/api/v2/users", h.handleGetUsers) h.HandlerFunc("GET", "/api/v2/users", h.handleGetUsers)
h.HandlerFunc("GET", "/api/v2/users/:id", h.handleGetUser) h.HandlerFunc("GET", "/api/v2/users/:id", h.handleGetUser)
h.HandlerFunc("PATCH", "/api/v2/users/:id", h.handlePatchUser) h.HandlerFunc("PATCH", "/api/v2/users/:id", h.handlePatchUser)
@ -69,6 +71,36 @@ func decodePostUserRequest(ctx context.Context, r *http.Request) (*postUserReque
}, nil }, nil
} }
// handleGetUser is the HTTP handler for the GET /api/v2/me.
func (h *UserHandler) handleGetMe(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
a, err := platcontext.GetAuthorizer(ctx)
if err != nil {
EncodeError(ctx, err, w)
return
}
id := []byte{}
switch s := a.(type) {
case *platform.Session:
id = s.UserID
case *platform.Authorization:
id = s.UserID
}
b, err := h.UserService.FindUserByID(ctx, id)
if err != nil {
EncodeError(ctx, err, w)
return
}
if err := encodeResponse(ctx, w, http.StatusOK, b); err != nil {
EncodeError(ctx, err, w)
return
}
}
// handleGetUser is the HTTP handler for the GET /api/v2/users/:id route. // handleGetUser is the HTTP handler for the GET /api/v2/users/:id route.
func (h *UserHandler) handleGetUser(w http.ResponseWriter, r *http.Request) { func (h *UserHandler) handleGetUser(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()